如果数据库更新失败,Paypal 回滚/退款 IPN?

Paypal Rollback / Refund IPN if database update fails?

我想知道我应该如何处理这样一种情况:客户收到了数字商品的账单,然后调用了 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 语句,这样就不会发生数据库错误。如果数据库连接错误,您可以 return 500 响应,PayPal 的 IPN 系统将继续重试,直到收到成功的 200 OK 响应。

如果您确实希望根据脚本中的任何失败提交退款,您可以通过 RefundTransaction API.