-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPayboxPaymentGateway.php
247 lines (219 loc) · 7.49 KB
/
PayboxPaymentGateway.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
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
<?php
namespace IDCI\Bundle\PaymentBundle\Gateway;
use GuzzleHttp\Client;
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 PayboxPaymentGateway extends AbstractPaymentGateway
{
/**
* @var string
*/
private $serverHostName;
private $keyPath;
private $publicKeyUrl;
public function __construct(
Environment $templating,
EventDispatcherInterface $dispatcher,
string $serverHostName,
string $keyPath,
string $publicKeyUrl
) {
parent::__construct($templating, $dispatcher);
$this->serverHostName = $serverHostName;
$this->keyPath = $keyPath;
$this->publicKeyUrl = $publicKeyUrl;
}
/**
* Get Paybox server url.
*
* @method getServerUrl
*/
private function getServerUrl(): string
{
return sprintf('https://%s/cgi/MYchoix_pagepaiement.cgi', $this->serverHostName);
}
/**
* Get Paybox key file path accoding to client site id.
*
* @method getKeyPath
*/
private function getKeyPath(string $clientSite): string
{
return sprintf('%s/%s.bin', $this->keyPath, $clientSite);
}
/**
* Get Paybox requested parameters for notification response.
*
* @method getPayboxReturnString
*/
private function getPayboxReturnString(): string
{
$codeMap = [
'M' => 'amount',
'R' => 'reference',
'A' => 'authorisation_id',
'P' => 'payment_type',
'T' => 'call',
'B' => 'subscription',
'C' => 'card_type',
'D' => 'card_validity',
/*
'N' => 'card_fnumber',
'J' => 'card_lnumber',
'O' => 'card_3dsecure',
'F' => 'card_3dsecurestate',
'H' => 'card_imprint',
'Q' => 'transaction_time',
'S' => 'transaction_number',
'W' => 'transaction_processtime',
'Z' => 'mixed_index',
*/
'E' => 'error',
'I' => 'country',
'Y' => 'bank_country',
'K' => 'hash',
];
return implode(';', array_map(
function ($k, $v) { return sprintf('%s:%s', $v, $k); },
array_keys($codeMap),
$codeMap
));
}
/**
* Build payment gateway options.
*
* @method buildOptions
*/
private function buildOptions(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction
): array {
return [
'PBX_SITE' => $paymentGatewayConfiguration->get('client_site'),
'PBX_RANG' => $paymentGatewayConfiguration->get('client_rang'),
'PBX_IDENTIFIANT' => $paymentGatewayConfiguration->get('client_id'),
'PBX_TOTAL' => $transaction->getAmount(),
'PBX_DEVISE' => (new ISO4217())->findByAlpha3($transaction->getCurrencyCode())->getNumeric(),
'PBX_CMD' => $transaction->getId(),
'PBX_PORTEUR' => 'me@mail.com',
'PBX_EFFECTUE' => $paymentGatewayConfiguration->get('return_url'),
'PBX_REFUSE' => $paymentGatewayConfiguration->get('return_url'),
'PBX_ANNULE' => $paymentGatewayConfiguration->get('return_url'),
'PBX_HASH' => 'sha512',
'PBX_RUF1' => 'POST',
'PBX_REPONDRE_A' => $paymentGatewayConfiguration->get('callback_url'),
'PBX_RETOUR' => $this->getPayboxReturnString(),
'PBX_TIME' => date('c'),
'PBX_TYPEPAIEMENT' => 'CARTE',
'PBX_TYPECARTE' => 'CB',
];
}
/**
* {@inheritdoc}
*/
public function initialize(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction,
array $options = []
): array {
$options = $this->buildOptions($paymentGatewayConfiguration, $transaction);
$builtOptions = [
'options' => $options,
'build' => implode('&', array_map(
function ($k, $v) { return sprintf('%s=%s', $k, $v); },
array_keys($options),
$options
)),
];
$binKey = file_get_contents($this->getKeyPath($options['PBX_SITE']));
$builtOptions['options']['PBX_HMAC'] = strtoupper(
hash_hmac($builtOptions['options']['PBX_HASH'], $builtOptions['build'], $binKey)
);
return [
'url' => $this->getServerUrl(),
'options' => $builtOptions['options'],
];
}
/**
* {@inheritdoc}
*/
public function buildHTMLView(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction,
array $options = []
): string {
$initializationData = $this->initialize($paymentGatewayConfiguration, $transaction);
return $this->templating->render('@IDCIPayment/Gateway/paybox.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('Paybox : Payment Gateway error (Request method should be POST)');
}
$gatewayResponse = (new GatewayResponse())
->setDate(new \DateTime())
->setStatus(PaymentStatus::STATUS_FAILED)
->setAmount($request->get('amount'))
->setTransactionUuid($request->get('reference'))
->setRaw($request->request->all())
;
if ('00000' !== $request->get('error')) {
return $gatewayResponse->setMessage('Transaction unauthorized');
}
$publicKey = openssl_pkey_get_public(
(new Client())->request('GET', $this->publicKeyUrl)->getBody()
);
$data = $request->request->all();
unset($data['hash']);
$builtQuery = implode('&', array_map(
function ($k, $v) {
return sprintf('%s=%s', $k, urlencode($v));
},
array_keys($data),
$data
));
if (!openssl_verify($builtQuery, base64_decode($request->get('hash')), $publicKey, 'sha1WithRSAEncryption')) {
return $gatewayResponse->setMessage('Could not verify the integrity of paybox return response');
}
openssl_free_key($publicKey);
return $gatewayResponse->setStatus(PaymentStatus::STATUS_APPROVED);
}
/**
* {@inheritdoc}
*/
public static function getParameterNames(): ?array
{
return array_merge(
parent::getParameterNames(),
[
'client_id',
'client_rang',
'client_site',
]
);
}
}