【问题标题】:Error creating paid plan with trial Laravel Cashier使用 Laravel Cashier 试用版创建付费计划时出错
【发布时间】:2019-03-18 14:46:32
【问题描述】:

简单的问题,但我无法终生解决这个问题。

我的 Stripe 帐户有 3 个订阅,一个是免费的,两个是付费的,有 14 天的试用期(在 Stripe 中设置)。一切正常,除了当我立即为新用户订阅付费试用计划时,我不断收到关于客户没有存档付款来源的错误消息。我的代码如下所示:

// Create new User
$user = new Account($validatedUserData);

// Create Stripe user and subscribe to plan. Laravel automatically adds Stripe Customer ID 
// Free plan, create customer and subscribe to free plan in one line of code 
if ($planInRequest == 1) { 
    $user->newSubscription('My Subscription', 'Free Plan')->create(null, ['email' => $user->email]); 
} 

// If Paid plan...
else { 
     // Create user in Stripe first (this works) 
     $user->createAsStripeCustomer(null);

     // Subscribe user to paid plan with trial period
     $user->newSubscription('My Subscription', $planToUse)->create(null, ['email' => $user->email]); }

但是,当我运行此代码时,我得到了错误

客户不存在付款来源。

我知道我还没有向客户添加 Stripe Source(因此传递 null),但我想如果有免费试用期,我可以暂时订阅他们。我也尝试不将 null 传递给付费计划订阅 (as per this SO post),但这给了我一个无效令牌错误。

我的用户模型还包含 Laravel 文档推荐的“trial_ends_at”、“updated_at”和“created_at”字段。 Stripe 客户添加得很好,当我尝试在 Stripe Dashboard 中向新客户添加付费订阅时,一切正常。

有人有什么想法吗?非常感谢您的帮助!

【问题讨论】:

  • 问题是 Stripe 的 API 默认忽略试用期。您必须明确传递 trial_from_plan: true 。我不知道 Laravel Cashier 是否支持这个功能
  • 嗯,有趣,我会进一步研究并报告。

标签: php laravel laravel-5 stripe-payments laravel-cashier


【解决方案1】:

在挖掘了 Cashier 的 Billable、Subscription 和 SubscriptionBuilder 控制器之后,我发现我必须将以下代码用于我的试用计划,并在此处发布以防其他人遇到同样的问题!

$user->newSubscription('My Subscription', $planToUse)->trialUntil($user->trial_ends_at)->create(null, [
                'email' => $user->email,
                'description' => $user->name
            ]);

【讨论】:

    猜你喜欢
    • 2015-12-22
    • 2014-10-09
    • 2020-06-09
    • 2021-06-12
    • 2021-09-22
    • 2016-03-15
    • 2022-01-01
    • 2021-02-27
    • 2022-11-18
    相关资源
    最近更新 更多