This episode is for members only

Sign up to access "Laravel Subscriptions" right now.

Get started
Already a member? Sign in to continue
Playing
17. Syncing customer details

Transcript

00:00
OK, so I've gone ahead and signed up for another subscription and we're going to look at something slightly different now, but potentially really important, and that is keeping our details over in our profile area or the account that we have on this site up to date with Stripe.
00:14
If, for example, I change my email address and hit save, this is changed in the database, but it wouldn't have been changed over on Stripe. This can be incredibly important, particularly if you have any kind of integrations with Stripe for tax purposes or anything like that where you need everything to be kept up to date. Let's look at how we do that now.
00:35
OK, I'm going to change my email address back to Alex and let's go ahead and create an observer for our user for when any of our user details are updated. So let's go and create out an observer. So let's say make observer. We don't need to use an observer, but find it a little bit cleaner. Once that user observer has been created over in app observers, we can open this up and hook
00:58
into the updated eloquent event. So when this user gets updated, we want to do something. Let's pull in the instance of the user that's updated here. And this is where we want to sync the details with Stripe. How do we do that? Well, the first thing that we want to check is if the user actually has a Stripe ID. So even if they don't have a subscription, we can't update
01:23
them over on Stripe if they don't have a customer ID. Remember that exists over on the users table just over here, which is what was added when we installed cache here. So if they do have a Stripe ID, how do we check that? Well, we can just say user has Stripe ID. Again, that comes from that billable trade. So if they have ever been a customer and they have a Stripe ID, we can actually
01:48
use a really simple method here. This is sync Stripe customer details. Let's dive into that and see what this looks like. OK, so what this does is it updates the Stripe customer on the API using all of the information that we have on Stripe that we can add using the data that comes from our database. So Stripe name, Stripe email, Stripe phone, Stripe address, all of that
02:11
information. If you dive into these, you can see that by default, this just returns an empty array. But you can take the Stripe address, for example, and you could add it to your user model. So if you always wanted to return a specific address here from your user, depending on how your database is structured, just return the address in here with the line one, whatever Stripe's API accepts. And
02:38
that will be overridden and it will be updated whenever your address gets updated. OK, finally, let's just go over to our user model and let's make sure this observer gets hooked up. We can use the observed by trait for this. And in here, we can just pass in an array of observers. In our case, that is the user observer that we've just created. So now anytime this gets updated, this
02:59
user observer updated method will get triggered. And if the Stripe user is a Stripe customer, the details will get updated. Let's try this out. So I'm going to go ahead and set my name to Alexander, change my email address as well, hit save. That will go ahead and hopefully update this over on Stripe for us as well and keep everything up to date.

Episode summary

In this episode, we focus on something super important for anyone using Stripe for their app: keeping your customer details in sync between your app and Stripe. Picture this—you’ve got a profile page, the user updates their email address, and now your app has the right info... but Stripe doesn’t (yet!). That can lead to trouble if you rely on Stripe for things like receipts, email notifications, or tax information.

We walk through the process of updating user details and making sure those changes automatically get reflected in Stripe whenever the user updates their profile. The magic happens with an Eloquent observer—we set it up so that whenever a user's details are changed in your app, the observer checks if they’re a Stripe customer (i.e., they have a Stripe ID), and if so, calls syncStripeCustomerDetails. This handy method will update the relevant info like email, name, phone, and even address on the Stripe side, ensuring everything’s always up-to-date.

You'll see how to customize the data that’s sent to Stripe (for example, if you want to include address details), and how to hook up your user model with the observer, so these syncs happen automatically. At the end, we do a test update on the profile and check that everything reflects correctly in Stripe. It's a practical workflow you'll want in any real-world subscription or billing setup!

Episode discussion

No comments, yet. Be the first!