c# - IPN 验证不会在应该失败的时候失败

标签 c# asp.net-mvc paypal paypal-ipn

我正在尝试设置 PayPal 来处理付款。一切正常,除了当我的 IPN 没有向 PayPal 发送验证消息时付款没有被取消。据我了解,如果 IPN 未验证付款,则交易应该被取消。 代码如下所示:

[HttpPost]
public IActionResult Receive()
{
    //Store the IPN received from PayPal
    LogRequest(Request);

    Task.Run(() => VerifyTask(Request));

    //Reply back a 200 code
    return new EmptyResult();
}

private void VerifyTask(HttpRequest ipnRequest)
{
    try
    {
        VerifyIpnParamsWithException(ipnRequest);
        var verificationRequest = (HttpWebRequest)WebRequest.Create("https://www.paypal.com/cgi-bin/webscr");

        //Set values for the verification request
        verificationRequest.Method = "POST";
        verificationRequest.ContentType = "application/x-www-form-urlencoded";
        var param = ReadRequestBody(ipnRequest.Body);
        var strRequest = Encoding.ASCII.GetString(param);

        //Add cmd=_notify-validate to the payload
        strRequest = "cmd=_notify-validate&" + strRequest;
        verificationRequest.ContentLength = strRequest.Length;

        //Attach payload to the verification request
        using (var streamOut = new StreamWriter(verificationRequest.GetRequestStream(), Encoding.ASCII))
        {
            streamOut.Write(strRequest);
            streamOut.Close();
        }
        var verificationResponse = string.Empty;

        //Send the request to PayPal and get the response
        using (var streamIn = new StreamReader(verificationRequest.GetResponse().GetResponseStream()))
        {
            verificationResponse = streamIn.ReadToEnd();
            streamIn.Close();
        }
        ProcessVerificationResponse(verificationResponse);
    }
    catch (Exception exception)
    {
        DAL.LogError(exception, ipnRequest);
    }
}

当我对其进行测试时,根据我的日志,在向 PayPal 发送验证消息之前抛出了一个异常。这并没有改变钱被转移到PayPal账户并完成交易的事实。我误会了吗?为了取消交易,我还应该做些什么吗?

最佳答案

IPN 的功能是通知商家有关支付事件的信息,其中包括 ;

  • 收到的付款,包括快速结帐和自适应付款。

  • 信用卡授权。

  • 电子支票付款和相关的待处理、已完成或已拒绝状态事件。

  • 定期付款和订阅操作。

  • 退款、争议、撤销和退款

如果您不验证发送给您的收听者的文本,IPN 不会取消付款。

来自 PayPal 的更多文章:https://developer.paypal.com/docs/classic/products/instant-payment-notification/

关于c# - IPN 验证不会在应该失败的时候失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48777001/

相关文章:

c# - 正则表达式选择单词的一部分

asp.net-mvc - MVC View 中的多个表单 : ModelState applied to all forms

c# - 为什么我的接口(interface)不实现公共(public)方法?

c# - 相当于 30 分钟的计时器滴答声是多少?

c# - ajax调用jquery后如何防止页面重新加载

jquery - ASP.net MVC 项目 : should I update jQuery version?

PayPal Checkout - 我们可以向其他人汇款吗?

PayPal Adaptive Payments 正在自动为订单退款

curl - Paypal API (NVP) TransactionSearch 给出错误 10004 - 开始日期无效

c# - 如果语句为真,为什么代码转到 else?