From 4dceb1c5118dd5847ece2d36283968f0c17975f7 Mon Sep 17 00:00:00 2001 From: Craig Paul Date: Thu, 20 Oct 2016 10:46:52 -0600 Subject: [PATCH] Adds refund transaction functionality --- src/Gateway.php | 27 +++++++++++++++++++++++++++ src/Transaction.php | 20 ++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/Gateway.php b/src/Gateway.php index 7f0ec0a..241eb7f 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -135,6 +135,33 @@ protected function process(Transaction $transaction) return Processor::process($transaction); } + /** + * @param \CraigPaul\Moneris\Transaction|string $transaction + * @param string|null $order + * + * @return \CraigPaul\Moneris\Response + */ + public function refund($transaction, string $order = null, $amount = null) + { + if ($transaction instanceof Transaction) { + $order = $transaction->order(); + $amount = $amount ?? $transaction->amount(); + $transaction = $transaction->number(); + } + + $params = [ + 'type' => 'refund', + 'crypt_type' => Crypt::SSL_ENABLED_MERCHANT, + 'amount' => $amount, + 'txn_number' => $transaction, + 'order_id' => $order, + ]; + + $transaction = $this->transaction($params); + + return $this->process($transaction); + } + /** * Get or create a new Transaction instance. * diff --git a/src/Transaction.php b/src/Transaction.php index 076df9e..87cc41a 100644 --- a/src/Transaction.php +++ b/src/Transaction.php @@ -54,6 +54,20 @@ public function __construct(Gateway $gateway, array $params = []) $this->params = $this->prepare($params); } + /** + * Retrieve the amount for the transaction. The is only available on certain transaction types. + * + * @return string|null + */ + public function amount() + { + if (isset($this->params['amount'])) { + return $this->params['amount']; + } + + return null; + } + /** * Check that the required parameters have not been provided to the transaction. * @@ -216,6 +230,12 @@ public function valid() $errors[] = Validator::set($params, 'order_id') ? null : 'Order id not provided.'; $errors[] = Validator::set($params, 'txn_number') ? null : 'Transaction number not provided.'; + break; + case 'refund': + $errors[] = Validator::set($params, 'amount') ? null : 'Amount not provided.'; + $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.';