Commit 9f451cdd authored by Bojan Živanović's avatar Bojan Živanović
Browse files

Issue #2854145 by bojanz, googletorp: Order payment method should be selectable and default

parent b9e55989
Loading
Loading
Loading
Loading
+10 −1
Changes for modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentInformation.php: 10 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -176,22 +176,31 @@ class PaymentInformation extends CheckoutPaneBase implements ContainerFactoryPlu
    $payment_gateway_plugin = $payment_gateway->getPlugin();
    $options = [];
    $default_option = NULL;
    $order_payment_method = $this->order->payment_method->entity;
    $customer = $this->order->getCustomer();
    if ($customer) {
      $payment_methods = $payment_method_storage->loadReusable($customer, $payment_gateway);
      foreach ($payment_methods as $payment_method) {
        $options[$payment_method->id()] = $payment_method->label();
      }
      // The order's payment method must always be available.
      if ($order_payment_method && !isset($options[$order_payment_method->id()])) {
        $options[$order_payment_method->id()] = $order_payment_method->label();
      }
    }
    $payment_method_types = $payment_gateway_plugin->getPaymentMethodTypes();
    foreach ($payment_method_types as $payment_method_type) {
      $id = 'new_' . $payment_method_type->getPluginId();
      $options[$id] = $payment_method_type->getCreateLabel();
    }
    $values = $form_state->getValue($pane_form['#parents']);
    $user_input = $form_state->getUserInput();
    $values = NestedArray::getValue($user_input, $pane_form['#parents']);
    if (!empty($values['payment_method'])) {
      $selected_option = $values['payment_method'];
    }
    elseif ($order_payment_method) {
      $selected_option = $this->order->payment_method->target_id;
    }
    else {
      $default_payment_method_type = $payment_gateway_plugin->getDefaultPaymentMethodType();
      $selected_option = 'new_' . $default_payment_method_type->getPluginId();
+31 −4
Changes for modules/payment/tests/src/Functional/PaymentCheckoutTest.php: 31 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -30,6 +30,13 @@ class PaymentCheckoutTest extends CommerceBrowserTestBase {
   */
  protected $product;

  /**
   * A payment method.
   *
   * @var \Drupal\commerce_payment\Entity\PaymentMethodInterface
   */
  protected $paymentMethod;

  /**
   * Modules to enable.
   *
@@ -78,6 +85,14 @@ class PaymentCheckoutTest extends CommerceBrowserTestBase {
    ]);
    $gateway->save();

    $this->paymentMethod = $this->createEntity('commerce_payment_method', [
      'type' => 'credit_card',
      'payment_gateway' => $gateway->id(),
      'card_type' => 'visa',
      'card_number' => '9999',
      'reusable' => FALSE,
    ]);

    // Cheat so we don't need JS to interact w/ Address field widget.
    /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $customer_form_display */
    $customer_form_display = EntityFormDisplay::load('profile.customer.default');
@@ -93,10 +108,23 @@ class PaymentCheckoutTest extends CommerceBrowserTestBase {
  public function testCheckoutWithPayment() {
    $this->drupalGet($this->product->toUrl()->toString());
    $this->submitForm([], 'Add to cart');
    $cart_link = $this->getSession()->getPage()->findLink('your cart');
    $cart_link->click();
    $this->submitForm([], 'Checkout');
    // The order's payment method should be available and default.
    $order = Order::load(1);
    $order->payment_method = $this->paymentMethod;
    $order->save();
    $this->drupalGet('checkout/1');
    $this->assertSession()->pageTextContains('Order Summary');
    $radio_button = $this->getSession()->getPage()->findField('Visa ending in 9999');
    $this->assertNotNull($radio_button);
    $this->assertTrue($radio_button->getAttribute('checked'));
    // Without it, 'New credit card' should be default.
    $order->payment_method = NULL;
    $order->save();
    $this->drupalGet('checkout/1');
    $radio_button = $this->getSession()->getPage()->findField('New credit card');
    $this->assertNotNull($radio_button);
    $this->assertTrue($radio_button->getAttribute('checked'));

    $this->submitForm([
      'payment_information[add_payment_method][payment_details][number]' => '4111111111111111',
      'payment_information[add_payment_method][payment_details][expiration][month]' => '02',
@@ -119,7 +147,6 @@ class PaymentCheckoutTest extends CommerceBrowserTestBase {
    $this->assertSession()->pageTextContains('Your order number is 1. You can view your order on your account page when logged in.');

    $order = Order::load(1);

    /** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
    $payment_gateway = $order->payment_gateway->entity;
    $this->assertEquals('example_onsite', $payment_gateway->id());