Example #1
0
        public override string RedirectForPayment(OrderData orderData)
        {
            orderData.OrderStatus = "020";
            orderData.PurchaseInfo.SetXmlProperty("genxml/paymenterror", "");
            orderData.PurchaseInfo.Lang = Utils.GetCurrentCulture();
            orderData.SavePurchaseData();
            try
            {
                var nonce = HttpContext.Current.Request.Cookies.Get("nonce") != null?HttpContext.Current.Request.Cookies.Get("nonce").Value : "";

                if (string.IsNullOrWhiteSpace(nonce))
                {
                    HttpContext.Current.Request.Cookies.Get("nonce").Expires = DateTime.Now.AddDays(-1d);

                    //No Nonce Return to Payment Tab with Failure message;
                    var param = new string[2];
                    param[0] = "orderid=" + orderData.PurchaseInfo.ItemID.ToString("");
                    param[1] = "status=0";
                    return(Globals.NavigateURL(StoreSettings.Current.PaymentTabId, "", param));
                }
                else
                {
                    // 010 = Incomplete, 020 = Waiting for Bank,030 = Cancelled,040 = Payment OK,050 = Payment Not Verified,060 = Waiting for Payment,070 = Waiting for Stock,080 = Waiting,090 = Shipped,010 = Closed,011 = Archived

                    HttpContext.Current.Response.Clear();

                    var response = ProviderUtils.GetChargeResponse(orderData, nonce);

                    var param = new string[2];
                    param[0] = "orderid=" + orderData.PurchaseInfo.ItemID.ToString("");

                    if (response.Errors == null || response.Errors.Count == 0)
                    {
                        //add external order id, payment id & status to PurchaseInfo for dev reference
                        orderData.PurchaseInfo.SetXmlProperty("genxml/externalorderid", response.Payment.OrderId);
                        orderData.PurchaseInfo.SetXmlProperty("genxml/externalpaymentid", response.Payment.Id);
                        orderData.PurchaseInfo.SetXmlProperty("genxml/externalstatus", response.Payment.Status);

                        //also add the Square payment id to the audit log for admins/managers to reference
                        orderData.AddAuditMessage("Square Payment ID " + response.Payment.Id, "notes", UserController.Instance.GetCurrentUserInfo().Username, "False");

                        // successful transaction
                        if (response.Payment.SourceType == "BANK_ACCOUNT")
                        {
                            if (response.Payment.Status == "PENDING")
                            {
                                //ACH payments can take 3-5 days to clear
                                //so set the status to Payment Not Verified 050
                                //and add an audit log entry for the Pending ACH Transfer

                                orderData.AddAuditMessage("Pending ACH Transfer", "notes", UserController.Instance.GetCurrentUserInfo().Username, "False");
                                orderData.PaymentOk("050");
                                param[1] = "status=1";
                            }
                            else
                            {
                                //ACH payments should not end up here
                                //since all payments will intially
                                //return a PENDING status
                                orderData.OrderStatus = "030";
                                param[1] = "status=0";
                                orderData.AddAuditMessage("Unhandled payment status", "notes", UserController.Instance.GetCurrentUserInfo().Username, "False");

                                throw new Exception("Unhandled payment status");
                            }
                        }
                        else
                        {
                            // cc payments
                            orderData.PaymentOk("040");
                            param[1] = "status=1";
                        }

                        NBrightBuyUtils.SendOrderEmail("OrderCreatedClient", orderData.PurchaseInfo.ItemID, "ordercreatedemailsubject");
                    }
                    else
                    {
                        // failed transaction
                        orderData.OrderStatus = "030";
                        param[1] = "status=0";

                        // create error string for output to the order audit log
                        var errorString = "";
                        if (response.Errors.Count > 0)
                        {
                            foreach (var e in response.Errors)
                            {
                                errorString += e.Detail;
                                errorString += " ";
                            }
                            ;
                        }

                        //add message for admins to view in the order audit log
                        orderData.AddAuditMessage(errorString, "notes", UserController.Instance.GetCurrentUserInfo().Username, "False");
                    }

                    orderData.SavePurchaseData();
                    HttpContext.Current.Response.Redirect(Globals.NavigateURL(StoreSettings.Current.PaymentTabId, "", param), false);
                }
            }
            catch (Exception ex)
            {
                // rollback transaction
                // NOTE: The errors returned by the gateway are not shown to the user
                //      DNN admin must be able to review the cart data for a user.
                orderData.PurchaseInfo.SetXmlProperty("genxml/paymenterror", "<div>ERROR: Invalid payment data </div><div>" + ex + "</div>");
                orderData.PaymentFail();
                var param = new string[2];
                param[0] = "orderid=" + orderData.PurchaseInfo.ItemID.ToString("");
                param[1] = "status=0";
                HttpContext.Current.Response.Redirect(Globals.NavigateURL(StoreSettings.Current.PaymentTabId, "", param));
            }

            try
            {
                HttpContext.Current.Response.End();
            }
            catch (Exception)
            {
                // this try/catch to avoid sending error 'ThreadAbortException'
            }

            return("");
        }