-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathApi.php
284 lines (218 loc) · 7.85 KB
/
Api.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
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
<?php
namespace Payum\Be2Bill;
use Http\Message\MessageFactory;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\Http\HttpException;
use Payum\Core\Exception\InvalidArgumentException;
use Payum\Core\Exception\LogicException;
use Payum\Core\HttpClientInterface;
class Api
{
public const VERSION = '2.0';
public const EXECCODE_SUCCESSFUL = '0000';
public const EXECCODE_3DSECURE_IDENTIFICATION_REQUIRED = '0001';
public const EXECCODE_PARAMETER_X_MISSING = '1001';
public const EXECCODE_INVALID_PARAMETER_X = '1002';
public const EXECCODE_HASH_ERROR = '1003';
public const EXECCODE_UNSUPPORTED_PROTOCOL = '1004';
public const EXECCODE_ALIAS_NOT_FOUND = '2001';
public const EXECCODE_UNSUCCESSFUL_REFERENCE_TRANSACTION = '2002';
public const EXECCODE_NON_REFUNDABLE_REFERENCE_TRANSACTION = '2003';
public const EXECCODE_REFERENCE_TRANSACTION_NOT_FOUND = '2004';
public const EXECCODE_NOT_ABLE_TO_CAPTURE_THE_REFERENCE_AUTHORIZATION = '2005';
public const EXECCODE_UNFINISHED_REFERENCE_TRANSACTION = '2006';
public const EXECCODE_INVALID_CAPTURE_AMOUNT = '2007';
public const EXECCODE_ACCOUNT_DEACTIVATED = '3001';
public const EXECCODE_UNAUTHORIZED_SERVER_IP_ADDRESS = '3002';
public const EXECCODE_UNAUTHORIZED_TRANSACTION = '3003';
public const EXECCODE_TRANSACTION_REFUSED_BY_THE_BANK = '4001';
public const EXECCODE_INSUFFICIENT_FUNDS = '4002';
public const EXECCODE_CARD_REFUSED_BY_THE_BANK = '4003';
public const EXECCODE_ABORTED_TRANSACTION = '4004';
public const EXECCODE_SUSPECTED_FRAUD = '4005';
public const EXECCODE_CARD_LOST = '4006';
public const EXECCODE_STOLEN_CARD = '4007';
public const EXECCODE_3DSECURE_AUTHENTICATION_FAILED = '4008';
public const EXECCODE_EXPIRED_3DSECURE_AUTHENTICATION = '4009';
public const EXECCODE_INTERNAL_ERROR = '5001';
public const EXECCODE_BANK_ERROR = '5002';
public const EXECCODE_UNDERGOING_MAINTENANCE = '5003';
public const EXECCODE_TIME_OUT = '5004';
/**
* The "payment" function is the basic function that allows collecting from a cardholder.
* This operation collects money directly.
*/
public const OPERATION_PAYMENT = 'payment';
/**
* The "authorization" function allows "freezing" temporarily the funds in a cardholder's bank
* account for 7 days. This application does not debit it.
* This type of operation is mainly used in the world of physical goods ("retail") when the merchant
* decides to debit his customer at merchandise shipping time.
*/
public const OPERATION_AUTHORIZATION = 'authorization';
/**
* The "capture" function allows collecting funds from a cardholder after an authorization
* ("authorization" function). This capture can take place within 7 days after the authorization.
*/
public const OPERATION_CAPTURE = 'capture';
/**
* This dual function is directly managed by the system:
* - Refund: Consists of returning the already collected funds to a cardholder
* - Cancellation: Consists of not sending a payment transaction as compensation
*/
public const OPERATION_REFUND = 'refund';
/**
* The "credit" function allows sending funds to a cardholder.
*/
public const OPERATION_CREDIT = 'credit';
/**
* @var HttpClientInterface
*/
protected $client;
/**
* @var MessageFactory
*/
protected $messageFactory;
/**
* @var array
*/
protected $options = [
'identifier' => null,
'password' => null,
'sandbox' => null,
];
/**
* @throws InvalidArgumentException if an option is invalid
*/
public function __construct(array $options, HttpClientInterface $client, MessageFactory $messageFactory)
{
$options = ArrayObject::ensureArrayObject($options);
$options->defaults($this->options);
$options->validateNotEmpty([
'identifier',
'password',
]);
if (! is_bool($options['sandbox'])) {
throw new LogicException('The boolean sandbox option must be set.');
}
$this->options = $options;
$this->client = $client;
$this->messageFactory = $messageFactory;
}
/**
* @return array
*/
public function payment(array $params)
{
$params['OPERATIONTYPE'] = static::OPERATION_PAYMENT;
$this->addGlobalParams($params);
return $this->doRequest([
'method' => 'payment',
'params' => $params,
]);
}
/**
* Verify if the hash of the given parameter is correct
*
* @return bool
*/
public function verifyHash(array $params)
{
if (empty($params['HASH'])) {
return false;
}
$hash = $params['HASH'];
unset($params['HASH']);
return $hash === $this->calculateHash($params);
}
/**
* @return string
*/
public function getOffsiteUrl()
{
return $this->options['sandbox'] ?
'https://secure-test.be2bill.com/front/form/process.php' :
'https://secure-magenta1.be2bill.com/front/form/process.php'
;
}
/**
* @return array
*/
public function prepareOffsitePayment(array $params)
{
$supportedParams = [
'CLIENTIDENT' => null,
'DESCRIPTION' => null,
'ORDERID' => null,
'AMOUNT' => null,
'CARDTYPE' => null,
'CLIENTEMAIL' => null,
'CARDFULLNAME' => null,
'LANGUAGE' => null,
'EXTRADATA' => null,
'CLIENTDOB' => null,
'CLIENTADDRESS' => null,
'CREATEALIAS' => null,
'3DSECURE' => null,
'3DSECUREDISPLAYMODE' => null,
'USETEMPLATE' => null,
'HIDECLIENTEMAIL' => null,
'HIDEFULLNAME' => null,
];
$params = array_filter(array_replace(
$supportedParams,
array_intersect_key($params, $supportedParams)
));
$params['OPERATIONTYPE'] = static::OPERATION_PAYMENT;
$this->addGlobalParams($params);
return $params;
}
/**
* @return string
*/
public function calculateHash(array $params)
{
#Alpha sort
ksort($params);
$clearString = $this->options['password'];
foreach ($params as $key => $value) {
$clearString .= $key . '=' . $value . $this->options['password'];
}
return hash('sha256', $clearString);
}
/**
* @return array
*/
protected function doRequest(array $fields)
{
$headers = [
'Content-Type' => 'application/x-www-form-urlencoded',
];
$request = $this->messageFactory->createRequest('POST', $this->getApiEndpoint(), $headers, http_build_query($fields));
$response = $this->client->send($request);
if (! ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300)) {
throw HttpException::factory($request, $response);
}
$result = json_decode($response->getBody()->getContents(), null, 512, JSON_THROW_ON_ERROR);
if (null === $result) {
throw new LogicException("Response content is not valid json: \n\n{$response->getBody()->getContents()}");
}
return $result;
}
protected function addGlobalParams(array &$params): void
{
$params['VERSION'] = self::VERSION;
$params['IDENTIFIER'] = $this->options['identifier'];
$params['HASH'] = $this->calculateHash($params);
}
/**
* @return string
*/
protected function getApiEndpoint()
{
return $this->options['sandbox'] ?
'https://secure-test.be2bill.com/front/service/rest/process' :
'https://secure-magenta1.be2bill.com/front/service/rest/process'
;
}
}