Reg: Issue in Checkout Complete step in Custom Payment plugin

5 years ago
hi

I am trying to write payment gateway plugin for HDFC bank for Nopcommerce3.3 .

Till now I'm able to redirect on HDFC's Payment page with all required Order detail but after making payment successfully ,User gets redirected to Home page instead of Checkout Completed page.

After redirect from Payment Gateway, the Order get marked as paid ; after debugging for several hours I discovered that the following code of Checkout Controller's  Completed action, is the cause :


  if (order == null || order.Deleted || _workContext.CurrentCustomer.Id != order.CustomerId)
            {
                _logger.Error("RedirectToRoute:home" );
                return RedirectToRoute("HomePage");
            }



_workContext.CurrentCustomer.Id shows as 2 but the actual order.CustomerId is 1.

Any guidance in this regard will be helpful.

thanks in advance! :)
5 years ago
abhigg wrote:
hi

I am trying to write payment gateway plugin for HDFC bank for Nopcommerce3.3 .

Till now I'm able to redirect on HDFC's Payment page with all required Order detail but after making payment successfully ,User gets redirected to Home page instead of Checkout Completed page.

After redirect from Payment Gateway, the Order get marked as paid ; after debugging for several hours I discovered that the following code of Checkout Controller's  Completed action, is the cause :


  if (order == null || order.Deleted || _workContext.CurrentCustomer.Id != order.CustomerId)
            {
                _logger.Error("RedirectToRoute:home" );
                return RedirectToRoute("HomePage");
            }



_workContext.CurrentCustomer.Id shows as 2 but the actual order.CustomerId is 1.

Any guidance in this regard will be helpful.

thanks in advance! :)


It all depends on how your "return url" Action is written. Without seeing your code it's hard to comment. :)
5 years ago
Hi Lam

Following is code snippet which handles the Payment status and Payment Gateway's response and redirect to Completed Action.




[ValidateInput(false)]
        public ActionResult PDTHandler(FormCollection form)
        {


            _logger.Error("1");


            string tx = _webHelper.QueryString<string>("tx");

            _logger.Error("2");
            Dictionary<string, string> values;

            string TranInqResponse, ResPaymentId, ResResult, ResErrorText, ResPosdate, ResTranId, ResAuth, ResAVR, ResAmount, ResErrorNo, ResTrackID, ResRef, okk, Resudf1, Resudf2, Resudf3, Resudf4, Resudf5;

            var processor = _paymentService.LoadPaymentMethodBySystemName("Payments.HDFC") as HDFCPaymentProcessor;
            if (processor == null ||
                !processor.IsPaymentMethodActive(_paymentSettings) || !processor.PluginDescriptor.Installed)
                throw new NopException("HDFC module cannot be loaded");
            string lg = "LG    " + Request["ErrorText"] + "   " + Request["paymentid"];
            _logger.Error(lg);


            ResErrorText = Request["ErrorText"];    //Error Text/message

            _logger.Error("ErrorText" + Request["ErrorText"]);
            ResPaymentId = Request["paymentid"];
            _logger.Error("paymentid" + Request["paymentid"]);
            //Payment Id
            ResTrackID = Request["trackid"];    //Merchant Track ID
            _logger.Error("trackid" + Request["trackid"]);


            ResErrorNo = Request["Error"];      //Error Number
            _logger.Error("Error" + Request["Error"]);

            //To collect transaction result
            ResResult = Request["result"];      //Transaction Result
            _logger.Error("result" + Request["result"]);

            ResPosdate = Request["postdate"];    //Postdate
            _logger.Error("postdate" + Request["postdate"]);

            //To collect Payment Gateway Transaction ID, this value will be used in dual verification request
            ResTranId = Request["tranid"];      //Transaction ID
            _logger.Error("tranid" + Request["tranid"]);
            ResAuth = Request["auth"];        //Auth Code    
            _logger.Error("auth" + Request["auth"]);
            ResAVR = Request["avr"];        //TRANSACTION avr          
            _logger.Error("avr" + Request["avr"]);
            ResRef = Request["ref"];        //Reference Number also called Seq Number
            _logger.Error("ref" + Request["ref"]);

            //To collect amount from response
            ResAmount = Request["amt"];        //Transaction Amount
            _logger.Error("amt" + Request["amt"]);
            Resudf1 = Request["udf1"]; //order guid        //UDF1
            _logger.Error("udf1" + Request["udf1"]);
            Resudf2 = Request["udf2"];        //UDF2
            _logger.Error("udf2" + Request["udf2"]);
            Resudf3 = Request["udf3"];        //UDF3
            Resudf4 = Request["udf4"];        //UDF4
            Resudf5 = Request["udf5"];        //UDF5


            //if (processor.GetPDTDetails(tx, out values, out response))
            _logger.Error("Before ResErrorNo" + ResErrorNo);
            if (ResErrorNo == null)
            {
                string strhashstring = "";//Declaration of Hashing String

                string strHashTraportalID = "9001081";
                _logger.Error("into ResErrorNo");

                strhashstring = strHashTraportalID.Trim();//Padding Tranportal ID Value
                /*Below code creates the Hashing String also it will check NULL and Blank parmeters and exclude from the hashing string */
                if (ResTrackID != null && ResTrackID.Trim() != "")
                    strhashstring = strhashstring + ResTrackID.Trim();//Padding TrackID Value
                if (ResAmount != null && ResAmount.Trim() != "")
                    strhashstring = strhashstring + ResAmount.Trim();//Padding Amount Value
                if (ResResult != null && ResResult.Trim() != "")
                    strhashstring = strhashstring + ResResult.Trim();//Padding Result Value
                if (ResPaymentId != null && ResPaymentId.Trim() != "")
                    strhashstring = strhashstring + ResPaymentId.Trim();//Padding PaymentId Value
                if (ResRef != null && ResRef.Trim() != "")
                    strhashstring = strhashstring + ResRef.Trim();//Padding Ref Value
                if (ResAuth != null && ResAuth.Trim() != "")
                    strhashstring = strhashstring + ResAuth.Trim();//Padding Auth Value
                if (ResTranId != null && ResTranId.Trim() != "")
                    strhashstring = strhashstring + ResTranId.Trim();//Padding TranId Value


                _logger.Error("into strhashstring" + strhashstring);

                string orderNumber = string.Empty;
                if (Resudf1 != null && Resudf1.Trim() != "")
                    orderNumber = Resudf1.Trim();//Padding TranId Value
                _logger.Error("into GetSHA256" + strhashstring);
                string hashvalue = GetSHA256(strhashstring.Trim());

                _logger.Error("orderNumber" + orderNumber);

                _logger.Error("into hashvalue1" + hashvalue);
                Guid orderNumberGuid = Guid.Empty;
                try
                {

                    orderNumberGuid = new Guid(orderNumber);
                }
                catch (Exception epp)
                {

                    _logger.Error("into hashvalue" + epp.Message + "\n\r" + orderNumber);
                }
                if (hashvalue == Resudf5)
                {


                    _logger.Error("into hashvalue == Resudf5" + hashvalue);
                    Order order = _orderService.GetOrderByGuid(orderNumberGuid);
                    if (order != null)
                    {

                        var sb = new StringBuilder();
                        sb.AppendLine("HDFC PDT:");
                        sb.AppendLine("total: " + ResAmount);
                        sb.AppendLine("Transaction status: " + ResResult);
                        sb.AppendLine("Pending reason: " + ResErrorText);
                        sb.AppendLine("txn_id: " + ResTrackID); //order id
                        sb.AppendLine("payment_type: " + "Hdfc ");
                        sb.AppendLine("PaymentID: " + ResPaymentId);
                        sb.AppendLine("Tran Reference No: " + ResRef);
                        sb.AppendLine("invoice: " + ResTrackID);
                        sb.AppendLine("avr: " + ResAVR);
                        sb.AppendLine("Auth Code: " + ResAuth);
                        sb.AppendLine("Transaction ID: " + ResTranId);
                        _logger.Error(sb.ToString());
                        //order note
                        order.OrderNotes.Add(new OrderNote()
                        {
                            Note = sb.ToString(),
                            DisplayToCustomer = false,
                            CreatedOnUtc = DateTime.UtcNow
                        });
                        _orderService.UpdateOrder(order);

                        //load settings for a chosen store scope
                        var storeScope = this.GetActiveStoreScopeConfiguration(_storeService, _workContext);
                        var hdfcPaymentSettings = _settingService.LoadSetting<HdfcPaymentSettings>(storeScope);

                        //validate order total
                        if (!Math.Round(decimal.Parse(ResAmount), 2).Equals(Math.Round(order.OrderTotal, 2)))
                        {
                            string errorStr = string.Format("HDFC CC. Returned order total {0} doesn't equal order total {1}", ResAmount, order.OrderTotal);
                            _logger.Error(errorStr);

                            return RedirectToAction("Index", "Home", new { area = "" });
                        }

                        //mark order as paid
                        if (_orderProcessingService.CanMarkOrderAsPaid(order))
                        {
                            order.AuthorizationTransactionId = ResTrackID;
                            _orderService.UpdateOrder(order);

                            _orderProcessingService.MarkOrderAsPaid(order);
                        }
                    }
                    _logger.Error("into Checkout");
                    return RedirectToAction("Completed", "Checkout", new { orderId = order.Id});
                 // return RedirectToRoute("Orderdetails", new { orderId = order.Id });
                }
                else
                {
                    string errorStr = string.Format("HDFC CC. Hashing Response Missmatch");
                    _logger.Error(errorStr);

                    return RedirectToAction("Index", "Home", new { area = "" });
                }

            }
            else
            {
                _logger.Error("elseee");
                Guid orderNumberGuid = Guid.Empty;
                try
                {
                    orderNumberGuid = new Guid(ResTrackID);
                }
                catch { }

                Order order = _orderService.GetOrderByGuid(orderNumberGuid);

                if (order != null)
                {
                    var sb = new StringBuilder();
                    sb.AppendLine("HDFC PDT:");
                    sb.AppendLine("Transaction status: " + ResResult);
                    sb.AppendLine("HDFC Payment Error: " + ResErrorNo);
                    sb.AppendLine("Pending reason: " + ResErrorText);
                    sb.AppendLine("txn_id: " + ResTrackID); //order id
                    sb.AppendLine("payment_type: " + "Hdfc ");
                    sb.AppendLine("PaymentID: " + ResPaymentId);
                    sb.AppendLine("Tran Reference No: " + ResRef);
                    sb.AppendLine("invoice: " + ResTrackID);
                    sb.AppendLine("payment_fee: " + ResAmount);
                    sb.AppendLine("Transaction ID: " + ResTranId);

                    //order note
                    order.OrderNotes.Add(new OrderNote()
                    {
                        Note = sb.ToString(),
                        DisplayToCustomer = false,
                        CreatedOnUtc = DateTime.UtcNow
                    });
                    _orderService.UpdateOrder(order);
                    return RedirectToRoute("OrderDetails", new { orderId = order.Id });
                }

                // return RedirectToAction("Index", "Home", new { area = "" });
                return RedirectToRoute("OrderDetails", new { orderId = order.Id });
            }
        }