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

Commit

Permalink
Adds void transaction functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Paul committed Oct 20, 2016
1 parent 6eb5dc3 commit 7e2ebb0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
27 changes: 27 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,31 @@ public function verify(array $params = [])

return $this->process($transaction);
}

/**
* Void a transaction.
*
* @param \CraigPaul\Moneris\Transaction|string $transaction
* @param string|null $order
*
* @return \CraigPaul\Moneris\Response
*/
public function void($transaction, string $order = null)
{
if ($transaction instanceof Transaction) {
$order = $transaction->order();
$transaction = $transaction->number();
}

$params = [
'type' => 'purchasecorrection',
'crypt_type' => Crypt::SSL_ENABLED_MERCHANT,
'txn_number' => $transaction,
'order_id' => $order,
];

$transaction = $this->transaction($params);

return $this->process($transaction);
}
}
39 changes: 36 additions & 3 deletions src/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @property-read array $errors
* @property-read \CraigPaul\Moneris\Gateway $gateway
* @property-read array $params
* @property SimpleXMLElement $response
* @property SimpleXMLElement|null $response
*/
class Transaction
{
Expand Down Expand Up @@ -38,9 +38,9 @@ class Transaction
protected $params;

/**
* @var SimpleXMLElement
* @var SimpleXMLElement|null
*/
protected $response;
protected $response = null;

/**
* Create a new Transaction instance.
Expand All @@ -64,6 +64,34 @@ public function invalid()
return !$this->valid();
}

/**
* Retrieve the transaction number, assuming the transaction has been processed.
*
* @return null|string
*/
public function number()
{
if (is_null($this->response)) {
return null;
}

return (string)$this->response->receipt->TransID;
}

/**
* Retrieve the order id for the transaction. The is only available on certain transaction types.
*
* @return string|null
*/
public function order()
{
if (isset($this->params['order_id'])) {
return $this->params['order_id'];
}

return null;
}

/**
* Prepare the transaction parameters.
*
Expand Down Expand Up @@ -183,6 +211,11 @@ public function valid()
$errors[] = Validator::set($params, 'cvd') ? null : 'CVD not provided.';
}

break;
case 'purchasecorrection':
$errors[] = Validator::set($params, 'order_id') ? null : 'Order id not provided.';
$errors[] = Validator::set($params, 'txn_number') ? null : 'Transaction number not provided.';

break;
default:
$errors[] = $params['type'].' is not a supported transaction type.';
Expand Down

0 comments on commit 7e2ebb0

Please sign in to comment.