You are here

protected function PaymentMethodWidget::buildOptions in Commerce Recurring Framework 8

Builds the payment options for the given subscription.

Parameters

\Drupal\commerce_recurring\Entity\SubscriptionInterface $subscription: A subscription entity.

Return value

\Drupal\commerce_payment\PaymentOption[] The payment options, keyed by option ID.

1 call to PaymentMethodWidget::buildOptions()
PaymentMethodWidget::formElement in src/Plugin/Field/FieldWidget/PaymentMethodWidget.php
Returns the form for a single field widget.

File

src/Plugin/Field/FieldWidget/PaymentMethodWidget.php, line 93

Class

PaymentMethodWidget
Plugin implementation of the 'commerce_recurring_payment_method' widget.

Namespace

Drupal\commerce_recurring\Plugin\Field\FieldWidget

Code

protected function buildOptions(SubscriptionInterface $subscription) {
  $customer = $subscription
    ->getCustomer();

  /** @var \Drupal\commerce_payment\PaymentGatewayStorageInterface $payment_gateway_storage */
  $payment_gateway_storage = $this->entityTypeManager
    ->getStorage('commerce_payment_gateway');
  $payment_gateways = $payment_gateway_storage
    ->loadByProperties([
    'status' => TRUE,
  ]);

  /** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface[] $payment_gateways_with_payment_methods */
  $payment_gateways_with_payment_methods = array_filter($payment_gateways, function ($payment_gateway) {

    /** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
    return $payment_gateway
      ->getPlugin() instanceof SupportsStoredPaymentMethodsInterface;
  });
  $options = [];

  // 1) Add options to reuse stored payment methods for known customers.
  if ($customer
    ->isAuthenticated()) {
    $billing_countries = $subscription
      ->getStore()
      ->getBillingCountries();

    /** @var \Drupal\commerce_payment\PaymentMethodStorageInterface $payment_method_storage */
    $payment_method_storage = $this->entityTypeManager
      ->getStorage('commerce_payment_method');
    foreach ($payment_gateways_with_payment_methods as $payment_gateway) {
      $payment_methods = $payment_method_storage
        ->loadReusable($customer, $payment_gateway, $billing_countries);
      foreach ($payment_methods as $payment_method_id => $payment_method) {
        $options[$payment_method_id] = new PaymentOption([
          'id' => $payment_method_id,
          'label' => $payment_method
            ->label(),
          'payment_gateway_id' => $payment_gateway
            ->id(),
          'payment_method_id' => $payment_method_id,
        ]);
      }
    }
  }

  // 2) Add the order's payment method if it was not included above.
  if ($subscription_payment_method = $subscription
    ->getPaymentMethod()) {
    $subscription_payment_method_id = $subscription_payment_method
      ->id();

    // Make sure that the payment method's gateway is still available.
    $payment_gateway_id = $subscription_payment_method
      ->getPaymentGatewayId();
    $payment_gateway_ids = EntityHelper::extractIds($payment_gateways_with_payment_methods);
    if (in_array($payment_gateway_id, $payment_gateway_ids) && !isset($options[$subscription_payment_method_id])) {
      $options[$subscription_payment_method_id] = new PaymentOption([
        'id' => $subscription_payment_method_id,
        'label' => $subscription_payment_method
          ->label(),
        'payment_gateway_id' => $subscription_payment_method
          ->getPaymentGatewayId(),
        'payment_method_id' => $subscription_payment_method_id,
      ]);
    }
  }
  return $options;
}