1
1
<?php
2
+
2
3
/**
3
4
* Copyright © 2017 SeQura Engineering. All rights reserved.
4
5
*/
@@ -43,25 +44,18 @@ public function isValidAuth()
43
44
$ this ->initCurl ($ this ->_endpoint . '/orders ' );
44
45
curl_setopt ($ this ->ch , CURLOPT_CUSTOMREQUEST , 'GET ' );
45
46
$ this ->sendRequest ();
46
- return $ this ->status != 401 ;
47
+ return $ this ->status != 401 ;
47
48
}
48
49
49
50
public function startSolicitation ($ order )
50
51
{
51
- if ( ! $ this ->qualifyForSolicitation ($ order ) && ! $ this ->_debug ) {
52
+ if (! $ this ->qualifyForSolicitation ($ order ) && !$ this ->_debug ) {
52
53
return ;
53
54
}
54
55
55
56
$ this ->initCurl ($ this ->_endpoint . '/orders ' );
56
57
$ this ->verbThePayload ('POST ' , array ('order ' => $ order ));
57
- if ($ this ->status == 204 ) {
58
- $ this ->success = true ;
59
- $ this ->log ("Start " . $ this ->status . ": Ok! " );
60
- } elseif ($ this ->status >= 200 && $ this ->status <= 299 || $ this ->status == 409 ) {
61
- $ this ->success = false ;
62
- $ this ->json = json_decode ($ this ->curl_result , true ); // return array, not object
63
- $ this ->log ("Start " . $ this ->status . ": " . $ this ->curl_result );
64
- }
58
+ $ this ->dealWithResponse ();
65
59
curl_close ($ this ->ch );
66
60
}
67
61
@@ -70,7 +64,7 @@ public function qualifyForSolicitation($order)
70
64
if ($ order ['cart ' ]['order_total_with_tax ' ] <= 0 ) {
71
65
return false ;
72
66
}
73
- if ( ! Helper::isConsistentCart ($ order ['cart ' ])) {
67
+ if (! Helper::isConsistentCart ($ order ['cart ' ])) {
74
68
return false ;
75
69
}
76
70
@@ -98,7 +92,10 @@ private function verbThePayload($verb, $payload)
98
92
$ data_string = json_encode (Helper::removeNulls ($ payload ));
99
93
curl_setopt ($ this ->ch , CURLOPT_CUSTOMREQUEST , $ verb );
100
94
curl_setopt ($ this ->ch , CURLOPT_POSTFIELDS , $ data_string );
101
- curl_setopt ($ this ->ch , CURLOPT_HTTPHEADER , array (
95
+ curl_setopt (
96
+ $ this ->ch ,
97
+ CURLOPT_HTTPHEADER ,
98
+ array (
102
99
'Accept: application/json ' ,
103
100
'Content-Type: application/json ' ,
104
101
'Content-Length: ' . strlen ($ data_string )
@@ -109,19 +106,32 @@ private function verbThePayload($verb, $payload)
109
106
110
107
private function sendRequest ()
111
108
{
109
+ $ this ->succeess = false ;
112
110
if ($ this ->_debug ) {
113
111
curl_setopt ($ this ->ch , CURLOPT_SSL_VERIFYPEER , false );
114
112
}
115
113
$ this ->curl_result = curl_exec ($ this ->ch );
116
114
$ this ->status = curl_getinfo ($ this ->ch , CURLINFO_HTTP_CODE );
117
115
}
118
116
119
- function log ($ msg )
117
+ private function dealWithResponse ()
118
+ {
119
+ if (200 <= $ this ->status && $ this ->status <= 299 ) {
120
+ $ this ->success = true ;
121
+ $ this ->log ("Start " . $ this ->status . ": Ok! " );
122
+ } else {
123
+ $ this ->success = false ;
124
+ $ this ->json = json_decode ($ this ->curl_result , true );
125
+ $ this ->log ("Start " . $ this ->status . ": " . $ this ->curl_result );
126
+ }
127
+ }
128
+
129
+ private function log ($ msg )
120
130
{
121
- if ( ! $ this ->_debug ) {
131
+ if (! $ this ->_debug ) {
122
132
return ;
123
133
}
124
- if ( ! $ this ->_logfile ) {
134
+ if (! $ this ->_logfile ) {
125
135
error_log ($ msg );
126
136
} else {
127
137
error_log ($ msg . "\n" , 3 , $ this ->_logfile );
@@ -136,15 +146,7 @@ public function getIdentificationForm($uri, $options = array())
136
146
curl_setopt ($ this ->ch , CURLOPT_CUSTOMREQUEST , 'GET ' );
137
147
curl_setopt ($ this ->ch , CURLOPT_HTTPHEADER , array ('Accept: text/html ' ));
138
148
$ this ->sendRequest ();
139
-
140
- if ($ this ->status >= 200 && $ this ->status <= 299 ) {
141
- curl_close ($ this ->ch );
142
- $ this ->success = true ;
143
- return $ this ->curl_result ;
144
- } else {
145
- $ this ->success = false ;
146
- $ this ->log ("Error " . $ this ->status . ": " . print_r ($ this ->curl_result , true ));
147
- }
149
+ $ this ->dealWithResponse ();
148
150
curl_close ($ this ->ch );
149
151
}
150
152
@@ -155,15 +157,7 @@ public function sendIdentificationForm($uri, $options = array())
155
157
$ options ["channel " ] = array_key_exists ('channel ' , $ options ) ? $ options ["channel " ] : "sms " ;
156
158
$ this ->initCurl ($ uri . '/form_deliveries ' );
157
159
$ this ->verbThePayload ('POST ' , $ options );
158
-
159
- if ($ this ->status >= 200 && $ this ->status <= 299 ) {
160
- curl_close ($ this ->ch );
161
- $ this ->success = true ;
162
- return $ this ->curl_result ;
163
- } else {
164
- $ this ->success = false ;
165
- $ this ->log ("Error " . $ this ->status . ": " . print_r ($ this ->curl_result , true ));
166
- }
160
+ $ this ->dealWithResponse ();
167
161
curl_close ($ this ->ch );
168
162
}
169
163
@@ -175,14 +169,7 @@ public function startCards($order)
175
169
176
170
$ this ->initCurl ($ this ->_endpoint . '/cards ' );
177
171
$ this ->verbThePayload ('POST ' , array ('order ' => $ order ));
178
- if ($ this ->status == 204 ) {
179
- $ this ->success = true ;
180
- $ this ->log ("Start " . $ this ->status . ": Ok! " );
181
- } elseif ($ this ->status >= 200 && $ this ->status <= 299 || $ this ->status == 409 ) {
182
- $ this ->success = false ;
183
- $ this ->json = json_decode ($ this ->curl_result , true ); // return array, not object
184
- $ this ->log ("Start " . $ this ->status . ": " . $ this ->curl_result );
185
- }
172
+ $ this ->dealWithResponse ();
186
173
curl_close ($ this ->ch );
187
174
}
188
175
@@ -192,26 +179,18 @@ public function getCardsForm($uri, $options = array())
192
179
curl_setopt ($ this ->ch , CURLOPT_CUSTOMREQUEST , 'GET ' );
193
180
curl_setopt ($ this ->ch , CURLOPT_HTTPHEADER , array ('Accept: text/html ' ));
194
181
$ this ->sendRequest ();
195
- $ this ->success = false ;
196
- if ($ this ->status >= 200 && $ this ->status <= 299 ) {
197
- $ this ->success = true ;
198
- $ this ->json = json_decode ($ this ->curl_result , true ); // return array, not object
199
- } elseif ($ this ->status >= 400 && $ this ->status <= 499 ) {
200
- $ this ->log ("Error " . $ this ->status . ": " . print_r ($ this ->curl_result , true ));
201
- } else {
202
- $ this ->log ("Error " . $ this ->status . ": " . print_r ($ this ->curl_result , true ));
203
- }
182
+ $ this ->dealWithResponse ();
204
183
curl_close ($ this ->ch );
205
184
}
206
185
207
186
public function qualifyForstartCards ($ order )
208
187
{
209
188
return
210
- isset ($ order ['customer ' ]['ref ' ]) && $ order ['customer ' ]['ref ' ]!= '' &&
211
- isset ($ order ['customer ' ]['email ' ]) && $ order ['customer ' ]['email ' ]!= '' &&
189
+ isset ($ order ['customer ' ]['ref ' ]) && $ order ['customer ' ]['ref ' ] != '' &&
190
+ isset ($ order ['customer ' ]['email ' ]) && $ order ['customer ' ]['email ' ] != '' &&
212
191
(
213
- (isset ($ order ['delivery_address ' ]['mobile_phone ' ]) && $ order ['delivery_address ' ]['mobile_phone ' ]!= '' ) ||
214
- (isset ($ order ['delivery_address ' ]['phone ' ]) && $ order ['delivery_address ' ]['phone ' ]!= '' )
192
+ (isset ($ order ['delivery_address ' ]['mobile_phone ' ]) && $ order ['delivery_address ' ]['mobile_phone ' ] != '' ) ||
193
+ (isset ($ order ['delivery_address ' ]['phone ' ]) && $ order ['delivery_address ' ]['phone ' ] != '' )
215
194
);
216
195
}
217
196
@@ -222,19 +201,11 @@ public function getMerchantPaymentMethods($merchant)
222
201
223
202
public function getPaymentMethods ($ uri , $ options = array ())
224
203
{
225
- $ this ->initCurl ($ uri . '/payment_methods ' . (count ($ options )> 0 ? '? ' . http_build_query ($ options ): '' ));
204
+ $ this ->initCurl ($ uri . '/payment_methods ' . (count ($ options ) > 0 ? '? ' . http_build_query ($ options ) : '' ));
226
205
curl_setopt ($ this ->ch , CURLOPT_CUSTOMREQUEST , 'GET ' );
227
206
curl_setopt ($ this ->ch , CURLOPT_HTTPHEADER , array ('Accept: text/html ' ));
228
207
$ this ->sendRequest ();
229
- $ this ->success = false ;
230
- if ($ this ->status >= 200 && $ this ->status <= 299 ) {
231
- $ this ->success = true ;
232
- $ this ->json = json_decode ($ this ->curl_result , true ); // return array, not object
233
- } elseif ($ this ->status >= 400 && $ this ->status <= 499 ) {
234
- $ this ->log ("Error " . $ this ->status . ": " . print_r ($ this ->curl_result , true ));
235
- } else {
236
- $ this ->log ("Error " . $ this ->status . ": " . print_r ($ this ->curl_result , true ));
237
- }
208
+ $ this ->dealWithResponse ();
238
209
curl_close ($ this ->ch );
239
210
}
240
211
@@ -244,31 +215,20 @@ public function getCreditAgreements($amount, $merchant)
244
215
$ this ->initCurl ($ uri );
245
216
curl_setopt ($ this ->ch , CURLOPT_CUSTOMREQUEST , 'GET ' );
246
217
$ this ->sendRequest ();
247
- if ($ this ->status >= 200 && $ this ->status <= 299 ) {
248
- $ this ->success = true ;
249
- curl_close ($ this ->ch );
250
- return json_decode ($ this ->curl_result , true );
251
- } else {
252
- $ this ->success = false ;
253
- $ this ->log ("Error " . $ this ->status . ": " . print_r ($ this ->curl_result , true ));
254
- }
218
+ $ this ->dealWithResponse ();
255
219
curl_close ($ this ->ch );
256
220
}
257
221
258
222
public function updateOrder ($ uri , $ order , $ verb = 'PUT ' )
259
223
{
260
- if ( ! preg_match ('!^https?://! ' , $ uri )) {
224
+ if (! preg_match ('!^https?://! ' , $ uri )) {
261
225
$ uri = $ this ->_endpoint . '/orders/ ' . $ uri ;
262
226
}
263
227
$ this ->initCurl ($ uri );
264
228
$ this ->verbThePayload ($ verb , array ('order ' => $ order ));
265
-
266
- if ($ this ->status >= 200 && $ this ->status <= 299 ) {
267
- $ this ->success = true ;
268
- } elseif ($ this ->status == 409 ) {
269
- $ this ->success = false ;
229
+ $ this ->dealWithResponse ();
230
+ if ($ this ->status == 409 ) {
270
231
$ this ->cart_has_changed = true ;
271
- $ this ->json = json_decode ($ this ->curl_result , true );
272
232
}
273
233
curl_close ($ this ->ch );
274
234
}
@@ -277,33 +237,18 @@ public function sendDeliveryReport($delivery_report)
277
237
{
278
238
$ this ->initCurl ($ this ->_endpoint . '/delivery_reports ' );
279
239
$ this ->verbThePayload ('POST ' , array ('delivery_report ' => $ delivery_report ));
280
-
281
- if ($ this ->status >= 200 && $ this ->status <= 299 ) {
282
- $ this ->success = true ;
283
- $ this ->log ("Delivery " . $ this ->status . ": Ok! " );
284
- } elseif ($ this ->status >= 200 && $ this ->status <= 299 || $ this ->status == 409 ) {
285
- $ this ->success = false ;
286
- $ this ->json = json_decode ($ this ->curl_result , true ); // return array, not object
287
- $ this ->log ("Delivery " . $ this ->status . ": " . print_r ($ this ->json , true ));
288
- }
240
+ $ this ->dealWithResponse ();
289
241
curl_close ($ this ->ch );
290
242
}
291
243
292
244
public function orderUpdate ($ order )
293
245
{
294
246
$ uri = $ this ->_endpoint .
295
- '/merchants/ ' . $ order ['merchant ' ]['id ' ] .
296
- '/orders/ ' . $ order ['merchant_reference ' ]['order_ref_1 ' ];
247
+ '/merchants/ ' . $ order ['merchant ' ]['id ' ] .
248
+ '/orders/ ' . $ order ['merchant_reference ' ]['order_ref_1 ' ];
297
249
$ this ->initCurl ($ uri );
298
250
$ this ->verbThePayload ('PUT ' , array ('order ' => $ order ));
299
-
300
- if ($ this ->status >= 200 && $ this ->status <= 299 ) {
301
- $ this ->success = true ;
302
- } elseif ($ this ->status == 409 ) {
303
- $ this ->success = false ;
304
- $ this ->cart_has_changed = true ;
305
- $ this ->json = json_decode ($ this ->curl_result , true );
306
- }
251
+ $ this ->dealWithResponse ();
307
252
curl_close ($ this ->ch );
308
253
}
309
254
0 commit comments