我使用 Stripe 来管理订阅服务,并使用 Laravel Cashier 作为 Stripe 服务的 API 接口(interface)。
我遇到过这样的情况:我想要让用户注册某项服务,然后偶尔将其订阅延长至原始订阅结束日期之后任意金额。这是由 Stripe 支持的,and the recommended way of doing so是使用“订阅更新”端点并传递新的 Trial_end 值和 false/null 的按比例分配值。
我想知道 Laravel Cashier 是否支持此功能。我试过:
$user->subscription()->noProrate()->trialFor($newSubscriptionEndDate);
但是,我的 Stripe 仪表板似乎没有显示正在注册的更改。
这是我可以专门使用 Cashier 完成的事情还是我需要使用 native Stripe API? StripeGateway 类确实有许多与试验和结束日期相关的方法,但我在破译它们的预期功能时遇到了困难。谢谢。
最佳答案
我相信您之后需要使用交换,例如
$user->subscription()->noProrate()->trialFor($newSubscriptionEndDate)->swap();
您可以在此处查看交换的作用:
https://github.com/laravel/cashier/blob/5.0/src/Laravel/Cashier/StripeGateway.php#L181-L215
它返回 create 方法,如果传递了客户对象,该方法也会实际更新,所以我假设如果没有这个方法,您的更改将不会到达 Stripe。
这是对某人使用与 swap() 相同的方法的引用 https://github.com/laravel/cashier/pull/142
如果您在本地数据库中保存新的试用日期时遇到问题,您的 Billable 特征(在您的用户模型中使用)中有一个 setTrialEndDate 方法,您可以在此处看到:
https://github.com/laravel/cashier/blob/5.0/src/Laravel/Cashier/Billable.php#L437-L448
您应该能够像这样使用它:
$user->setTrialEndDate( $date )->save();
编辑
// getStripeKey is a static method
\Stripe\Stripe::setApiKey( $user::getStripeKey() );
// Get stripe id for customer using public method
$cu = \Stripe\Customer::retrieve( $user->getStripeId() );
// Get current stripe subscription id using public method
$subscription = $cu->subscriptions->retrieve( $user->getStripeSubscription() );
// Update trial end date and save
$subscription->trial_end = $date;
$subscription->save();
然后您可以在收银台中手动更新它:
$user->setTrialEndDate( $date )->save();
关于php - "Extend"用户使用 Laravel 收银员订阅 Stripe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33776484/