Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for void #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion src/base/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -395,6 +415,14 @@ public function supportsCapture(): bool
return $this->gateway()->supportsCapture();
}

/**
* @inheritdoc
*/
public function supportsVoid(): bool
{
return $this->gateway()->supportsVoid();
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -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.
*
Expand Down