Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ryan Helmn 26 posts 188 karma points
    Jun 11, 2021 @ 20:12
    Ryan Helmn
    0

    Deadlock issue when making order updates

    Got a client request to return some third party API data to a customer's order and display it on the payment successful page but I'm running into deadlock issues whilst trying to save said data.

    A breakdown of the setup:

    1. User goes through checkout
    2. User gets sent to the third party payment provider
    3. The payment callback URL is triggered and the order is updated accordingly. However, if the payment is successful the customer is also sent to the payment successful page where the further updates to the order are made.

    Example of the method called during the ProcessCallback method:

            private static CallbackResult ProcessPaymentProviderCallback(OrderReadOnly order)
        {
            var client = new VivaWalletClient();
            var vivaWalletOrder = client.RetrieveOrder(int.Parse(order.Properties["vivaWalletOrderCode"]));
            var paymentState = (VivaWalletState) vivaWalletOrder.StateId;
    
            switch (paymentState)
            {
                case VivaWalletState.Pending:
                case VivaWalletState.Expired:
                case VivaWalletState.Cancelled:
                    return CallbackResult.Ok();
                case VivaWalletState.Paid:
                    return new CallbackResult
                    {
                        TransactionInfo = new TransactionInfo
                        {
                            AmountAuthorized = order.TransactionAmount.Value,
                            TransactionFee = 0,
                            TransactionId = order.Properties["vivaWalletTransactionId"],
                            PaymentStatus = PaymentStatus.Captured
                        }
                    };
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
    

    Example of the method called in the PaymentSuccessfulPageController, this is the one that gets sacrificed to the deadlock gods:

    private void ProcessEcologi(OrderReadOnly order)
        {
            try
            {
                if (order.Properties.ContainsKey(OrderConstants.TreesPlanted) &&
                    !order.Properties[OrderConstants.TreesPlanted].Value.IsNullOrEmpty())
                {
                    return;
                }
    
                var client = new EcologiClient();
                var purchaseTreesResponse =
                    client.PurchaseTrees($"{order.CustomerInfo.FirstName} {order.CustomerInfo.LastName}", 2); // TODO
    
                using (var unitOfWork = _vendrApi.Uow.Create())
                {
                    var writableOrder = order.AsWritable(unitOfWork)
                        .SetProperties(new Dictionary<string, string>
                        {
                            {OrderConstants.TreesPlanted, 2.ToString()}, // TODO
                            {OrderConstants.TreesUrl, purchaseTreesResponse.TreeUrl},
                            {OrderConstants.ContributionUrl, ""} // TODO
                        });
    
                    _vendrApi.SaveOrder(writableOrder);
                    unitOfWork.Complete();
                }
            }
            catch (Exception e)
            {
                _logger.Error<PaymentSuccessfulPageController>(e);
            }
        }
    

    From face value, I'd imagine because of the callback response finalizing the order and doing it's magic that trying to manipulate the order further at this stage isn't the best idea.

    Unfortunately, I can't move the logic from the PaymentSuccessfulPageController to the Callback action as I need the data as soon as the page has loaded. Any ideas?

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jun 11, 2021 @ 20:26
    Matt Brailsford
    0

    Hi Ryan,

    Are you able to identify what statements are deadlocking? And any deadlock debugging information so we can try and work out why it would deadlock?

    There are some good debugging tips if you google for them but any detailed info you can provide the more we have to look into / try and replicate.

Please Sign in or register to post replies

Write your reply to:

Draft