我想知道我应该如何处理客户为数字商品付费的情况,然后调用 IPN 并交付数字商品。如果交付过程中出现问题怎么办?此案应如何处理?在这种情况下我可以取消/退款吗?
我的 IPN 代码基于我找到的示例
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")
{
//UPDATE YOUR DATABASE
//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")
{
//UPDATE YOUR DATABASE
}
else
{ //UPDATE YOUR DATABASE
}
}
如果我的数据库更新失败怎么办?
首先,我会确保使用干净的 SQL 语句,这样数据库就不会出现错误。如果数据库连接错误,您可以返回 500 响应,PayPal 的 IPN 系统将不断重试,直到收到成功的 200 OK 响应。
如果您确实想根据脚本中的任何失败提交退款,您可以通过 RefundTransaction API 来完成。