2

我使用 Stripe 来管理订阅服务,并使用 Laravel Cashier 作为 Stripe 服务的 API 接口。

我有一种情况,我想为用户注册一项服务,然后偶尔将他们的订阅延长到原始订阅结束日期之后任意数量。Stripe 支持这一点,推荐的做法是使用“订阅更新”端点并传递一个新trial_end值和一个proratefalse/null 值。

我想知道 Laravel Cashier 是否支持此功能。我试过了:

$user->subscription()->noProrate()->trialFor($newSubscriptionEndDate); 

但是,我的条纹仪表板似乎没有显示更改注册。

这是我可以专门使用 Cashier 完成的事情,还是我需要使用原生 Stripe API?该StripeGateway课程确实有许多与试验和结束日期有关的方法,但我在解读它们的预期功能时遇到了麻烦。谢谢。

4

2 回答 2

2

我相信您之后需要使用交换,例如

$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();

然后您可以在 Cashier 中手动更新它:

$user->setTrialEndDate( $date )->save();
于 2015-11-20T10:55:24.543 回答
0

您可以在模型中编写一个新函数(您Billable可能在其中使用了 trait User):

class User extends Model implements BillableContract
{
    use Billable;

    public function MyCustomeCashierFunction($newSubscriptionEndDate)
    {
        retrun $this->subscription()->noProrate()->trialFor($newSubscriptionEndDate); 
    }
}

然后你可以在你的控制器中使用这个函数和你的模型对象:

$user->MyCustomeCashierFunction($newSubscriptionEndDate);

编辑:

如果该功能不起作用,也试试这个:

public function MyCustomeCashierFunction($newSubscriptionEndDate)
{
    $stripe_gateway = new StripeGateway($this);

    retrun $stripe_gateway->subscription()->noProrate()->trialFor($newSubscriptionEndDate); 
}

请让我知道它是否有效。

于 2015-11-26T05:54:06.217 回答