Stripe Subscriptions: Getting Started, Cancel, Completed, Trials, Grace Periods...

We are going to give an introduction to the use of subscriptions, learning about their basic use and main methods and management.

We are going to get into an interesting topic which would be subscriptions, which, if you're not redundant, would be to subscribe, that is, to make recurring payments, so here you can see an implementation. In this case, we have the user and we indicate New subscription:

Route::post('/user/subscribe', function (Request $request) {
    $request->user()->newSubscription(
        'default', 'price_monthly'
    )->create($request->paymentMethodId);

We indicate what the plan and price would be and so here we create it through some payment method it would be And from here we have a lot of information for example we can make trial subscriptions the days that this will last is for the invoice part that I am not really going to cover in this section or in this course the coupons and others well that I generally would handle coupons directly in the application but you can also generate or manage it directly from stripe that is to say if I would do the discount before establishing it here based on some condition there you have like the part of them you put them and we also have a tral over there that I don't know where it is we are going to continue going down this is also interesting to know if the user is already subscribed So it is a bit of what was commented before in which we already have Practically everything

Check subscription

So if the user is already subscribed we simply leave everything enabled and when he is no longer subscribed either because he unsubscribed or his time ran out or he has simply never been subscribed, he simply would not enter here:

if ($user->subscribed('default')) {
    // ...
}

So in the end they are all conditional. On the one hand we subscribe and on the other hand we always check if the subscriber is subscribed. It's that simple. We completely remove ourselves from all the logic for the subscription part. That is, implementing all that manually. Here is the part that I was telling you about, to give yes to know if it is in its test days.

In case you want to manage it as well. Otherwise, there is not much information here. It is really a bit abstract. Searching a little on Google. Here, for example, we can see some implementations from the community. You can see that what should be placed would be a product ID and a plan ID. Here, we have another one that is a little more complete. Here, for example, if you put some shipping information, it would be, but otherwise, here, also establish the same parameters. In this case, it would be the return URLs. In other words, we can establish multiple parameters. So, at this point, we are going to start. Also, remember that you should have a method enabled.

Create a recurring price in the Stripe dashboard

The first thing you need to do is create a recurring price as shown in Figure 20-12, remember that you need to set up a recurring payment and NOT a one-time payment:

https://dashboard.stripe.com/dashboard

And you copy the price ID:

route::get('/stripe/new-subcription', function () {
    $user = User::find(1);

    dd(
        $user->newSubscription(
            'default',
            'YOUR_RECURRENT_PRICE_ID'
        )->create('<USER_PAYMENT_METHOD_ID>')
    );
});

The label or where it says default, you can put anything since it is exactly as indicated to be used internally, it is a kind of label you could say

Other important methods

Here you have a little information, it is precisely because it has a grace period that is known, that is to say, if the user paid, they still have it here, you can read it, you can also ask in this way here:

$user->subscription('default')->onGracePeriod()

Even if you cancel the subscription, the user must enjoy the days that he or she has just paid for.

If the subscription is already paid for:

$user->subscription('default')->recurring()

User can cancel subscription:

$user->subscription('swimming')->cancel();

- Andrés Cruz

En español

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.

!Courses from!

10$

On Udemy

There are 0d 03:50!


Udemy

!Courses from!

4$

In Academy

View courses

!Books from!

1$

View books
¡Become an affiliate on Gumroad!