4

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?

zangarmarsh
  • 319
  • 5
  • 18
  • Just want to double check - you're sure that the user you get from `User::first()` is the same user you're creating the subscription for right? – karbi Oct 20 '21 at 00:47
  • Did you set up webhooks? – NerdOfLinux Oct 20 '21 at 02:31
  • @karbi there's only one user in my dev environment, hence yes I'm sure. But I have to admin I doublechecked again. – zangarmarsh Oct 20 '21 at 16:45
  • @NerdOfLinux I didn't. Quoting the laravel doc: "By default, a route that points to Cashier's webhook controller is automatically registered by the Cashier service provider. This controller will handle all incoming webhook requests. By default, the Cashier webhook controller will automatically handle cancelling subscriptions that have too many failed charges" I thought it handled it automatically, creating a "default" subscription as declared in the controller. For your experience, am I wrong? Should I handle the events with a custom listener? – zangarmarsh Oct 20 '21 at 16:47
  • You don't need to set up a customer listener, but you do need to make sure the route is registered (`php artisan route:list`), and update your Stripe settings to send webhooks to your app. – NerdOfLinux Oct 20 '21 at 20:50

1 Answers1

2

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.

ob1y2k
  • 45
  • 5