diff -u b/modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentInformation.php b/modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentInformation.php --- b/modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentInformation.php +++ b/modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentInformation.php @@ -364,7 +364,7 @@ $pane_form['#payment_options'] = []; $user_input = $form_state->getUserInput(); - $add_new_payment_method = $user_input && !empty($user_input['payment_information']['add_new_payment_method']); + $add_new_payment_method_selected = $user_input && !empty($user_input['payment_information']['add_new_payment_method']); if ($stored_options = $this->paymentOptionsBuilder->buildStoredOptions($this->order, $payment_gateways)) { $stored_option_labels = array_map(function (PaymentOption $option) { @@ -405,7 +405,7 @@ // Fix order billing profile address in case when we select another // payment method and then refresh the checkout page. /** @var \Drupal\commerce_payment\entity\PaymentMethodInterface $default_stored_option */ - if ($this->configuration['require_billing_information'] && $this->order->getBillingProfile() && !$add_new_payment_method) { + if ($this->configuration['require_billing_information'] && $this->order->getBillingProfile() && !$add_new_payment_method_selected) { /** @var \Drupal\commerce_payment\entity\PaymentMethodInterface $default_payment_method */ $default_payment_method = $this->entityTypeManager->getStorage('commerce_payment_method')->load($default_stored_option->getId()); /** @var \Drupal\profile\entity\Profile $default_payment_method_profile */ @@ -427,7 +427,7 @@ $pane_form['add_new_payment_method'] = [ '#type' => 'checkbox', '#title' => $this->t('Add new payment method'), - '#default_value' => $add_new_payment_method, + '#default_value' => empty($stored_options) || $add_new_payment_method_selected, '#access' => !empty($stored_options), '#weight' => 0, '#ajax' => [ @@ -649,6 +649,16 @@ } /** @var \Drupal\commerce_payment\PaymentGatewayStorageInterface $payment_gateway_storage */ $payment_gateway_storage = $this->entityTypeManager->getStorage('commerce_payment_gateway'); + + if ($this->configuration['require_billing_information'] && + is_null($selected_option) && + $form_state->has('billing_profile') + ) { + $this->order->setBillingProfile($form_state->get('billing_profile')); + $this->order->save(); + return; + } + /** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */ $payment_gateway = $payment_gateway_storage->load($selected_option->getPaymentGatewayId()); if (!$payment_gateway) { @@ -671,7 +681,7 @@ if (is_numeric($selected_address)) { $selected_address_profile = $this->entityTypeManager->getStorage('profile')->load($selected_address); $order_billing_profile->populateFromProfile($selected_address_profile); - $order_billing_profile->populateFromProfile($selected_address_profile, ['data']); + $order_billing_profile->setData('address_book_profile_id', $selected_address_profile->id()); $order_billing_profile->save(); $this->order->save(); } @@ -758,7 +768,6 @@ /** @var \Drupal\commerce\Plugin\Commerce\InlineForm\EntityInlineFormInterface $inline_form */ $inline_form = $pane_form['add_payment_method']['#inline_form']; // The payment method was just created. - /** @var PaymentMethodInterface $payment_method */ $payment_method = $inline_form->getEntity(); if ($form_state->has('billing_profile') && $payment_gateway_plugin->collectsBillingInformation()) { $payment_method->setBillingProfile($form_state->get('billing_profile')); @@ -795,24 +804,7 @@ $this->order->set('payment_gateway', $payment_gateway); $this->order->set('payment_method', NULL); if ($form_state->has('billing_profile')) { - - $payment_method_profile = $form_state->get('billing_profile'); - if ($payment_method_profile) { - $order_billing_profile = $this->order->getBillingProfile(); - if (!$order_billing_profile) { - $order_billing_profile = $this->entityTypeManager->getStorage('profile') - ->create([ - 'type' => 'customer', - 'uid' => 0, - ]); - } - $order_billing_profile->populateFromProfile($payment_method_profile); - // The data field is not copied by default but needs to be. - // For example, both profiles need to have an address_book_profile_id. - $order_billing_profile->populateFromProfile($payment_method_profile, ['data']); - $order_billing_profile->save(); - $this->order->setBillingProfile($order_billing_profile); - } + $this->order->setBillingProfile($form_state->get('billing_profile')); } } }