From b4a6409617ccef711677197c1297276b0d09ff18 Mon Sep 17 00:00:00 2001 From: Justin Jones Date: Fri, 20 Sep 2013 16:55:47 +0800 Subject: [PATCH] Update pin gateway to add support for purchasing with card tokens from Pin.js --- src/Omnipay/Pin/Message/PurchaseRequest.php | 6 +- .../Pin/Message/PurchaseRequestTest.php | 60 +++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 tests/Omnipay/Pin/Message/PurchaseRequestTest.php diff --git a/src/Omnipay/Pin/Message/PurchaseRequest.php b/src/Omnipay/Pin/Message/PurchaseRequest.php index 06d67780..23f40c7a 100644 --- a/src/Omnipay/Pin/Message/PurchaseRequest.php +++ b/src/Omnipay/Pin/Message/PurchaseRequest.php @@ -40,8 +40,11 @@ public function getData() $data['currency'] = strtolower($this->getCurrency()); $data['description'] = $this->getDescription(); $data['ip_address'] = $this->getClientIp(); + $data['email'] = $this->getCard()->getEmail(); - if ($this->getCard()) { + if ($this->getToken()) { + $data['card_token'] = $this->getToken(); + } elseif ($this->getCard()) { $this->getCard()->validate(); $data['card']['number'] = $this->getCard()->getNumber(); @@ -55,7 +58,6 @@ public function getData() $data['card']['address_postcode'] = $this->getCard()->getPostcode(); $data['card']['address_state'] = $this->getCard()->getState(); $data['card']['address_country'] = $this->getCard()->getCountry(); - $data['email'] = $this->getCard()->getEmail(); } return $data; diff --git a/tests/Omnipay/Pin/Message/PurchaseRequestTest.php b/tests/Omnipay/Pin/Message/PurchaseRequestTest.php new file mode 100644 index 00000000..38b5af93 --- /dev/null +++ b/tests/Omnipay/Pin/Message/PurchaseRequestTest.php @@ -0,0 +1,60 @@ +request = new PurchaseRequest($this->getHttpClient(), $this->getHttpRequest()); + $this->request->initialize( + array( + 'amount' => '10.00', + 'currency' => 'AUD', + 'card' => $this->getValidCard(), + ) + ); + } + + public function testDataWithToken() + { + $this->request->setToken('abc'); + $data = $this->request->getData(); + + $this->assertSame('abc', $data['card_token']); + } + + public function testDataWithCard() + { + $card = $this->getValidCard(); + $this->request->setCard($card); + $data = $this->request->getData(); + + $this->assertSame($card['number'], $data['card']['number']); + } + + public function testSendSuccess() + { + $this->setMockHttpResponse('PurchaseSuccess.txt'); + + $response = $this->request->send(); + + $this->assertTrue($response->isSuccessful()); + $this->assertFalse($response->isRedirect()); + $this->assertEquals('ch_fXIxWf0gj1yFHJcV1W-d-w', $response->getTransactionReference()); + $this->assertSame('Success!', $response->getMessage()); + } + + public function testSendError() + { + $this->setMockHttpResponse('PurchaseFailure.txt'); + $response = $this->request->send(); + + $this->assertFalse($response->isSuccessful()); + $this->assertFalse($response->isRedirect()); + $this->assertNull($response->getTransactionReference()); + $this->assertSame('The current resource was deemed invalid.', $response->getMessage()); + } +}