使用 Payum 对网关通知执行自定义操作

Perform custom operations on gateway notification with Payum

我将 Symfony 与 PayumBundle and payum-redsys 结合使用,以便通过西班牙 Redsys 网关接受付款。

每次尝试付款后(无论成功与否),Redsys 都会向 url 发送通知。我想捕捉这个通知并根据它的有效负载采取行动(支付成功了吗?哪个订单号?等等)。例如,如果支付成功,我想在数据库中将相应的订单标记为已支付,否则我想记录错误。类似于:

if ($paymentSuccessful) {
    $order = $orderRep->find($notif['Ds_Order']);
    $order->setDatePaid($notif['Ds_Date']);
    $em->flush();
} else {
    $logger->error('Failed payment for order ' . $notif['Ds_Order']);
}

如果我让 Payum 使用其默认 NotifyController 处理通知请求,它会执​​行自动令牌验证、参数解码等,这是完美的 但是 我可以'看不到如何执行我自己的自定义操作(见上面的代码),因此这个问题的标题。

Payum 似乎支持监听网关上的事件,如 PayumEvents. The Event being passed around basically only contains a Context from which you need to gather the information you need. You probably want to look at the actions inside this context, especially the CapturePaymentAction and the Request 中所示?我不清楚这一切是如何联系在一起的,因为我基本上是从源代码中破译它。

如果你不想弄清楚所有事件的内容,你可以创建一个监听器来监听适当的事件,然后使用 xdebug 或 var_dump 从事件中读出数据并继续从那里。基本上,事件侦听器应该确保您采取正确的操作,获取 Ds_OrderDs_Date(可能来自请求),然后您可以添加一个片段,如您在上面发布的那样。

如果您不熟悉 Symfony 的事件系统,我建议您阅读文档:

编辑:Payum 还提供了一些它如何处理事件的文档:https://github.com/Payum/Payum/blob/master/docs/event-dispatcher.md