Skip to content

Commit

Permalink
Updates for implementation
Browse files Browse the repository at this point in the history
1. Swap the transactionId and transactionReference values to properly map to the correct CyberSource fields.
2. Updated the Response class to crawl the response and determine if the request was a success. This method will set the isSuccess flag as well as record a relevant error message. A custom error message is generate for Missing Fields and Invalid Field errors but the rest will map to a standard error message from CyberSource documentation.
  • Loading branch information
dongreenmedl committed Nov 5, 2019
1 parent ceb437f commit b3b3c67
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 19 deletions.
13 changes: 9 additions & 4 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Omnipay\CyberSourceSimpleOrder\Message;

use CybsClient;
use Omnipay\Common\Exception\InvalidRequestException;

abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest
{
Expand Down Expand Up @@ -51,11 +52,15 @@ public function sendData($data)
$this->getApiHosts()
);

$client = new CybsClient(array(), $options, true);
$requestNvp = $this->arrayToNvp($data);
$reply = $client->runTransaction($requestNvp);
try {
$client = new CybsClient(array(), $options, true);
$requestNvp = $this->arrayToNvp($data);
$reply = $client->runTransaction($requestNvp);

return $this->response = new Response($this, $reply);
return $this->response = new Response($this, $reply);
} catch (\Exception $e) {
throw new InvalidRequestException($e->getMessage());
}
}

protected function getResourcePath()
Expand Down
2 changes: 1 addition & 1 deletion src/Message/AuthorizeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function getData()
$data = array(
'ccAuthService_run' => 'true',
'merchantID' => $this->getMerchantId(),
'merchantReferenceCode' => $this->getTransactionReference(),
'merchantReferenceCode' => $this->getTransactionId(),
'recurringSubscriptionInfo_subscriptionID' => $this->getToken(),
'purchaseTotals_currency' => $this->getCurrency(),
'purchaseTotals_grandTotalAmount' => $this->getAmount()
Expand Down
4 changes: 2 additions & 2 deletions src/Message/CaptureRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public function getData()
{
$data = array(
'ccCaptureService_run' => 'true',
'ccCaptureService_authRequestID' => $this->getTransactionId(),
'ccCaptureService_authRequestID' => $this->getTransactionReference(),
'merchantID' => $this->getMerchantId(),
'merchantReferenceCode' => $this->getTransactionReference(),
'merchantReferenceCode' => $this->getTransactionId(),
'purchaseTotals_currency' => $this->getCurrency(),
'purchaseTotals_grandTotalAmount' => $this->getAmount()
);
Expand Down
2 changes: 1 addition & 1 deletion src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function getData()
'ccAuthService_run' => 'true',
'ccCaptureService_run' => 'true',
'merchantID' => $this->getMerchantId(),
'merchantReferenceCode' => $this->getTransactionReference(),
'merchantReferenceCode' => $this->getTransactionId(),
'recurringSubscriptionInfo_subscriptionID' => $this->getToken(),
'purchaseTotals_currency' => $this->getCurrency(),
'purchaseTotals_grandTotalAmount' => $this->getAmount()
Expand Down
4 changes: 2 additions & 2 deletions src/Message/RefundRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public function getData()
{
$data = array(
'ccCreditService_run' => 'true',
'ccCreditService_captureRequestID' => $this->getTransactionId(),
'ccCreditService_captureRequestID' => $this->getTransactionReference(),
'merchantID' => $this->getMerchantId(),
'merchantReferenceCode' => $this->getTransactionReference(),
'merchantReferenceCode' => $this->getTransactionId(),
'purchaseTotals_currency' => $this->getCurrency(),
'purchaseTotals_grandTotalAmount' => $this->getAmount()
);
Expand Down
115 changes: 108 additions & 7 deletions src/Message/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,116 @@
class Response extends AbstractResponse
{
protected $statusCode;
private $isSuccess = false;
private $errorMessage = "";

private static $result_codes = array(
'100' => 'Successful transaction.',
'101' => 'The request is missing one or more required fields.',
'102' => 'One or more fields in the request contains invalid data.',
'110' => 'Only a partial amount was approved.',
'150' => 'Error: General system failure.',
'151' => 'Error: The request was received but there was a server timeout.',
'152' => 'Error: The request was received, but a service did not finish running in time.',
'200' => 'The authorization request was approved by the issuing bank but declined by CyberSource because it did not pass the Address Verification Service (AVS) check.',
'201' => 'The issuing bank has questions about the request.',
'202' => 'Expired card.',
'203' => 'General decline of the card.',
'204' => 'Insufficient funds in the account.',
'205' => 'Stolen or lost card.',
'207' => 'Issuing bank unavailable.',
'208' => 'Inactive card or card not authorized for card-not-present transactions.',
'209' => 'American Express Card Identification Digits (CID) did not match.',
'210' => 'The card has reached the credit limit.',
'211' => 'Invalid CVN.',
'221' => 'The customer matched an entry on the processor\'s negative file.',
'230' => 'The authorization request was approved by the issuing bank but declined by CyberSource because it did not pass the CVN check.',
'231' => 'Invalid credit card number.',
'232' => 'The card type is not accepted by the payment processor.',
'233' => 'General decline by the processor.',
'234' => 'There is a problem with your CyberSource merchant configuration.',
'235' => 'The requested amount exceeds the originally authorized amount.',
'236' => 'Processor failure.',
'237' => 'The authorization has already been reversed.',
'238' => 'The authorization has already been captured.',
'239' => 'The requested transaction amount must match the previous transaction amount.',
'240' => 'The card type sent is invalid or does not correlate with the credit card number.',
'241' => 'The request ID is invalid.',
'242' => 'You requested a capture, but there is no corresponding, unused authorization record.',
'243' => 'The transaction has already been settled or reversed.',
'246' => 'The capture or credit is not voidable because the capture or credit information has laready been submitted to your processor. Or, you requested a void for a type of transaction that cannot be voided.',
'247' => 'You requested a credit for a capture that was previously voided.',
'250' => 'Error: The request was received, but there was a timeout at the payment processor.',
'520' => 'The authorization request was approved by the issuing bank but declined by CyberSource based on your Smart Authorization settings.',
);


public function __construct(RequestInterface $request, $data)
{
parent::__construct($request, $data);
$this->crawlResponse();
}

private function crawlResponse()
{
if ($this->getData()->decision != 'ACCEPT') {
// Request is missing fields
if ($this->getData()->reasonCode == 101) {
$missing_fields = 'Missing fields: ';

$fields = $this->preg_grep_keys('/^missingField_\d+$/', (array)$this->getData());
if (empty($fields)) {
$missing_fields = $missing_fields . 'Unknown';
} else {
$missing_fields = $missing_fields . implode(', ', $this->vals_from_keys($fields, $this->getData()));
}

$this->isSuccess = false;
$this->errorMessage = $missing_fields;
return;
}

// Request contains invalid fields
if ($this->getData()->reasonCode == 102) {
$invalid_fields = 'Invalid fields: ';

$fields = $this->preg_grep_keys('/^invalidField_\d+$/', (array)$this->getData());
if (empty($fields)) {
$invalid_fields = $invalid_fields . 'Unknown';
} else {
$invalid_fields = $invalid_fields . implode(', ', $this->vals_from_keys($fields, $this->getData()));
}

$this->isSuccess = false;
$this->errorMessage = $invalid_fields;
return;
}

// Throw error defined by CyberSource
$this->isSuccess = false;
$this->errorMessage = self::$result_codes[$this->getData()->reasonCode ];
} else {
$this->isSuccess = true;
}
}

/**
* @param $keys {array}
* @param $input {object}
* @return array
*/
private function vals_from_keys($keys, $input)
{
$vals = [];
foreach ($keys as $key) {
array_push($vals, $input->$key);
}
return $vals;
}

private function preg_grep_keys($pattern, $input, $flags = 0)
{
return preg_grep($pattern, array_keys($input), $flags);
}

/**
Expand All @@ -24,17 +130,12 @@ public function __construct(RequestInterface $request, $data)
public function isSuccessful()
{
// Success if the statusCode is within the 2xx range
return $this->getCode() >= 200 && $this->getCode() < 300;
return $this->isSuccess;
}

public function getMessage()
{
return $this->getData();
}

public function getCode()
{
return $this->statusCode = 200;
return $this->errorMessage;
}

public function getData()
Expand Down
4 changes: 2 additions & 2 deletions src/Message/VoidRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public function getData()
$data = array(
'voidService_run' => 'true',
'merchantID' => $this->getMerchantId(),
'merchantReferenceCode' => $this->getTransactionReference(),
'voidService_voidRequestID' => $this->getTransactionId()
'merchantReferenceCode' => $this->getTransactionId(),
'voidService_voidRequestID' => $this->getTransactionReference()
);

return $data;
Expand Down

0 comments on commit b3b3c67

Please sign in to comment.