用户第一次支付订阅费用时进行条带化

时间:2021-03-26 07:16:29

标签: stripe-payments

如果这是该特定客户第一次为此特定订阅付费。 我必须根据选择的计划 ID 设置订阅的 cancel_at 字段。 (我不想在创建订阅时设置它)。为了解决这个第一次,我捕获了 invoice.payment_succeeded 事件,并执行了以下操作:

 $invoices = \Stripe\Invoice::all([
                    "limit" => 3,  //no need to get all of the invoices just checking if their number is = 1.
                    "status" => 'paid',
                    "customer" => $customer_id,
                    "subscription" => $subscription_id,
                    "expand" => ["data.charge"],
                  ]);
 if ( count($invoices->data) == 1 ) {
            //First time this user pays for this specific subscription;

你能告诉我它是否正确,或者我是否遗漏了什么,因为我是 php 和 stripe 新手。我应该做 count($invoices) 而不是我所做的吗?

1 个答案:

答案 0 :(得分:1)

您的代码看起来不错,但判断它是否有效的唯一方法是让您运行它,看看它是否按照您的预期运行。您可以使用您的测试模式 API 密钥和 Stripe CLI 来测试流程:

stripe trigger customer.subscription.created

这将创建一个测试客户、一个计划和一个订阅。后者将立即创建并支付发票,因此您应该获得一个 invoice.payment_succeeded 网络钩子事件以进行测试。