0

I have an Ecommerce website build with UCommerce. During the checkout process the user will be redirected to the payment portal for the payment.

I want to prevent users from adding new items in the basket while the user is in the payment portal. My current solution is to save the basket to a Session before redirecting the user to the payment portal.

Session["checkoutOrder"] = TransactionLibrary.GetBasket(!TransactionLibrary.HasBasket()).PurchaseOrder;

How can I overwrite the current basket with the one in the Session After the payment? This is to revert the basket to its original state before the payment.

I tried this:

[HttpPost]
public ActionResult ExecutePayment()
{
var order = Session["checkoutOrder"] as PurchaseOrder;
order.Save();
...
}

But I'm getting an error on order.Save():

Batch update returned unexpected row count from update; actual row count: 0; expected: 1
Romeo
  • 1,791
  • 8
  • 25
  • 41
  • The payment total is validated when the user returns from the payment portal, so you get an error if the basket total is greater than the amount paid, and the basket is then not converted to an order. – lasseeskildsen Aug 29 '16 at 04:22
  • Hi @lasseeskildsen, I'm aware of the ValidatePaymentsMadeAgainstOrderTotal in the checkout pipeline.But I don't want the user to get an error after paying in the payment portal. This will complicate the process since the user's card is already debited. I prefer reverting back the purchase order to its original state so only the paid items will be checkedout. – Romeo Aug 29 '16 at 04:49
  • That makes perfect sense. Maybe you can add a new order status ("Waiting for payment") and change the status to that when the user is redirected to the payment portal? If a user then adds for stuff to the basket while at the payment portal, a new basket is then created? Only thing is if there's anyhing in any of the checkout pipelines that requires the status to be "Basket" – lasseeskildsen Aug 29 '16 at 07:57

2 Answers2

2

I'd just add to this as well that your Session["orderInProcess"] is an anti pattern in uCommerce. You may run into nasty exceptions as you're persisting NHibernate entities through requests which can/will lead to Session disposed exceptions. It may also lead to the initial exception that you're experiencing as you're actually by-passing the sesssion state of NHibernate.

Just use TransactionLibrary.GetBasket(!TransactionLibrary.HasBasket()).PurchaseOrder; every time you're retrieving your basket. NHibernate will take care of caching the order for you.

Then you can use order properties to save the state you're in.

var basket = TransactionLibrary.GetBasket(!TransactionLibrary.HasBasket()).PurchaseOrder;
basket["CheckoutInProcess"] = "True";

Best regards Morten

0

I handled this differently since I have no way of reverting back the basket to its original state.

I decided to block the user from adding items in the basket when the payment is in process.

I created a session Session["orderInProcess"]=true before I redirect the user to the payment gateway.

Now every time the user tries to add a new item in the basket, I will check first if his current order is in process. like so:

[HttpPost]
public ActionResult AddToBasket(string sku, string quantity, string variant = null)
{

     if (Session["orderInProcess"] != null)
     {
         if (bool.Parse(Session["orderInProcess"].ToString()))
         {
             return Json(new
             {
                  Success = false,
                  ErrorMessage = "Order is currently in process."
             });
          }
      }
   .....
}

I hope this helps.

Romeo
  • 1,791
  • 8
  • 25
  • 41