Hi,

I am developing a web-site where user can purchase items and pay through PAYPAL.

Its working fine on test and all the payment processing is working well. But aI want to update my database after succesful payment and for that I have created one THANKS.ASPX page where I fetch the response and if it is verified that update the database and also it working fine.

After redirect to paypal and successful patment I received page from Paypal site for successful payment where transaction ID and other information are displayed.

There is a link in paypal page called "Return to Merchant Test Score" and when user click on the link it redirects to my Thanks.aspx where I fetch the response and if verified the update the database.

For Automatic redirection :-

I am able to redirect user to my web-site from paypal after setting the "AutoReturn" to true and specify the URL.


Now another problem is that when "AutoReturn" is true and url is specified I receive the response as INVALID and If I again set the "AutoReturn" to false and after payment user click on the link "Return to Merchant" then I received the response as VALID.

Please let me know where I am wrong?

Below is my code:-

1) Redirect User from my web-site to Paypal for payment

Response.Redirect("Paypal.aspx?" + "business=" + objDb.paypalemailid + "&quantity=" + Qty + " &item_name=" + prodname + "&item_number=" + ID + "&amount=" + Rate + "&no_shipping=1&return=" + objDb.UrlBasePay() + "Thanks.aspx&cancel_return=" + objDb.UrlBasePay() + "Default.aspx&notify_url=" + objDb.UrlBasePay() + "IPNValidatorTestAnalysis.aspx&cn=How+did+you+hear+about+us%3F&currency_code=USD");

2) My paypal.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Paypal.aspx.cs" Inherits="Paypal" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>  Payment   </title>
</head>
<body>
    <%--<form action="<%= Connect.PayPalRequestPath %>" method="post" name="PayPalForm">--%> <%-- <%=System.Web.Configuration.WebConfigurationManager.AppSettings["PayPalRequestPath"] %>--%>
   
		<form action="<%=System.Web.Configuration.WebConfigurationManager.AppSettings["PayPalRequestPath"] %>"
		method="post" name="PayPalForm">
		<input type="hidden" name="cmd" value="_xclick" />
		<input type="hidden" name="business" value="<%= Request.QueryString["business"]%>" />
			<input type="hidden" name="quantity" value="<%=Request.QueryString["quantity"]%>" />
			<input type="hidden" name="item_name" value="<%=Request.QueryString["item_name"]%>" />
			<input type="hidden" name="item_number" value="<%=Request.QueryString["item_number"]%>"/>
			<input type="hidden" name="amount"  value="<%=Request.QueryString["amount"]%>"/>
			<input type="hidden" name="no_shipping" value="<%=Request.QueryString["no_shipping"]%>" />
			<input type="hidden" name="return"  value="<%=Request.QueryString["return"]%>?item_number=<%=Request.QueryString["item_number"]%>" />
			<input type="hidden" name="cancel_return" value="<%=Request.QueryString["cancel_return"]%>" />
			<input type="hidden" name="notify_url"  value="<%=Request.QueryString["notify_url"]%>" />
			<input type="hidden" name="cn"  value="<%=Request.QueryString["cn"]%>" />
			<input type="hidden" name="currency_code" value="<%=Request.QueryString["currency_code"]%>" />					
		</form>
		 <script type="text/javascript" language="JavaScript">
  document.PayPalForm.submit();  
</script>

</body>
</html>

3) Redirect user to my web-site from Paypal and check the Thanks.aspx.

If response is valid then update the databse.

string PayPalEmailId = System.Web.Configuration.WebConfigurationManager.AppSettings["paypalAccount"];
        
        string PayPalRequestPath = Convert.ToString(System.Web.Configuration.WebConfigurationManager.AppSettings["PayPalRequestPath"]);
        if (!IsPostBack)
        {
            //'Dim req As HttpWebRequest = CType(WebRequest.Create(strSandbox), HttpWebRequest) 
            CreateLog();
            //' to maintain the log 
            StepByStepLog("start:", DateTime.Now.ToString() + "----------------------------------------------------------");

            //GolfConfiguration.PayPalRequestPath

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(System.Web.Configuration.WebConfigurationManager.AppSettings["PayPalRequestPath"].ToString());

            //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);
            strRequest = strRequest + "&cmd=_notify-validate";
            req.ContentLength = strRequest.Length;

            StepByStepLog("request To paypal:", strRequest);


            //Send the request to PayPal and get the response 

            StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
            streamOut.Write(strRequest);
            streamOut.Close();
            StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
            string strResponse = streamIn.ReadToEnd();
            streamIn.Close();

            StepByStepLog("response from paypal:", strResponse);

            WriteRequestFormVariables(Request.Form);
            if (Request.QueryString["Res"] != null)
            {
                strResponse = Request.QueryString["Res"].ToString();
            }

            if (strResponse == "VERIFIED" || strResponse == "Free")
            {
   else
            {
               
            }
}
    }

ABOVE CODE IS WORKING FINE IF THE AUTO-RETURN IS FALSE ( RECEIVED THE RESPONSE AS "VALID" in THANKS.ASPX), BUT WHEN AUTO-RETURN TRUE ( REQUIRED TO AUTOMATICALLY REDIRECT USER TO MY WEB_SITE FROM PAYPAL) IT IS NOT WORKING ( ALWAYS RECEIVED THE RESPONSE AS "INVALID" IN THANKS.ASPX).

Thanks and Regards.

Prashant Trivedi

Dani AI

Generated

Short answer: AutoReturn only redirects the buyer’s browser back to your return URL; it does not replace PayPal’s server-to-server IPN. The code in your Thanks.aspx is doing IPN-style verification (posting the received POST back to PayPal with cmd=_notify-validate) — that will work only when PayPal actually POSTs form data to your page. When AutoReturn is enabled the buyer is usually redirected (GET) and the IPN payload is sent separately to your notify_url, so your POST-back contains nothing and PayPal replies INVALID. (developer.paypal.com)

Recommended approach

  • Treat IPN (notify_url) as the authoritative, asynchronous notifier and perform database updates there. IPN was built for backend processing and will arrive even if the user closes the browser. (developer.paypal.com)
  • Keep AutoReturn on for a better UX, but don’t rely on the return page to perform the final DB update. If you need immediate transaction data on the return page (to show a receipt), enable PDT (Payment Data Transfer) and use cmd=_notify-synch + the tx token to fetch the details from PayPal — still keep IPN as the final authority. (manualzilla.com)

Practical changes you can make now

  • Inspect your logs: log Request.HttpMethod, Request.ContentLength, Request.Form.Count and the raw payload. When AutoReturn=true you will likely see an empty POST body.
  • If you want PayPal to POST variables to the return URL, add rm=2 to your form (return method = POST), but note this is for convenience only and is not a substitute for IPN. (manualzilla.com)

PDT example (return page):

// detect return vs IPN
if (Request.HttpMethod == "GET" && !string.IsNullOrEmpty(Request.QueryString["tx"])) {
  // PDT: post back to PayPal with cmd=_notify-synch&tx={tx}&at={identityToken}
  // POST to https://ipnpb.paypal.com/cgi-bin/webscr (or sandbox) and parse response
}

Security checklist: verify payment_status == Completed, receiver_email matches your account, amount and currency match expected values, and reject duplicate txn_id. Also confirm you’re posting back to the correct PayPal endpoint (sandbox vs live). ’s pointer to PayPal docs is useful — follow those pages for exact endpoints and samples. (developer.paypal.com)

Recommended Answers

All 2 Replies

HI

These guys can probably help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.