gpt4 book ai didi

php - 您如何在 Stripe 预建结账流程中的 webhook 中捕获付款?

转载 作者:行者123 更新时间:2023-12-05 06:55:48 38 4
gpt4 key购买 nike

我目前有一个 Opayo (SagePay) 服务器集成,我显然被一种能力宠坏了,如果在我的订单上履行订单时出现问题,我可以简单地响应带有错误状态的付款通知回调以拒绝/作废交易边。我认为,在幕后发生了某种自动授权然后捕获,其中仅当我的通知处理程序返回成功/OK 状态时才捕获付款。

我正在研究如何通过 Stripe 结账集成(预构建结账、PHP 和 webhooks)实现同样的目标,看来我需要使用授权和手动捕获:

$session = \Stripe\Checkout\Session::create([
...
'payment_intent_data' => [
'capture_method' => 'manual',
],
]);

在示例代码和文档中(总体来说非常好,但我觉得这里不够),它展示了如何创建一个 webhook 来处理结帐 session 的完成:

// Handle the checkout.session.completed event
if ($event->type == 'checkout.session.completed') {
$session = $event->data->object;

// Fulfill the purchase...
fulfill_order($session);
}

奇怪的是,交易的 payment_status 没有被检查,这个例子表明您只需在这里完成您的订单。我只能假设,如果您没有使用延迟付款方式,那么此时您可以假设付款已成功,尽管做出这样的假设似乎是一个危险的假设。

在文档的后面,在“处理服务器端延迟通知付款方式”下,我们有一个更完整的示例,在执行订单之前实际检查付款状态:

switch ($event->type) {
case 'checkout.session.completed':
$session = $event->data->object;

// Save an order in your database, marked as 'awaiting payment'
create_order($session);

// Check if the order is paid (e.g., from a card payment)
//
// A delayed notification payment will have an `unpaid` status, as
// you're still waiting for funds to be transferred from the customer's
// account.
if ($session->payment_status == 'paid') {
// Fulfill the purchase
fulfill_order($session);
}
...

如果我使用手动捕获方法,究竟应该如何使用/改编上面的代码?据推测 payment_status 不会被 paid 那么我要检查什么以及如何访问 PaymentIntent 以便能够捕获它?这一切都应该发生在 checkout.session.completed 事件中吗?

代码示例会很棒;不幸的是,文档中只有这个脚注:

To capture an uncaptured payment, you can use either the Dashboard or the capture endpoint. Programmatically capturing payments requires access to the PaymentIntent created during the Checkout Session, which you can get from the Session object.

我已经检查了在线代码示例,但没有涵盖这个确切的场景。

编辑:在下面添加我对流程的理解以及问题:

switch ($event->type) {
case 'checkout.session.completed':

$session = $event->data->object;

if ($session->payment_status == 'unpaid') {

// We need to capture the payment if we have arrived here?
// Is this the only scenario where we would end up here?
// How do we get the PaymentIntent, what status if any should we check on it?

// Attempt to fulfil the purchase here, if all good then
// capture the payment

} else if ($session->payment_status == 'paid') {
// Assume this was not an auth/capture type payment?
// There is no other scenario where we would end up here?
}
...

最佳答案

如果您收到 checkout.session.completed事件,则同步支付方式成功完成。

对于异步支付方式,there are other events:

  • checkout.session.async_payment_succeeded
  • checkout.session.async_payment_failed

我不确定你为什么要在这里使用 auth 和 capture,因为付款是在收到 checkout.session.completed 后成功的或 checkout.session.async_payment_succeeded ,但如果需要,您可以设置选项来执行此操作 here然后你需要 capture the Payment Intent相关with the Checkout Session .

如果您需要确定费用是否已被捕获,您需要检索付款意向并查看费用的 captured值:https://stripe.com/docs/api/charges/object#charge_object-captured

关于php - 您如何在 Stripe 预建结账流程中的 webhook 中捕获付款?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65281615/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com