ASP.net WebForms PayPal IPN错误地标记为成功

如何解决ASP.net WebForms PayPal IPN错误地标记为成功

我正在解决网站购物车/付款系统的问题。我们的网站使用C#ASP.NET WebForms

编写

问题是来自PayPal的IPN响应似乎未在运行。查看相关记录中的PayPal IPN历史记录,PayPal会从我的站点显示200成功响应。

我在网站上添加了日志记录功能,因此对该页面的任何点击都将被记录下来,并且我的通知页面没有看到点击。

有人知道我是否做错了什么,或者他们是否更改了我需要检查的内容才能使它更稳定?

这是我的网站处理PayPal交易的方式:

使用发票号,项目的一些文本以及总费用,我填充了一个命名值集合并将信息发布到PayPal:

NameValueCollection nvc = new NameValueCollection();
nvc.Add("cmd",HttpUtility.HtmlAttributeEncode("_xclick"));
nvc.Add("business",HttpUtility.HtmlAttributeEncode("email@example.com"));
nvc.Add("currency_code",HttpUtility.HtmlAttributeEncode("USD"));
nvc.Add("item_name",HttpUtility.HtmlAttributeEncode(InvoiceText));
nvc.Add("no_shipping",HttpUtility.HtmlAttributeEncode("1"));
nvc.Add("notify_url",HttpUtility.HtmlAttributeEncode("https://example.com/ipn_PayPal.aspx"));
nvc.Add("invoice",InvoiceID);
nvc.Add("amount",HttpUtility.HtmlAttributeEncode(TotalCharge.ToString()));
nvc.Add("image_url",HttpUtility.HtmlAttributeEncode("https://example.com/Content/images/Logo.png"));
nvc.Add("return",HttpUtility.HtmlAttributeEncode("https://example.com/?PayPalThanks=" + InvoiceID));
nvc.Add("rm",HttpUtility.HtmlAttributeEncode("2"));
HttpHelper.RedirectAndPOST(this.Page,PPFee.PostPage,nvc);

作为参考,她是HttpHelper类:

public class HttpHelper
{
    /// <summary>
    /// This method prepares an Html form which holds all data in hidden field in the addetion to form submitting script.
    /// </summary>
    /// <param name="url">The destination Url to which the post and redirection will occur,the Url can be in the same App or ouside the App.</param>
    /// <param name="data">A collection of data that will be posted to the destination Url.</param>
    /// <returns>Returns a string representation of the Posting form.</returns>
    /// <Author>Samer Abu Rabie</Author>
    private static String PreparePOSTForm(string url,NameValueCollection data)
    {
        //Set a name for the form
        string formID = "PostForm";

        //Build the form using the specified data to be posted.
        StringBuilder strForm = new StringBuilder();
        strForm.Append("<form id=\"" + formID + "\" name=\"" + formID + "\" action=\"" + url + "\" method=\"POST\">");
        foreach (string key in data)
        {
            strForm.Append("<input type=\"hidden\" name=\"" + key + "\" value=\"" + data[key] + "\">");
        }
        strForm.Append("</form>");

        //Build the JavaScript which will do the Posting operation.
        StringBuilder strScript = new StringBuilder();
        strScript.Append("<script language='javascript'>");
        strScript.Append("var v" + formID + " = document." + formID + ";");
        strScript.Append("v" + formID + ".submit();");
        strScript.Append("</script>");

        //Return the form and the script concatenated. (The order is important,Form then JavaScript)
        return strForm.ToString() + strScript.ToString();
    }

    public static void RedirectAndPOST(Page page,string destinationUrl,NameValueCollection data)
    {
        //Prepare the Posting form
        string strForm = PreparePOSTForm(destinationUrl,data);
        //Add a literal control the specified page holding the Post Form,this is to submit the Posting form with the request.
        page.Controls.Add(new LiteralControl(strForm));
    }
}

交易确实转到PayPal,人们可以正常付款,但是在重定向回我的感谢页面之后,IPN似乎从未发送出去。

在我的IPN页面上,首先检查发票,然后将其记录到数据库:

int Invoice = Utils.ReturnInt(Request.Form["invoice"],-1);
if (Invoice == -1)
    Invoice = Utils.ReturnInt(Request.QueryString["invoice"],0);
LogInvHit(Invoice);

如果页面在此上中断,我预计贝宝(PayPal)的IPN历史记录中会出现错误,因此它已经超越了这一要求,或者在达到200成功之前从未到达过它

此外,作为参考,这是我告诉PayPal是否成功的方法:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strPayPalResponse);
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;

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();

据我估计,如果页面被点击,我至少应该有一个条目,但我没有,所以我怀疑我的页面实际上是错误的或者是从PayPal获得IPN匹配。

总而言之,我认为IPN通知工作不正确,但是假设我做错了什么。寻找一些有助于排除我可以纠正的潜在问题的帮助

解决方法

您误解了IPN交付和验证的工作原理。

当PayPal向您的监听器发布消息时,您的监听器将以一些HTTP状态代码进行响应。如果它以2xx状态响应,则IPN被标记为成功交付。如果它没有以2xx状态进行响应,则IPN将重试,如果没有收到2xx成功作为响应,则IPN最终将失败。


您突出显示的代码,页面的中断位置以及将IPN发送回PayPal进行验证的位置,与上述内容完全无关(因为它不影响返回的HTTP状态代码)。

将收到的IPN消息发回PayPal进行验证是出于您自己的目的,因此您知道消息来自PayPal而不是其他实体。

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

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res