Hi vereato,
To set return url from code behind you need to set the return QueryString value in the url you are requesting.
Check the below example.
C#
string payPalUrl = "https://www.paypal.com/us/cgi-bin/webscr";
string paypalEmail = "paypal@gmail.com";
string successURL = "http://localhost:4076/Success.aspx";
string failedURL = "http://localhost:4076/Failed.aspx";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendFormat("{0}?cmd=_xclick&business={1}", payPalUrl, HttpUtility.UrlEncode(paypalEmail));
// Currency.
sb.Append("&lc=US&no_note=0¤cy_code=USD");
// Product Name.
sb.AppendFormat("&item_name={0}", HttpUtility.UrlEncode("Tube Light"));
// Invoice number.
sb.AppendFormat("&invoice={0}", "12345");
// Product Amount.
sb.AppendFormat("&amount={0}", "200");
// Product Quantity.
sb.AppendFormat("&quantity={0}", "5");
// Product Number.
sb.AppendFormat("&item_number={0}", HttpUtility.UrlEncode("12345"));
// Return Url.
sb.AppendFormat("&return={0}", HttpUtility.UrlEncode(successURL));
// Failed Url.
sb.AppendFormat("&cancel_return={0}", HttpUtility.UrlEncode(failedURL));
Response.Redirect(sb.ToString());
VB.Net
Dim payPalUrl As String = "https://www.paypal.com/us/cgi-bin/webscr"
Dim paypalEmail As String = "paypal@gmail.com"
Dim successURL As String = "http://localhost:4076/Success.aspx"
Dim failedURL As String = "http://localhost:4076/Failed.aspx"
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
sb.AppendFormat("{0}?cmd=_xclick&business={1}", payPalUrl, HttpUtility.UrlEncode(paypalEmail))
' Currency.
sb.Append("&lc=US&no_note=0¤cy_code=USD")
' Product Name.
sb.AppendFormat("&item_name={0}", HttpUtility.UrlEncode("Tube Light"))
' Invoice number.
sb.AppendFormat("&invoice={0}", "12345")
' Product Amount.
sb.AppendFormat("&amount={0}", "200")
' Product Quantity.
sb.AppendFormat("&quantity={0}", "5")
' Product Number.
sb.AppendFormat("&item_number={0}", HttpUtility.UrlEncode("12345"))
' Return Url.
sb.AppendFormat("&return={0}", HttpUtility.UrlEncode(successURL))
' Failed Url.
sb.AppendFormat("&cancel_return={0}", HttpUtility.UrlEncode(failedURL))
Response.Redirect(sb.ToString())
Setting Auto Return in PayPal Account
You can set the Auto Return in your PayPal Profile.
You need to turn on the Auto Return in your PayPal Profile and you can specify the return url.
If you have multiple return url's, then you need to specify the return url in your button code in the section Take my customer to this url when they finish checkout.
Following is the steps to enable Auto Return from PayPal website.
- Go to the Account Settings.
- Click Website payments.
- Click Update across from "Website preferences".
- Click on under "Auto Return".
- In the Return URL field, enter the URL where you want to send your buyer after the payment.
- The Return URL is applied to all Auto Return payments unless otherwise specified within the payment button or link.
- Click Save.
Reference:
https://www.paypal.com/us/cshelp/article/how-do-i-use-paypals-auto-return-feature-help197
https://www.paypal-community.com/t5/How-to-use-PayPal-Archive/Redirect-to-new-URL-after-payment/m-p/17779/highlight/true#M36608