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.
26 episodes2 hrs 38 mins

Overview

Learn to start accepting subscriptions with Laravel and build a solid foundation for your next application. In fact, it’s exactly how we do things here on Codecourse!

Even if you’re completely new to Laravel, by the end of this course you’ll have a platform where customers can purchase subscriptions and gain access to restricted areas of your application.

We’ll also cover managing subscriptions, cancellations, invoices, giving customers trial periods — and more.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!