You are here

function commerce_paypal_form_commerce_checkout_flow_alter in Commerce PayPal 8

Implements hook_form_BASE_FORM_ID_alter() for commerce_checkout_flow.

File

./commerce_paypal.module, line 81
Implements PayPal payment services for use with Drupal Commerce.

Code

function commerce_paypal_form_commerce_checkout_flow_alter(&$form, FormStateInterface $form_state) {

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = \Drupal::routeMatch()
    ->getParameter('commerce_order');

  // Loop over the payment methods to remove potentially duplicate PayPal
  // options (See http://www.drupal.org/project/commerce_paypal/issues/3154770).
  if (isset($form['payment_information']['payment_method'], $form['payment_information']['#payment_options'])) {

    /** @var \Drupal\commerce_payment\PaymentOption $payment_option */
    $paypal_checkout_options_count = 0;
    foreach ($form['payment_information']['#payment_options'] as $key => $payment_option) {

      /** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
      $payment_gateway = PaymentGateway::load($payment_option
        ->getPaymentGatewayId());
      $plugin = $payment_gateway
        ->getPlugin();
      if ($plugin instanceof CheckoutInterface && $plugin
        ->getPaymentSolution() === 'smart_payment_buttons') {
        $paypal_checkout_options_count++;

        // This will ensure we only keep the first paypal checkout option found.
        if ($paypal_checkout_options_count > 1 && isset($form['payment_information']['payment_method']['#options'][$key])) {
          unset($form['payment_information']['payment_method']['#options'][$key]);
        }
      }
    }
  }

  // Inject the Smart payment buttons on the review page.
  if ($form['#step_id'] !== 'review') {
    return;
  }
  if ($order
    ->get('payment_gateway')
    ->isEmpty() || !$order
    ->get('payment_gateway')->entity || $order
    ->get('checkout_flow')->target_id === 'paypal_checkout') {
    return;
  }

  // Skip injecting the smart payment buttons if the order total is zero or
  // negative.
  if (!$order
    ->getTotalPrice() || !$order
    ->getTotalPrice()
    ->isPositive()) {
    return;
  }

  /** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
  $payment_gateway = $order
    ->get('payment_gateway')->entity;
  $payment_gateway_plugin = $payment_gateway
    ->getPlugin();
  if (!$payment_gateway_plugin instanceof CheckoutInterface || $payment_gateway_plugin
    ->getPaymentSolution() !== 'smart_payment_buttons') {
    return;
  }

  /** @var \Drupal\commerce_paypal\SmartPaymentButtonsBuilderInterface $builder */
  $builder = \Drupal::service('commerce_paypal.smart_payment_buttons_builder');
  $form['paypal_smart_payment_buttons'] = $builder
    ->build($order, $payment_gateway, TRUE);
  $form['actions']['#access'] = FALSE;

  // Put back the "go back" link.
  if (isset($form['actions']['next']['#suffix'])) {
    $form['paypal_smart_payment_buttons']['#suffix'] = $form['actions']['next']['#suffix'];
  }
}