Developer forum

Forum » Ecommerce - Standard features » Context.RemoveCart what should be the given parameter?

Context.RemoveCart what should be the given parameter?

Hans Kloppenborg
Reply

Hello,

We are trying to manually remove the cart from the ordercontext that is there even after we complete the order, because if we don't the completed order gets overwritten/downgraded to a cart again. This issue occurs when from the payment provider the client closes the browser after completed payment and only the server push response gets handled. So long as you do not visit the site again all is well, but if you open it (even in a complete different browser) the completed order gets loaded as the current cart.

To fix it we try to remove the cart with the api call Context.RemoveCart(string key), but whatever we fill in as key value, nothing seems to change (at least not in the [EcomOrderContextAccessUserRelation] table in the database.

We have tried the following values:

  • cart.Id
  • cart.FixedId
  • cart.AutoId
  • OrderContextId

If anyone knows what this parameter must be, and if there is some save action that has to occur we would be helped.

The documentation of the call can be found on https://doc.dynamicweb.com/api/html/08724ff1-5ff7-a8a3-8b82-e829206316a0.htm 

Greets Hans


Replies

 
Morten Snedker Dynamicweb Employee
Morten Snedker
Reply

Hi Hans,

Which payment provider are you using, and what is the version of Dynamicweb?

Best regards
Morten Snedker

 
Hans Kloppenborg
Reply

Hi Morten,

We are using the Ogone payment provider, and are on DW 9.6.3

Greets Hans

 
Peter Leleulya
Reply

The question is: What can we do to remove an 'ordercontext - user' combination from table [EcomOrderContextAccessUserRelation] and memory.

This happens automatically when the payment provider has its client return to the receipt page.
This does NOT happen when the payment provider only has the server response reaching the website .... (impatient user clicks browser away after payment)

I think this is happening to all our Ogone implementations, because several clients have been complaining about paid orders missing from the CMS.
I've never been able to reproduce, until now ...

 
Vladimir Shushunov Dynamicweb Employee
Vladimir Shushunov
Reply

Hi Hans and Peter,

To remove cart from memory/DB I suggest to use:   CartCatch.ClearCart() 

It performs:

Context.RemoveCart("EcomCart") 
UPDATE AccessUser SET AccessUserCartID = NULL WHERE AccessUserID = {currentUserId}

And If there is an order context:

Context.RemoveCart("EcomCart.'{OrderContextID}'") 
DELETE FROM EcomOrderContextAccessUserRelation WHERE OrderContextAccessUserAccessUserID = {currentUserId} AND OrderContextAccessUserOrderContextID = {OrderContextID}

Best regards,
Vladimir

 
Hans Kloppenborg
Reply

Hi Vladimir,

This did not work. Since the push request from the payment provider is stateless, and the CartCatch.ClearCart() has no parameters, I have tried to fix this by setting the current user to the user in the order like this:

Dynamicweb.Security.UserManagement.User.SetCurrentUser(Dynamicweb.Security.UserManagement.PagePermissionLevels.Frontend, Dynamicweb.Security.UserManagement.User.GetUserByID(item.Order.CustomerAccessUserId));
//todo: fix obsolete when replacement function exists
Dynamicweb.Ecommerce.Frontend.Cart.CartCatch.ClearCart();

Sadly reopening the site again shows the cart still exists, and in the database the EcomOrderContextAccessUserRelation is still there also.

Let us know if we can do more to get this working for push requests of a payment provider.

Greets Hans.

 
Vladimir Shushunov Dynamicweb Employee
Vladimir Shushunov
Reply

hmm yes, you right - push request from the payment provider is stateless.

Probably it is better to fix the problem in EcomCartLoaded NotificationSubscriber:

    [Dynamicweb.Extensibility.Notifications.Subscribe(Dynamicweb.Ecommerce.Notifications.Ecommerce.Cart.Loaded)]
    public class EcomCartLoadedObserver : Dynamicweb.Extensibility.Notifications.NotificationSubscriber
    {
        public override void OnNotify(string notification, NotificationArgs args)
        {
            if (args == null || !(args is Dynamicweb.Ecommerce.Notifications.Ecommerce.Cart.LoadedArgs))
                return;

            var myArgs = (Dynamicweb.Ecommerce.Notifications.Ecommerce.Cart.LoadedArgs)args;

            if (myArgs.Cart != null && !myArgs.Cart.IsCart) 
                Dynamicweb.Ecommerce.Frontend.Cart.CartCatch.ClearCart();
        }
    }
 
Best regards,
Vladimir

 

 

 
Hans Kloppenborg
Reply

Goodmorning Vladimir,

Somehow strange things are happening in the EcomCartLoadedObserver. Most of the time the Cart argument is null, while debugging it seemed only to get filled at the moment I added something to the cart.

Since the cart is null, the code does not get executed. I even tried to skip the checks using a breakpoint and just calling Dynamicweb.Ecommerce.Frontend.Cart.CartCatch.ClearCart() anyways, and it seems that does nothing if the cart is null in the argument. If I force the ClearCart at the moment the Cart argument is filled, it seems to work, but that does not help me with our problem.

How can the CartLoaded observer go off and then have no Cart? Somehow this seems illogical.

Concluding, the given solution sounds like it should work, but sadly it does not.

Greets Hans

 
Vladimir Shushunov Dynamicweb Employee
Vladimir Shushunov
Reply

Goodmorning Hans:)

Cart loaded notification fires when a basket is opened(page) or a product is added to a basket - so it is OK that it has null Cart argument.

The idea of my suggestion is to check, that the current order has the 'CART' state (order has 'ORDER' state - when it was passed to a checkouthandler). And if it so - remove such order from the basket to force a new cart initiating.

Best regards,
Vladimir

 
Hans Kloppenborg
Reply

Hi Vladimir,

I understand what you mean, but it just does not work. When I return on the site after closing the browser tab after payment succes from the Ogone paymentprovider, and before the redirect back to the client, so that the payment confirmation only gets handled by the push message of Ogone, there is no cart in the argument when the page gets loaded and the observer gets triggered.I even tried to just get the order myself, and check if its complete, and if so clear the cart, but even that does not work. I suspect the order is already destroyed (set back to incomplete) in memory.

As soon as I visit the cart page the order gets saved again, keeping its orderid but the ordercomplete is false and our orderstate OS2 becomes null.

To be honest it seems like a bug to me that the EcomCartLoadedObserver can go off and not have a cart in its argument (especially when I can get the Context.Cart using my own code), its like as if the checkoutorderdone observer goes off and has no order in its argument, it should not be possible.

Is there any development for an official fix of the Ogone paymentprovider? Since this is an issue with all production sites that use this paymentprovider.

Greets Hans

 
Vladimir Shushunov Dynamicweb Employee
Vladimir Shushunov
Reply
This post has been marked as an answer

Hi Hans,
I made few test orders on your solution to reproduce the bug.
It seems that when order is successfully completed in push request from ogone, and at the same moment it become overwritten/reseted in an observer .- that is why CartLoaded observer didn't help. Lets try to clear cart before order is sent to checkouthandler, using  Ecommerce.Notifications.Ecommerce.Cart.OrderIsPassedToCheckoutHandler :

    [Dynamicweb.Extensibility.Notifications.Subscribe(Dynamicweb.Ecommerce.Notifications.Ecommerce.Cart.OrderIsPassedToCheckoutHandler)]
    public class EcomCartOrderIsPassedToCheckoutHandlerObserver : Dynamicweb.Extensibility.Notifications.NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.Notifications.NotificationArgs args)
        {
            Dynamicweb.Ecommerce.Frontend.Cart.CartCatch.ClearCart();
        }
    }

Pleease Look at attached picture: Could you say me what is going on when "push order to the AX communication database for order id VI-3038 succeeded"
or better show me a code?

Best regards,
Vladimir

screen4.png
Votes for this answer: 1
 
Hans Kloppenborg
Reply

Goodmorning Vladimir,

This last one seems to have done the trick! 

Only downside is that when you abort the payment or it fails and you go back the cart is gone too, that is much less important though as the destruction of completed orders.

Thanks for your efforts.

Concerning the picture and the somewhat confusing logging about completed - not completed orders, that is the beforeOrderSaved observer where we check completed orders on the existence of a value, and then if the orderState is not complete (i.e. OS1) we set it to complete (OS2). We do not do a save action ourselves there, just set the orderstate value, since we are in the beforesave observer the order is already being saved.

Might be that somewhere the cart is being requested using Context.Cart, since we found out in another project that this not just gets the cart, but also saves the order eacht time you request it. (An unexpected result from a "Get" action to be honest)

Greets Hans

 

You must be logged in to post in the forum