-
Notifications
You must be signed in to change notification settings - Fork 1
/
AtosSipsPostPaymentGateway.php
208 lines (180 loc) · 6.48 KB
/
AtosSipsPostPaymentGateway.php
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
<?php
namespace IDCI\Bundle\PaymentBundle\Gateway;
use IDCI\Bundle\PaymentBundle\Gateway\StatusCode\AtosSipsStatusCode;
use IDCI\Bundle\PaymentBundle\Model\GatewayResponse;
use IDCI\Bundle\PaymentBundle\Model\PaymentGatewayConfigurationInterface;
use IDCI\Bundle\PaymentBundle\Model\Transaction;
use IDCI\Bundle\PaymentBundle\Payment\PaymentStatus;
use Payum\ISO4217\ISO4217;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Twig\Environment;
class AtosSipsPostPaymentGateway extends AbstractPaymentGateway
{
/**
* @var string
*/
private $serverHostName;
public function __construct(
Environment $templating,
EventDispatcherInterface $dispatcher,
string $serverHostName
) {
parent::__construct($templating, $dispatcher);
$this->serverHostName = $serverHostName;
}
/**
* Get Atos Sips POST server url.
*
* @method getServerUrl
*/
private function getServerUrl(): string
{
return sprintf('https://%s/paymentInit', $this->serverHostName);
}
/**
* Build payment gateway options.
*
* @method buildOptions
*/
private function buildOptions(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction
): array {
return [
'amount' => $transaction->getAmount(),
'automaticResponseUrl' => $paymentGatewayConfiguration->get('callback_url'),
'captureDay' => $paymentGatewayConfiguration->get('capture_day'),
'captureMode' => $paymentGatewayConfiguration->get('capture_mode'),
'currencyCode' => (new ISO4217())->findByAlpha3($transaction->getCurrencyCode())->getNumeric(),
'merchantId' => $paymentGatewayConfiguration->get('merchant_id'),
'normalReturnUrl' => $paymentGatewayConfiguration->get('return_url'),
'transactionReference' => $transaction->getId(),
'keyVersion' => $paymentGatewayConfiguration->get('version'),
];
}
/**
* Build payment gateway signature hash.
*
* @method buildSeal
*/
private function buildSeal(array $options): string
{
return hash('sha256', mb_convert_encoding($options['build'].$options['secret'], 'UTF-8'));
}
/**
* {@inheritdoc}
*/
public function initialize(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction
): array {
$options = $this->buildOptions($paymentGatewayConfiguration, $transaction);
$builtOptions = [
'options' => $options,
'build' => implode('|', array_map(
function ($k, $v) {
if (null !== $v) {
return sprintf('%s=%s', $k, $v);
}
},
array_keys($options),
$options
)),
'secret' => $paymentGatewayConfiguration->get('secret'),
];
return [
'url' => $this->getServerUrl(),
'interfaceVersion' => $paymentGatewayConfiguration->get('interface_version'),
'build' => $builtOptions['build'],
'seal' => $this->buildSeal($builtOptions),
];
}
/**
* {@inheritdoc}
*/
public function buildHTMLView(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction
): string {
$initializationData = $this->initialize($paymentGatewayConfiguration, $transaction);
return $this->templating->render('@IDCIPayment/Gateway/atos_sips_post.html.twig', [
'initializationData' => $initializationData,
]);
}
/**
* {@inheritdoc}
*/
public function getReturnResponse(
Request $request,
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration
): GatewayResponse {
return new GatewayResponse();
}
/**
* {@inheritdoc}
*
* @throws \UnexpectedValueException If the request method is not POST
*/
public function getCallbackResponse(
Request $request,
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration
): GatewayResponse {
if (!$request->isMethod('POST')) {
throw new \UnexpectedValueException('Atos SIPS : Payment Gateway error (Request method should be POST)');
}
$gatewayResponse = (new GatewayResponse())
->setDate(new \DateTime())
->setStatus(PaymentStatus::STATUS_FAILED)
;
if (!$request->request->has('Data')) {
return $gatewayResponse->setMessage('The request do not contains "Data"');
}
$seal = hash('sha256', $request->get('Data').$paymentGatewayConfiguration->get('secret'));
if ($request->request->get('Seal') != $seal) {
return $gatewayResponse->setMessage('Seal check failed');
}
$returnParams = [];
foreach (explode('|', $request->get('Data')) as $data) {
$param = explode('=', $data);
$returnParams[$param[0]] = $param[1];
}
$gatewayResponse
->setTransactionUuid($returnParams['transactionReference'])
->setAmount($returnParams['amount'])
->setCurrencyCode((new ISO4217())->findByNumeric($returnParams['currencyCode'])->getAlpha3())
->setRaw($returnParams)
;
if ('00' !== $returnParams['responseCode']) {
$gatewayResponse->setMessage(AtosSipsStatusCode::getStatusMessage($returnParams['responseCode']));
if ('17' === $returnParams['responseCode']) {
$gatewayResponse->setStatus(PaymentStatus::STATUS_CANCELED);
}
return $gatewayResponse;
}
if (
'SUCCESS' !== $returnParams['holderAuthentStatus'] &&
'3D_SUCCESS' !== $returnParams['holderAuthentStatus']
) {
return $gatewayResponse->setMessage('Transaction unauthorized');
}
return $gatewayResponse->setStatus(PaymentStatus::STATUS_APPROVED);
}
/**
* {@inheritdoc}
*/
public static function getParameterNames(): ?array
{
return array_merge(
parent::getParameterNames(),
[
'version',
'secret',
'merchant_id',
'capture_mode',
'capture_day',
'interface_version',
]
);
}
}