From 7828a54c5882f051ea58bc4475517a1198b4cfce Mon Sep 17 00:00:00 2001 From: Pedro Tanaka Date: Mon, 19 Jun 2017 18:39:08 -0300 Subject: [PATCH 1/6] Creating tests for token message. --- tests/Message/CreateTokenRequestTest.php | 79 ++++++++++++++++++++++++ tests/Mock/CreateTokenFailure.txt | 22 +++++++ tests/Mock/CreateTokenSuccess.txt | 50 +++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 tests/Message/CreateTokenRequestTest.php create mode 100644 tests/Mock/CreateTokenFailure.txt create mode 100644 tests/Mock/CreateTokenSuccess.txt diff --git a/tests/Message/CreateTokenRequestTest.php b/tests/Message/CreateTokenRequestTest.php new file mode 100644 index 00000000..21fa26f8 --- /dev/null +++ b/tests/Message/CreateTokenRequestTest.php @@ -0,0 +1,79 @@ +request = new CreateTokenRequest($this->getHttpClient(), $this->getHttpRequest()); + + $this->request->setCustomer('cus_example123'); + $this->request->setConnectedStripeAccountHeader('acct_12oh2oi3'); + } + + public function testEndpoint() + { + $this->assertSame('https://api.stripe.com/v1/tokens', $this->request->getEndpoint()); + } + + public function testGetDataInvalid() + { + $this->setExpectedException(InvalidRequestException::class, "You must pass either the card or the customer"); + + $this->request->setCustomer(null); + $this->request->setCard(null); + + $this->request->getData(); + } + + public function getDataWithCard() + { + $card = $this->getValidCard(); + $this->request->setCard($card); + + $data = $this->request->getData(); + + $this->assertSame($card['number'], $data['card']['number']); + } + + public function testResponseFailure() + { + $this->setMockHttpResponse('CreateTokenFailure.txt'); + $response = $this->request->send(); + + $this->assertFalse($response->isSuccessful()); + + $this->assertNull($response->getTransactionReference()); + } + + public function testResponseSuccess() + { + $this->setMockHttpResponse('CreateTokenSuccess.txt'); + $response = $this->request->send(); + + $this->assertTrue($response->isSuccessful()); + $this->assertFalse($response->isRedirect()); + $this->assertNull($response->getTransactionReference()); + $this->assertSame('tok_1AWDl1JqXiFraDuL2xOKEXKy', $response->getData()['id']); + $this->assertSame('token', $response->getData()['object']); + } + + +} diff --git a/tests/Mock/CreateTokenFailure.txt b/tests/Mock/CreateTokenFailure.txt new file mode 100644 index 00000000..b738b63e --- /dev/null +++ b/tests/Mock/CreateTokenFailure.txt @@ -0,0 +1,22 @@ +HTTP/1.1 400 Bad Request +Server: nginx +Date: Mon, 19 Jun 2017 00:29:43 GMT +Content-Type: application/json +Content-Length: 140 +Connection: keep-alive +Access-Control-Allow-Credentials: true +Access-Control-Allow-Methods: GET, POST, HEAD, OPTIONS, DELETE +Access-Control-Allow-Origin: * +Access-Control-Max-Age: 300 +Cache-Control: no-cache, no-store +Request-Id: req_Arxhy7yn9a1MsY +Stripe-Account: acct_19jzyNJqXiFraDuL +Stripe-Version: 2015-04-07 + +{ + "error": { + "type": "invalid_request_error", + "message": "No such customer: cus_ArtrMQYPgb0QaUasd", + "param": "customer" + } +} diff --git a/tests/Mock/CreateTokenSuccess.txt b/tests/Mock/CreateTokenSuccess.txt new file mode 100644 index 00000000..8fe5736a --- /dev/null +++ b/tests/Mock/CreateTokenSuccess.txt @@ -0,0 +1,50 @@ +HTTP/1.1 200 OK +Server: nginx +Date: Mon, 19 Jun 2017 00:28:43 GMT +Content-Type: application/json +Content-Length: 798 +Connection: keep-alive +Access-Control-Allow-Credentials: true +Access-Control-Allow-Methods: GET, POST, HEAD, OPTIONS, DELETE +Access-Control-Allow-Origin: * +Access-Control-Max-Age: 300 +Cache-Control: no-cache, no-store +Request-Id: req_ArxgXN0W9YsXss +Stripe-Account: acct_14901h0a0fh01293 +Stripe-Version: 2015-04-07 +Strict-Transport-Security: max-age=31556926; includeSubDomains + +{ + "id": "tok_1AWDl1JqXiFraDuL2xOKEXKy", + "object": "token", + "card": { + "id": "card_1AWDl1JqXiFraDuL8KJpKlxe", + "object": "card", + "address_city": null, + "address_country": null, + "address_line1": null, + "address_line1_check": null, + "address_line2": null, + "address_state": null, + "address_zip": null, + "address_zip_check": null, + "brand": "Visa", + "country": "US", + "currency": "usd", + "cvc_check": null, + "dynamic_last4": null, + "exp_month": 8, + "exp_year": 2019, + "fingerprint": "OfIzYIod5lokCObt", + "funding": "credit", + "last4": "4242", + "metadata": {}, + "name": null, + "tokenization_method": null + }, + "client_ip": "179.24.124.12", + "created": 1497832123, + "livemode": false, + "type": "card", + "used": false +} From c8eb874fe528794d2f46ff617137721097126d05 Mon Sep 17 00:00:00 2001 From: Pedro Tanaka Date: Mon, 19 Jun 2017 19:07:00 -0300 Subject: [PATCH 2/6] Creating Token Request. --- src/Message/CreateTokenRequest.php | 76 ++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/Message/CreateTokenRequest.php diff --git a/src/Message/CreateTokenRequest.php b/src/Message/CreateTokenRequest.php new file mode 100644 index 00000000..74208061 --- /dev/null +++ b/src/Message/CreateTokenRequest.php @@ -0,0 +1,76 @@ +. + * Only use this if you are using Connect API + * + * @param string $customer The id of the customer + */ + public function setCustomer($customer) + { + $this->setParameter('customer', $customer); + } + + /** + * Get the raw data array for this message. The format of this varies from gateway to + * gateway, but will usually be either an associative array, or a SimpleXMLElement. + * @return mixed + * @throws InvalidRequestException + */ + public function getData() + { + $data = []; + + if ($this->getParameter('customer')) { + $data['customer'] = $this->getParameter('customer'); + } else if ($this->getParameter('card')) { + $data['card'] = $this->getParameter('card'); + } else { + throw new InvalidRequestException("You must pass either the card or the customer"); + } + + return $data; + } + + /** + * @inheritdoc + * + * @return string The endpoint for the create token request. + */ + public function getEndpoint() + { + return $this->endpoint . '/tokens'; + } +} \ No newline at end of file From 39c95046770f7efe02cf7cdc3250ef011e07132d Mon Sep 17 00:00:00 2001 From: Pedro Tanaka Date: Mon, 19 Jun 2017 19:51:26 -0300 Subject: [PATCH 3/6] Fixing code sniffer problems. --- src/Message/CreateTokenRequest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Message/CreateTokenRequest.php b/src/Message/CreateTokenRequest.php index 74208061..79d1605f 100644 --- a/src/Message/CreateTokenRequest.php +++ b/src/Message/CreateTokenRequest.php @@ -55,7 +55,7 @@ public function getData() if ($this->getParameter('customer')) { $data['customer'] = $this->getParameter('customer'); - } else if ($this->getParameter('card')) { + } elseif ($this->getParameter('card')) { $data['card'] = $this->getParameter('card'); } else { throw new InvalidRequestException("You must pass either the card or the customer"); @@ -73,4 +73,4 @@ public function getEndpoint() { return $this->endpoint . '/tokens'; } -} \ No newline at end of file +} From d640ecb0f39379a7caa2d3d24875163e2ba98fd2 Mon Sep 17 00:00:00 2001 From: Pedro Tanaka Date: Sun, 25 Jun 2017 08:15:35 -0300 Subject: [PATCH 4/6] Using PHPUnit comment based exception testing. --- tests/Message/CreateTokenRequestTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/Message/CreateTokenRequestTest.php b/tests/Message/CreateTokenRequestTest.php index 21fa26f8..5d3ddeda 100644 --- a/tests/Message/CreateTokenRequestTest.php +++ b/tests/Message/CreateTokenRequestTest.php @@ -33,10 +33,12 @@ public function testEndpoint() $this->assertSame('https://api.stripe.com/v1/tokens', $this->request->getEndpoint()); } + /** + * @expectedException \Omnipay\Common\Exception\InvalidRequestException + * @expectedExceptionMessage You must pass either the card or the customer + */ public function testGetDataInvalid() { - $this->setExpectedException(InvalidRequestException::class, "You must pass either the card or the customer"); - $this->request->setCustomer(null); $this->request->setCard(null); From d1e29bc13eab36360c75c06d8de1ac78b366910d Mon Sep 17 00:00:00 2001 From: Pedro Tanaka Date: Sun, 25 Jun 2017 08:21:38 -0300 Subject: [PATCH 5/6] Removing access to array by reference (PHP 5.3 compatibility issue) --- tests/Message/CreateTokenRequestTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Message/CreateTokenRequestTest.php b/tests/Message/CreateTokenRequestTest.php index 5d3ddeda..8ccc2ef6 100644 --- a/tests/Message/CreateTokenRequestTest.php +++ b/tests/Message/CreateTokenRequestTest.php @@ -9,7 +9,6 @@ namespace Omnipay\Stripe\Message; -use Omnipay\Common\Exception\InvalidRequestException; use Omnipay\Tests\TestCase; class CreateTokenRequestTest extends TestCase @@ -70,11 +69,12 @@ public function testResponseSuccess() $this->setMockHttpResponse('CreateTokenSuccess.txt'); $response = $this->request->send(); + $data = $response->getData(); $this->assertTrue($response->isSuccessful()); $this->assertFalse($response->isRedirect()); $this->assertNull($response->getTransactionReference()); - $this->assertSame('tok_1AWDl1JqXiFraDuL2xOKEXKy', $response->getData()['id']); - $this->assertSame('token', $response->getData()['object']); + $this->assertSame('tok_1AWDl1JqXiFraDuL2xOKEXKy', $data['id']); + $this->assertSame('token', $data['object']); } From ccf90512d4134895f576390ee0455329972ac968 Mon Sep 17 00:00:00 2001 From: Pedro Tanaka Date: Sun, 25 Jun 2017 08:25:37 -0300 Subject: [PATCH 6/6] Switching to old array syntax (PHP 5.3) --- src/Message/CreateTokenRequest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Message/CreateTokenRequest.php b/src/Message/CreateTokenRequest.php index 79d1605f..5b7c16dc 100644 --- a/src/Message/CreateTokenRequest.php +++ b/src/Message/CreateTokenRequest.php @@ -51,7 +51,7 @@ public function setCustomer($customer) */ public function getData() { - $data = []; + $data = array(); if ($this->getParameter('customer')) { $data['customer'] = $this->getParameter('customer');