You are here

protected function Stripe::doCreatePaymentMethod in Commerce Stripe 8

Creates the payment method on the gateway.

Parameters

\Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method: The payment method.

array $payment_details: The gateway-specific payment details.

Return value

array The payment method information returned by the gateway. Notable keys:

  • token: The remote ID.

Credit card specific keys:

  • card_type: The card type.
  • last4: The last 4 digits of the credit card number.
  • expiration_month: The expiration month.
  • expiration_year: The expiration year.
1 call to Stripe::doCreatePaymentMethod()
Stripe::createPaymentMethod in src/Plugin/Commerce/PaymentGateway/Stripe.php
Creates a payment method with the given payment details.

File

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

Class

Stripe
Provides the Stripe payment gateway.

Namespace

Drupal\commerce_stripe\Plugin\Commerce\PaymentGateway

Code

protected function doCreatePaymentMethod(PaymentMethodInterface $payment_method, array $payment_details) {
  $stripe_payment_method_id = $payment_details['stripe_payment_method_id'];
  $owner = $payment_method
    ->getOwner();
  $customer_id = NULL;
  if ($owner && $owner
    ->isAuthenticated()) {
    $customer_id = $this
      ->getRemoteCustomerId($owner);
  }
  try {
    $stripe_payment_method = PaymentMethod::retrieve($stripe_payment_method_id);
    if ($customer_id) {
      $stripe_payment_method
        ->attach([
        'customer' => $customer_id,
      ]);
      $email = $owner
        ->getEmail();
    }
    elseif ($owner && $owner
      ->isAuthenticated()) {
      $email = $owner
        ->getEmail();
      $customer = Customer::create([
        'email' => $email,
        'description' => $this
          ->t('Customer for :mail', [
          ':mail' => $email,
        ]),
        'payment_method' => $stripe_payment_method_id,
      ]);
      $customer_id = $customer->id;
      $this
        ->setRemoteCustomerId($owner, $customer_id);
      $owner
        ->save();
    }
    else {
      $email = NULL;
    }
    if ($customer_id && $email) {
      $payment_method_data = [
        'email' => $email,
      ];
      if ($billing_profile = $payment_method
        ->getBillingProfile()) {
        $billing_address = $billing_profile
          ->get('address')
          ->first()
          ->toArray();
        $payment_method_data['address'] = [
          'city' => $billing_address['locality'],
          'country' => $billing_address['country_code'],
          'line1' => $billing_address['address_line1'],
          'line2' => $billing_address['address_line2'],
          'postal_code' => $billing_address['postal_code'],
          'state' => $billing_address['administrative_area'],
        ];
        $payment_method_data['name'] = $billing_address['given_name'] . ' ' . $billing_address['family_name'];
      }
      PaymentMethod::update($stripe_payment_method_id, [
        'billing_details' => $payment_method_data,
      ]);
    }
  } catch (ApiErrorException $e) {
    ErrorHelper::handleException($e);
  }
  return $stripe_payment_method->card;
}