PayPal 如果数据库更新失败,则回滚/退款 IPN
本文关键字:退款 IPN 如果 数据库 更新 失败 PayPal | 更新日期: 2023-09-27 18:37:10
我想知道如何处理客户被收取数字商品费用的情况,然后调用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 执行此操作。