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
<?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 Hpp extends \Magento\Payment\Model\Method\AbstractMethod implements GatewayInterface
{
const METHOD_CODE = 'adyen_hpp';
/**
* @var string
*/
protected $_code = self::METHOD_CODE;
/**
* @var GUEST_ID , used when order is placed by guests
*/
const GUEST_ID = 'customer_';
// protected $_formBlockType = 'Adyen\Payment\Block\Form\Hpp';
protected $_infoBlockType = 'Adyen\Payment\Block\Info\Hpp';
/**
* Payment Method feature
*
* @var bool
*/
protected $_isGateway = true;
protected $_canAuthorize = true;
protected $_isInitializeNeeded = true;
/**
* @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;
/**
* @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\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->_urlBuilder = $urlBuilder;
$this->_adyenHelper = $adyenHelper;
$this->storeManager = $storeManager;
$this->resolver = $resolver;
$this->_adyenLogger = $adyenLogger;
}
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();
if(isset($data['brand_code'])) {
$infoInstance->setAdditionalInformation('brand_code', $data['brand_code']);
}
if(isset($data['issuer_id'])) {
$infoInstance->setAdditionalInformation('issuer_id', $data['issuer_id']);
}
$this->_adyenLogger->info(print_r($data,1));
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/redirect');
}
/**
* 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.
}
/**
* @desc Get url of Adyen payment
* @return string
*/
public function getFormUrl()
{
$paymentRoutine = $this->getConfigData('payment_routine');
switch ($this->_adyenHelper->isDemoMode()) {
case true:
if ($paymentRoutine == 'single' && $this->getPaymentMethodSelectionOnAdyen()) {
$url = 'https://test.adyen.com/hpp/pay.shtml';
} else {
$url = ($this->getPaymentMethodSelectionOnAdyen())
? 'https://test.adyen.com/hpp/select.shtml'
: "https://test.adyen.com/hpp/details.shtml";
}
break;
default:
if ($paymentRoutine == 'single' && $this->getPaymentMethodSelectionOnAdyen()) {
$url = 'https://live.adyen.com/hpp/pay.shtml';
} else {
$url = ($this->getPaymentMethodSelectionOnAdyen())
? 'https://live.adyen.com/hpp/select.shtml'
: "https://live.adyen.com/hpp/details.shtml";
}
break;
}
return $url;
}
public function getFormFields()
{
$paymentInfo = $this->getInfoInstance();
$order = $paymentInfo->getOrder();
$realOrderId = $order->getRealOrderId();
$orderCurrencyCode = $order->getOrderCurrencyCode();
$skinCode = trim($this->getConfigData('skin_code'));
$amount = $this->_adyenHelper->formatAmount($order->getGrandTotal(), $orderCurrencyCode);
$merchantAccount = trim($this->_adyenHelper->getAdyenAbstractConfigData('merchant_account'));
$shopperEmail = $order->getCustomerEmail();
$customerId = $order->getCustomerId();
$shopperIP = $order->getRemoteIp();
$browserInfo = $_SERVER['HTTP_USER_AGENT'];
$deliveryDays = $this->getConfigData('delivery_days');
$shopperLocale = trim($this->getConfigData('shopper_locale'));
$shopperLocale = (!empty($shopperLocale)) ? $shopperLocale : $this->resolver->getLocale();
$countryCode = trim($this->getConfigData('country_code'));
$countryCode = (!empty($countryCode)) ? $countryCode : false;
// if directory lookup is enabled use the billingadress as countrycode
if ($countryCode == false) {
if ($order->getBillingAddress() && $order->getBillingAddress()->getCountryId() != "") {
$countryCode = $order->getBillingAddress()->getCountryId();
}
}
$formFields = array();
$formFields['merchantAccount'] = $merchantAccount;
$formFields['merchantReference'] = $realOrderId;
$formFields['paymentAmount'] = (int)$amount;
$formFields['currencyCode'] = $orderCurrencyCode;
$formFields['shipBeforeDate'] = date(
"Y-m-d",
mktime(date("H"), date("i"), date("s"), date("m"), date("j") + $deliveryDays, date("Y"))
);
$formFields['skinCode'] = $skinCode;
$formFields['shopperLocale'] = $shopperLocale;
$formFields['countryCode'] = $countryCode;
$formFields['shopperIP'] = $shopperIP;
$formFields['browserInfo'] = $browserInfo;
$formFields['sessionValidity'] = date(
DATE_ATOM,
mktime(date("H") + 1, date("i"), date("s"), date("m"), date("j"), date("Y"))
);
$formFields['shopperEmail'] = $shopperEmail;
// recurring
$recurringType = trim($this->_adyenHelper->getAdyenAbstractConfigData('recurring_type'));
$formFields['recurringContract'] = $recurringType;
$formFields['shopperReference'] = (!empty($customerId)) ? $customerId : self::GUEST_ID . $realOrderId;
//blocked methods
$formFields['blockedMethods'] = "";
$baseUrl = $this->storeManager->getStore($this->getStore())
->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK);
$formFields['resURL'] = $baseUrl . 'adyen/process/result';
$hmacKey = $this->_adyenHelper->getHmac();
$brandCode = $order->getPayment()->getAdditionalInformation("brand_code");
if($brandCode) {
$formFields['brandCode'] = $brandCode;
}
$issuerId = $order->getPayment()->getAdditionalInformation("issuer_id");
if($issuerId) {
$formFields['issuerId'] = $issuerId;
}
// Sort the array by key using SORT_STRING order
ksort($formFields, SORT_STRING);
// Generate the signing data string
$signData = implode(":",array_map(array($this, 'escapeString'),array_merge(array_keys($formFields), array_values($formFields))));
$merchantSig = base64_encode(hash_hmac('sha256',$signData,pack("H*" , $hmacKey),true));
$formFields['merchantSig'] = $merchantSig;
$this->_adyenLogger->info(print_r($formFields, true));
return $formFields;
}
/*
* @desc The character escape function is called from the array_map function in _signRequestParams
* $param $val
* return string
*/
protected function escapeString($val)
{
return str_replace(':','\\:',str_replace('\\','\\\\',$val));
}
public function getPaymentMethodSelectionOnAdyen() {
return $this->getConfigData('payment_selection_on_adyen');
}
}