From 95c51ecdcdeed8a5ab7110b3b6947eebfea13b67 Mon Sep 17 00:00:00 2001 From: Joel Date: Fri, 29 May 2020 13:25:03 -0500 Subject: [PATCH] Added support for void --- src/base/Gateway.php | 47 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/base/Gateway.php b/src/base/Gateway.php index 928a0e1..333f783 100644 --- a/src/base/Gateway.php +++ b/src/base/Gateway.php @@ -111,6 +111,11 @@ abstract class Gateway extends BaseGateway */ public $sendCartInfo = false; + /** + * @var int The amount of time in seconds a transaction can be voided after being captured. + */ + public $voidWindow = 86400; + /** * @var AbstractGateway */ @@ -148,6 +153,21 @@ public function capture(Transaction $transaction, string $reference): RequestRes return $this->performRequest($captureRequest, $transaction); } + /** + * @inheritdoc + */ + public function void(Transaction $transaction, string $reference): RequestResponseInterface + { + if (!$this->supportsVoid()) { + throw new NotSupportedException(Craft::t('commerce', 'Voiding is not supported by this gateway')); + } + + $request = $this->createRequest($transaction); + $captureRequest = $this->prepareVoidRequest($request, $reference); + + return $this->performRequest($captureRequest, $transaction); + } + /** * @inheritdoc */ @@ -314,7 +334,7 @@ public function deletePaymentSource($token): bool * * @param CreditCard $card The credit card to populate. * @param CreditCardPaymentForm $paymentForm The payment form. - * + * * @return void */ public function populateCard($card, CreditCardPaymentForm $paymentForm) @@ -395,6 +415,14 @@ public function supportsCapture(): bool return $this->gateway()->supportsCapture(); } + /** + * @inheritdoc + */ + public function supportsVoid(): bool + { + return $this->gateway()->supportsVoid(); + } + /** * @inheritdoc */ @@ -800,6 +828,23 @@ protected function prepareCaptureRequest($request, string $reference): RequestIn return $captureRequest; } + /** + * Prepare a void request from request data and reference of the transaction being voided. + * + * @param array $request + * @param string $reference + * + * @return RequestInterface + */ + protected function prepareVoidRequest($request, string $reference): RequestInterface + { + /** @var AbstractRequest $captureRequest */ + $captureRequest = $this->gateway()->void($request); + $captureRequest->setTransactionReference($reference); + + return $captureRequest; + } + /** * Prepare a purchase request from request data. *