You are here

public function SquareApi::refund in Commerce Square Connect 7

Refunds a transaction.

Parameters

object $transaction: The payment transaction.

int $amount: The amount.

Return value

\SquareConnect\Model\CreateRefundResponse The transaction response.

File

includes/SquareApi.php, line 111
Square Connect SDK wrapper.

Class

SquareApi
A wrapper around the Square Connect SDK.

Code

public function refund($transaction, $amount) {
  $payment_method = commerce_payment_method_instance_load($transaction->instance_id);
  $mode = $payment_method['settings']['mode'];
  $location_id = $mode = $payment_method['settings'][$mode . '_location_id'];
  list($transaction_id, $tender_id) = explode('|', $transaction->remote_id);
  $transaction_api = new TransactionsApi($this
    ->getClient());
  $refund_request = new CreateRefundRequest(array(
    'idempotency_key' => uniqid(),
    'tender_id' => $tender_id,
    'amount_money' => new Money(array(
      'amount' => (int) $amount,
      'currency' => $transaction->currency_code,
    )),
    'reason' => 'Refunded through store backend',
  ));
  return $transaction_api
    ->createRefund($location_id, $transaction_id, $refund_request);
}