sand box redirection destroys sessions

ぃ、小莉子 提交于 2019-12-11 18:40:10

问题


why it happen when i redirect user to PAYPAL's sandbox (https://www.sandbox.paypal.com) from my localhost then after returning to .aspx page (successful.aspx), it losses session,

e.g. SESSION["tempUser"], it works on everypage but not on Successful.aspx page, it returns null, i am working on this error for 2 weeks, but no progress, i tried different browsers, even different system, changed code a bit but nothing helped, its been 1month+ and after several posts on several forms , nothing helped even cookies have same problem, null upon return , my prior post.

Redirection page code:

<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>

Return to this age if successful

</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>


回答1:


I think you are testing this on your local machine.

Check what the value of the cookie ASP.NET_SessionId is before you redirect.

Then check the value after successful.aspx is called.

If the cookie is lost: Paypal is not able to do anything with your cookie. It can only be changed on the same domain (locahost).

I would test this without paypal: just have a simple page that redirects to your successful.aspx. If the cookie is lost, you might have a very short Session Timeout configured or have some code running that deletes cookies (check in global.asax)




回答2:


I am explaining the way i have implemented. it may vary person to person.

  1. First you have to create an account on developer paypal api. account.
  2. Then create two test accounts, one for client and another for business(merchant).
  3. Declare these values in the web.config file.

     <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. Now you have to set paypal html varaible accordingly , for details Paypal variables

  5. write following code in the button_click event

    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);
    

Cancel page:- if user cancel the transaction from paypal.it works for localhost also.
Thank you page: after payment, paypal will redirect to the page.
Notify url: sometime its called IPN(Instant Payment notification). This is the place where you will get the value from paypal. it only works on hosted page. it will not work on localhost

Code for the Notifyurl page
add these namespaces:

 using System.Net;
 using System.IO;
 using System.Text;
 using System.Collections.Specialized;

in the 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
        }
        }
    }

Then how will you debug on the localhost? use rm value =2; and paste the same code in the thankyou page's page_load event. it will work.

For further details : http://www.codeproject.com/Articles/42894/Introduction-to-PayPal-for-C-ASP-NET-developers
Paypal variables and its usage
Hope , it will help you.



来源:https://stackoverflow.com/questions/14092465/sand-box-redirection-destroys-sessions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!