微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ASP.NET Web窗体中的Paypal IPN事件监听器无法正确处理请求

如何解决ASP.NET Web窗体中的Paypal IPN事件监听器无法正确处理请求

Paypal的文档提供了有关ASP.NET MVC的说明,但是在Webforms中几乎没有指导。

要点,我不得不将HttpRequestBase ipnRequest更改为HttpRequest ipnRequest,因为...

无法从HttpRequestBase转换为HttpRequest

这阻止了Request在我的代码中工作。我猜我从MVC到网络表单的翻译有问题吗?

我对有效的MVC代码的翻译...

protected void Page_Load(object sender,EventArgs e)
{
    //Store the IPN received from PayPal
    LogRequest(Request);

    //Fire and forget verification task
    Task.Run(() => VerifyTask(Request));

    //Reply back a 200 code
    Response.StatusCode = 200;
}

这是MVC版本,来自Paypal的文档:

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

            //Fire and forget verification task
            Task.Run(() =>  VerifyTask(Request));

            //Reply back a 200 code
            return new HttpStatusCodeResult(HttpStatusCode.OK);
        }

这就是MVC版本正在执行的内容,也是我正在尝试通过Webforms转换执行的内容

private void VerifyTask(HttpRequest ipnRequest) // would be HttpRequestBase following official docs / mvc
{
   var verificationResponse = string.Empty;

    try
    {

        var verificationRequest = (HttpWebRequest)WebRequest.Create("https://www.sandBox.paypal.com/cgi-bin/webscr");

        var Request = HttpContext.Current.Request;

        //Set values for the verification request
        verificationRequest.Method = "POST";
        verificationRequest.ContentType = "application/x-www-form-urlencoded";
        var param = Request.BinaryRead(ipnRequest.ContentLength);
        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
        var streamOut = new StreamWriter(verificationRequest.GetRequestStream(),Encoding.ASCII);
        streamOut.Write(strRequest);
        streamOut.Close();

        //Send the request to PayPal and get the response
        var streamIn = new StreamReader(verificationRequest.GetResponse().GetResponseStream());
        verificationResponse = streamIn.ReadToEnd();
        streamIn.Close();
    }
    catch (Exception exception)
    {
        var hehe = exception;
        //Capture exception for manual investigation
    }

    ProcessverificationResponse(verificationResponse);
}

由于以下原因,使用我的Webforms版本失败:

HttpContext.Current.Request ='HttpContext.Current.Request'引发了类型'System.NullReferenceException'的异常

感觉就像我正在大范围内失败,并且不确定我是否在正确的轨道上或完全丢失了某些东西。我知道我在这个问题上还不清楚,我的头很模糊,而且我在Paypal Ipn的东西上呆了三天了。如果我可以添加可以帮助解决问题的任何内容,请发表评论,我将尽一切可能。非常感谢!

解决方法

如果您正在寻找一种可靠的方式来通知成功的PayPal付款(比等待异步IPN消息传递更可靠,更可靠),只需直接从服务器中捕获它们即可。

您将需要两条新路线,一条用于“设置交易”,一条用于“捕获交易”,documented here.。如果Checkout-Net-SDK在您的环境中不起作用,请使用直接HTTPS(没有SDK)调用PayPal REST API。

一旦拥有了这两条路线,就将它们与该前端配对:https://developer.paypal.com/demo/checkout/#/pattern/server

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。