Skip to content

Commit cd20795

Browse files
authored
Merge pull request #52 from afterpay/feature/migrate-to-global-api
Migrate to the Afterpay Global API
2 parents 541217f + 807021f commit cd20795

File tree

10 files changed

+33
-40
lines changed

10 files changed

+33
-40
lines changed

.github/workflows/php.yml

-3
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,3 @@ jobs:
120120
DB_USER: ${{ secrets.MYSQL_USER }}
121121
DB_PASS: ${{ secrets.MYSQL_PASSWORD }}
122122
run: composer test-service
123-
124-
- name: Run network test suite
125-
run: composer test-network

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Version 1.4.0
4+
5+
_Thu 24 Feb 2022_
6+
7+
- Upgrade to the Afterpay Global API (EIT-782)
8+
39
## Version 1.3.10
410

511
_Mon 24 Jan 2022_

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ alpha-2 two-character country code of the merchant account. The following method
144144
2. As the `MERCHANT_ID`, `SECRET_KEY` and `COUNTRY_CODE` environment variables.
145145
3. By manually defining an object of class `\Afterpay\SDK\MerchantAccount`, passing your account details using its
146146
`setMerchantId`, `setSecretKey` and `setCountryCode` methods, then passing this object to the the HTTP Request object using
147-
its `setMerchantAccount` method.
147+
its `setMerchantAccount` method.
148148

149149
Sample code is provided in the [sample](sample) directory:
150150

@@ -247,17 +247,17 @@ echo $createCheckoutRequest->getRawLog();
247247
```
248248
########## BEGIN RAW HTTP REQUEST ##########
249249
POST /v2/checkouts HTTP/2
250-
Host: api-sandbox.afterpay.com
250+
Host: global-api-sandbox.afterpay.com
251251
Authorization: Basic MzM******************************************************************************************************************************************************************************TA=
252-
User-Agent: afterpay-sdk-php/0.1.0 (PHP/7.3.11; cURL/7.64.1; Merchant/*****)
252+
User-Agent: afterpay-sdk-php/1.4.0 (PHP/8.1.1; cURL/7.77.0; Merchant/*****)
253253
Accept: */*
254254
Content-Type: application/json
255255
Content-Length: 1223
256256
257257
{"amount":{"amount":"10.00","currency":"AUD"},"consumer":{"phoneNumber":"**** *** ***","givenNames":"****","surname":"****","email":"****************"},"billing":{"name":"*** ********","line1":"***** *","line2":"*** ******* ******","area1":"*********","region":"***","postcode":"****","countryCode":"AU","phoneNumber":"**** *** ***"},"shipping":{"name":"*** ********","line1":"***** *","line2":"*** ******* ******","area1":"*********","region":"***","postcode":"****","countryCode":"AU","phoneNumber":"**** *** ***"},"courier":{"shippedAt":"2019-01-01T00:00:00+10:00","name":"********* ****","tracking":"AA0000000000000","priority":"STANDARD"},"items":[{"name":"******* * **** * **** *","sku":"TSH0001B1MED","quantity":10,"pageUrl":"https:\/\/www.example.com\/page.html","imageUrl":"https:\/\/www.example.com\/image.jpg","price":{"amount":"10.00","currency":"AUD"},"categories":[["Clothing","T-Shirts","Under $25"],["Sale","Clothing"]]}],"discounts":[{"displayName":"20% off SALE","amount":{"amount":"24.00","currency":"AUD"}}],"merchant":{"redirectConfirmUrl":"http:\/\/localhost","redirectCancelUrl":"http:\/\/localhost"},"taxAmount":{"amount":"0.00","currency":"AUD"},"shippingAmount":{"amount":"0.00","currency":"AUD"}}
258258
########## END RAW HTTP REQUEST ##########
259259
########## BEGIN RAW HTTP RESPONSE ##########
260-
HTTP/2 201
260+
HTTP/2 201
261261
date: Tue, 15 Sep 2020 14:20:49 GMT
262262
content-type: application/json
263263
content-length: 249

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "afterpay-global/afterpay-sdk-php",
33
"license": "Apache-2.0",
44
"description": "Official Afterpay SDK for PHP",
5-
"version": "1.3.10",
5+
"version": "1.4.0",
66
"authors": [
77
{
88
"name": "Afterpay",

sample/HTTPRequestGetConfigurationWithPersistence.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@
7777
\["errorCode"\]=>
7878
string\(12\) "unauthorized"
7979
\["errorId"\]=>
80-
string\(16\) "[0-9a-f]{16}"
80+
string\(9\) "cf-worker"
8181
\["message"\]=>
82-
string\(49\) "Credentials are required to access this resource\."
82+
string\(55\) "unauthorized - Unable to route request from Cloudflare\."
8383
\["httpStatusCode"\]=>
8484
int\(401\)
8585
\}

src/HTTP/Request.php

+6-22
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Request extends HTTP
3636
private $merchant;
3737

3838
/**
39-
* @var resource $ch
39+
* @var \CurlHandle|resource $ch
4040
*/
4141
protected $ch;
4242

@@ -280,6 +280,9 @@ public function getUri()
280280
}
281281

282282
/**
283+
* 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
284+
* the Afterpay Global API. The client implementation is no longer responsible for routing requests to the correct region.
285+
*
283286
* @param string $uri
284287
* @return \Afterpay\SDK\HTTP\Request
285288
*/
@@ -288,31 +291,12 @@ public function setUri($uri)
288291
$this->uri = $uri;
289292

290293
$merchant = $this->getMerchantAccount();
291-
$countryCode = $merchant->getCountryCode();
292294
$apiEnvironment = $merchant->getApiEnvironment();
293295

294-
# Use the Country Code to determine the geographic region
295-
# Defaults to Oceania / Asia Pacific (AU/NZ)
296-
297-
$region_suffix = '';
298-
$sandbox_suffix = '-sandbox';
299-
$tld = 'afterpay.com';
300-
301-
if (is_string($countryCode) && strlen($countryCode) == 2) {
302-
if (preg_match('/CA|US/', $countryCode)) {
303-
$region_suffix = '.us'; # North America
304-
} elseif (preg_match('/GB|UK/', $countryCode)) {
305-
$region_suffix = '.eu'; # Europe
306-
} elseif (preg_match('/ES|FR|IT|PT/', $countryCode)) {
307-
$tld = 'clearpay.com'; # Southern Europe
308-
$sandbox_suffix = '.sandbox';
309-
}
310-
}
311-
312296
if (strtolower($apiEnvironment) == 'production') {
313-
$this->apiEnvironmentUrl = "https://api{$region_suffix}.{$tld}";
297+
$this->apiEnvironmentUrl = "https://global-api.afterpay.com";
314298
} else {
315-
$this->apiEnvironmentUrl = "https://api{$region_suffix}{$sandbox_suffix}.{$tld}";
299+
$this->apiEnvironmentUrl = "https://global-api-sandbox.afterpay.com";
316300
}
317301

318302
curl_setopt($this->ch, CURLOPT_URL, $this->apiEnvironmentUrl . $this->uri);

src/HTTP/Request/Ping.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public function __construct(...$args)
2626
{
2727
parent::__construct(... $args);
2828

29-
$this->setUri('/ping');
29+
$this
30+
->setUri('/ping')
31+
->configureBasicAuth()
32+
;
3033
}
3134
}

test/Integration/HTTPIntegrationTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ public function __construct()
3232
public function test404()
3333
{
3434
$invalidRequest = new \Afterpay\SDK\HTTP\Request();
35-
$invalidRequest->setUri('/');
35+
$invalidRequest
36+
->setUri('/')
37+
->configureBasicAuth()
38+
;
3639
$this->assertFalse($invalidRequest->send());
3740
$this->assertEquals(404, $invalidRequest->getResponse()->getHttpStatusCode());
3841
}

test/Network/HTTPNetworkTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function testConnectionTimeout()
4545
$this->{ method_exists($this, 'assertMatchesRegularExpression') ? 'assertMatchesRegularExpression' : 'assertRegExp' }('/^(7|28)$/', (string) $e->getCode());
4646

4747
if ($e->getCode() == 7) {
48-
$this->{ method_exists($this, 'assertMatchesRegularExpression') ? 'assertMatchesRegularExpression' : 'assertRegExp' }('/^Failed to connect to api-sandbox\.afterpay\.com port 443: (Operation|Connection) timed out$/', $e->getMessage());
48+
$this->{ method_exists($this, 'assertMatchesRegularExpression') ? 'assertMatchesRegularExpression' : 'assertRegExp' }('/^Failed to connect to global-api-sandbox\.afterpay\.com port 443: (Operation|Connection) timed out$/', $e->getMessage());
4949
} else {
5050
$this->{ method_exists($this, 'assertMatchesRegularExpression') ? 'assertMatchesRegularExpression' : 'assertRegExp' }('/^((Connection|Resolving) timed out after \d+ milliseconds|remaining timeout of \d+ too small to resolve via SIGALRM method|Connection time-out)$/', $e->getMessage());
5151
}

test/Unit/HTTPTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function testApiEnvironmentInheritance()
8282

8383
$pingRequest = new \Afterpay\SDK\HTTP\Request\Ping();
8484

85-
$this->assertEquals('https://api.afterpay.com', $pingRequest->getApiEnvironmentUrl());
85+
$this->assertEquals('https://global-api.afterpay.com', $pingRequest->getApiEnvironmentUrl());
8686

8787
$merchant = new \Afterpay\SDK\MerchantAccount([
8888
'apiEnvironment' => 'Sandbox'
@@ -92,7 +92,7 @@ public function testApiEnvironmentInheritance()
9292

9393
$pingRequest->setMerchantAccount($merchant);
9494

95-
$this->assertEquals('https://api-sandbox.afterpay.com', $pingRequest->getApiEnvironmentUrl()); # The Request will use the apiEnvironment of its MerchantAccount instance
95+
$this->assertEquals('https://global-api-sandbox.afterpay.com', $pingRequest->getApiEnvironmentUrl()); # The Request will use the apiEnvironment of its MerchantAccount instance
9696
$this->assertEquals('Production', $pingRequest::getApiEnvironment()); # Even though the static property on the parent class hasn't changed
9797
}
9898

@@ -104,7 +104,7 @@ public function testEuRegionalApiEnvironmentSelection()
104104

105105
$pingRequest = new \Afterpay\SDK\HTTP\Request\Ping();
106106

107-
$this->assertEquals('https://api.eu-sandbox.afterpay.com', $pingRequest->getApiEnvironmentUrl());
107+
$this->assertEquals('https://global-api-sandbox.afterpay.com', $pingRequest->getApiEnvironmentUrl());
108108
}
109109

110110
public function testNaRegionalApiEnvironmentSelection()
@@ -115,7 +115,7 @@ public function testNaRegionalApiEnvironmentSelection()
115115

116116
$pingRequest = new \Afterpay\SDK\HTTP\Request\Ping();
117117

118-
$this->assertEquals('https://api.us-sandbox.afterpay.com', $pingRequest->getApiEnvironmentUrl());
118+
$this->assertEquals('https://global-api-sandbox.afterpay.com', $pingRequest->getApiEnvironmentUrl());
119119
}
120120

121121
public function testOcRegionalApiEnvironmentSelection()
@@ -126,6 +126,6 @@ public function testOcRegionalApiEnvironmentSelection()
126126

127127
$pingRequest = new \Afterpay\SDK\HTTP\Request\Ping();
128128

129-
$this->assertEquals('https://api-sandbox.afterpay.com', $pingRequest->getApiEnvironmentUrl());
129+
$this->assertEquals('https://global-api-sandbox.afterpay.com', $pingRequest->getApiEnvironmentUrl());
130130
}
131131
}

0 commit comments

Comments
 (0)