当我也有一次性费用时,如何处理 Stripe webhook 以支付订阅费用? [英] How can I handle a Stripe webhook for paymentintent on a subscription when I have one-off charges too?
问题描述
我在一个需要订阅和一次性收费的应用中使用 Stripes PHP SDK.
I'm using Stripes PHP SDK in an app that has a subscription and one-off charges.
我正在通过网络钩子跟踪以下事件:
I am tracking the following events via a webhook:
// Handle the event
switch ($event->type) {
// Fired for every payment, including subscriptions and one-offs
case 'payment_intent.succeeded':
$payment = $event->data->object;
$this->handleOneOffPaymentSuccess($payment);
break;
// When a one-off payment fails
case 'charge.failed':
$payment = $event->data->object;
$this->handleOneOffPaymentFailed($payment);
break;
case 'payment_intent.payment_failed':
$payment = $event->data->object;
$this->handleOneOffPaymentFailed($payment, 'The payment was not authorized (#Virtuoso-3)');
break;
// Subscription handling
case 'invoice.payment_succeeded':
$payment = $event->data->object;
$this->handleSubscriptionPaymentSuccess($payment);
break;
// Subscription failure
case 'invoice.payment_failed':
$payment = $event->data->object;
$this->handleSubscriptionPaymentFailed($payment);
break;
// ... handle other event types
default:
// Unexpected event type
http_response_code(400);
exit();
}
一次性付款后,我会收到payment_intent.succeeded
.这有我的元字段数据,所以我可以将项目标记为已购买.一切都很好.
When a one-off payment is made I get a payment_intent.succeeded
. This has my metafield data in, so I can mark the item as purchased. All good.
当我订阅时,我会收到一个 invoice.payment_succeeded
事件.这里面有我的元字段数据,所以我可以将订阅标记为已付费.也都不错.
When I make a subscription I get a invoice.payment_succeeded
event. This has my metafield data in, so I can mark the subscription as paid. Also all good.
但是,随着订阅,我收到了一个 payment_intent.succeeded
事件.我想这是因为有收费.我没问题,但它不包含我创建订阅时的任何元字段数据,所以我无法跟踪它的用途.目前它只是失败了,这似乎不对.
However, along with the subscription I'm getting a payment_intent.succeeded
event. I presume this is because there's a charge taken. I'm alright with that, but it doesn't contain any of the metafield data from when I created the subscription, so I have no way of tracking what it's for. At the moment it's just failing, and that doesn't seem right.
这是我创建订阅的方式:
Here's how I create the subscription:
$subscription = \Stripe\Subscription::create([
'customer' => $payment_method->stripe_customer_id,
'items' => [
[
'plan' => App::environment('production') ? $user->userType->stripe_plan : $user->userType->stripe_plan_test,
],
],
'expand' => ['latest_invoice.payment_intent'],
'metadata' => [
'internal_subscription_id' => $internalSubscription->id,
'user_id' => $user_id,
]
]);
理想情况下,我希望 user_id
成为费用的一部分,然后我可以将其记录在用户身上,并将其连同一次性费用一起展示给他们.
Ideally I'd want that user_id
to be part of the charge, then I can just log it against the user and show it to them along with their one-off charges.
有什么想法我会出错吗?
Any ideas where I'm going awry?
推荐答案
这里有几个选择:
- 检查
发票
付款意图的属性对象.与订阅付款相关时,这将设置为发票 ID,一次性付款时将设置为 null.注意:如果您还使用一次性发票,您也需要处理这种区别(检索发票并检查subscription
属性). - 为您的一次性付款意向设置一些
元数据
.例如,设置'metadata' =>['曾经'=>'true']
并在您的事件处理程序中检查它.
- Check the
invoice
attribute on the payment intent object. This will be set to an invoice ID when related to a subscription payment, and null for your one-time payments. Note: if you also use one-off invoices you'll need to handle that distinction too (retrieve the invoice and check thesubscription
attribute). - Set some
metadata
on your one-time payment intents. For example, set'metadata' => [ 'onetime' => 'true']
and inspect this in your event handler.
这篇关于当我也有一次性费用时,如何处理 Stripe webhook 以支付订阅费用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!