用户第一次为订阅付费时条纹

stripe first time the user pays for a subscription

如果这是该特定客户第一次为此特定订阅付费。 我必须根据所选计划 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) 而不是我做的吗?

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

stripe trigger customer.subscription.created

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