Skip to content

Commit 50bdec8

Browse files
authored
Merge pull request #67 from kinglozzer/62-error-message-code
FIX: error message/code may not always be set (fixes #62)
2 parents 035f147 + 3837060 commit 50bdec8

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

src/Message/Response.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public function getInvoiceItemReference()
294294
*/
295295
public function getMessage()
296296
{
297-
if (!$this->isSuccessful()) {
297+
if (!$this->isSuccessful() && isset($this->data['error']) && isset($this->data['error']['message'])) {
298298
return $this->data['error']['message'];
299299
}
300300

@@ -310,7 +310,7 @@ public function getMessage()
310310
*/
311311
public function getCode()
312312
{
313-
if (!$this->isSuccessful()) {
313+
if (!$this->isSuccessful() && isset($this->data['error']) && isset($this->data['error']['code'])) {
314314
return $this->data['error']['code'];
315315
}
316316

tests/Message/ResponseTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,32 @@ public function testPurchaseFailure()
4444
$this->assertNull($response->getSource());
4545
}
4646

47+
public function testPurchaseFailureWithoutMessage()
48+
{
49+
$httpResponse = $this->getMockHttpResponse('PurchaseFailureWithoutMessage.txt');
50+
$response = new Response($this->getMockRequest(), $httpResponse->json());
51+
52+
$this->assertFalse($response->isSuccessful());
53+
$this->assertFalse($response->isRedirect());
54+
$this->assertSame('ch_1JEJGNWFYxAwgF', $response->getTransactionReference());
55+
$this->assertNull($response->getCardReference());
56+
$this->assertNull($response->getMessage());
57+
$this->assertNull($response->getSource());
58+
}
59+
60+
public function testPurchaseFailureWithoutCode()
61+
{
62+
$httpResponse = $this->getMockHttpResponse('PurchaseFailureWithoutCode.txt');
63+
$response = new Response($this->getMockRequest(), $httpResponse->json());
64+
65+
$this->assertFalse($response->isSuccessful());
66+
$this->assertFalse($response->isRedirect());
67+
$this->assertSame('ch_1KGNWMAOUdAbbC', $response->getTransactionReference());
68+
$this->assertNull($response->getCardReference());
69+
$this->assertNull($response->getCode());
70+
$this->assertNull($response->getSource());
71+
}
72+
4773
public function testCreateCustomerSuccess()
4874
{
4975
$httpResponse = $this->getMockHttpResponse('CreateCustomerSuccess.txt');
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
HTTP/1.1 402 Payment Required
2+
Server: nginx
3+
Date: Fri, 15 Feb 2013 18:26:37 GMT
4+
Content-Type: application/json;charset=utf-8
5+
Content-Length: 151
6+
Connection: keep-alive
7+
Cache-Control: no-cache, no-store
8+
Access-Control-Allow-Credentials: true
9+
Access-Control-Max-Age: 300
10+
11+
{
12+
"error": {
13+
"message": "Your card was declined",
14+
"type": "card_error",
15+
"charge": "ch_1KGNWMAOUdAbbC"
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
HTTP/1.1 402 Payment Required
2+
Server: nginx
3+
Date: Fri, 15 Feb 2013 18:26:37 GMT
4+
Content-Type: application/json;charset=utf-8
5+
Content-Length: 151
6+
Connection: keep-alive
7+
Cache-Control: no-cache, no-store
8+
Access-Control-Allow-Credentials: true
9+
Access-Control-Max-Age: 300
10+
11+
{
12+
"error": {
13+
"type": "card_error",
14+
"code": "card_declined",
15+
"charge": "ch_1JEJGNWFYxAwgF"
16+
}
17+
}

0 commit comments

Comments
 (0)