-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRequest.php
622 lines (506 loc) · 17.2 KB
/
Request.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
<?php
/**
* @copyright Copyright (c) 2020-2021 Afterpay Corporate Services Pty Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Afterpay\SDK\HTTP;
use Afterpay\SDK\Config;
use Afterpay\SDK\MerchantAccount;
use Afterpay\SDK\HTTP;
use Afterpay\SDK\HTTP\Response;
use Afterpay\SDK\Exception\InvalidArgumentException;
use Afterpay\SDK\Exception\NetworkException;
use Afterpay\SDK\Exception\ParsingException;
class Request extends HTTP
{
use \Afterpay\SDK\Shared\ModelMethods;
/**
* @var \Afterpay\SDK\MerchantAccount $merchant
*/
private $merchant;
/**
* @var \CurlHandle|resource $ch
*/
protected $ch;
/**
* @var string $apiEnvironmentUrl
*/
protected $apiEnvironmentUrl;
/**
* @var string $uri
*/
protected $uri;
/**
* @var array $headers
*/
protected $headers;
/**
* @var \Afterpay\SDK\HTTP\Response $response
*/
protected $response;
/**
* @var int $curl_errno
*/
protected $curl_errno;
/**
* @var string $curl_error
*/
protected $curl_error;
/**
* @var string $mock_mode
*/
private $mock_mode;
/**
* Class constructor
*/
public function __construct(...$args)
{
parent::__construct();
if (count($args) == 1 && $args[ 0 ] instanceof MerchantAccount) {
$this->merchant = $args[ 0 ];
} elseif (count($args) > 0) {
$this->passConstructArgsToMagicSetters(... $args);
}
$this->ch = curl_init();
$this->headers = array();
# Boolean options
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->ch, CURLOPT_HEADER, true);
curl_setopt($this->ch, CURLINFO_HEADER_OUT, true);
# Integer options
curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($this->ch, CURLOPT_TIMEOUT, 70);
$this->configureUserAgent();
}
/**
* @return \Afterpay\SDK\HTTP\Request
*/
private function configureUserAgent()
{
$composer_json = Config::get('composerJson');
$ua_extra_a = HTTP::getPlatformDetailsAsString();
$php_version_str = phpversion();
$curl_version_arr = curl_version();
$curl_version_str = $curl_version_arr[ 'version' ];
$ua_extra_b = '';
$merchant_id = $this->getMerchantAccount()->getMerchantId();
$ua_extra_c = '';
$store_url = HTTP::getStoreUrl();
if (! empty($merchant_id)) {
$ua_extra_b .= "; Merchant/{$merchant_id}";
}
if (! empty($store_url)) {
$ua_extra_c .= " {$store_url}";
}
curl_setopt($this->ch, CURLOPT_USERAGENT, "afterpay-sdk-php/{$composer_json->version} ({$ua_extra_a}PHP/{$php_version_str}; cURL/{$curl_version_str}{$ua_extra_b}){$ua_extra_c}");
return $this;
}
/**
* @return \Afterpay\SDK\MerchantAccount
*/
private function getMerchantAccount()
{
if ($this->merchant instanceof MerchantAccount) {
# First, look for a MerchantAccount instance as a property of this individual object.
# This allows multiple Requests to be instantiated simultaneously,
# each using different credentials.
return $this->merchant;
} else {
# Otherwise, look for credentials as static properties of the parent class.
# This allows credentials to be set once on the class, then used by
# many different Requests.
# If nothing is set on the class yet, as a last resort, try to
# find credentials in the .env.php configuration file.
$merchant = new MerchantAccount();
if (is_null(self::getMerchantId())) {
self::setMerchantId(Config::get('merchantId'));
}
if (is_null(self::getSecretKey())) {
self::setSecretKey(Config::get('secretKey'));
}
if (is_null(self::getCountryCode())) {
self::setCountryCode(Config::get('countryCode'));
}
if (is_null(self::getApiEnvironment())) {
self::setApiEnvironment(Config::get('apiEnvironment'));
}
$merchant
->setMerchantId(self::getMerchantId())
->setSecretKey(self::getSecretKey())
->setCountryCode(self::getCountryCode())
->setApiEnvironment(self::getApiEnvironment())
;
return $merchant;
}
}
/**
* Allows the retrieval of getMerchantAccount()->getCountryCode()
* from the linked Response class. (self::getMerchantAccount is private.)
*
* @return string
*/
public function getMerchantAccountCountryCode()
{
$merchant = $this->getMerchantAccount();
return $merchant->getCountryCode();
}
/**
* Allows the retrieval of getMerchantAccount()->getApiEnvironment()
* from the linked Response class. (self::getMerchantAccount is private.)
*
* @return string
*/
public function getMerchantAccountApiEnvironment()
{
$merchant = $this->getMerchantAccount();
return $merchant->getApiEnvironment();
}
/**
* @param \Afterpay\SDK\MerchantAccount $merchant
* @return \Afterpay\SDK\HTTP\Request
* @throws \Afterpay\SDK\Exception\InvalidArgumentException
*/
public function setMerchantAccount($merchant)
{
if (! $merchant instanceof MerchantAccount) {
$type = gettype($merchant);
if ($type == 'object') {
$type = get_class($merchant);
}
throw new InvalidArgumentException("Afterpay\SDK\MerchantAccount expected; {$type} given");
}
$this->merchant = $merchant;
$this->setUri($this->uri); # If the Country Code or API Environment has changed we'll need to update the CURLOPT_URL option
$this->configureBasicAuth(); # If the MerchantAccount credentials have changed we'll need to update the CURLOPT_USERPWD option
$this->configureUserAgent(); # If the Merchant ID has changed we'll need to update the CURLOPT_USERAGENT option
return $this;
}
/**
* @param int $milliseconds
* @return \Afterpay\SDK\HTTP\Request
* @throws \Afterpay\SDK\Exception\InvalidArgumentException
*/
public function setConnectionTimeout($milliseconds)
{
if (! is_int($milliseconds)) {
throw new InvalidArgumentException('Integer expected; ' . gettype($milliseconds) . ' given');
}
curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT_MS, $milliseconds);
return $this;
}
/**
* @param int $milliseconds
* @return \Afterpay\SDK\HTTP\Request
* @throws \Afterpay\SDK\Exception\InvalidArgumentException
*/
public function setTimeout($milliseconds)
{
if (! is_int($milliseconds)) {
throw new InvalidArgumentException('Integer expected; ' . gettype($milliseconds) . ' given');
}
curl_setopt($this->ch, CURLOPT_TIMEOUT_MS, $milliseconds);
return $this;
}
/**
* @return string
*/
public function getApiEnvironmentUrl()
{
return $this->apiEnvironmentUrl;
}
/**
* @return string
*/
public function getUri()
{
return $this->uri;
}
/**
* Note: As of version 1.4.0, the countryCode of the MerchantAccount is not used to construct the API URL, as all regions now use
* the Afterpay Global API. The client implementation is no longer responsible for routing requests to the correct region.
*
* @param string $uri
* @return \Afterpay\SDK\HTTP\Request
*/
public function setUri($uri)
{
$this->uri = $uri;
$merchant = $this->getMerchantAccount();
$apiEnvironment = $merchant->getApiEnvironment();
if (strtolower($apiEnvironment) == 'production') {
$this->apiEnvironmentUrl = "https://global-api.afterpay.com";
} else {
$this->apiEnvironmentUrl = "https://global-api-sandbox.afterpay.com";
}
curl_setopt($this->ch, CURLOPT_URL, $this->apiEnvironmentUrl . $this->uri);
return $this;
}
/**
* @param string $method
* @return \Afterpay\SDK\HTTP\Request
* @throws \Afterpay\SDK\Exception\InvalidArgumentException
*/
public function setHttpMethod($method = 'GET')
{
switch ($method) {
case 'GET':
curl_setopt($this->ch, CURLOPT_HTTPGET, true);
break;
case 'POST':
curl_setopt($this->ch, CURLOPT_POST, true);
break;
case 'PUT':
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, $method);
break;
default:
throw new InvalidArgumentException("Unexpected HTTP Method given: {$method}");
}
return $this;
}
/**
* @param mixed $body
* @return \Afterpay\SDK\HTTP\Request
*/
public function setRequestBody($body_mixed)
{
if (is_string($body_mixed)) {
$body_string = $body_mixed;
} elseif (is_array($body_mixed) || is_object($body_mixed)) {
$body_string = json_encode($body_mixed);
}
$this->addHeader('Content-Type', 'application/json');
$this->addHeader('Content-Length', strlen($body_string));
$this->setRawBody($body_string);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $body_string);
return $this;
}
/**
* @return \Afterpay\SDK\HTTP\Request
*/
public function configureBasicAuth()
{
$merchant = $this->getMerchantAccount();
$merchantId = $merchant->getMerchantId();
$secretKey = $merchant->getSecretKey();
if ($merchantId && $secretKey) {
curl_setopt($this->ch, CURLOPT_USERPWD, "{$merchantId}:{$secretKey}");
}
return $this;
}
/**
* @return array
*/
public function getHeaders()
{
return $this->headers;
}
/**
* @param string $key
* @param string $value
* @return \Afterpay\SDK\HTTP\Request
*/
public function addHeader($key, $value)
{
$this->headers[] = "{$key}: {$value}";
return $this;
}
/**
* @param array $headers
* @return \Afterpay\SDK\HTTP\Request
* @throws \Afterpay\SDK\Exception\InvalidArgumentException
*/
public function setHeaders($headers)
{
if (! is_array($headers)) {
throw new InvalidArgumentException('Array expected; ' . gettype($headers) . ' given');
}
$this->headers = $headers;
return $this;
}
/**
* @return string
*/
public function getRawLog()
{
$str = '';
$str .= "########## BEGIN RAW HTTP REQUEST ##########\n";
$str .= $this->getRaw() . "\n";
$str .= "########## END RAW HTTP REQUEST ##########\n";
if ($this->getResponse()) {
$str .= "########## BEGIN RAW HTTP RESPONSE ##########\n";
$str .= $this->getResponse()->getRaw() . "\n";
$str .= "########## END RAW HTTP RESPONSE ##########\n";
}
return $this->maybeObfuscate($str);
}
/**
* @return \Afterpay\SDK\HTTP\Response
*/
public function getResponse()
{
return $this->response;
}
/**
* @return int
*/
public function getCurlErrno()
{
return $this->curl_errno;
}
/**
* @param int $curl_errno
* @return \Afterpay\SDK\HTTP\Request
* @throws \Afterpay\SDK\Exception\InvalidArgumentException
*/
public function setCurlErrno($curl_errno)
{
if (! is_int($curl_errno)) {
throw new InvalidArgumentException('Integer expected; ' . gettype($curl_errno) . ' given');
}
$this->curl_errno = $curl_errno;
return $this;
}
/**
* @return string
*/
public function getCurlError()
{
return $this->curl_error;
}
/**
* @param string $curl_errno
* @return \Afterpay\SDK\HTTP\Request
* @throws \Afterpay\SDK\Exception\InvalidArgumentException
*/
public function setCurlError($curl_error)
{
if (! is_string($curl_error)) {
throw new InvalidArgumentException('String expected; ' . gettype($curl_error) . ' given');
}
$this->curl_error = $curl_error;
return $this;
}
/**
* @return string
*/
public function getMockMode()
{
return $this->mock_mode;
}
/**
* @param string $mock_mode
* @return \Afterpay\SDK\HTTP\Request
* @throws \Afterpay\SDK\Exception\InvalidArgumentException
*/
public function setMockMode($mock_mode)
{
if (! is_string($mock_mode)) {
throw new InvalidArgumentException('String expected; ' . gettype($mock_mode) . ' given');
}
if (
! in_array($mock_mode, [
'alwaysReceiveServiceUnavailable',
'alwaysThrowNetworkException',
'alwaysThrowParsingException'
])
) {
throw new InvalidArgumentException("Invalid mock mode: '{$mock_mode}'");
}
$this->mock_mode = $mock_mode;
return $this;
}
/**
* @return bool
* @throws \Afterpay\SDK\Exception\InvalidArgumentException
* @throws \Afterpay\SDK\Exception\NetworkException
* @throws \Afterpay\SDK\Exception\ParsingException
*/
public function send()
{
if (method_exists($this, 'beforeSend')) {
$this->beforeSend();
}
if ($this->getMockMode() == 'alwaysThrowNetworkException') {
throw new NetworkException('Connection timed out after 0 milliseconds (mock)', 7);
}
$preferred_response_class = str_replace('Afterpay\SDK\HTTP\Request', 'Afterpay\SDK\HTTP\Response', get_class($this));
if (class_exists($preferred_response_class)) {
$this->response = new $preferred_response_class();
} else {
$this->response = new Response();
}
$this->response->setRequest($this);
if (method_exists($this, 'jsonSerialize')) {
$model_data = $this->jsonSerialize();
if (is_null($this->getRawBody()) && ! empty($model_data)) {
$this->setRequestBody($model_data);
}
}
if (is_null($this->getRawBody())) {
$this->addHeader('Content-Type', null);
}
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->getHeaders());
if ($this->getMockMode() == 'alwaysReceiveServiceUnavailable') {
$this->response
->setHttpStatusCode(503)
->setContentType('application/json')
->setRawBody(
'{
"errorCode" : "service_unavailable_mock",
"errorId" : "0123456789abcdef",
"message" : "Service Unavailable (Mock)",
"httpStatusCode" : 503
}'
)
;
return false;
} elseif ($this->getMockMode() == 'alwaysThrowParsingException') {
$this->response
->setHttpStatusCode(200)
->setContentType('text/plain;charset=iso-8859-1')
;
throw new ParsingException('Syntax error (mock)', 4);
}
$rs = curl_exec($this->ch);
$this
->setRawHeaders(curl_getinfo($this->ch, CURLINFO_HEADER_OUT))
->setCurlErrno(curl_errno($this->ch))
->setCurlError(curl_error($this->ch))
;
$this->response
->setHttpStatusCode(curl_getinfo($this->ch, CURLINFO_RESPONSE_CODE))
->setContentType(curl_getinfo($this->ch, CURLINFO_CONTENT_TYPE))
;
curl_close($this->ch);
if ($rs === false) {
// 7 and 28 are common timeout errno's
throw new NetworkException($this->curl_error, $this->curl_errno);
}
$rs = str_replace("\r\n", "\n", $rs); # Warning: this manipulates the raw response data!
$response_parts = explode("\n\n", $rs);
$response_headers = [];
if (stripos($rs, 'HTTP/1.1 100') === 0 || stripos($rs, 'HTTP/2 100') === 0) {
$response_headers[] = array_shift($response_parts);
}
$response_headers[] = array_shift($response_parts);
$this->response
->setRawHeaders(implode("\n\n", $response_headers) . "\n\n")
->setRawBody(implode("\n\n", $response_parts))
;
if (method_exists($this->response, 'afterReceive')) {
$this->response->afterReceive();
}
return $this->response->isSuccessful();
}
}