L5 How to handle forever free subscription Stipe

amar 2 months ago | Last updated 2 months ago

Hi, This course is handsome, but i got some questions. First, what's the best way to handle forever free subscription after signing in ? I've found somethink like this:

$user = new User($request->all());
// let's set up some basic details for this Stripe customer
        // these fields are based on the Stripe API documentation
        $details = [
            'plan' => "free",
            'email' => $user->email,
            // the rest is optional, you can add whatever metadata you like
            'metadata' => [
                'user_id' => $user->id,
                'first_name' => $user->first_name,
                'last_name' => $user->last_name
            ]
        ];

        // createStripeCustomer($token, $properties) is an undocumented Laravel
        // subscription function. As the function name implies it allows you
        // to manually create a Stripe customer.
        $customer = $user->subscription()->createStripeCustomer(null, $details);

        $subscriptions = Collection::make($customer->subscriptions->data);
        $subscription = $subscriptions->last();

        // since we are bypassing the standard Laravel subscription model
        // we need to manually update the user table with the Stripe details
        $user->stripe_active = true;
        $user->stripe_id = $customer->id;
        $user->stripe_subscription = $subscription->id;
        $user->stripe_plan = "free";

        // save the user object
        $user->save();

Thanks in advance