From 3837060754153e293860a423c720ec857b1477e1 Mon Sep 17 00:00:00 2001 From: Loz Calver Date: Wed, 14 Dec 2016 16:38:13 +0000 Subject: [PATCH] FIX: error message/code may not always be set (fixes #62) --- src/Message/Response.php | 4 +-- tests/Message/ResponseTest.php | 26 ++++++++++++++++++++ tests/Mock/PurchaseFailureWithoutCode.txt | 17 +++++++++++++ tests/Mock/PurchaseFailureWithoutMessage.txt | 17 +++++++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 tests/Mock/PurchaseFailureWithoutCode.txt create mode 100644 tests/Mock/PurchaseFailureWithoutMessage.txt diff --git a/src/Message/Response.php b/src/Message/Response.php index 5249ff9b..10d6582b 100644 --- a/src/Message/Response.php +++ b/src/Message/Response.php @@ -294,7 +294,7 @@ public function getInvoiceItemReference() */ public function getMessage() { - if (!$this->isSuccessful()) { + if (!$this->isSuccessful() && isset($this->data['error']) && isset($this->data['error']['message'])) { return $this->data['error']['message']; } @@ -310,7 +310,7 @@ public function getMessage() */ public function getCode() { - if (!$this->isSuccessful()) { + if (!$this->isSuccessful() && isset($this->data['error']) && isset($this->data['error']['code'])) { return $this->data['error']['code']; } diff --git a/tests/Message/ResponseTest.php b/tests/Message/ResponseTest.php index 54390bb1..29ac58d9 100644 --- a/tests/Message/ResponseTest.php +++ b/tests/Message/ResponseTest.php @@ -44,6 +44,32 @@ public function testPurchaseFailure() $this->assertNull($response->getSource()); } + public function testPurchaseFailureWithoutMessage() + { + $httpResponse = $this->getMockHttpResponse('PurchaseFailureWithoutMessage.txt'); + $response = new Response($this->getMockRequest(), $httpResponse->json()); + + $this->assertFalse($response->isSuccessful()); + $this->assertFalse($response->isRedirect()); + $this->assertSame('ch_1JEJGNWFYxAwgF', $response->getTransactionReference()); + $this->assertNull($response->getCardReference()); + $this->assertNull($response->getMessage()); + $this->assertNull($response->getSource()); + } + + public function testPurchaseFailureWithoutCode() + { + $httpResponse = $this->getMockHttpResponse('PurchaseFailureWithoutCode.txt'); + $response = new Response($this->getMockRequest(), $httpResponse->json()); + + $this->assertFalse($response->isSuccessful()); + $this->assertFalse($response->isRedirect()); + $this->assertSame('ch_1KGNWMAOUdAbbC', $response->getTransactionReference()); + $this->assertNull($response->getCardReference()); + $this->assertNull($response->getCode()); + $this->assertNull($response->getSource()); + } + public function testCreateCustomerSuccess() { $httpResponse = $this->getMockHttpResponse('CreateCustomerSuccess.txt'); diff --git a/tests/Mock/PurchaseFailureWithoutCode.txt b/tests/Mock/PurchaseFailureWithoutCode.txt new file mode 100644 index 00000000..92b27c29 --- /dev/null +++ b/tests/Mock/PurchaseFailureWithoutCode.txt @@ -0,0 +1,17 @@ +HTTP/1.1 402 Payment Required +Server: nginx +Date: Fri, 15 Feb 2013 18:26:37 GMT +Content-Type: application/json;charset=utf-8 +Content-Length: 151 +Connection: keep-alive +Cache-Control: no-cache, no-store +Access-Control-Allow-Credentials: true +Access-Control-Max-Age: 300 + +{ + "error": { + "message": "Your card was declined", + "type": "card_error", + "charge": "ch_1KGNWMAOUdAbbC" + } +} diff --git a/tests/Mock/PurchaseFailureWithoutMessage.txt b/tests/Mock/PurchaseFailureWithoutMessage.txt new file mode 100644 index 00000000..200c6a39 --- /dev/null +++ b/tests/Mock/PurchaseFailureWithoutMessage.txt @@ -0,0 +1,17 @@ +HTTP/1.1 402 Payment Required +Server: nginx +Date: Fri, 15 Feb 2013 18:26:37 GMT +Content-Type: application/json;charset=utf-8 +Content-Length: 151 +Connection: keep-alive +Cache-Control: no-cache, no-store +Access-Control-Allow-Credentials: true +Access-Control-Max-Age: 300 + +{ + "error": { + "type": "card_error", + "code": "card_declined", + "charge": "ch_1JEJGNWFYxAwgF" + } +}