-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathClient.php
630 lines (504 loc) · 23 KB
/
Client.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
623
624
625
626
627
628
629
630
<?php
// See ClientInterface.php for public function documentation
namespace Amazon\Pay\API;
use phpseclib\Crypt\RSA;
require_once 'ClientInterface.php';
require_once 'HttpCurl.php';
class Client implements ClientInterface
{
const SDK_VERSION = '2.2.5';
const HASH_ALGORITHM = 'sha256';
const AMAZON_SIGNATURE_ALGORITHM = 'AMZN-PAY-RSASSA-PSS';
const API_VERSION = 'v2';
private $config = array();
private $apiServiceUrls = array(
'eu' => 'pay-api.amazon.eu',
'na' => 'pay-api.amazon.com',
'jp' => 'pay-api.amazon.jp');
private $regionMappings = array(
'eu' => 'eu',
'de' => 'eu',
'uk' => 'eu',
'us' => 'na',
'na' => 'na',
'jp' => 'jp');
public function __construct($config = null) {
if (isset($config)) {
if (!is_array($config)) {
throw new \Exception('$config is of the incorrect type ' . gettype($config) . ' and should be of the type array');
}
$this->config = $config;
if (!empty($config['sandbox'])) {
// setSandbox verifies boolean
$this->setSandbox($config['sandbox']);
} else {
$this->setSandbox(false);
}
} else {
throw new \Exception('Expecting config array{region, sandbox, public_key_id, private_key}', 1);
}
}
/* Getter
* Gets the value for the key if the key exists in config
*/
public function __get($name)
{
if (array_key_exists(strtolower($name), $this->config)) {
return $this->config[strtolower($name)];
} else {
throw new \Exception('Key ' . $name . ' is either not a part of the configuration array config or the ' . $name . ' does not match the key name in the config array', 1);
}
}
/* Create API service URL and the Endpoint path */
private function createServiceUrl()
{
// Comprehensive SDK use requires 'sandbox' and 'region' parameters to be specified in the config array
// These attributes are not needed for Signature-only SDK usage
$modePath = strtolower($this->config['sandbox']) ? 'sandbox' : 'live';
if (!empty($this->config['region'])) {
$region = strtolower($this->config['region']);
if (array_key_exists($region, $this->regionMappings)) {
if (isset($this->config['override_service_url'])) {
$apiEndpointUrl = preg_replace("(https?://)", "", $this->config['override_service_url']);
} else {
$apiEndpointUrl = $this->apiServiceUrls[$this->regionMappings[$region]];
}
if($this->isEnvSpecificPublicKeyId($this->config['public_key_id'])){
return 'https://' . $apiEndpointUrl . '/';
}
return 'https://' . $apiEndpointUrl . '/' . $modePath . '/';
} else {
throw new \Exception($region . ' is not a valid region');
}
} else {
throw new \Exception("config['region'] is a required parameter and is not set");
}
}
// Method used to validate whether PublicKeyId starts with prefix LIVE or SANDBOX
private function isEnvSpecificPublicKeyId($publicKeyId) {
return $this->startsWith($this->config['public_key_id'], 'LIVE') || $this->startsWith($this->config['public_key_id'], 'SANDBOX');
}
// Method used to validate whether String starts with Prefix or not
private function startsWith($string, $prefix)
{
$length = strlen($prefix);
return (substr(strtoupper($string), 0, $length) === $prefix);
}
/* canonicalURI
* Strips the http and https off the URL
* Strips the Host off the URL
* Removes data after ?
* Replaces special characters with their URL encoding
*/
private function getCanonicalURI($unEncodedURI)
{
if ($unEncodedURI === '') {
return '/';
}
$urlArray = parse_url($unEncodedURI);
if (empty($urlArray['path'])) {
return '/';
}
else {
return $urlArray['path'];
}
}
/* sortCanonicalArray
* Sorts given array
* Breaks out values in array that are arrays themselves and
returns a new array with that data
* Creates new sorted array with all data
*/
private function sortCanonicalArray($canonicalArray)
{
$sortedCanonicalArray = array();
foreach ($canonicalArray as $key => $val) {
if (is_array($val)) {
foreach ($this->subArrays($val, "$key") as $newKey => $subVal) {
$sortedCanonicalArray["$newKey"] = $subVal;
}
}
else if ((is_null($val)) || ($val === '')) {}
else {
$sortedCanonicalArray["$key"] = $val;
}
}
ksort($sortedCanonicalArray);
return $sortedCanonicalArray;
}
/* subArrays - helper function used to break out arays in an array */
private function subArrays($parameters, $catagory)
{
$categoryIndex = 0;
$newParameters = array();
$categoryString = "$catagory.";
foreach ($parameters as $value) {
$categoryIndex++;
$newParameters[$categoryString . $categoryIndex] = $value;
}
return $newParameters;
}
/* createCanonicalQuery
* Returns a string of request parameters
*/
private function createCanonicalQuery($requestParameters)
{
$sortedRequestParameters = $this->sortCanonicalArray($requestParameters);
return $this->getParametersAsString($sortedRequestParameters);
}
/* Convert paremeters to Url encoded query string */
private function getParametersAsString(array $parameters)
{
$queryParameters = array();
foreach ($parameters as $key => $value) {
$queryParameters[] = $key . '=' . $this->urlEncode($value);
}
return implode('&', $queryParameters);
}
private function urlEncode($value)
{
return str_replace('%7E', '~', rawurlencode($value));
}
/* HexEncode and hash data */
private function hexAndHash($data)
{
$hash = self::HASH_ALGORITHM;
return bin2hex(hash($hash, $data, true));
}
/* Formats date as ISO 8601 timestamp */
private function getFormattedTimestamp()
{
return gmdate("Ymd\THis\\Z", time());
}
private function getCanonicalHeaders($headers)
{
$sortedCanonicalArray = array();
foreach ($headers as $key => $val) {
if ((is_null($val)) || ($val === '')) {}
else {
$sortedCanonicalArray[strtolower("$key")] = $val;
}
}
ksort($sortedCanonicalArray);
return $sortedCanonicalArray;
}
/* getCanonicalHeaderNames
* Returns a string of the header names
*/
private function getCanonicalHeadersNames($headers)
{
$sortedHeader = $this->getCanonicalHeaders($headers);
foreach (array_keys($sortedHeader) as $key) {
$parameters[] = $key;
}
ksort($parameters);
$headerNames = implode(';', $parameters);
return $headerNames;
}
/* getHost
* Returns the host
*/
private function getHost($unEncodedURI)
{
if ($unEncodedURI === '') {
return '/';
}
$urlArray = parse_url($unEncodedURI);
if (empty($urlArray['host'])) {
return '/';
}
else {
return $urlArray['host'];
}
}
/* getHeaderString
* Returns the Canonical Headers as a string
*/
public function getHeaderString($headers)
{
$queryParameters = array();
$sortedHeaders = $this->getCanonicalHeaders($headers);
foreach ($sortedHeaders as $key => $value) {
if (is_array($value)) {
$value = $this->collectSubVals($value);
}
else {
$queryParameters[] = $key . ':' . $value;
}
}
$returnString = implode("\n", $queryParameters);
return $returnString . "\n";
}
/* collectSubVals
* Helper function to take an array and return the values as a string
*/
private function collectSubVals($parameters)
{
$categoryIndex = 0;
$collectedValues = '';
foreach ($parameters as $value) {
if ($categoryIndex != 0) {
$collectedValues .= ' ';
}
$collectedValues .= $value;
$categoryIndex++;
}
return $collectedValues;
}
/* stringFromArray - helper function used to check if parameters is an array.
* If it is array it returns all the values as a string
* Otherwise it returns parameters
*/
private function stringFromArray($parameters)
{
if (is_array($parameters)) {
return $this->collectSubVals($parameters);
}
else {
return $parameters;
}
}
/* Create the User Agent Header sent with the POST request */
/* Protected because of PSP module usaged */
protected function constructUserAgentHeader()
{
return 'amazon-pay-api-sdk-php/' . self::SDK_VERSION . ' ('
. 'PHP/' . phpversion() . '; '
. php_uname('s') . '/' . php_uname('m') . '/' . php_uname('r') . ')';
}
public function getPostSignedHeaders($http_request_method, $request_uri, $request_parameters, $request_payload, $other_presigned_headers = null)
{
$preSignedHeaders = array();
$preSignedHeaders['accept'] = 'application/json';
$preSignedHeaders['content-type'] = 'application/json';
$preSignedHeaders['X-Amz-Pay-Region'] = $this->config['region'];
// Header x-amz-pay-idempotency-key is a special user-supplied header that must be pre-signed if used
if (isset($other_presigned_headers)) {
foreach ($other_presigned_headers as $key => $val) {
if (strtolower($key) === 'x-amz-pay-idempotency-key') {
if (isset($val) && ($val !== '')) {
$preSignedHeaders['x-amz-pay-idempotency-key'] = $val;
}
}
}
}
$timeStamp = $this->getFormattedTimestamp();
$signature = $this->createSignature($http_request_method, $request_uri, $request_parameters, $preSignedHeaders, $request_payload, $timeStamp);
$public_key_id = $this->config['public_key_id'];
$headers = $this->getCanonicalHeaders($preSignedHeaders);
$headers['X-Amz-Pay-Date'] = $timeStamp;
$headers['X-Amz-Pay-Host'] = $this->getHost($request_uri);
$signedHeaders = "SignedHeaders=" . $this->getCanonicalHeadersNames($headers) . ", Signature=" . $signature;
// Do not add x-amz-pay-idempotency-key header here, as user-supplied headers get added later
$headerArray = array(
'accept' => $this->stringFromArray($headers['accept']),
'content-type' => $this->stringFromArray($headers['content-type']),
'x-amz-pay-host' => $this->getHost($request_uri),
'x-amz-pay-date' => $timeStamp,
'x-amz-pay-region' => $this->config['region'],
'authorization' => self::AMAZON_SIGNATURE_ALGORITHM . " PublicKeyId=" . $public_key_id . ", " . $signedHeaders,
'user-agent' => $this->constructUserAgentHeader()
);
ksort($headerArray);
foreach ($headerArray as $key => $value) {
$queryParameters[] = $key . ':' . $value;
}
return $queryParameters;
}
public function createSignature($http_request_method, $request_uri, $request_parameters, $pre_signed_headers, $request_payload, $timeStamp)
{
$rsa = $this->setupRSA();
$pre_signed_headers['X-Amz-Pay-Date'] = $timeStamp;
$pre_signed_headers['X-Amz-Pay-Host'] = $this->getHost($request_uri);
$hashedPayload = $this->hexAndHash($request_payload);
$canonicalURI = $this->getCanonicalURI($request_uri);
$canonicalQueryString = $this->createCanonicalQuery($request_parameters);
$canonicalHeader = $this->getHeaderString($pre_signed_headers);
$signedHeaders = $this->getCanonicalHeadersNames($pre_signed_headers);
$canonicalRequest = (
$http_request_method . "\n" .
$canonicalURI . "\n" .
$canonicalQueryString . "\n" .
$canonicalHeader . "\n" .
$signedHeaders . "\n" .
$hashedPayload
);
$hashedCanonicalRequest = self::AMAZON_SIGNATURE_ALGORITHM . "\n" . $this->hexAndHash($canonicalRequest);
$signature = $rsa->sign($hashedCanonicalRequest);
if ($signature === false) {
throw new \Exception('Unable to sign request, is your RSA private key valid?');
}
return base64_encode($signature);
}
public function generateButtonSignature($payload) {
$rsa = $this->setupRSA();
// if array is passed in, developer will need to ensure same json_encode function is used on website,
// otherwise the signed payload may not match if a different JSON to string function is used which may
// generate a string with different whitespace
if (is_array($payload)) {
$payload = json_encode($payload);
}
// stripcslashes function is used on payload to unescape sequences like http:\/\/ to http://
// and \"hello\" to "hello"
$hashedButtonRequest = self::AMAZON_SIGNATURE_ALGORITHM . "\n" . $this->hexAndHash(stripcslashes($payload));
$signature = $rsa->sign($hashedButtonRequest);
if ($signature === false) {
throw new \Exception('Unable to sign request, is your RSA private key valid?');
}
return base64_encode($signature);
}
private function setupRSA() {
$rsa = new RSA();
$rsa->setHash(self::HASH_ALGORITHM);
$rsa->setMGFHash(self::HASH_ALGORITHM);
$rsa->setSaltLength(20);
$key_spec = $this->config['private_key'];
if ((strpos($key_spec, 'BEGIN RSA PRIVATE KEY') === false) && (strpos($key_spec, 'BEGIN PRIVATE KEY') === false)) {
$contents = file_get_contents($key_spec);
if ($contents === false) {
throw new \Exception('Unable to load file: ' . $key_spec);
}
$rsa->loadKey($contents);
} else {
$rsa->loadKey($key_spec);
}
return $rsa;
}
public function testPrivateKeyIntegrity() {
echo "Testing private key: ";
$rsa = $this->setupRSA();
for ($i = 0; $i < 100; $i++) {
echo ($i+1) . " ";
$rsa->sign("TESTING PRIVATE KEY");
}
echo "[Passed]\n";
return true;
}
public function apiCall($method, $urlFragment, $payload, $headers = null, $queryParams = null) {
if (is_array($payload)) {
// json_encode will fail if non-UTF-8 encodings are present, need to convert them to UTF-8
array_walk_recursive($payload, function (&$item, $key) {
if (is_string($item) && mb_detect_encoding($item, 'UTF-8', true) === false) {
$item = utf8_encode($item);
}
});
$payload = json_encode($payload);
}
$url = $this->createServiceUrl() . $urlFragment;
if (isset($queryParams)) {
if (!is_array($queryParams)) {
throw new \Exception('queryParameters must be a key-value array; e.g. array(\'accountId\' => \'ABCD1234XYZIJK\')');
}
$url = $url . '?' . $this->createCanonicalQuery($queryParams);
$requestParameters = $queryParams;
} else {
$requestParameters = array();
}
$postSignedHeaders = $this->getPostSignedHeaders($method, $url, $requestParameters, $payload, $headers);
if (isset($headers)) {
if (!is_array($headers)) {
throw new \Exception('headers must be a key-value array; e.g. array(\'x-amz-pay-authtoken\' => \'abcd1234xyzIJK\')');
}
foreach ($headers as $key => $value) {
$postSignedHeaders[] = $key . ':' . $value;
}
}
$httpCurlRequest = new HttpCurl();
$response = $httpCurlRequest->invokeCurl($method, $url, $payload, $postSignedHeaders);
return $response;
}
/* Setter for sandbox
* @param value - [boolean]
* Sets the boolean value for config['sandbox'] variable
*/
public function setSandbox($value)
{
if (is_bool($value)) {
$this->config['sandbox'] = $value;
} else {
throw new \Exception('sandbox value ' . $value . ' is of type ' . gettype($value) . ' and should be a boolean value');
}
}
// ----------------------------------- DELIVERY NOTIFICATIONS API -----------------------------------
public function deliveryTrackers($payload, $headers = null)
{
// Current implementation on deliveryTrackers API does not support the use of auth token
return $this->apiCall('POST', self::API_VERSION . '/deliveryTrackers', $payload, $headers);
}
// ----------------------------------- AUTHORIZATION TOKENS API -----------------------------------
public function getAuthorizationToken($mwsAuthToken, $merchantId, $headers = null)
{
$queryParams = array('merchantId' => $merchantId);
return $this->apiCall('GET', self::API_VERSION . '/authorizationTokens/' . $mwsAuthToken, null, $headers, $queryParams);
}
// ----------------------------------- IN-STORE API -----------------------------------
public function instoreMerchantScan($payload, $headers = null)
{
return $this->apiCall('POST', self::API_VERSION . '/in-store/merchantScan', $payload, $headers);
}
public function instoreCharge($payload, $headers = null)
{
return $this->apiCall('POST', self::API_VERSION . '/in-store/charge', $payload, $headers);
}
public function instoreRefund($payload, $headers = null)
{
return $this->apiCall('POST', self::API_VERSION . '/in-store/refund', $payload, $headers);
}
// ----------------------------------- Amazon Checkout v2 API -----------------------------------
public function getBuyer($buyerToken, $headers = null) {
return $this->apiCall('GET', self::API_VERSION . '/buyers/' . $buyerToken, null, $headers);
}
public function createCheckoutSession($payload, $headers)
{
return $this->apiCall('POST', self::API_VERSION . '/checkoutSessions', $payload, $headers);
}
public function getCheckoutSession($checkoutSessionId, $headers = null)
{
return $this->apiCall('GET', self::API_VERSION . '/checkoutSessions/' . $checkoutSessionId, null, $headers);
}
public function updateCheckoutSession($checkoutSessionId, $payload, $headers = null)
{
return $this->apiCall('PATCH', self::API_VERSION . '/checkoutSessions/' . $checkoutSessionId, $payload, $headers);
}
public function completeCheckoutSession($checkoutSessionId, $payload, $headers = null)
{
return $this->apiCall('POST', self::API_VERSION . '/checkoutSessions/' . $checkoutSessionId . '/complete', $payload, $headers);
}
public function getChargePermission($chargePermissionId, $headers = null)
{
return $this->apiCall('GET', self::API_VERSION . '/chargePermissions/' . $chargePermissionId, null, $headers);
}
public function updateChargePermission($chargePermissionId, $payload, $headers = null)
{
return $this->apiCall('PATCH', self::API_VERSION . '/chargePermissions/' . $chargePermissionId, $payload, $headers);
}
public function closeChargePermission($chargePermissionId, $payload, $headers = null)
{
return $this->apiCall('DELETE', self::API_VERSION . '/chargePermissions/' . $chargePermissionId . '/close', $payload, $headers);
}
public function createCharge($payload, $headers)
{
return $this->apiCall('POST', self::API_VERSION . '/charges', $payload, $headers);
}
public function getCharge($chargeId, $headers = null)
{
return $this->apiCall('GET', self::API_VERSION . '/charges/' . $chargeId, null, $headers);
}
public function captureCharge($chargeId, $payload, $headers)
{
return $this->apiCall('POST', self::API_VERSION . '/charges/' . $chargeId . '/capture', $payload, $headers);
}
public function cancelCharge($chargeId, $payload, $headers = null)
{
return $this->apiCall('DELETE', self::API_VERSION . '/charges/' . $chargeId . '/cancel', $payload, $headers);
}
public function createRefund($payload, $headers)
{
return $this->apiCall('POST', self::API_VERSION . '/refunds', $payload, $headers);
}
public function getRefund($refundId, $headers = null)
{
return $this->apiCall('GET', self::API_VERSION . '/refunds/' . $refundId, null, $headers);
}
}
?>