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

Commit

Permalink
Adds refund transaction functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Paul committed Oct 20, 2016
1 parent c6d524f commit 4dceb1c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
20 changes: 20 additions & 0 deletions src/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.';
Expand Down

0 comments on commit 4dceb1c

Please sign in to comment.