1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2015 Adyen BV (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <magento@adyen.com>
*/
namespace Adyen\Payment\Model\Method;
use Magento\Framework\DataObject;
use Magento\Payment\Model\Method\ConfigInterface;
use Magento\Payment\Model\Method\Online\GatewayInterface;
/**
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Pos extends \Magento\Payment\Model\Method\AbstractMethod implements GatewayInterface
{
const METHOD_CODE = 'adyen_pos';
/**
* @var string
*/
protected $_code = self::METHOD_CODE;
/**
* @var GUEST_ID , used when order is placed by guests
*/
const GUEST_ID = 'customer_';
protected $_infoBlockType = 'Adyen\Payment\Block\Info\Pos';
/**
* Payment Method feature
*
* @var bool
*/
protected $_isGateway = true;
protected $_canAuthorize = true;
protected $_isInitializeNeeded = true;
protected $_canRefund = true;
protected $_canRefundInvoicePartial = true;
/**
* Payment Method feature
*
* @var bool
*/
protected $_canUseInternal = false;
/**
* @var \Adyen\Payment\Model\Api\PaymentRequest
*/
protected $_paymentRequest;
/**
* @var \Adyen\Payment\Helper\Data
*/
protected $_adyenHelper;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
* @var \Magento\Framework\UrlInterface
*/
protected $_urlBuilder;
/**
* @var ResolverInterface
*/
protected $resolver;
/**
* @var \Adyen\Payment\Logger\AdyenLogger
*/
protected $_adyenLogger;
/**
* Currency factory
*
* @var CurrencyFactory
*/
protected $_currencyFactory;
/**
* @param \Adyen\Payment\Model\Api\PaymentRequest $paymentRequest
* @param \Magento\Framework\UrlInterface $urlBuilder
* @param \Adyen\Payment\Helper\Data $adyenHelper
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\Locale\ResolverInterface $resolver
* @param \Adyen\Payment\Logger\AdyenLogger $adyenLogger
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
* @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
* @param \Magento\Payment\Helper\Data $paymentData
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Payment\Model\Method\Logger $logger
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
* @param array $data
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Directory\Model\CurrencyFactory $currencyFactory,
\Magento\Framework\App\RequestInterface $request,
\Adyen\Payment\Model\Api\PaymentRequest $paymentRequest,
\Magento\Framework\UrlInterface $urlBuilder,
\Adyen\Payment\Helper\Data $adyenHelper,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\Locale\ResolverInterface $resolver,
\Adyen\Payment\Logger\AdyenLogger $adyenLogger,
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
\Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
\Magento\Payment\Helper\Data $paymentData,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Payment\Model\Method\Logger $logger,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
parent::__construct(
$context,
$registry,
$extensionFactory,
$customAttributeFactory,
$paymentData,
$scopeConfig,
$logger,
$resource,
$resourceCollection,
$data
);
$this->_paymentRequest = $paymentRequest;
$this->_urlBuilder = $urlBuilder;
$this->_adyenHelper = $adyenHelper;
$this->storeManager = $storeManager;
$this->resolver = $resolver;
$this->_adyenLogger = $adyenLogger;
$this->_request = $request;
$this->_currencyFactory = $currencyFactory;
}
protected $_paymentMethodType = 'hpp';
public function getPaymentMethodType() {
return $this->_paymentMethodType;
}
public function initialize($paymentAction, $stateObject)
{
/*
* do not send order confirmation mail after order creation wait for
* Adyen AUTHORIISATION notification
*/
$payment = $this->getInfoInstance();
$order = $payment->getOrder();
$order->setCanSendNewEmailFlag(false);
$state = \Magento\Sales\Model\Order::STATE_NEW;
$stateObject->setState($state);
$stateObject->setStatus($this->_adyenHelper->getAdyenAbstractConfigData('order_status'));
}
/**
* Assign data to info model instance
*
* @param \Magento\Framework\DataObject|mixed $data
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function assignData(\Magento\Framework\DataObject $data)
{
parent::assignData($data);
$infoInstance = $this->getInfoInstance();
return $this;
}
/**
* Checkout redirect URL getter for onepage checkout (hardcode)
*
* @see \Magento\Checkout\Controller\Onepage::savePaymentAction()
* @see \Magento\Quote\Model\Quote\Payment::getCheckoutRedirectUrl()
* @return string
*/
public function getCheckoutRedirectUrl()
{
return $this->_urlBuilder->getUrl('adyen/process/redirectPos',['_secure' => $this->_getRequest()->isSecure()]);
}
/**
* Retrieve request object
*
* @return \Magento\Framework\App\RequestInterface
*/
protected function _getRequest()
{
return $this->_request;
}
/**
* Post request to gateway and return response
*
* @param Object $request
* @param ConfigInterface $config
*
* @return Object
*
* @throws \Exception
*/
public function postRequest(DataObject $request, ConfigInterface $config)
{
// Implement postRequest() method.
}
public function getLaunchLink()
{
$paymentInfo = $this->getInfoInstance();
$order = $paymentInfo->getOrder();
$realOrderId = $order->getRealOrderId();
$orderCurrencyCode = $order->getOrderCurrencyCode();
$amount = $this->_adyenHelper->formatAmount($order->getGrandTotal(), $orderCurrencyCode);
$shopperEmail = $order->getCustomerEmail();
$customerId = $order->getCustomerId();
$callbackUrl = $this->_urlBuilder->getUrl('adyen/process/resultpos',['_secure' => $this->_getRequest()->isSecure()]);
$addReceiptOrderLines = $this->_adyenHelper->getAdyenPosConfigData("add_receipt_order_lines");
$recurringContract = $this->_adyenHelper->getAdyenPosConfigData('recurring_type');
$currencyCode = $orderCurrencyCode;
$paymentAmount = $amount;
$merchantReference = $realOrderId;
$shopperReference = (!empty($customerId)) ? $customerId : self::GUEST_ID . $realOrderId;
$shopperEmail = $shopperEmail;
$recurringParams = "";
if($order->getPayment()->getAdditionalInformation("store_cc") != "") {
$recurringParams = "&recurringContract=".urlencode($recurringContract)."&shopperReference=".urlencode($shopperReference). "&shopperEmail=".urlencode($shopperEmail);
}
$receiptOrderLines = "";
if($addReceiptOrderLines) {
$orderLines = base64_encode($this->getReceiptOrderLines($order));
$receiptOrderLines = "&receiptOrderLines=" . urlencode($orderLines);
}
// extra parameters so that you alway's return these paramters from the application
$extra_paramaters = urlencode("/?originalCustomCurrency=".$currencyCode."&originalCustomAmount=".$paymentAmount. "&originalCustomMerchantReference=".$merchantReference . "&originalCustomSessionId=".session_id());
$launchlink = "adyen://payment?sessionId=".session_id()."&amount=".$paymentAmount."¤cy=".$currencyCode."&merchantReference=".$merchantReference. $recurringParams . $receiptOrderLines . "&callback=".$callbackUrl . $extra_paramaters;
$this->_adyenLogger->debug(print_r($launchlink, true));
return $launchlink;
}
private function getReceiptOrderLines($order) {
$myReceiptOrderLines = "";
// temp
$currency = $order->getOrderCurrencyCode();
$formattedAmountValue = $this->_currencyFactory->create()->format(
$order->getGrandTotal(),
array('display'=>\Magento\Framework\Currency::NO_SYMBOL),
false
);
$taxAmount = $order->getTaxAmount();
$formattedTaxAmount = $this->_currencyFactory->create()->format(
$taxAmount,
array('display'=>\Magento\Framework\Currency::NO_SYMBOL),
false
);
$paymentAmount = "1000";
$myReceiptOrderLines .= "---||C\n".
"====== YOUR ORDER DETAILS ======||CB\n".
"---||C\n".
" No. Description |Piece Subtotal|\n";
foreach ($order->getItemsCollection() as $item) {
//skip dummies
if ($item->isDummy()) continue;
$singlePriceFormat = $this->_currencyFactory->create()->format(
$item->getPriceInclTax(),
array('display'=>\Magento\Framework\Currency::NO_SYMBOL),
false
);
$itemAmount = $item->getPriceInclTax() * (int) $item->getQtyOrdered();
$itemAmountFormat = $this->_currencyFactory->create()->format(
$itemAmount,
array('display'=>\Magento\Framework\Currency::NO_SYMBOL),
false
);
$myReceiptOrderLines .= " " . (int) $item->getQtyOrdered() . " " . trim(substr($item->getName(),0, 25)) . "| " . $currency . " " . $singlePriceFormat . " " . $currency . " " . $itemAmountFormat . "|\n";
}
//discount cost
if($order->getDiscountAmount() > 0 || $order->getDiscountAmount() < 0)
{
$discountAmountFormat = $this->_currencyFactory->create()->format(
$order->getDiscountAmount(),
array('display'=>\Magento\Framework\Currency::NO_SYMBOL),
false
);
$myReceiptOrderLines .= " " . 1 . " " . $this->__('Total Discount') . "| " . $currency . " " . $discountAmountFormat ."|\n";
}
//shipping cost
if($order->getShippingAmount() > 0 || $order->getShippingTaxAmount() > 0)
{
$shippingAmountFormat = $this->_currencyFactory->create()->format(
$order->getShippingAmount(),
array('display'=>\Magento\Framework\Currency::NO_SYMBOL),
false
);
$myReceiptOrderLines .= " " . 1 . " " . $order->getShippingDescription() . "| " . $currency . " " . $shippingAmountFormat ."|\n";
}
if($order->getPaymentFeeAmount() > 0) {
$paymentFeeAmount = $this->_currencyFactory->create()->format(
$order->getPaymentFeeAmount(),
array('display'=>\Magento\Framework\Currency::NO_SYMBOL),
false
);
$myReceiptOrderLines .= " " . 1 . " " . $this->__('Payment Fee') . "| " . $currency . " " . $paymentFeeAmount ."|\n";
}
$myReceiptOrderLines .= "|--------|\n".
"|Order Total: ".$currency." ".$formattedAmountValue."|B\n".
"|Tax: ".$currency." ".$formattedTaxAmount."|B\n".
"||C\n";
//Cool new header for card details section! Default location is After Header so simply add to Order Details as separator
$myReceiptOrderLines .= "---||C\n".
"====== YOUR PAYMENT DETAILS ======||CB\n".
"---||C\n";
return $myReceiptOrderLines;
}
/**
* Capture on Adyen
*
* @param \Magento\Payment\Model\InfoInterface $payment
* @param float $amount
*/
public function capture(\Magento\Payment\Model\InfoInterface $payment, $amount)
{
parent::capture($payment, $amount);
$this->_paymentRequest->capture($payment, $amount);
return $this;
}
/**
* Refund specified amount for payment
*
* @param \Magento\Framework\DataObject|InfoInterface $payment
* @param float $amount
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
* @api
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function refund(\Magento\Payment\Model\InfoInterface $payment, $amount)
{
parent::refund($payment, $amount);
// get pspReference
$pspReference = $payment->getAdyenPspReference();
$order = $payment->getOrder();
// if amount is a full refund send a refund/cancelled request so if it is not captured yet it will cancel the order
$grandTotal = $order->getGrandTotal();
if($grandTotal == $amount) {
$this->_paymentRequest->cancelOrRefund($payment);
} else {
$this->_paymentRequest->refund($payment, $amount);
}
return $this;
}
}