首先,我要说的是,我从事这项工作已经太久了,我认为我遇到困难的部分应该是最容易完成的任务。然而,我做不到。我真的很困惑。
我正在将 PayPal 集成到我的网站中。我以前开发过很多网站,但这是我第一次做支付。
我有以下代码(未修改 - 我复制并粘贴了 [不要 panic !我这样做是有原因的]):
// ASP .NET C#
using System;
using System.IO;
using System.Text;
using System.Net;
using System.Web;
public partial class csIPNexample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Post back to either sandbox or live
string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
string strLive = "https://www.paypal.com/cgi-bin/webscr";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);
//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 += "&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
}
else if (strResponse == "INVALID")
{
//log for manual investigation
}
else
{
//log response/ipn data for manual investigation
}
}
}
现在,我知道这段代码的含义,也知道它的作用。我唯一遇到困难的部分是这部分:
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
}
else if (strResponse == "INVALID")
{
//log for manual investigation
}
else
{
//log response/ipn data for manual investigation
}
您现在可能正在大笑。没关系:
//check the payment_status is Completed
我认为那一行应该这样写:
@{
if(Request.QueryString["payment_status"] == "Completed")
{
// Payment status is completed.
}
}
而且,我认为我应该为其余的注释行做几乎相同的事情(获取 Request["etc"] 变量并比较它们的值),并将数据与数据库中的相关数据进行匹配给用户。
有人可以帮帮我吗?我真的到处都看了,似乎我能找到的所有在线代码示例都没有向您展示这部分的代码。
非常感谢您的帮助。
谢谢
最佳答案
你的台词是这样的,用一个帖子重播!内容是经过验证的,但值是在帖子上,而不是在 url 上。
if (strResponse == "VERIFIED")
{
// Now All informations are on
HttpContext.Current.Request.Form;
// for example you get the invoice like this
HttpContext.Current.Request.Form["invoice"]
// or the payment_status like
HttpContext.Current.Request.Form["payment_status"]
}
else
{
//log for manual investigation
}
现在,在验证响应并完成 Payment_status 后,您在 txn_type 上有额外的选项,您可以检查电子邮件是否正确,金额是否正确等。所以您要求的代码将如下所示:
@{
if(HttpContext.Current.Request.Form["payment_status"] == "Completed")
{
// Payment status is completed.
}
}
您可以检查payment_status == "Completed"是否表示订单已完成,但您还需要检查金额是否正确,电子邮件是否正确,其他原因是否正确(如挂起、电子检查、保留)
关于c# - Paypal 与 ASP.NET/C# 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9047426/