Skip to content
This repository has been archived by the owner on Oct 30, 2020. It is now read-only.

Commit

Permalink
Add some extra handling for invalid cc / exp date where moneris doesn…
Browse files Browse the repository at this point in the history
…t return a status code.
  • Loading branch information
tappleby committed Oct 26, 2016
1 parent 09849d2 commit 63bcbad
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 46 deletions.
121 changes: 76 additions & 45 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Response
const DUPLICATE_TRANSACTION = -19;
const DECLINED = -20;
const NOT_AUTHORIZED = -21;
const INVALID_EXPIRY_DATE = -22;

const CVD = -4;
const CVD_NO_MATCH = -5;
Expand Down Expand Up @@ -142,51 +143,7 @@ public function validate()
$code = (int)$receipt->read('code');

if ($code >= 50 || $code === 0) {
switch ($receipt->read('code')) {
case '050':
case '074':
case 'null':
$this->status = Response::SYSTEM_UNAVAILABLE;
break;
case '051':
case '482':
case '484':
$this->status = Response::CARD_EXPIRED;
break;
case '075':
$this->status = Response::INVALID_CARD;
break;
case '076':
case '079':
case '080':
case '081':
case '082':
case '083':
$this->status = Response::INSUFFICIENT_FUNDS;
break;
case '077':
$this->status = Response::PREAUTH_FULL;
break;
case '078':
$this->status = Response::DUPLICATE_TRANSACTION;
break;
case '481':
case '483':
$this->status = Response::DECLINED;
break;
case '485':
$this->status = Response::NOT_AUTHORIZED;
break;
case '486':
case '487':
case '489':
case '490':
$this->status = Response::CVD;
break;
default:
$this->status = Response::ERROR;
}

$this->status = $this->convertReceiptCodeToStatus($receipt);
$this->successful = false;

return $this;
Expand Down Expand Up @@ -238,4 +195,78 @@ public function validate()

return $this;
}

protected function convertReceiptCodeToStatus(Receipt $receipt) {
$code = $receipt->read('code');

if ($code === 'null' && $message_status = $this->convertReceiptMessageToStatus($receipt)) {
$status = $message_status;
} else {
switch ($receipt->read('code')) {
case '050':
case '074':
case 'null':
$status = Response::SYSTEM_UNAVAILABLE;
break;
case '051':
case '482':
case '484':
$status = Response::CARD_EXPIRED;
break;
case '075':
$status = Response::INVALID_CARD;
break;

case '208':
case '475':
$status = Response::INVALID_EXPIRY_DATE;
break;

case '076':
case '079':
case '080':
case '081':
case '082':
case '083':
$status = Response::INSUFFICIENT_FUNDS;
break;
case '077':
$status = Response::PREAUTH_FULL;
break;
case '078':
$status = Response::DUPLICATE_TRANSACTION;
break;
case '481':
case '483':
$status = Response::DECLINED;
break;
case '485':
$status = Response::NOT_AUTHORIZED;
break;
case '486':
case '487':
case '489':
case '490':
$status = Response::CVD;
break;
default:
$status = Response::ERROR;
}
}

return $status;
}

protected function convertReceiptMessageToStatus(Receipt $receipt) {
$message = (string)$receipt->read('message');
$status = null;

if (preg_match('/invalid pan/i', $message)) {
$status = Response::INVALID_CARD;
} else if (preg_match('/invalid expiry date/i', $message)) {
$status = Response::INVALID_EXPIRY_DATE;
}

return $status;
}
}
34 changes: 33 additions & 1 deletion tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,36 @@ public function it_can_receive_a_receipt_from_a_properly_processed_transaction()
$this->assertEquals($this->params['order_id'], $receipt->read('id'));
$this->assertObjectHasAttribute('data', $receipt);
}
}

/** @test */
public function it_processes_expdate_error_edge_cases_from_message()
{
$response = $this->process_transaction([
'expdate' => 'foo'
]);

$this->assertFalse($response->successful);
$this->assertEquals(Response::INVALID_EXPIRY_DATE, $response->status);
}

/** @test */
public function it_processes_credit_card_error_edge_cases_from_message()
{
$response = $this->process_transaction([
'credit_card' => '1234'
]);

$this->assertFalse($response->successful);
$this->assertEquals(Response::INVALID_CARD, $response->status);
}

protected function process_transaction($extra_params = []) {
$this->params = array_merge($this->params, $extra_params);
$this->transaction = new Transaction($this->gateway, $this->params);

$response = $this->processor->process($this->transaction);
$response = $response->validate();

return $response;
}
}

0 comments on commit 63bcbad

Please sign in to comment.