使用.NET SDK Paypal的IPN列表器?

时间:2018-08-09 13:15:56

标签: c# asp.net-mvc paypal sdk

我正在使用.NET SDK Paypal。我不确定SDK是否提供IPN侦听器功能。我试过下面的代码,该代码始终返回true,是否是IPN验证的正确方法,我尝试在Google上找到。但什么也没找到。

 var ipn = Request.Form.AllKeys.ToDictionary(k => k, k => Request[k]);

        var param = Request.BinaryRead(Request.ContentLength);
        PayPal.IPNMessage iPNMessage = new PayPal.IPNMessage(ipn, param);
        PayPal.IPNMessage service = null;

        try
        {
            Dictionary<string, string> configurationMap = Configuration.GetAcctAndConfig();
            service = new PayPal.IPNMessage(configurationMap, param);
            var response = service.Validate();
        }
        catch (System.Exception ex)
        {
        }

当我尝试使用HTTPWebRequest时,它总是向我发送每笔交易无效的信息:

这是我的代码:

 public HttpStatusCodeResult Receive()
    {
        LogRequest(Request);
        Task.Run(() => VerifyTask(Request));
        return new HttpStatusCodeResult(HttpStatusCode.OK);
    }

    private void VerifyTask(HttpRequestBase ipnRequest)
    {
        var verificationResponse = string.Empty;
        try
        {

            var verificationRequest = (HttpWebRequest)WebRequest.Create("https://www.sandbox.paypal.com/cgi-bin/webscr");
            verificationRequest.Method = "POST";
            verificationRequest.ContentType = "application/x-www-form-urlencoded";
            var param = Request.BinaryRead(ipnRequest.ContentLength);
            var strRequest = Encoding.ASCII.GetString(param);
            strRequest = "cmd=_notify-validate&" + strRequest;
            verificationRequest.ContentLength = strRequest.Length;
            var streamOut = new StreamWriter(verificationRequest.GetRequestStream(), Encoding.ASCII);
            streamOut.Write(strRequest);
            streamOut.Close();
            var streamIn = new StreamReader(verificationRequest.GetResponse().GetResponseStream());
            verificationResponse = streamIn.ReadToEnd();
            streamIn.Close();
        }
        catch (Exception exception)
        {
        }
        ProcessVerificationResponse(verificationResponse);
    }

1 个答案:

答案 0 :(得分:0)

您必须在贝宝(Paypals)网站上使用IPN测试仪工具,除非您这样做,否则贝宝(Paypal)端没有匹配的交易,因此所有交易都将显示为无效。您当前正在使用沙盒端点,因此将仅验证来自测试人员的请求。

对于使用实际付款进行的实时交易,您不使用沙盒端点。