Пример #1
0
    private void MobileSuccess(string sageRedirectPath)
    {
        try
        {
            if (Session["mb_OrderDetail"] != null)
            {
                var    orderInfo       = (OrderInfo)Session["mb_OrderDetail"];
                var    giftCardUsage   = (List <GiftCardUsage>)Session["mb_GiftCardUsage"];
                var    coupons         = (List <CouponSession>)Session["mb_CouponSession"];
                var    billingAddress  = (UserAddressInfo)Session["mb_BillingAddress"];
                var    shippingAddress = (UserAddressInfo)Session["mb_ShippingAddress"];
                var    itemsInfo       = (List <OrderItem>)Session["mb_ItemDetails"];
                int    storeId         = orderInfo.StoreId;
                int    portalId        = orderInfo.PortalId;
                string userName        = orderInfo.AddedBy;
                int    customerId      = orderInfo.CustomerId;
                bool   isTestPaypal    = false;



                _invoice = orderInfo.InvoiceNumber;
                if (!string.IsNullOrEmpty(orderInfo.TransactionId) && orderInfo.TransactionId.Trim() != "0")
                {
                    lblTransaction.Text   = orderInfo.TransactionId;
                    lblInvoice.Text       = orderInfo.InvoiceNumber;
                    lblPaymentMethod.Text = orderInfo.PaymentMethodName;
                    lblDateTime.Text      = orderInfo.AddedOn.ToString("dddd, dd MMMM yyyy ");
                    lblOrderNo.Text       = "#" + orderInfo.OrderId;
                }
                else
                {
                    var pw = new PayPalWCFService();
                    List <PayPalSettingInfo> setting = pw.GetAllPayPalSetting(orderInfo.PaymentGatewayTypeId, storeId,
                                                                              portalId);
                    _authToken   = setting[0].AuthToken;
                    isTestPaypal = bool.Parse(setting[0].IsTestPaypal);

                    if (giftCardUsage != null && giftCardUsage.Count > 0)
                    {
                        AspxGiftCardController.UpdateGiftCardUsage(giftCardUsage, storeId,
                                                                   portalId,
                                                                   orderInfo.OrderId,
                                                                   userName,
                                                                   orderInfo.CultureName);
                    }



                    _txToken = Request.QueryString.Get("tx");
                    _query   = string.Format("cmd=_notify-synch&tx={0}&at={1}", _txToken, _authToken);
                    const string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
                    const string strLive    = "https://www.paypal.com/cgi-bin/webscr";
                    string       test;
                    if (Session["IsTestPayPal"] != null)
                    {
                        test = bool.Parse(Session["IsTestPayPal"].ToString()) ? strSandbox : strLive;
                    }
                    else
                    {
                        test = isTestPaypal ? strSandbox : strLive;
                    }
                    var req = (HttpWebRequest)WebRequest.Create(test);

                    req.Method        = "POST";
                    req.ContentType   = "application/x-www-form-urlencoded";
                    req.ContentLength = _query.Length;

                    var stOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
                    stOut.Write(_query);
                    stOut.Close();

                    var stIn = new StreamReader(req.GetResponse().GetResponseStream());
                    _strResponse = stIn.ReadToEnd();
                    stIn.Close();

                    if (_strResponse.StartsWith("SUCCESS"))
                    {
                        string sessionCode = orderInfo.SessionCode;
                        try
                        {
                            var paypalHandler = new PayPalHandler();

                            paypalHandler.ParseAfterIPNMobile(orderInfo, coupons, itemsInfo, _strResponse,
                                                              billingAddress,
                                                              shippingAddress,
                                                              TemplateName,
                                                              _addressPath);
                        }
                        catch (Exception)
                        {
                            lblerror.Text = GetSageMessage("Payment", "PaymentParsingIPNError");
                        }

                        String[] stringArray = _strResponse.Split('\n');
                        int      i;
                        string   status = string.Empty;
                        for (i = 1; i < stringArray.Length - 1; i++)
                        {
                            String[] stringArray1 = stringArray[i].Split('=');

                            String sKey   = stringArray1[0];
                            String sValue = HttpUtility.UrlDecode(stringArray1[1]);

                            switch (sKey)
                            {
                            case "txn_id":
                                _transID = Convert.ToString(sValue);
                                break;

                            case "payment_status":
                                status = Convert.ToString(sValue);
                                break;
                            }
                        }
                        lblOrderNo.Text       = "#" + orderInfo.OrderId;
                        lblTransaction.Text   = _transID;
                        lblInvoice.Text       = _invoice;
                        lblPaymentMethod.Text = "Paypal";
                        lblDateTime.Text      = DateTime.Now.ToString("dddd, dd MMMM yyyy ");
                        if (status.ToLower().Trim() == "completed")
                        {
                            lblerror.Text = GetSageMessage("Payment", "PaymentProcessed");
                        }
                        else if (status.ToLower().Trim() == "pending")
                        {
                            lblerror.Text = GetSageMessage("Payment", "PaymentPending");
                        }
                        Session.Clear();
                    }
                }
                Session.Clear();
            }
            else
            {
                Response.Redirect(sageRedirectPath);
            }
        }
        catch (Exception ex)
        {
            ProcessException(ex);
        }
    }