diff --git a/src/Gateway.php b/src/Gateway.php index fc73319..7f0ec0a 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -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); + } } \ No newline at end of file diff --git a/src/Transaction.php b/src/Transaction.php index 01c2e40..076df9e 100644 --- a/src/Transaction.php +++ b/src/Transaction.php @@ -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 { @@ -38,9 +38,9 @@ class Transaction protected $params; /** - * @var SimpleXMLElement + * @var SimpleXMLElement|null */ - protected $response; + protected $response = null; /** * Create a new Transaction instance. @@ -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. * @@ -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.';