public function Onsite::capturePayment in Commerce Core 8.2
Captures the given authorized payment.
Only payments in the 'authorization' state can be captured.
Parameters
\Drupal\commerce_payment\Entity\PaymentInterface $payment: The payment to capture.
\Drupal\commerce_price\Price $amount: The amount to capture. If NULL, defaults to the entire payment amount.
Throws
\Drupal\commerce_payment\Exception\PaymentGatewayException Thrown when the transaction fails for any reason.
Overrides SupportsAuthorizationsInterface::capturePayment
File
- modules/
payment_example/ src/ Plugin/ Commerce/ PaymentGateway/ Onsite.php, line 129
Class
- Onsite
- Provides the On-site payment gateway.
Namespace
Drupal\commerce_payment_example\Plugin\Commerce\PaymentGatewayCode
public function capturePayment(PaymentInterface $payment, Price $amount = NULL) {
$this
->assertPaymentState($payment, [
'authorization',
]);
// If not specified, capture the entire amount.
$amount = $amount ?: $payment
->getAmount();
// Perform the capture request here, throw an exception if it fails.
// See \Drupal\commerce_payment\Exception for the available exceptions.
$remote_id = $payment
->getRemoteId();
$number = $amount
->getNumber();
$payment
->setState('completed');
$payment
->setAmount($amount);
$payment
->save();
}