diff --git a/src/Gateway.php b/src/Gateway.php index 71f45297..2b939d67 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -247,6 +247,16 @@ public function void(array $parameters = array()) return $this->createRequest('\Omnipay\Stripe\Message\VoidRequest', $parameters); } + /** + * @param array $parameters + * + * @return \Omnipay\Stripe\Message\FetchChargeRequest + */ + public function fetchCharge(array $parameters = array()) + { + return $this->createRequest('\Omnipay\Stripe\Message\FetchChargeRequest', $parameters); + } + /** * @param array $parameters * diff --git a/src/Message/CreateCustomerRequest.php b/src/Message/CreateCustomerRequest.php index b425cab3..9745194b 100644 --- a/src/Message/CreateCustomerRequest.php +++ b/src/Message/CreateCustomerRequest.php @@ -114,7 +114,7 @@ public function getData() } elseif ($this->getCard()) { $data['card'] = $this->getCardData(); $data['email'] = $this->getCard()->getEmail(); - } elseif ($this->getMetadata()) { + } elseif ($this->getEmail()) { $data['email'] = $this->getEmail(); } diff --git a/src/Message/FetchChargeRequest.php b/src/Message/FetchChargeRequest.php new file mode 100644 index 00000000..6aeefb9e --- /dev/null +++ b/src/Message/FetchChargeRequest.php @@ -0,0 +1,50 @@ +getParameter('chargeReference'); + } + + /** + * Set the charge reference. + * + * @param string + * @return FetchChargeRequest provides a fluent interface. + */ + public function setChargeReference($value) + { + return $this->setParameter('chargeReference', $value); + } + + public function getData() + { + $this->validate('chargeReference'); + } + + public function getEndpoint() + { + return $this->endpoint.'/charges/'.$this->getChargeReference(); + } + + public function getHttpMethod() + { + return 'GET'; + } +} diff --git a/src/Message/Response.php b/src/Message/Response.php index 6f75896d..432982ba 100644 --- a/src/Message/Response.php +++ b/src/Message/Response.php @@ -26,6 +26,20 @@ public function isSuccessful() return !isset($this->data['error']); } + /** + * Get the charge reference from the response of FetchChargeRequest. + * + * @return array|null + */ + public function getChargeReference() + { + if (isset($this->data['object']) && $this->data['object'] == 'charge') { + return $this->data['id']; + } + + return null; + } + /** * Get the transaction reference. * diff --git a/tests/Message/FetchChargeRequestTest.php b/tests/Message/FetchChargeRequestTest.php new file mode 100644 index 00000000..29e53d25 --- /dev/null +++ b/tests/Message/FetchChargeRequestTest.php @@ -0,0 +1,43 @@ +request = new FetchChargeRequest($this->getHttpClient(), $this->getHttpRequest()); + $this->request->setChargeReference('ch_180ZdUCryC0oikg4v4lc4F59D'); + } + + public function testEndpoint() + { + $this->assertSame('https://api.stripe.com/v1/charges/ch_180ZdUCryC0oikg4v4lc4F59D', $this->request->getEndpoint()); + } + + public function testSendSuccess() + { + $this->setMockHttpResponse('FetchChargeSuccess.txt'); + $response = $this->request->send(); + + $this->assertTrue($response->isSuccessful()); + $this->assertFalse($response->isRedirect()); + $this->assertSame('ch_180ZdUCryC0oikg4v4lc4F59D', $response->getChargeReference()); + $this->assertInternalType('array', $response->getSource()); + $this->assertNull($response->getMessage()); + } + + public function testSendFailure() + { + $this->setMockHttpResponse('FetchChargeFailure.txt'); + $response = $this->request->send(); + + $this->assertFalse($response->isSuccessful()); + $this->assertFalse($response->isRedirect()); + $this->assertNull($response->getChargeReference()); + $this->assertNull($response->getSource()); + $this->assertSame('No such charge: ch_180ZdUCryC0oikg4v4lc4F59D', $response->getMessage()); + } +} diff --git a/tests/Mock/FetchChargeFailure.txt b/tests/Mock/FetchChargeFailure.txt new file mode 100644 index 00000000..12bde922 --- /dev/null +++ b/tests/Mock/FetchChargeFailure.txt @@ -0,0 +1,16 @@ +HTTP/1.1 404 Not Found +Server: nginx +Date: Fri, 15 Apr 2016 21:42:18 GMT +Content-Type: application/json +Content-Length: 138 +Connection: keep-alive +Access-Control-Allow-Credentials: true +Cache-Control: no-cache, no-store + +{ + "error": { + "type": "invalid_request_error", + "message": "No such charge: ch_180ZdUCryC0oikg4v4lc4F59D", + "param": "id" + } +} \ No newline at end of file diff --git a/tests/Mock/FetchChargeSuccess.txt b/tests/Mock/FetchChargeSuccess.txt new file mode 100644 index 00000000..2e161414 --- /dev/null +++ b/tests/Mock/FetchChargeSuccess.txt @@ -0,0 +1,70 @@ +HTTP/1.1 200 OK +Server: nginx +Date: Fri, 15 Apr 2016 21:37:26 GMT +Content-Type: application/json +Content-Length: 1521 +Connection: keep-alive +Cache-Control: no-cache, no-store + +{ + "id": "ch_180ZdUCryC0oikg4v4lc4F59D", + "object": "charge", + "created": 1460437056, + "livemode": false, + "paid": true, + "status": "succeeded", + "amount": 1000, + "currency": "usd", + "refunded": false, + "source": { + "id": "card_990JozzC88C4rAAg4vg5yLG3j3", + "object": "card", + "last4": "1234", + "brand": "Visa", + "funding": "credit", + "exp_month": 1, + "exp_year": 2020, + "fingerprint": "32Q1po9Ujn5DpPgL", + "country": "US", + "name": "", + "address_line1": "", + "address_line2": "", + "address_city": "", + "address_state": "", + "address_zip": "", + "address_country": "", + "cvc_check": null, + "address_line1_check": null, + "address_zip_check": null, + "tokenization_method": null, + "dynamic_last4": null, + "metadata": {}, + "customer": "cus_8GoWuzFake3R8C" + }, + "source_transfer": null, + "captured": true, + "balance_transaction": "txn_180ZdUCry4Lot2g4vHVZH6y4A", + "failure_message": null, + "failure_code": null, + "amount_refunded": 0, + "customer": "cus_8GoWuzFake3R8C", + "invoice": "in_180Z1234yC4r2g4vCYO4qcIY", + "order": null, + "description": null, + "dispute": null, + "metadata": {}, + "statement_descriptor": "STATEMENT", + "fraud_details": {}, + "receipt_email": null, + "receipt_number": null, + "shipping": null, + "destination": null, + "application_fee": null, + "refunds": { + "object": "list", + "total_count": 0, + "has_more": false, + "url": "/v1/charges/ch_180ZdUCryC0oikg4v4lc4F59D/refunds", + "data": [] + } +} \ No newline at end of file