8
8
9
9
class Client
10
10
{
11
+ const HEADER_SEQURA_MERCHANT_ID = 'Sequra-Merchant-Id ' ;
12
+ const HEADER_ACCEPT = 'Accept ' ;
13
+ const HEADER_CONTENT_TYPE = 'Content-Type ' ;
14
+ const HEADER_CONTENT_LENGTH = 'Content-Length ' ;
15
+ const TYPE_JSON = 'application/json ' ;
16
+ const TYPE_HTML = 'text/html ' ;
11
17
12
18
public static $ endpoint = '' ;
13
19
public static $ user = '' ;
@@ -22,11 +28,60 @@ class Client
22
28
23
29
private $ success = false ;
24
30
private $ cart_has_changed ;
25
- private $ headers ;
31
+ private $ response_headers ;
26
32
private $ status ;
27
33
private $ curl_result = null ;
28
34
private $ json = null ;
29
35
private $ ch = null ;
36
+ /**
37
+ * An array to store request headers.
38
+ * @var array<string, string>
39
+ */
40
+ private $ request_headers ;
41
+
42
+ /**
43
+ * Sets a request header for the cURL request.
44
+ *
45
+ * @param string $name The name of the header. Must not be empty.
46
+ * @param string $value The value of the header. Must not be empty.
47
+ */
48
+ private function setRequestHeader ($ name , $ value )
49
+ {
50
+ if (!is_array ($ this ->request_headers )) {
51
+ $ this ->resetRequestHeaders ();
52
+ }
53
+ $ name = trim ((string ) $ name );
54
+ $ value = trim ((string ) $ value );
55
+ if ($ value === '' || $ name === '' ) {
56
+ return ;
57
+ }
58
+ $ this ->request_headers [$ name ] = $ value ;
59
+ }
60
+
61
+ /**
62
+ * Returns the request headers as an array of strings formatted as "Name: Value".
63
+ *
64
+ * @return array<string> An array of request headers formatted as "Name: Value".
65
+ */
66
+ private function getRequestHeaders ()
67
+ {
68
+ if (!is_array ($ this ->request_headers )) {
69
+ return array ();
70
+ }
71
+ $ headers = array ();
72
+ foreach ($ this ->request_headers as $ name => $ value ) {
73
+ $ headers [] = $ name . ': ' . $ value ;
74
+ }
75
+ return $ headers ;
76
+ }
77
+
78
+ /**
79
+ * Reset the request headers to an empty array.
80
+ */
81
+ private function resetRequestHeaders ()
82
+ {
83
+ $ this ->request_headers = array ();
84
+ }
30
85
31
86
public function __construct ($ user = null , $ password = null , $ endpoint = null , $ debug = false , $ logfile = null )
32
87
{
@@ -42,14 +97,15 @@ public function __construct($user = null, $password = null, $endpoint = null, $d
42
97
$ this ->log (self ::class . " created! " );
43
98
}
44
99
45
- public function isValidAuth ($ merchant = '' )
100
+ public function isValidAuth ($ merchant_id = '' )
46
101
{
47
102
$ this ->initCurl (
48
103
$ this ->_endpoint .
49
- '/merchants ' .
50
- ( $ merchant ? '/ ' . urlencode ($ merchant ): '' ).
51
- '/credentials '
104
+ '/merchants ' .
105
+ ( $ merchant_id ? '/ ' . urlencode ($ merchant_id ) : '' ) .
106
+ '/credentials '
52
107
);
108
+ $ this ->setRequestHeader (self ::HEADER_SEQURA_MERCHANT_ID , $ merchant_id );
53
109
curl_setopt ($ this ->ch , CURLOPT_CUSTOMREQUEST , 'GET ' );
54
110
$ this ->sendRequest ();
55
111
$ this ->dealWithResponse ();
@@ -63,6 +119,7 @@ public function startSolicitation($order)
63
119
}
64
120
65
121
$ this ->initCurl ($ this ->_endpoint . '/orders ' );
122
+ $ this ->setRequestHeader (self ::HEADER_SEQURA_MERCHANT_ID , $ order ['merchant ' ]['id ' ] ?? '' );
66
123
$ this ->verbThePayload ('POST ' , array ('order ' => $ order ));
67
124
$ this ->dealWithResponse ();
68
125
curl_close ($ this ->ch );
@@ -80,25 +137,38 @@ public function qualifyForSolicitation($order)
80
137
return true ;
81
138
}
82
139
83
- public function getIdentificationForm ($ uri , $ options = array ())
140
+ /**
141
+ * Retrieves the identification form for a given URI and options.
142
+ *
143
+ * @param string $uri The URI to retrieve the identification form from.
144
+ * @param array $options An associative array of options for the form.
145
+ * @param string $merchant_id The merchant ID to be used in the request.
146
+ *
147
+ * @return string The HTML content of the identification form.
148
+ */
149
+ public function getIdentificationForm ($ uri , $ options = array (), $ merchant_id = '' )
84
150
{
85
151
$ options ["product " ] = array_key_exists ('product ' , $ options ) ? $ options ["product " ] : "i1 " ;
86
152
$ options ["ajax " ] = (isset ($ options ["ajax " ]) && $ options ["ajax " ]) ? "true " : "false " ;
87
153
$ this ->initCurl ($ uri . '/form_v2 ' . '? ' . http_build_query ($ options ));
88
154
curl_setopt ($ this ->ch , CURLOPT_CUSTOMREQUEST , 'GET ' );
89
- curl_setopt ($ this ->ch , CURLOPT_HTTPHEADER , array ('Accept: text/html ' ));
155
+
156
+ $ this ->setRequestHeader (self ::HEADER_ACCEPT , self ::TYPE_HTML );
157
+ $ this ->setRequestHeader (self ::HEADER_SEQURA_MERCHANT_ID , $ merchant_id );
158
+
90
159
$ this ->sendRequest ();
91
160
$ this ->dealWithResponse ();
92
161
curl_close ($ this ->ch );
93
162
return $ this ->curl_result ;
94
163
}
95
164
96
- public function sendIdentificationForm ($ uri , $ options = array ())
165
+ public function sendIdentificationForm ($ uri , $ options = array (), $ merchant_id = '' )
97
166
{
98
167
$ options ["product " ] = array_key_exists ('product ' , $ options ) ? $ options ["product " ] : "i1 " ;
99
168
$ options ["product_code " ] = $ options ["product " ];
100
169
$ options ["channel " ] = array_key_exists ('channel ' , $ options ) ? $ options ["channel " ] : "sms " ;
101
170
$ this ->initCurl ($ uri . '/form_deliveries ' );
171
+ $ this ->setRequestHeader (self ::HEADER_SEQURA_MERCHANT_ID , $ merchant_id );
102
172
$ this ->verbThePayload ('POST ' , $ options );
103
173
$ this ->dealWithResponse ();
104
174
curl_close ($ this ->ch );
@@ -112,16 +182,20 @@ public function startCards($order)
112
182
}
113
183
114
184
$ this ->initCurl ($ this ->_endpoint . '/cards ' );
185
+ $ this ->setRequestHeader (self ::HEADER_SEQURA_MERCHANT_ID , $ order ['merchant ' ]['id ' ] ?? '' );
115
186
$ this ->verbThePayload ('POST ' , array ('order ' => $ order ));
116
187
$ this ->dealWithResponse ();
117
188
curl_close ($ this ->ch );
118
189
}
119
190
120
- public function getCardsForm ($ uri , $ options = array ())
191
+ public function getCardsForm ($ uri , $ options = array (), $ merchant_id = '' )
121
192
{
122
193
$ this ->initCurl ($ uri . '? ' . http_build_query ($ options ));
123
194
curl_setopt ($ this ->ch , CURLOPT_CUSTOMREQUEST , 'GET ' );
124
- curl_setopt ($ this ->ch , CURLOPT_HTTPHEADER , array ('Accept: text/html ' ));
195
+
196
+ $ this ->setRequestHeader (self ::HEADER_ACCEPT , self ::TYPE_HTML );
197
+ $ this ->setRequestHeader (self ::HEADER_SEQURA_MERCHANT_ID , $ merchant_id );
198
+
125
199
$ this ->sendRequest ();
126
200
$ this ->dealWithResponse ();
127
201
curl_close ($ this ->ch );
@@ -138,51 +212,68 @@ public function qualifyForstartCards($order)
138
212
);
139
213
}
140
214
141
- public function getMerchantPaymentMethods ($ merchant )
215
+ public function getMerchantPaymentMethods ($ merchant_id )
142
216
{
143
- $ this ->getPaymentMethods ($ this ->_endpoint . '/merchants/ ' . $ merchant );
217
+ $ this ->getPaymentMethods ($ this ->_endpoint . '/merchants/ ' . $ merchant_id , array (), $ merchant_id );
144
218
}
145
219
146
- public function getPaymentMethods ($ uri , $ options = array ())
220
+ public function getPaymentMethods ($ uri , $ options = array (), $ merchant_id = '' )
147
221
{
148
222
if (!preg_match ('!^https?://! ' , $ uri )) {
149
223
$ uri = $ this ->_endpoint . '/orders/ ' . $ uri ;
150
224
}
151
225
$ this ->initCurl ($ uri . '/payment_methods ' . (count ($ options ) > 0 ? '? ' . http_build_query ($ options ) : '' ));
226
+
227
+ $ this ->setRequestHeader (self ::HEADER_ACCEPT , self ::TYPE_JSON );
228
+ $ this ->setRequestHeader (self ::HEADER_SEQURA_MERCHANT_ID , $ merchant_id );
229
+
152
230
curl_setopt ($ this ->ch , CURLOPT_CUSTOMREQUEST , 'GET ' );
153
- curl_setopt ($ this ->ch , CURLOPT_HTTPHEADER , array ('Accept: application/json ' ));
154
231
$ this ->sendRequest ();
155
232
$ this ->dealWithResponse ();
156
233
curl_close ($ this ->ch );
157
234
}
158
235
159
- public function getAvailableDisbursements ($ merchant ) {
160
- $ this ->initCurl ($ this ->_endpoint . '/merchants/ ' . $ merchant . '/disbursements ' );
236
+ public function getAvailableDisbursements ($ merchant_id )
237
+ {
238
+ $ this ->initCurl ($ this ->_endpoint . '/merchants/ ' . $ merchant_id . '/disbursements ' );
161
239
curl_setopt ($ this ->ch , CURLOPT_CUSTOMREQUEST , 'GET ' );
162
- curl_setopt ($ this ->ch , CURLOPT_HTTPHEADER , array ('Accept: application/json ' ));
240
+
241
+ $ this ->setRequestHeader (self ::HEADER_ACCEPT , self ::TYPE_JSON );
242
+ $ this ->setRequestHeader (self ::HEADER_SEQURA_MERCHANT_ID , $ merchant_id );
243
+
163
244
$ this ->sendRequest ();
164
245
$ this ->dealWithResponse ();
165
246
curl_close ($ this ->ch );
166
247
}
167
248
168
- public function getDisbursementDetails ($ path ) {
249
+ public function getDisbursementDetails ($ path )
250
+ {
169
251
$ this ->initCurl ($ this ->_endpoint . $ path );
170
252
curl_setopt ($ this ->ch , CURLOPT_CUSTOMREQUEST , 'GET ' );
171
- curl_setopt ($ this ->ch , CURLOPT_HTTPHEADER , array ('Accept: application/json ' ));
253
+
254
+ $ this ->setRequestHeader (self ::HEADER_ACCEPT , self ::TYPE_JSON );
255
+
256
+ if (preg_match ('~merchants/([^/]+)/disbursements~ ' , $ path , $ matches )) {
257
+ $ this ->setRequestHeader (self ::HEADER_SEQURA_MERCHANT_ID , $ matches [1 ]);
258
+ }
259
+
172
260
$ this ->sendRequest ();
173
261
$ this ->dealWithResponse ();
174
262
curl_close ($ this ->ch );
175
263
}
176
264
177
- public function getCreditAgreements ($ amount , $ merchant , $ locale = 'es-ES ' , $ country = 'ES ' , $ currency = 'EUR ' )
265
+ public function getCreditAgreements ($ amount , $ merchant_id , $ locale = 'es-ES ' , $ country = 'ES ' , $ currency = 'EUR ' )
178
266
{
179
267
$ uri = $ this ->_endpoint .
180
- '/merchants/ ' . urlencode ($ merchant ) .
268
+ '/merchants/ ' . urlencode ($ merchant_id ) .
181
269
'/credit_agreements?total_with_tax= ' . urlencode ($ amount ) .
182
270
'¤cy= ' . urlencode ($ currency ) .
183
271
'&locale= ' . urlencode ($ locale ) .
184
272
'&country= ' . urlencode ($ country );
185
273
$ this ->initCurl ($ uri );
274
+
275
+ $ this ->setRequestHeader (self ::HEADER_SEQURA_MERCHANT_ID , $ merchant_id );
276
+
186
277
curl_setopt ($ this ->ch , CURLOPT_CUSTOMREQUEST , 'GET ' );
187
278
$ this ->sendRequest ();
188
279
$ this ->dealWithResponse ();
@@ -195,6 +286,7 @@ public function updateOrder($uri, $order, $verb = 'PUT')
195
286
$ uri = $ this ->_endpoint . '/orders/ ' . $ uri ;
196
287
}
197
288
$ this ->initCurl ($ uri );
289
+ $ this ->setRequestHeader (self ::HEADER_SEQURA_MERCHANT_ID , $ order ['merchant ' ]['id ' ] ?? '' );
198
290
$ this ->verbThePayload ($ verb , array ('order ' => $ order ));
199
291
$ this ->dealWithResponse ();
200
292
if ($ this ->status == 409 ) {
@@ -206,6 +298,7 @@ public function updateOrder($uri, $order, $verb = 'PUT')
206
298
public function sendDeliveryReport ($ delivery_report )
207
299
{
208
300
$ this ->initCurl ($ this ->_endpoint . '/delivery_reports ' );
301
+ $ this ->setRequestHeader (self ::HEADER_SEQURA_MERCHANT_ID , $ delivery_report ['merchant ' ]['id ' ] ?? '' );
209
302
$ this ->verbThePayload ('POST ' , array ('delivery_report ' => $ delivery_report ));
210
303
$ this ->dealWithResponse ();
211
304
curl_close ($ this ->ch );
@@ -217,6 +310,7 @@ public function orderUpdate($order)
217
310
'/merchants/ ' . urlencode ($ order ['merchant ' ]['id ' ]) .
218
311
'/orders/ ' . urlencode ($ order ['merchant_reference ' ]['order_ref_1 ' ]);
219
312
$ this ->initCurl ($ uri );
313
+ $ this ->setRequestHeader (self ::HEADER_SEQURA_MERCHANT_ID , $ order ['merchant ' ]['id ' ] ?? '' );
220
314
$ this ->verbThePayload ('PUT ' , array ('order ' => $ order ));
221
315
$ this ->dealWithResponse ();
222
316
if ($ this ->status == 409 ) {
@@ -262,7 +356,7 @@ public function getRawResult()
262
356
263
357
public function getOrderUri ()
264
358
{
265
- if ($ this ->headers && preg_match ('/^Location:\s+([^\n\r]+)/mi ' , $ this ->headers , $ m )) {
359
+ if ($ this ->response_headers && preg_match ('/^Location:\s+([^\n\r]+)/mi ' , $ this ->response_headers , $ m )) {
266
360
return $ m [1 ];
267
361
}
268
362
}
@@ -281,6 +375,11 @@ public function dump()
281
375
282
376
// Private methods below
283
377
378
+ /**
379
+ * Initializes the cURL session with the given URL and sets the necessary options.
380
+ *
381
+ * @param string $url The URL to initialize the cURL session with.
382
+ */
284
383
private function initCurl ($ url )
285
384
{
286
385
$ this ->success = $ this ->json = null ;
@@ -293,24 +392,20 @@ private function initCurl($url)
293
392
// http://www.supermind.org/blog/763/solved-curl-56-received-problem-2-in-the-chunky-parser
294
393
curl_setopt ($ this ->ch , CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_0 );
295
394
// From http://it.toolbox.com/wiki/index.php/Use_curl_from_PHP_-_processing_response_headers
296
- curl_setopt ($ this ->ch , CURLOPT_HEADERFUNCTION , array (&$ this , 'storeHeaders ' ));
297
- $ this ->headers = '' ;
395
+ curl_setopt ($ this ->ch , CURLOPT_HEADERFUNCTION , array (&$ this , 'storeResponseHeaders ' ));
396
+ $ this ->response_headers = '' ;
298
397
}
299
398
300
399
private function verbThePayload ($ verb , $ payload )
301
400
{
302
401
$ data_string = json_encode (Helper::removeNulls ($ payload ));
303
402
curl_setopt ($ this ->ch , CURLOPT_CUSTOMREQUEST , $ verb );
304
403
curl_setopt ($ this ->ch , CURLOPT_POSTFIELDS , $ data_string );
305
- curl_setopt (
306
- $ this ->ch ,
307
- CURLOPT_HTTPHEADER ,
308
- array (
309
- 'Accept: application/json ' ,
310
- 'Content-Type: application/json ' ,
311
- 'Content-Length: ' . strlen ($ data_string )
312
- )
313
- );
404
+
405
+ $ this ->setRequestHeader (self ::HEADER_ACCEPT , self ::TYPE_JSON );
406
+ $ this ->setRequestHeader (self ::HEADER_CONTENT_TYPE , self ::TYPE_JSON );
407
+ $ this ->setRequestHeader (self ::HEADER_CONTENT_LENGTH , (string ) strlen ($ data_string ));
408
+
314
409
$ this ->sendRequest ();
315
410
}
316
411
@@ -320,11 +415,19 @@ private function sendRequest()
320
415
if ($ this ->_debug ) {
321
416
curl_setopt ($ this ->ch , CURLOPT_SSL_VERIFYPEER , false );
322
417
}
418
+
419
+ $ headers = $ this ->getRequestHeaders ();
420
+ if (!empty ($ headers )) {
421
+ curl_setopt ($ this ->ch , CURLOPT_HTTPHEADER , $ headers );
422
+ }
423
+ $ this ->resetRequestHeaders ();
424
+
323
425
$ this ->curl_result = curl_exec ($ this ->ch );
426
+
324
427
if ($ this ->curl_result === false ) {
325
428
$ this ->log (
326
429
"cURL error: " . curl_errno ($ this ->ch ) .
327
- " msg: " . curl_error ($ this ->ch )
430
+ " msg: " . curl_error ($ this ->ch )
328
431
);
329
432
}
330
433
$ this ->status = curl_getinfo ($ this ->ch , CURLINFO_HTTP_CODE );
@@ -354,9 +457,9 @@ private function log($msg)
354
457
}
355
458
}
356
459
357
- private function storeHeaders ($ ch , $ header )
460
+ private function storeResponseHeaders ($ ch , $ header )
358
461
{
359
- $ this ->headers .= $ header ;
462
+ $ this ->response_headers .= $ header ;
360
463
361
464
return strlen ($ header );
362
465
}
0 commit comments