Skip to content

Commit

Permalink
Merge pull request #67 from kinglozzer/62-error-message-code
Browse files Browse the repository at this point in the history
FIX: error message/code may not always be set (fixes #62)
  • Loading branch information
delatbabel committed Dec 15, 2016
2 parents 035f147 + 3837060 commit 50bdec8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Message/Response.php
Expand Up @@ -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'];
}

Expand All @@ -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'];
}

Expand Down
26 changes: 26 additions & 0 deletions tests/Message/ResponseTest.php
Expand Up @@ -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');
Expand Down
17 changes: 17 additions & 0 deletions 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"
}
}
17 changes: 17 additions & 0 deletions 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"
}
}

0 comments on commit 50bdec8

Please sign in to comment.