PayPal IPN 和 PDT 混合
我在我的网站上实施了 PayPal。当用户选择支付服务时,他会被重定向到支付宝,在那里他进行付款,最后他会被重定向回我的网站。
这里我混合了IPN和PDT。付款后我收到 IPN 消息并将所有变量保存在数据库中。
当用户返回此处的站点时,我获取他的 TXN_ID 和 UserID(来自会话)并将其存储在数据库中。
我在某处读到混合 pdt 和 ipn 是不好的。有没有更好的方法用 UserID 保存 IPN 变量?我能否以某种方式将 UserId 发送给支付宝,并在用户完成付款后通过 IPN 消息获取它?
很抱歉我必须重新提出问题。我添加了“发票”字段以将用户 ID 发送给付款伙伴。但是,当我下次尝试使用相同的用户 ID 付款时,我会被重定向到支付宝页面,并且收到以下错误消息:
此发票已付款。欲了解更多信息,请 联系商家。
看来发票并不是将用户 ID 发送给支付宝的好选择。
我尝试将用户 ID 放入“自定义”字段中。
<input name="custom" type="hidden" id="custom" value="<%= UserInfo.UserID%>">
但我在 IPN 消息中收到的只是:
自定义=%3C%25%3D+UserInfo.UserID%25%3E
I have implemented PayPal on my site. When user choose service to pay he is redirected to pay pal where he commit payment and in the end he is redirected back to my site.
Here I mix IPN and PDT. After payment I receive IPN message and save all variables in database.
When user is back to the site here I get his TXN_ID and UserID (from session) and store it in database.
I read somewhere that it is bad to mix pdt and ipn. Is there any better way to save IPN variables with UserID? Can I somehow send UserId to pay pal and get it with IPN message later when user finish payment?
I'm sorry I have to reopen question. I added 'invoice' field to send UserID to pay pal. But when I try to pay next time with same user ID I am redirected to pay pal page and I receive this error message:
This invoice has already been paid. For more information, please
contact the merchant.
It seams that invoice is not a good option to send user id to pay pal.
I tried to put user ID in "custom" field.
<input name="custom" type="hidden" id="custom" value="<%= UserInfo.UserID%>">
But all I get in IPN message is:
custom=%3C%25%3D+UserInfo.UserID%25%3E
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的方法是使用发票字段。您可以将此字段发送给 PayPal,他们将在响应中将其发回。这就是我识别从 PayPal 返回的交易的方式。
就我而言,我不处理 PDT 和 IPN 消息。我只处理第一个击中我的那个。当第二条消息(PDT 或 IPN)到达时,我检查是否已经处理了该事务,然后将其丢弃。
另外,请不要忘记您需要验证消息是否来自 PayPal。收到 PDT 或 IPN 消息后,您需要将您的唯一 Auth_id 以及他们在原始消息中发送的 tx 令牌发回 PayPal。您不应依赖原始消息中的任何字段。
PayPal 收到您的确认请求后,将向您发送第二条包含交易详细信息的消息。这是您应该依赖的人。
我认为处理同一事务的 PDT 和 IPN 消息没有任何好处。话虽如此,我确实鼓励您同时实施这两种方法,因为它们都不能保证到达您手中。
编辑以包括如何将发票传递给 PayPal
要将发票传递给 PayPal,只需创建一个字段,例如:
唯一 ID 可以是您的用户 ID。
发送到 PayPal 的方式类似于
Paypal 会在 PDT 和 IPN 消息中发回发票。
编辑 - 非唯一发票 ID 号
我做了进一步研究,您可以将 PayPal 配置为接受同一发票 ID 的多笔交易,或每个发票 ID 仅接受一笔交易。请参阅以下直接来自 PayPal 网站的信息:
另一种选择是使用用户 ID + 随机数创建发票 ID。这将使它独一无二。
请参阅下面的示例代码以获取唯一的发票号码。
您还可以使用日期和;时间而不是随机数。
在 PayPal 的响应中,您只需丢弃破折号后面的内容即可。
我希望这有帮助。
The way I do it is to use the invoice field. This is a field you can send PayPal and they will send it back in the response. This is how I identify transactions coming back from PayPal.
And in my case I don't process both PDT and IPN messages. I process only the one that hits me first. When the second message (PDT or IPN) arrives I check that I have already processed that transaction and I simply discard it.
Also, please don't forget that you need to verify that the messages came from PayPal. Once you get the PDT or IPN message you need to post back to PayPal with your unique Auth_id and the tx token they sent in the original message. You should not rely on any field from the original message.
Once PayPal receives your confirmation request, it will send you a second message with the transaction details. This is the one you should rely on.
I don't see any benefit on processing both PDT and IPN messages for the same transaction. Having said that I do encourage you to implement both, because none of them are guaranteed to reach you.
Edit to include how to pass the invoice to PayPal
To pass the invoice to PayPal, just create a field like:
The unique id could be your user id.
The post to PayPal will be something like
Paypal will send back the invoice in the PDT and IPN messages.
Edit - non-unique invoice id number
I did further research and you can configure PayPal to accept multiple transactions for the same invoice id or to accept only one transaction per invoice id. See below the information straight from PayPal's website:
Another option would be to create your invoice id with user id + a random number. This would make it unique.
See below a sample code to get unique invoice numbers.
You could also use date & time instead of a random number.
And in the response back from PayPal, you just discard what comes after the dash.
I hope this helps.