@@ -56,7 +56,7 @@ class PaymentService extends AbstractPaymentHandler
5656 public const ALTAPAY_TRANSACTION_ID_CUSTOM_FIELD = "wexoAltaPayTransactionId " ;
5757 public const ALTAPAY_TRANSACTION_PAYMENT_SCHEME_NAME_CUSTOM_FIELD = "wexoAltapayTransactionPaymentSchemeName " ;
5858 public const ALTAPAY_TRANSACTION_PAYMENT_NATURE_CUSTOM_FIELD = "wexoAltapayTransactionPaymentNature " ;
59- public const ALTAPAY_IP_ADDRESS_SET = [ " 185.206.120.0/24 " , " 2a10:a200::/29 " ] ;
59+ public const ALTAPAY_ORDER_STATUS = " altaPayOrderStatus " ;
6060
6161 public function __construct (
6262 protected readonly SystemConfigService $ systemConfigService ,
@@ -268,6 +268,12 @@ public function transactionCallback(
268268 case "Open " :
269269 break ;
270270 case "Success " :
271+ $ customFields = $ order ->getCustomFields () ?? [];
272+ $ orderStatus = $ customFields [self ::ALTAPAY_ORDER_STATUS ] ?? null ;
273+
274+ if ($ orderStatus === 'processed ' ) {
275+ break ;
276+ }
271277 // Delete cart when either customer or AltaPay reaches this page.
272278 $ cartToken = $ order ->getCustomFieldsValue (field: WexoAltaPay::ALTAPAY_CART_TOKEN );
273279 if (!empty ($ cartToken )) {
@@ -280,41 +286,47 @@ public function transactionCallback(
280286 $ stateMachineState = $ transaction ->getStateMachineState ();
281287 // Handle case when state machine state is null - force status update
282288 if (!$ stateMachineState ) {
283- // Force the transaction to open state first, then process
284- $ this ->orderTransactionStateHandler ->reopen (
285- $ transaction ->getId (),
286- $ salesChannelContext ->getContext ()
287- );
288-
289- // Now process to in_progress
290- $ this ->orderTransactionStateHandler ->process (
291- $ transaction ->getId (),
292- $ salesChannelContext ->getContext ()
293- );
294-
295- // Handle payment status
296- if ($ result ->Body ->Transactions ->Transaction ->CapturedAmount > 0 ) {
297- $ this ->orderTransactionStateHandler ->paid (
289+ try {
290+ // Force the transaction to open state first, then process
291+ $ this ->orderTransactionStateHandler ->reopen (
298292 $ transaction ->getId (),
299293 $ salesChannelContext ->getContext ()
300294 );
301295
302- if ($ updateOrderStateAfterPayment ) {
303- // Update order state to "in progress"
304- $ this ->updateOrderStateToInProgress ($ order , $ salesChannelContext ->getContext ());
305- }
306- } elseif ($ result ->Body ->Transactions ->Transaction ->ReservedAmount > 0 ) {
307- $ this ->orderTransactionStateHandler ->authorize (
296+ // Now process to in_progress
297+ $ this ->orderTransactionStateHandler ->process (
308298 $ transaction ->getId (),
309299 $ salesChannelContext ->getContext ()
310300 );
311301
312- if ($ updateOrderStateAfterPayment ) {
313- // Update order state to "in progress"
314- $ this ->updateOrderStateToInProgress ($ order , $ salesChannelContext ->getContext ());
302+ // Handle payment status
303+ if ($ result ->Body ->Transactions ->Transaction ->CapturedAmount > 0 ) {
304+ $ this ->orderTransactionStateHandler ->paid (
305+ $ transaction ->getId (),
306+ $ salesChannelContext ->getContext ()
307+ );
308+
309+ if ($ updateOrderStateAfterPayment ) {
310+ // Update order state to "in progress"
311+ $ this ->updateOrderStateToInProgress ($ order , $ salesChannelContext ->getContext ());
312+ }
313+ } elseif ($ result ->Body ->Transactions ->Transaction ->ReservedAmount > 0 ) {
314+ $ this ->orderTransactionStateHandler ->authorize (
315+ $ transaction ->getId (),
316+ $ salesChannelContext ->getContext ()
317+ );
318+
319+ if ($ updateOrderStateAfterPayment ) {
320+ // Update order state to "in progress"
321+ $ this ->updateOrderStateToInProgress ($ order , $ salesChannelContext ->getContext ());
322+ }
315323 }
324+ $ order ->changeCustomFields ([
325+ self ::ALTAPAY_ORDER_STATUS => 'processed '
326+ ]);
327+ } catch (\Exception $ e ) {
328+ $ this ->logger ->error ("order transaction state error: " . $ e ->getMessage ());
316329 }
317-
318330 break ;
319331 }
320332
0 commit comments