From 27488b97ad4ff4f687b6e808a9ef4f340271e410 Mon Sep 17 00:00:00 2001 From: Remco Tolsma <869674+remcotolsma@users.noreply.github.com> Date: Thu, 19 Oct 2023 20:01:20 +0200 Subject: [PATCH] Also update payment transaction ID for cancelled orders. --- src/Gateway.php | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/src/Gateway.php b/src/Gateway.php index 6a2fd89..c1ee7ab 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -646,28 +646,50 @@ private function handle_merchant_order_status_changed( Notification $notificatio private function update_payment_transaction_id_from_order_result( $payment, $order_result ) { $transaction_id = (string) $payment->get_transaction_id(); - if ( '' !== $transaction_id ) { - return; - } + $transaction_statuses = [ + TransactionStatus::SUCCESS, + TransactionStatus::ACCEPTED, + TransactionStatus::CANCELLED, + TransactionStatus::EXPIRED, + TransactionStatus::FAILURE, + ]; - if ( 'COMPLETED' !== $order_result->get_order_status() ) { - return; + switch ( $order_result->get_order_status() ) { + case OrderStatus::COMPLETED: + $transaction_statuses = [ + TransactionStatus::SUCCESS, + TransactionStatus::ACCEPTED, + ]; + + break; + case OrderStatus::CANCELLED: + $transaction_statuses = [ + TransactionStatus::CANCELLED, + ]; + + break; + case OrderStatus::EXPIRED: + $transaction_statuses = [ + TransactionStatus::EXPIRED, + ]; + + break; } - $successful_transactions = \array_filter( + $transactions = \array_filter( $order_result->get_transactions(), - static function ( $transaction ) { - return 'SUCCESS' === $transaction->get_status(); + function ( $transaction ) use ( $transaction_statuses ) { + return \in_array( $transaction->get_status(), $transaction_statuses, true ); } ); - $successful_transaction = \array_shift( $successful_transactions ); + $transaction = \array_shift( $transactions ); - if ( null === $successful_transaction ) { + if ( null === $transaction ) { return; } - $payment->set_transaction_id( $successful_transaction->get_id() ); + $payment->set_transaction_id( $transaction->get_id() ); } /**