From 2b5c54fe4a4a7103317415a8a1ac6a3ac0b611bc Mon Sep 17 00:00:00 2001 From: Adrian Macneil Date: Wed, 25 Feb 2015 11:49:53 -0800 Subject: [PATCH 1/3] Add missing Content-Type header --- src/Message/AbstractRequest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index b5e091d..5dd0021 100644 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -58,6 +58,7 @@ function ($event) { $body = $data ? http_build_query($data) : null; $httpRequest = $this->httpClient->createRequest($method, $url, null, $body); + $httpRequest->setHeader('Content-Type', 'application/x-www-form-urlencoded'); $httpRequest->setHeader('ACCESS_KEY', $this->getApiKey()); $httpRequest->setHeader('ACCESS_SIGNATURE', $this->generateSignature($url, $body, $nonce)); $httpRequest->setHeader('ACCESS_NONCE', $nonce); From 2a8d2477acc51bcc3e78fe44a9980a372b5e7ad0 Mon Sep 17 00:00:00 2001 From: Adrian Macneil Date: Wed, 25 Feb 2015 11:50:33 -0800 Subject: [PATCH 2/3] Update API endpoint --- src/Message/AbstractRequest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index 5dd0021..607edd5 100644 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -9,7 +9,7 @@ */ abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest { - protected $endpoint = 'https://coinbase.com/api/v1'; + protected $endpoint = 'https://api.coinbase.com/v1'; public function getApiKey() { From 0c3bd1a20fc6c6ec2d021512a237e53ad4a401c5 Mon Sep 17 00:00:00 2001 From: truonghoang Date: Fri, 7 Aug 2015 21:33:07 +0700 Subject: [PATCH 3/3] support coinbase v2 and sandbox API --- src/Message/AbstractRequest.php | 13 +++++++++++-- src/Message/PurchaseResponse.php | 13 +++++++++++-- tests/Message/PurchaseRequestTest.php | 2 +- tests/Message/PurchaseResponseTest.php | 6 ++++-- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index 607edd5..e80a503 100644 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -9,7 +9,10 @@ */ abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest { - protected $endpoint = 'https://api.coinbase.com/v1'; + const API_VERSION = 'v2'; + + protected $liveEndpoint = 'https://api.coinbase.com'; + protected $testEndpoint = 'https://api.sandbox.coinbase.com'; public function getApiKey() { @@ -54,7 +57,7 @@ function ($event) { ); $nonce = $this->generateNonce(); - $url = $this->endpoint.$action; + $url = $this->getEndpoint().$action; $body = $data ? http_build_query($data) : null; $httpRequest = $this->httpClient->createRequest($method, $url, null, $body); @@ -77,4 +80,10 @@ public function generateSignature($url, $body, $nonce) return hash_hmac('sha256', $message, $this->getSecret()); } + + protected function getEndpoint() + { + $base = $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint; + return $base . '/' . self::API_VERSION; + } } diff --git a/src/Message/PurchaseResponse.php b/src/Message/PurchaseResponse.php index 4230bd9..02a1c5e 100644 --- a/src/Message/PurchaseResponse.php +++ b/src/Message/PurchaseResponse.php @@ -9,7 +9,10 @@ */ class PurchaseResponse extends Response implements RedirectResponseInterface { - protected $redirectEndpoint = 'https://coinbase.com/checkouts'; + const API_VERSION = 'v2'; + + protected $redirectLiveEndpoint = 'https://api.coinbase.com'; + protected $redirectTestEndpoint = 'https://api.sandbox.coinbase.com'; public function isSuccessful() { @@ -29,7 +32,7 @@ public function getRedirectMethod() public function getRedirectUrl() { if ($this->isRedirect()) { - return $this->redirectEndpoint.'/'.$this->getTransactionReference(); + return $this->getCheckoutEndpoint().'/'.$this->getTransactionReference(); } } @@ -44,4 +47,10 @@ public function getTransactionReference() return $this->data['button']['code']; } } + + protected function getCheckoutEndpoint() + { + $base = $this->getRequest()->getTestMode() ? $this->redirectTestEndpoint : $this->redirectLiveEndpoint; + return $base . '/' . self::API_VERSION . '/checkouts'; + } } diff --git a/tests/Message/PurchaseRequestTest.php b/tests/Message/PurchaseRequestTest.php index d07ffd6..1640653 100644 --- a/tests/Message/PurchaseRequestTest.php +++ b/tests/Message/PurchaseRequestTest.php @@ -63,7 +63,7 @@ public function testSendSuccess() $this->assertTrue($response->isRedirect()); $this->assertNull($response->getMessage()); $this->assertSame('GET', $response->getRedirectMethod()); - $this->assertSame('https://coinbase.com/checkouts/30dae91b81299066ba126e3858f89fd8', $response->getRedirectUrl()); + $this->assertSame('https://api.coinbase.com/v2/checkouts/30dae91b81299066ba126e3858f89fd8', $response->getRedirectUrl()); $this->assertNull($response->getRedirectData()); $this->assertSame('30dae91b81299066ba126e3858f89fd8', $response->getTransactionReference()); } diff --git a/tests/Message/PurchaseResponseTest.php b/tests/Message/PurchaseResponseTest.php index 4fce973..72f4e65 100644 --- a/tests/Message/PurchaseResponseTest.php +++ b/tests/Message/PurchaseResponseTest.php @@ -9,13 +9,15 @@ class PurchaseResponseTest extends TestCase public function testSuccess() { $httpResponse = $this->getMockHttpResponse('PurchaseSuccess.txt'); - $response = new PurchaseResponse($this->getMockRequest(), $httpResponse->json()); + $request = $this->getMockRequest(); + $request->shouldReceive('getTestMode')->once()->andReturn(false); + $response = new PurchaseResponse($request, $httpResponse->json()); $this->assertFalse($response->isSuccessful()); $this->assertTrue($response->isRedirect()); $this->assertNull($response->getMessage()); $this->assertSame('GET', $response->getRedirectMethod()); - $this->assertSame('https://coinbase.com/checkouts/30dae91b81299066ba126e3858f89fd8', $response->getRedirectUrl()); + $this->assertSame('https://api.coinbase.com/v2/checkouts/30dae91b81299066ba126e3858f89fd8', $response->getRedirectUrl()); $this->assertNull($response->getRedirectData()); $this->assertSame('30dae91b81299066ba126e3858f89fd8', $response->getTransactionReference()); }