laravel / cashier-stripe

Laravel Cashier provides an expressive, fluent interface to Stripe's subscription billing services.
https://laravel.com/docs/billing
MIT License
2.36k stars 667 forks source link

Need current_period_end and current_period_start column in subscriptions table #515

Closed tareqtms closed 5 years ago

tareqtms commented 6 years ago

After a user cancels the subscription, I need to show the user how many days remaining for the current period. There is no column for those values in the subscriptions table.

How can I achieve this?

kyranb commented 6 years ago

You can compare the cancels at column to the current date/time using carbon's difference methods.

On Thu., 10 May 2018, 6:16 pm Tareq Mahmood, notifications@github.com wrote:

After a user cancels the subscription, I need to show the user how many days remaining for the current period. There is no column for those values in the subscriptions table.

How can I achieve this?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/laravel/cashier/issues/515, or mute the thread https://github.com/notifications/unsubscribe-auth/AFLO7ro2JXNBl4JkNd3yxd1zsUAgEvNTks5tw_dcgaJpZM4T5iAB .

tareqtms commented 6 years ago

Ok, that will solve my initial issue. What if I want to show the next billing date to the user (when the subscription is not canceled)?

tareqtms commented 6 years ago

Basically, I need to know, is there any way to add any additional fields to the subscriptions table, if I feel the need for any additional data (Which is available in Stripe Subscription model)?

coryrose1 commented 6 years ago

Hi @tareqtms

I'm sure you've moved on from this issue but I found it searching for the same thing.

It's actually simpler than expected. Simply add a field to the subscriptions table using a new migration (or even manually works).

You can then update the field like so:

$user->subscription('main')->new_field_name = 'new_value'; $user->subscription('main')->save();

driesvints commented 5 years ago

Think this issue can be closed by using the examples provided here. Feel free to provide additional feedback if you want.

stueynet commented 5 years ago

Sorry to ressurect this but the above examples don't actually solve much as far as I can tell. current_period_end is a field provided by the stripe API. It is referenced in the Cashier code here: https://github.com/laravel/cashier/blob/master/src/Subscription.php#L309 however it is not accessible via the cashier subscription object which only gives you:

    "id" => 123
    "user_id" => 123
    "name" => "default"
    "stripe_id" => "sub_123"
    "stripe_plan" => "name_of_plan"
    "quantity" => 1
    "trial_ends_at" => null
    "ends_at" => null
    "created_at" => "2018-10-13 16:51:23"
    "updated_at" => "2018-10-13 16:57:09"

If people want to get the actual subscription from Stripe you can do:

return \Stripe\Subscription::retrieve($id);
driesvints commented 5 years ago

@stueynet you can do $user->subscription()->asStripeSubscription()->current_period_end.

stueynet commented 5 years ago

Oh wow thanks @driesvints thats the method I need.

NassimRehali15 commented 5 years ago

https://gist.github.com/NassimRehali15/415939b46a70dfc0cb92175bdb467116

yoeriboven commented 5 years ago

@driesvints This would mean my app has to connect to the Stripe API every time a user wants to see the end of their subscription.

Using @NassimRehali15 solution means the date is incorrect until the command is run.

What's the reason for not putting this in a column on the subscriptions table? It's a value used in almost every app.

I was going to issue a PR until I saw this thread.

driesvints commented 5 years ago

It's a value used in almost every app.

I've never had to use this in my apps and neither do we use this in Spark afaik. I think this is a bit of an objective opinion. You're free to add the code from your PR to your own app. But I think adding this to the core isn't interesting to everyone out there. It's just not feasible to add every single field of a subscription to the subscriptions table.

Try the following code snippet by @coryrose1 after creating a subscription and then overwrite the webhook in our own controller.

$user->subscription('main')->new_field_name = 'new_value';
$user->subscription('main')->save();
johnwc commented 2 years ago

@driesvints We are in need for this to be in the database, so that we are able to do a join query with our other tables to quickly count how many credits a customer has until next billing cycle. What do you suggest would be the best way of getting this value updated when the billing cycle updates in stripe? You mention overwriting the webhook, have a quick example of this? Do you know what webhook we'd need to subscribe to in Stripe to get updated on each new billing cycle?

bokele3 commented 1 year ago

$user->asStripeSubscription()->current_period_end