使用支付网关时如何确认支付成功?

How to confirm that a payment was successful when using payments gateways?

据我了解,这就是支付网关的工作方式。

  1. 我们向支付网关(2checkouts、Paypal 等)发送必要的 POST 请求。

  2. 付款由 Payments Getaway 处理。

  3. 然后 Payment Getaway 向我们发送 POST 参数。假设 Payment Getaway return 参数为 example.com/return.php page.

我知道他们会发送 POST 参数,例如状态之类的。我们可以拿它来验证付款。

所以我们要做的是,我们在 example.com/return.php page 中编写代码来验证付款。

但是 如果 user/hacker 将所有 POST 参数(我的意思是支付网关发送)发送到 example.com/return.php page 会发生什么。

我该如何处理?

您应该验证您是否从预期来源获得了 POST 参数。就 Paypal 而言,我们以他们的 Instant Payment Notification(或 IPN)为例。

查看他们的 IPN docs,他们建议:

Check email address to make sure that this is not a spoof

然而,更重要的是,您应该看看:

verify_sign = AtkOfCXbDm2hu0ZELryHFjY-Vb7PAUvS6nMXgysbElEn9v-1XcmSoGtf

Before you can trust the contents of the message, you must first verify that the message came from PayPal. To verify the message, you must send back the contents in the exact order they were received and precede it with the command _notify-validate, as follows:

这意味着,当您收到到 example.com/return.php page 的 IPN 时,它可以在任何时候发生,而不是在您的最终用户将触发的 HTTP 请求/响应的正常流程中,然后您将此信息发送回 PayPal 并让 他们 验证您收到的信息是否正确,并且来自他们。

PayPal will then send one single-word message, either VERIFIED, if the message is valid, or INVALID if the messages is not valid.

因此,在有人向您的端点发送欺骗性数据的假设示例中,PayPal 无论如何都会将其验证为无效,然后您可以采取您需要做的事情来确保它不会再次发生(记录, IPTables 等)。