You are here

public function StoredOffsiteRedirect::createPayment in Commerce Core 8.2

Creates a payment.

Parameters

\Drupal\commerce_payment\Entity\PaymentInterface $payment: The payment.

bool $capture: Whether the created payment should be captured (VS authorized only). Allowed to be FALSE only if the plugin supports authorizations.

Throws

\InvalidArgumentException If $capture is FALSE but the plugin does not support authorizations.

\Drupal\commerce_payment\Exception\PaymentGatewayException Thrown when the transaction fails for any reason.

Overrides SupportsStoredPaymentMethodsInterface::createPayment

File

modules/payment_example/src/Plugin/Commerce/PaymentGateway/StoredOffsiteRedirect.php, line 117

Class

StoredOffsiteRedirect
Provides an example offsite payment gateway with stored payment methods.

Namespace

Drupal\commerce_payment_example\Plugin\Commerce\PaymentGateway

Code

public function createPayment(PaymentInterface $payment, $capture = TRUE) {
  $this
    ->assertPaymentState($payment, [
    'new',
  ]);
  $payment_method = $payment
    ->getPaymentMethod();
  $this
    ->assertPaymentMethod($payment_method);

  // Perform the create payment request here, throw an exception if it fails.
  // See \Drupal\commerce_payment\Exception for the available exceptions.
  // Remember to take into account $capture when performing the request.
  $amount = $payment
    ->getAmount();
  $payment_method_token = $payment_method
    ->getRemoteId();

  // The remote ID returned by the request.
  $remote_id = '123456';
  $next_state = $capture ? 'completed' : 'authorization';
  $payment
    ->setState($next_state);
  $payment
    ->setRemoteId($remote_id);
  $payment
    ->save();
}