Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

singh's avatar
Level 1

Laravel 8 Stripe Syncing up with the Subscription Table in Real Time

I am aware that Laravel has the following function for to sync the subscriptions table with Cashier/Stipe.

protected static function booted() { static::updated(queueable(function ($customer) { $customer->syncStripeCustomerDetails(); })); }

However, this method is dependent on queues. However, in certain cases - such as Plan Cancellation or Upgrade a user may want to see the updates right away and that requires syncing in the real time. Can this be done in the controller? For example lets say I switch a user to a different plan. that is effective immediately by using the following code; How do I make a sync immediately after the successful upgrade so that the user can see their new plan in the user dashboard of my app once they are redirected to it. $user->subscription('default')->swapAndInvoice('price_yearly');

0 likes
8 replies
martinbean's avatar

@singh The subscription will be updated via a webhook Stripe sends to your server.

martinbean's avatar

@singh What do you mean, how does that work? It’s how most things in Cashier work. You do something, Stripe sends a webhook event to your server’s webhook endpoint and your database is updated.

singh's avatar
Level 1

@martinbean Fair enough. But in this case, it hasn't happened. When I sign up for a new plan or cancel one it updates the Subscriptions table. However, if I do a swapAndInvoice - which should have an immediate effect it doesn't. Basically swapAndInvoice should cancel the original subscription immediately and upgrade to the new one. I can see the changes in the stripe dashboard but not in my subscription table; I think there is probably another step that I am missing.

singh's avatar
Level 1

Additional details i posted on github.

Cashier Version: 13.5 Laravel Version: 8 PHP Version: 8.0.11 Database Driver & Version: 8.0.23 Apache, Linux, Mysql Description: I have multiple subscription plans. A user can switch them from a lower tier to a higher tier. $oldname below is the name of the existing plan. and $plan, $newprice, $newname all correspond to the new plan and price that user would like to update too. I have tried several variations of this code. But swapAndInvoice doesn't update the database despite showing the updates in Stripe console.

    $plan = Plan::where('stripe_name', $request->plan)->first();  // queries mysql database
   $oldplan = Plan::where('stripe_name', $stripename)->first();  // queries mysql database
   $user->subscription($oldname)->swapAndInvoice($newprice);  // swaps the plan - charges correctly
    $user->subscription($oldname)->name = $newname;  // attempt to rename the plan in my susbscriptions database
    $user->subscription($oldname)->save();  //  Error:  Call to a member function save() on null 

even tried the solution posted by @driesvints #962

But I had no luck. Contrary to this experience with swapAndInvoice and swap other calls work fine.

For example: $user->subscription($plan->stripe_name)->resume(); // resumes a canceled subscription $user->subscription($plan->stripe_name)->cancel(); // cancels a subscription. $user->newSubscription($plan->stripe_name, $plan->stripe_price_id)->create($paymentMethod); // creates a new one

Only swapAndInvoice and swap do not update my database in anyway. They should be through webhooks.

singh's avatar
singh
OP
Best Answer
Level 1

After doing additional search - it updates the stripe_price field in the db but not the name . That throws off what my customers will see.

Temporary Hack:

            $user->subscription($oldname)->swapAndInvoice($newpriceid);

             DB::table('subscriptions')
                            ->where('user_id', $user->id)->where('stripe_status', 'active')->where('name', $oldname)
                            ->update([
                                               'name' => $newname,
                             ]);

II have requested the Laravel team to see if they can treat this as a bug. Cashier should be able to update the Subscription table upon an upgrade. Otherwise this requires querying for the stripe_price to determine what plan user has been subscribed to.

AlexElementarteilchen's avatar

Hi,

I have been struggling with Stripe's product and price concept for a bit myself until I finally (fully) understood how products and prices are connected (yes in hindsight it is totally obvious but... )

In the stripe docs it says:

Products define what you’re selling and prices define how to charge for the product. For example, if you offer photo hosting for 15 USD a month, photo hosting is your product and the 15 USD a month is your price.

And

Products can have multiple prices so you can vary pricing by billing interval, currency, or amount.

So "upgrading" means you need to change the product. Changing the price would mean you stay on the same product but now pay a different price for it. For example when switching from "pay 10 $ per month" to "pay 100$ per year" (for the same product).

https://stripe.com/docs/billing/subscriptions/model

singh's avatar
Level 1

@AlexElementarteilchen Thanks for these highlights. In my case Stripe's dashboard upgrades the plan and charges additional fees. The webhook does not fully update the subscription table. Anyways, I have a work around for my issue and hopefully others will be able to benefit from it too.

Please or to participate in this conversation.