- Issue created by @wengerk
- 🇨🇭Switzerland wengerk Lausanne
+++ b/src/Plugin/Commerce/PaymentGateway/PostFinanceCheckout.php @@ -822,6 +825,14 @@ class PostFinanceCheckout extends OffsitePaymentGatewayBase implements PostFinan + // Prevents \Drupal\commerce_payment\EventSubscriber\OrderPaidSubscriber::onPaid + // to place the order on cancellation. + if ($state === 'canceled') { + $order = $order_storage->loadForUpdate($payment->getOrderId()); + $order->state->value = 'canceled'; + $order->save(); + } +
I was unable to find a more effective way to prevent the order from being placed automatically.
In our project, when the payment status was changed to 'canceled,' the system placed the order instead of canceling it, triggering the 'placed' transition and sending out order-related emails and other actions.
Is there a better approach to handle order cancellations without triggering the order placement process?
- Status changed to Needs review
2 months ago 3:06pm 30 November 2024 - 🇨🇭Switzerland znerol
Hi! Thanks for the report and the patch, and sorry it took that long before somebody took a look.
I guess the
$payment->completed
field shouldn't be updated for failed payments. It looks like this is subsequently checked by the PaymentOrderUpdater::updateOrder() to decide whether to update thetotal_paid
field whenever the order is saved. Based on that, OrderStorage::doOrderPreSave() will dispatch theORDER_PAID
event. - 🇨🇭Switzerland znerol
+++ b/src/Plugin/Commerce/PaymentGateway/PostFinanceCheckout.php @@ -567,6 +567,9 @@ class PostFinanceCheckout extends OffsitePaymentGatewayBase implements PostFinan + if ($postfinance_state == 'FAILED') { + $commerce_sate = 'canceled'; + }
I was attempting to add tests for this change. However, it turns out that
canceled
is not valid as a payment state. See the commerce docs for a list of valid states, as well as the commerce_payment.workflows.yml for possible transitions.