沙盒重定向破坏会话

时间:2012-12-30 16:42:50

标签: asp.net paypal sandbox

为什么当我从我的localhost将用户重定向到PAYPAL的沙箱(https://www.sandbox.paypal.com)然后返回到.aspx页面(successful.aspx)后,它会丢失会话,

e.g。 SESSION [“tempUser”],它适用于所有页面但不在Successful.aspx页面上,它返回null,我正在处理此错误2周,但没有进展, 我试过不同的浏览器,甚至不同的系统,改变了一些代码,但没有任何帮助,它已经过了1个月+并且在几个表格上几个帖子之后,甚至没有任何帮助饼干有同样的问题,返回时返回null,我之前的帖子。

重定向页面代码:

<body>
    <form id="form1" runat="server">
<div>
 <h4> Congrats <b> <%# Session["tempSubAdminName"]%>  </b>, Your basic informaton has been submitted , For full activation you need to pay via paypal. Please click the PayPal icon given below. </h4>
</div>
<div>
 <asp:HyperLink ID="homeBtnImage" runat="server" ImageUrl="~/btnPayPal.gif"
      NavigateUrl= "https://www.sandbox.paypal.com/us/cgi-bin/webscr?cmd=_xclick&business=smile2_1355775057_biz@yahoo.com&item_name=MemberShip&amount=20&currency=USD&return=http://127.0.0.1:57135/Online%20Recruitment%20System-Final/paymentSuccessful.aspx?emailAdmin='1234' &cancel_return=https://www.google.com/" >PayPal</asp:HyperLink>
</div>
<h6>Or if you don't have paypal account, click <asp:HyperLink ID="paypalSite" runat="server"
    NavigateUrl="https://www.sandbox.paypal.com/" >here</asp:HyperLink> </h6>
</form>

如果成功,请回到这个年龄

</head>
 <body>
   <form id="form1" runat="server">
   <div>
     <h5> Dear <%# Session["tempSubAdmin"] %>, You have successfully registered ,   Please wait for the approval by admin. </h5>
</div>
</form>

2 个答案:

答案 0 :(得分:0)

我正在解释我实施的方式。它可能因人而异。

  1. 首先,您必须在developer paypal api上创建一个帐户。帐户。
  2. 然后创建两个测试帐户,一个用于客户,另一个用于商业(商家)。
  3. web.config 文件中声明这些值。

     <appSettings>    
        <!--these keys are for Paypal-->
        <add key="paypalURL" value="https://www.sandbox.paypal.com" />
        <add key="paypalAccount" value="arshad_Mer_biz@gmail.com" />
        <add key="websiteUrl" value="http://www.yourstie.com" />
      </appSettings>
    
  4. 现在您必须相应地设置paypal html变量,详细信息Paypal variables

  5. button_click 事件

    中编写以下代码
    string redirectUrl = ConfigurationManager.AppSettings["paypalURL"]+"/cgi-bin/webscr?cmd=_xclick";
    string sellersEmail = "&business=";
    string buyersEmail = "&email=";
    string productName = "&item_name=";
    string amount = "&amount=";
    string shippingOption = "&no_shipping=";
    string noteOpton = "&no_note=";
    string returnUrl = "&return=";
    string cancelUrl = "&cancel_return=";
    string rmOption = "&rm=";
    string notifyUrl = "&notify_url=";
    string custom = "&custom=";
    
    // Merchant account
    sellersEmail += ConfigurationManager.AppSettings["paypalAccount"];
    
    //calling a method that will return current user email id.
    buyersEmail += GeneralClass.GetUserEmail();
    
    //optional value if you want to carry
    custom += GeneralClass.GetUseriD();
    
    productName += lblProdeutName.Text;
    amount +=lblAmount.Text ;
    
    shippingOption += "1";                      //1 means no shipping option;
    noteOpton += "1";                           //1 means no note option;
    rmOption += "1";                            
    
    returnUrl +=ConfigurationManager.AppSettings["websiteUrl"]+"/PaypalThankYou.aspx";
    cancelUrl +=ConfigurationManager.AppSettings["websiteUrl"] + "/PaypalCancel.aspx";
    notifyUrl += ConfigurationManager.AppSettings["websiteUrl"] + "/PaypalNotifyUrl.aspx";
    
    redirectUrl += sellersEmail + buyersEmail + productName + amount + shippingOption +    noteOpton + returnUrl + cancelUrl + notifyUrl + rmOption + custom;
    Response.Redirect(redirectUrl);
    
  6. 取消页面: - 如果用户从paypal取消交易,也适用于localhost 感谢您的页面:付款后,paypal将重定向到该页面 通知网址:有时它叫IPN(即时付款通知)。这是您从paypal获得价值的地方。它只适用于托管页面。 它不适用于localhost

    Notifyurl 页面的代码
    添加这些namespaces

     using System.Net;
     using System.IO;
     using System.Text;
     using System.Collections.Specialized;
    
    page_load中的

      protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
             //Post back to either sandbox or live
            string strURL =ConfigurationManager.AppSettings["paypalURL"]+ "/cgi-bin/webscr";
    
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strURL);
    
            //Set values for the request back
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
            string strRequest =Encoding.ASCII.GetString(param);
            string strResponse_copy = strRequest;  //Save a copy of the initial info sent by PayPal
            strRequest += "&cmd=_notify-validate";
            req.ContentLength = strRequest.Length;
    
            //for proxy
            //WebProxy proxy = new WebProxy(new Uri("http://url:port#"));
            //req.Proxy = proxy;
            //Send the request to PayPal and get the response
    
            StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
            streamOut.Write(strRequest);
            streamOut.Close();
            StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
            string strResponse = streamIn.ReadToEnd();
            streamIn.Close();
    
            if (strResponse == "VERIFIED")
            {
                //check the payment_status is Completed
                //check that txn_id has not been previously processed
                //check that receiver_email is your Primary PayPal email
                //check that payment_amount/payment_currency are correct
                //process payment
                // pull the values passed on the initial message from PayPal
    
                NameValueCollection these_argies = HttpUtility.ParseQueryString(strResponse_copy);
    
                string pay_stat = these_argies["payment_status"];
    
                //.
                //.  more args as needed look at the list from paypal IPN doc
                //.
    
    
                if (pay_stat.Equals("Completed"))
                {
                //inserting the database
                    int intUserID;
                    int.TryParse(these_argies["custom"],out intUserID);
                    objUserEntity.UserID=intUserID;
                    objPapalPayment.strTransactionID = these_argies["txn_id"];
                    objPapalPayment.dblPaymentAmount = Convert.ToDouble(these_argies["payment_gross"]);
                    objPapalPayment.strBuyerMail = these_argies["payer_email"];
                    objPapalPayment.dtmDateAppliedOn = DateTime.Now;
                    objPapalPayment.blnIsGlobalAdvertisement = true;
                    objSubscription.blnIsPaid = false;
                    objSubscription.blnSubscriptionWithHeld = true;
                    objUserFunction.AddPaypalPayment(objUserEntity, objPapalPayment, objSubscription, enmSubscriptionType.Global);                         
                }
    
    
                // more checks needed here specially your account number and related stuff
            }
            else if (strResponse == "INVALID")
            {
                //log for manual investigation
            }
            else
            {
                //log response/ipn data for manual investigation
            }
            }
        }
    

    那么你将如何在localhost上调试?使用rm value = 2;并在thankyou页面的page_load事件中粘贴相同的代码。它会起作用。

    了解更多详情: http://www.codeproject.com/Articles/42894/Introduction-to-PayPal-for-C-ASP-NET-developers
    Paypal variables and its usage
    希望,它会帮助你。

答案 1 :(得分:0)

我认为您正在本地计算机上测试它。

在重定向之前检查cookie ASP.NET_SessionId的值。

然后在调用successful.aspx后检查值。

如果Cookie丢失:Paypal无法对您的Cookie执行任何操作。它只能在同一个域(locahost)上更改。

我会在没有paypal的情况下测试这个:只需要一个简单的页面重定向到你的successful.aspx。如果cookie丢失,您可能配置了非常短的会话超时或运行了一些删除cookie的代码(检入global.asax)