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
<?php
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
*
* Adyen Payment Module
*
* Copyright (c) 2018 Adyen B.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
* Author: Adyen <magento@adyen.com>
*/
namespace Adyen\Payment\Gateway\Http\Client;
use Adyen\Util\Util;
use Magento\Payment\Gateway\Http\ClientInterface;
class TransactionPosCloudSync implements ClientInterface
{
/**
* @var int
*/
protected $storeId;
/**
* @var \Adyen\Client
*/
protected $client;
/**
* @var \Adyen\Payment\Helper\Data
*/
protected $adyenHelper;
/**
* @var \Adyen\Payment\Logger\AdyenLogger
*/
protected $adyenLogger;
public function __construct(
\Adyen\Payment\Helper\Data $adyenHelper,
\Adyen\Payment\Logger\AdyenLogger $adyenLogger,
\Magento\Store\Model\StoreManagerInterface $storeManager,
array $data = []
) {
$this->adyenHelper = $adyenHelper;
$this->adyenLogger = $adyenLogger;
$this->storeId = $storeManager->getStore()->getId();
$apiKey = $this->adyenHelper->getPosApiKey($this->storeId);
// initialize client
$client = $this->adyenHelper->initializeAdyenClient($this->storeId, $apiKey);
//Set configurable option in M2
$posTimeout = $this->adyenHelper->getAdyenPosCloudConfigData('pos_timeout', $this->storeId);
if (!empty($posTimeout)) {
$client->setTimeout($posTimeout);
}
$this->client = $client;
}
/**
* Places request to gateway. Returns result as ENV array
*
* @param \Magento\Payment\Gateway\Http\TransferInterface $transferObject
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function placeRequest(\Magento\Payment\Gateway\Http\TransferInterface $transferObject)
{
$request = $transferObject->getBody();
if (!empty($request['response']['SaleToPOIResponse']['PaymentResponse'])) {
$paymentResponse = $request['response']['SaleToPOIResponse']['PaymentResponse'];
//Initiate has already a response
return $paymentResponse;
}
//always do status call and return the response of the status call
$service = $this->adyenHelper->createAdyenPosPaymentService($this->client);
$poiId = $this->adyenHelper->getPoiId($this->storeId);
$newServiceID = date("dHis");
$statusDate = date("U");
$timeDiff = (int)$statusDate - (int)$request['initiateDate'];
$totalTimeout = $this->adyenHelper->getAdyenPosCloudConfigData('total_timeout', $this->storeId);
if ($timeDiff > $totalTimeout) {
throw new \Magento\Framework\Exception\LocalizedException(__("POS connection timed out."));
}
//Provide receipt to the shopper
$request = [
'SaleToPOIRequest' => [
'MessageHeader' => [
'ProtocolVersion' => '3.0',
'MessageClass' => 'Service',
'MessageCategory' => 'TransactionStatus',
'MessageType' => 'Request',
'ServiceID' => $newServiceID,
'SaleID' => 'Magento2CloudStatus',
'POIID' => $poiId
],
'TransactionStatusRequest' => [
'MessageReference' => [
'MessageCategory' => 'Payment',
'SaleID' => 'Magento2Cloud',
'ServiceID' => $request['serviceID']
],
'DocumentQualifier' => [
"CashierReceipt",
"CustomerReceipt"
],
'ReceiptReprintFlag' => true
]
]
];
try {
$response = $service->runTenderSync($request);
} catch (\Adyen\AdyenException $e) {
$response['error'] = $e->getMessage();
return $response;
}
if (!empty($response['SaleToPOIResponse']['TransactionStatusResponse'])) {
$statusResponse = $response['SaleToPOIResponse']['TransactionStatusResponse'];
if ($statusResponse['Response']['Result'] == 'Failure') {
$errorMsg = __('In Progress');
throw new \Magento\Framework\Exception\LocalizedException(__($errorMsg));
} else {
$paymentResponse = $statusResponse['RepeatedMessageResponse']['RepeatedResponseMessageBody']['PaymentResponse'];
}
} else {
// probably SaleToPOIRequest, that means terminal unreachable, log the response as error
$this->adyenLogger->error(json_encode($response));
throw new \Magento\Framework\Exception\LocalizedException(__("The terminal could not be reached."));
}
return $paymentResponse;
}
}