You are here

public function Stripe::createPaymentIntent in Commerce Stripe 8

Create a payment intent for an order.

Parameters

\Drupal\commerce_order\Entity\OrderInterface $order: The order.

bool $capture: Whether the created payment intent capture is automatic or manual.

Return value

\Stripe\PaymentIntent The payment intent.

Overrides StripeInterface::createPaymentIntent

File

src/Plugin/Commerce/PaymentGateway/Stripe.php, line 433

Class

Stripe
Provides the Stripe payment gateway.

Namespace

Drupal\commerce_stripe\Plugin\Commerce\PaymentGateway

Code

public function createPaymentIntent(OrderInterface $order, $capture = TRUE) {

  /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
  $payment_method = $order
    ->get('payment_method')->entity;
  $payment_method_remote_id = $payment_method
    ->getRemoteId();
  $customer_remote_id = $this
    ->getRemoteCustomerId($order
    ->getCustomer());
  $amount = $this->minorUnitsConverter
    ->toMinorUnits($order
    ->getTotalPrice());
  $order_id = $order
    ->id();
  $capture_method = $capture ? 'automatic' : 'manual';
  $intent_array = [
    'amount' => $amount,
    'currency' => strtolower($order
      ->getTotalPrice()
      ->getCurrencyCode()),
    'payment_method_types' => [
      'card',
    ],
    'metadata' => [
      'order_id' => $order_id,
      'store_id' => $order
        ->getStoreId(),
    ],
    'payment_method' => $payment_method_remote_id,
    'capture_method' => $capture_method,
  ];
  if (!empty($customer_remote_id)) {
    $intent_array['customer'] = $customer_remote_id;
  }
  try {
    $intent = PaymentIntent::create($intent_array);
    $order
      ->setData('stripe_intent', $intent->id)
      ->save();
  } catch (ApiErrorException $e) {
    ErrorHelper::handleException($e);
  }
  return $intent;
}