Redirect Payment Plugin By Process Payment Method.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
3 года назад
For the redirects payment method, the developer must need to write related code at PostProcessPayment but the problem is if the customer goes to the payment form and close it or the payment gets failed then the cart becomes empty and the order has been created. But for the direct payment order generated after successful payment. Does anybody try to handle the redirect payment method by ProcessPayment? Actually I need to keep the cart as it is if the payment gets failed or customers simply close the third part payment window. I am assuming to show the payment form at the iframe before confirming the order.
3 года назад
Firstly you would need to generate a temporary order number or something to be able to associate the payment with the real order later
Then also the scenario you would also need to check for is the redirection happens and the payment is made and then the call back does not work - Then you have a payment with no order

In the case where the payment fails you can recreate the cart from the order
and then delete the order
public virtual List<string> AddOrderToCart(Order order, out int count)
{
    var addToCartWarnings = new List<string>();

    var cart = _shoppingCartService.GetShoppingCart(_workContext.CurrentCustomer, ShoppingCartType.ShoppingCart, _storeContext.CurrentStore.Id);

    foreach (var item in order.OrderItems)
    {
        var product = item.Product;

        //quantity
        var quantity = item.Quantity;

        //product attributes
        var attributes = item.AttributesXml;

        //rental attributes
        DateTime? rentalStartDate = null;
        DateTime? rentalEndDate = null;
        if (item.Product.IsRental)
        {
            rentalStartDate = item.RentalStartDateUtc;
            rentalEndDate = item.RentalEndDateUtc;
        }
        addToCartWarnings.AddRange(_shoppingCartService.AddToCart(_workContext.CurrentCustomer,
            product, ShoppingCartType.ShoppingCart, _storeContext.CurrentStore.Id,
            attributes, decimal.Zero,
            rentalStartDate, rentalEndDate, quantity, true));
    }

    cart = _shoppingCartService.GetShoppingCart(_workContext.CurrentCustomer, ShoppingCartType.ShoppingCart, _storeContext.CurrentStore.Id);
    count = cart.Count();

    return addToCartWarnings;
}

Then in your payment error routine add
int count;
var order = _orderService.GetOrderById(orderId);
if (order != null)
{
    var warnings = AddOrderToCart(order, out count);
    if (order.PaymentStatus == PaymentStatus.Pending)
    {
        if (_orderProcessingService.CanCancelOrder(order))
            _orderProcessingService.CancelOrder(order, false);

        order = _orderService.GetOrderById(orderId);
        if (order != null)
        {
            if (order.OrderStatus == OrderStatus.Cancelled)
                _orderService.DeleteOrder(order);
        }
    }

    redirectUrl = _webHelper.GetStoreLocation() + "cart";
    return Redirect(redirectUrl);
3 года назад
Yidna wrote:
Firstly you would need to generate a temporary order number or something to be able to associate the payment with the real order later
Then also the scenario you would also need to check for is the redirection happens and the payment is made and then the call back does not work - Then you have a payment with no order

In the case where the payment fails you can recreate the cart from the order
and then delete the order
public virtual List<string> AddOrderToCart(Order order, out int count)
{
    var addToCartWarnings = new List<string>();

    var cart = _shoppingCartService.GetShoppingCart(_workContext.CurrentCustomer, ShoppingCartType.ShoppingCart, _storeContext.CurrentStore.Id);

    foreach (var item in order.OrderItems)
    {
        var product = item.Product;

        //quantity
        var quantity = item.Quantity;

        //product attributes
        var attributes = item.AttributesXml;

        //rental attributes
        DateTime? rentalStartDate = null;
        DateTime? rentalEndDate = null;
        if (item.Product.IsRental)
        {
            rentalStartDate = item.RentalStartDateUtc;
            rentalEndDate = item.RentalEndDateUtc;
        }
        addToCartWarnings.AddRange(_shoppingCartService.AddToCart(_workContext.CurrentCustomer,
            product, ShoppingCartType.ShoppingCart, _storeContext.CurrentStore.Id,
            attributes, decimal.Zero,
            rentalStartDate, rentalEndDate, quantity, true));
    }

    cart = _shoppingCartService.GetShoppingCart(_workContext.CurrentCustomer, ShoppingCartType.ShoppingCart, _storeContext.CurrentStore.Id);
    count = cart.Count();

    return addToCartWarnings;
}

Then in your payment error routine add
int count;
var order = _orderService.GetOrderById(orderId);
if (order != null)
{
    var warnings = AddOrderToCart(order, out count);
    if (order.PaymentStatus == PaymentStatus.Pending)
    {
        if (_orderProcessingService.CanCancelOrder(order))
            _orderProcessingService.CancelOrder(order, false);

        order = _orderService.GetOrderById(orderId);
        if (order != null)
        {
            if (order.OrderStatus == OrderStatus.Cancelled)
                _orderService.DeleteOrder(order);
        }
    }

    redirectUrl = _webHelper.GetStoreLocation() + "cart";
    return Redirect(redirectUrl);

Can you please clear a little more about "Then also the scenario you would also need to check for is the redirection happens".

If customer go to the payment form and close it then there is no call back function fired. But if the customer comes back to the site then I can do some extra work depending on the payment status(given by the bank). Like if it is paid then the whole life cycle of the order is reached to the happy path. If there is an error I can reorder it, so ordered items will stay at the cart. And before that, I will cancel the order which will take care of the stock reverse calculation.
But I do not get any way to catch any event when customers close the browser of the third party payment method.

And yes there are lots of discussions on this at the forum and also to the GitHub(https://github.com/nopSolutions/nopCommerce/issues/223).
3 года назад
What I was referring to is when your website posts and redirects to the payment website and the credit card is entered and the payment made and you provide a URL for the return. After the payment is made the payment website should post back to your website with a status Paid or Card Invalid, etc . In most cases this will work.
Maybe in one case it does not post correctly back to your website. Then in this case there is a payment but no order has been created.
Maybe you do solve this using the iFrame which means you don’t leave your website.

nopStation wrote:
But I do not get any way to catch any event when customers close the browser of the third party payment method

Yes, but unless it is an accident this probably means they have changed their mind and don't want to buy, or their credit card is over limit or not working.

In my payment method I allow retry payment for all methods and also send an email to the customer the next day saying “you have an unpaid order – click here to make payment”. Maybe the customer will change their mind or now have the funds and they do want to purchase – you cannot do that if you have no order stored with payment pending.

Not sure there is a perfect solution for redirection payment. As is evident on all the posts many have tried to propose a problem and come up with a solution. In the end nopCommerce has resolve it the best way it can. Then allowing each to do it in their own way in their own payment method.

So maybe you can give it a try, see if you can come up with a better solution using iFrame 😊
3 года назад
RE:  "...in one case it does not post correctly back to your website. Then in this case there is a payment but no order has been created."
If there is a payment, even if it did not get back to the site, then the order has already been created.  I'm not sure that there is anything to solve in this case.  The store owner just needs to click the "Mark as paid" button.  (and can manually copy/paste transaction info from the payment site's into an order note)

RE: "...changed their mind and don't want to buy, or their credit card is over limit or not working."
Then it would seem that your "recreate the cart from the order and then delete the order" applies.
3 года назад
RE: "...then delete the order"
Actually it would be better to Cancel the order.  That would properly restore the inventory (without you having to do it yourself).   And for those using custom order numbers, then also deleting may not be a good idea, or some additional work needs to be done.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.