From d705c25522c18d58704f85aad8ef0fce6ac6a44a Mon Sep 17 00:00:00 2001 From: mglaman Date: Mon, 17 Apr 2017 16:19:05 -0500 Subject: [PATCH 1/2] Issue #2870040 by mglaman: Remove payment method from order if there is a decline exception --- .../payment/src/Plugin/Commerce/CheckoutPane/PaymentProcess.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentProcess.php b/modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentProcess.php index b9d3bc0098..9fee06a599 100644 --- a/modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentProcess.php +++ b/modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentProcess.php @@ -118,6 +118,8 @@ public function buildPaneForm(array $pane_form, FormStateInterface $form_state, catch (DeclineException $e) { $message = $this->t('We encountered an error processing your payment method. Please verify your details and try again.'); drupal_set_message($message, 'error'); + $this->order->get('payment_gateway')->setValue(NULL); + $this->order->get('payment_method')->setValue(NULL); $this->redirectToPreviousStep(); } catch (PaymentGatewayException $e) { From 4b14ae3e3e23c74bf3bf8b387101ff313a186b0e Mon Sep 17 00:00:00 2001 From: Matt Glaman Date: Sat, 15 Sep 2018 06:41:31 +0200 Subject: [PATCH 2/2] Updates from issue --- .../src/Plugin/Commerce/CheckoutPane/PaymentProcess.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentProcess.php b/modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentProcess.php index d75f44b64d..4fa72d96c5 100644 --- a/modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentProcess.php +++ b/modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentProcess.php @@ -167,16 +167,21 @@ public function buildPaneForm(array $pane_form, FormStateInterface $form_state, $next_step_id = $this->checkoutFlow->getNextStepId($this->getStepId()); if ($payment_gateway_plugin instanceof OnsitePaymentGatewayInterface) { + /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */ + $payment_method = $this->order->payment_method->entity; try { - $payment->payment_method = $this->order->payment_method->entity; + $payment->payment_method = $payment_method; $payment_gateway_plugin->createPayment($payment, $this->configuration['capture']); $this->redirectToStep($next_step_id); } catch (DeclineException $e) { - $message = $this->t('We encountered an error processing your payment method. Please verify your details and try again.'); + $message = $this->t('"We encountered an error processing your payment method. Please re-enter your payment information.'); $this->messenger->addError($message); $this->order->get('payment_gateway')->setValue(NULL); $this->order->get('payment_method')->setValue(NULL); + if (!$payment_method->isReusable() || $payment_method->getOwner()->isAnonymous()) { + $payment_method->delete(); + } $this->redirectToPreviousStep(); } catch (PaymentGatewayException $e) {