zangarmarsh
zangarmarsh

Reputation: 359

Laravel cashier it's not creating automatically the subscription

This is the code:

<?php

namespace App\Http\Controllers;

use Inertia\Inertia;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class BillingController extends Controller
{
    public function index(Request $request) {
        return Auth::user()
            ->newSubscription('default', 'price_#################')
            ->checkout();
    }
}

It correctly redirects me on stripe's checkout page, it correctly handles the payment and redirects me back on my website. I can actually see the active subscriptions in stripe's panel but I have no active subscription on my website's account.

>>> User::first()->subscriptions;
=> Illuminate\Database\Eloquent\Collection {#4476
     all: [],
   }
>>> User::first()->subscribed('default');
=> false

There's a point in the Laravel docs which says it automatically handles the subscription association (through a magic internal method I guess) but it's not working out.

Stripe's CLI caught these events at my payment action:

2021-10-19 23:34:16   --> charge.succeeded [evt_############]
2021-10-19 23:34:16   --> checkout.session.completed [evt_############]
2021-10-19 23:34:16   --> invoice.created [evt_############]
2021-10-19 23:34:16   --> invoice.finalized [evt_############]
2021-10-19 23:34:16   --> customer.subscription.created [evt_############]
2021-10-19 23:34:17   --> invoice.updated [evt_############]
2021-10-19 23:34:17   --> customer.subscription.updated [evt_############]
2021-10-19 23:34:17   --> invoice.paid [evt_############]
2021-10-19 23:34:17   --> invoice.payment_succeeded [evt_############]
2021-10-19 23:34:17   --> payment_intent.succeeded [evt_############]
2021-10-19 23:34:17   --> payment_intent.created [evt_############]

What am I missing? Should I manually listen for customer.subscription.created and use my own logic?

Upvotes: 6

Views: 1962

Answers (1)

ob1y2k
ob1y2k

Reputation: 75

You are doing things good, but you will need to update your WebhookController.php code with new/updated code from \vendor\laravel\cashier\src\Http\Controllers\WebhookController.php

i.e if you inspect those 2 files, you will notice that functions handleCustomerSubscriptionCreated and handleCustomerSubscriptionUpdated have been changed in order to reflect subscription items support, etc.

so copy those functions to yours WebhookController.php and update them for your need if you have some custom logic in them.

Upvotes: 3

Related Questions