无法从Paypal IPN

时间:2018-09-11 21:36:16

标签: c# asp.net paypal

我有一个应该由Paypal IPN请求的C#片段,请确认付款,然后从请求中获取一些参数。问题是我无法获取这些参数。

public class ipn : IHttpHandler {

    protected string paypalUrl = "https://www.sandbox.paypal.com/cgi-bin/webscr";

    public void ProcessRequest (HttpContext context) {
        Task.Run(() => VerifyTask(context));
        context.Response.Write("");
    }

    private void VerifyTask(HttpContext context)
    {
        var verificationResponse = string.Empty;
        StreamReader reader = new StreamReader(context.Request.InputStream);
        string requestFromPost = reader.ReadToEnd();

        // at this point I am getting the request parameters correctly
        File.WriteAllText(context.Server.MapPath("request.txt"), "Request: " + requestFromPost);

        context.Request.InputStream.Position = 0;

        try
        {
            // validating request here, all fine
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            var verificationRequest = (HttpWebRequest)WebRequest.Create(paypalUrl);

            // set values for the verification request
            verificationRequest.Method = "POST";
            verificationRequest.ContentType = "application/x-www-form-urlencoded";

            // add cmd=_notify-validate to the payload
            var strRequest = "cmd=_notify-validate&" + requestFromPost;
            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)
        {
            // handling exception here, later
        }

        // sending context and response
        ProcessVerificationResponse(context, verificationResponse);
    }

    private void ProcessVerificationResponse(HttpContext context, string verificationResponse)
    {
        // verifications response is VERIFIED
        if (verificationResponse.Equals("VERIFIED"))
        {
            try {
                // trying to get payment status
                File.WriteAllText(context.Server.MapPath("status.txt"), "Verified: " + context.Request.QueryString["payment_status"]);
            }
            catch ( Exception exception)
            {
                // capture exception for manual investigation
                File.WriteAllText(context.Server.MapPath("error.txt"), "Error: " + exception.Message);
            }
        }
    }
}

抛出一个异常,并且创建了 error.txt 文件,并出现“对象引用未设置为对象实例”错误。无论我对context.Request

做什么,都会发生这种情况

context.Request.QueryString.Count.ToString()做同样的事情

context.Request["payment_status"]

context.Request.Params.Count.ToString()触发相同的异常

所以我不知道未初始化的是什么,为什么,因为context.Request.InputStream至少不为空

0 个答案:

没有答案