pra*_*ech 6 .net c# asp.net-mvc paypal paypal-sandbox
我将测试应用程序与Paypal快速结账相结合,运行正常.但现在似乎有一些问题.在我的IPN处理程序检查响应paypal后成功支付paypal后,将响应返回为html页面而不是"已验证"或"无效",因此我无法验证付款.Paypal最近是否有快速结账的变化?以下是完整的代码
<form id="Paypal" name="Paypal" action="https://www.sandbox.paypal.com/cgi-bin/webscr"
method="post">
@Html.Hidden("cmd", "_xclick")
@Html.Hidden("business", "myemail@test.com")
@Html.Hidden("item_name", "Payment for course registration")
@Html.Hidden("amount", 100 )
@Html.Hidden("no_shipping", "1")
@Html.Hidden("return", "http://localhost:49319/cart/IPN")
@Html.Hidden("rm", "2")
@Html.Hidden("notify_url", "http://localhost:49319/cart/IPN")
@Html.Hidden("cancel_return", "http://localhost:49319/cart/PaymentFailure")
@Html.Hidden("currency_code", "CAD")
@Html.Hidden("tax_cart", "1")
@Html.Hidden("custom", "12")
<div class="checkout-button">
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif"
align="left" style="margin-right: 7px;" />
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
IPN处理程序代码:
string paypalUrl = useSandbox ? "https://www.sandbox.paypal.com/cgi-bin/webscr"
: "https://www.paypal.com/cgi-bin/webscr";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(paypalUrl);
// Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] param = Request.BinaryRead(Request.ContentLength);
string strRequest = Encoding.ASCII.GetString(param);
StringBuilder sb = new StringBuilder();
sb.Append(strRequest);
foreach (string key in formVals.Keys)
{
sb.AppendFormat("&{0}={1}", key, formVals[key]);
}
strRequest += sb.ToString();
req.ContentLength = strRequest.Length;
//for proxy
//WebProxy proxy = new WebProxy(new Uri("http://urlort#");
//req.Proxy = proxy;
//Send the request to PayPal and get the response
string response = "";
using (StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII))
{
streamOut.Write(strRequest);
streamOut.Close();
using (StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream()))
{
response = streamIn.ReadToEnd();
}
}
return response;
Run Code Online (Sandbox Code Playgroud)
编辑:这是请求字符串发送到Paypal
CONTEXT = wtgSziM4C5x0SI-9CmKcv2vkSeTLK5P_g6HqzC__YTYkcqziFNcB84p79Ja&myAllTextSubmitID = CMD = _flow&transaction_subject = 12&txn_type = web_accept&PAYMENT_DATE = 01%3A59%3A21 +月+ 02%2C + 2011 + PDT&姓氏=用户&residence_country = CA&pending_reason = paymentreview&ITEM_NAME =付款+为+当然+登记&payment_gross =&mc_currency = CAD&业务= pramod_1298956597_biz% 40sumerusolutions.com&payment_type =即时与protection_eligibility =不合格的&payer_status =验证和verify_sign = Ag7LtkvrF1u9.1ScLJwRM4btR1G1A16qsCs-xUl6EpI1rE1UWpodXJsc&txn_id = 15Y20623GD922445F&PAYER_EMAIL = pramod_1298961722_per%40sumerusolutions.com&税= 0.00&的test_ipn = 1&FIRST_NAME =测试与RECEIVER_EMAIL = pramod_1298956597_biz%40sumerusolutions.com和数量= 1&payer_id = Z2MRT3Q9L6E28&receiver_id = RT3M59WESZHEE&ITEM_NUMBER = PAYMENT_STATUS =待定&handling_amount = 0.00及运送= 0.00&mc_gross = 850.00和定制= 12&的charset =窗口1252¬ify_version = 3.1&merchant_return_link =点击这里+&form_charset = UTF-8CONTEXT = wtgSziM4C5x0SI-9CmKcv2vkSeTLK5P_g6HqzC__YTYkcqz iFNcB84p79Ja&myAllTextSubmitID = CMD = _flow&transaction_subject = 12&txn_type = web_accept&PAYMENT_DATE = 01%3A59%3A21 +月+ 02%2C + 2011 + PDT&姓氏=用户&residence_country = CA&pending_reason = paymentreview&ITEM_NAME =付款+为+当然+登记&payment_gross =&mc_currency = CAD&业务= pramod_1298956597_biz%40sumerusolutions.com&payment_type =即时与protection_eligibility =不合格的&payer_status =验证和verify_sign = Ag7LtkvrF1u9.1ScLJwRM4btR1G1A16qsCs-xUl6EpI1rE1UWpodXJsc&txn_id = 15Y20623GD922445F&PAYER_EMAIL = pramod_1298961722_per%40sumerusolutions.com&税= 0.00&的test_ipn = 1&FIRST_NAME =测试与RECEIVER_EMAIL = pramod_1298956597_biz%40sumerusolutions.com和数量= 1&payer_id = Z2MRT3Q9L6E28&receiver_id = RT3M59WESZHEE&ITEM_NUMBER = PAYMENT_STATUS =待定&handling_amount = 0.00及运送= 0.00&mc_gross = 850.00 &定制= 12&字符集=窗口1252¬ify_version = 3.1&merchant_return_link =点击+此处&form_charset = UTF-8&CMD = _notify-验证
好.经过大量的反复试验并在paypal论坛上发布此问题后,我的支付网关工作正常.虽然我对解决方案并不满意.解决方案是使用PDT而不使用IPN.我从paypal支持得到的回答是"问题是IPN是一个额外的通知系统,它有一点延迟.所以它不是100%完全自动回复页面的解决方案."
Paypal建议我使用PDT,它现在正在运行.我会把这些步骤放在这里,以便它可以帮助那些也面临这样问题的人:
激活PayPal帐户中的"AutoReturn"和PDT功能.步骤:登录您的PayPal帐户点击个人资料点击网站付款偏好设置自动返回打开在返回URL文本框中输入有效的URL将付款数据转移到打开.记下显示的PDT令牌.滚动到页面底部,然后单击"保存"
将以下参数传递给paypal cmd,business,item_name,amount,return,currency_code,custom.
用PDT代码更改IPN处理程序代码,在我的情况下PDT代码如下 -
string authToken,txToken,query; string strResponse;
authToken = ConfigurationManager.AppSettings["PDTToken"];
//read in txn token from querystring
txToken = Request.QueryString.Get("tx");
query = string.Format("cmd=_notify-synch&tx={0}&at={1}", txToken, authToken);
// Create the request back
string url = ConfigurationManager.AppSettings["PayPalUrl"];
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
// Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = query.Length;
// Write the request back IPN strings
StreamWriter stOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(query);
stOut.Close();
// Do the request to PayPal and get the response
StreamReader stIn = new StreamReader(req.GetResponse().GetResponseStream());
strResponse = stIn.ReadToEnd();
stIn.Close();
// If response was SUCCESS, parse response string and output details
if (strResponse.StartsWith("SUCCESS"))
{
}
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
4715 次 |
| 最近记录: |