#Yul
1 messages ยท Page 1 of 1 (latest)
My system is built on PHP
Hello ๐ We have solids docs around updating subscriptions, Have you looked at these docs already?
https://stripe.com/docs/billing/subscriptions/upgrade-downgrade
Well, yeah, I believe it, but I'm lazy. Can't get to the point..
๐ฆ
I know how callbacks work and my task is quite precise and simple. https://stripe.com/docs/billing this is the one I should be looking at, right?
for subscription docs? yes
You can also just look at the sidebar
Is it mandatory to create a customer for each subscription payment?
Yeah without a customer the subscription can't exist as such
https://stripe.com/docs/billing/customer
this is the only place where I've found that I can add my customer_id to the params
On the webhook part (callback): how do I distinguish to whom of my customers I should update the subscription?
I'm looking at this one currently: https://stripe.com/docs/billing/subscriptions/build-subscriptions?ui=checkout#provision-and-monitor
"You should provision the subscription and save the customer ID to your database." - what customer ID? From your side or from mine?
From Stripe
Okay and how do I know who is this on my system?
Your code creates the customer on Stripe so you should be able to add a reference to your local user ID in a field such as metadata
this stripe customer ID is different for each payment?
Even if the user is reccuring? For example, he just renews the subscription..
It shouldn't be different, no.
The technique is:
user init payment: I create stripe customer, update reference on my side, redirect to payment
->
The I receive the callback with this reference and in this way I can distinguish who it is...
hold on, let's take a step back.
What's the end goal here? What problem are you trying to solve?
Is it just keeping Stripe customer and your local customer in-sync OR something else?
yes
exactly
To update his subscription on my end if he paid..
He's a customer, he's already already in my mysql db and he goes to pay for the subscription, he's logged in ... To be precise
In that case here's how I would handle it,
1/ Create a customer before creating the subscription and store their customer ID in local database
2/ Listen for subscription related webhook events
3/ When I receive invoice.paid , I'll look at the associated customer ID and look for the same in my local database
4/ Once I find them, I'll provision access.
Above handles provision asynchronously
Yeah.. If the customer wants to upgrade the subscription... I created subscriptions as products. Would I have to create a new customer?
Nope, you'd just update the existing one as shown in the examples here
https://stripe.com/docs/billing/subscriptions/upgrade-downgrade
So, before \Stripe\Checkout\Session::create I need to $stripe->customers->create() ?
Yeah
And how do I pass this customer to checkout session ?
there's a customer parameter where you can set the existing customer ID when creating a Checkout Session
https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-customer
So, I can for example,
$session = \Stripe\Checkout\Session::create([
'customer' => '12345',
'success_url' => 'https://example.com/success.html?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => 'https://example.com/canceled.html',
'mode' => 'subscription',
'line_items' => [[
'price' => $priceId,
// For metered billing, do not pass quantity
'quantity' => 1,
]],
]);
?
Yes, more like 'customer' => 'cus_xxx'
since customer objects on Stripe have cus_ prefix ๐
Do I have to check if customer already have in my db cus_xxx? Do I have to check it before each payment attempt?
It depends on your usecase, but I guess yes? So that you don't have two customers with the same cus_xxx in your local db
Well, no, I will simply rewrite the cus_xxx if it already exists for some customer
If I'm not going to check it
Is it possible stripe to create a duplicate cus_xxx ?
usually all the customer ID's are unique..
Hello! I'm taking over and catching up...
Hi hi
Customer IDs in Stripe are unique across your account. You won't see the same cus_ ID more than once across your account's Customers.
Ok, the technique is clear for me I think:
- I check if the customer in my local db already has the cus_ID
- I not then I create stripe customer. Get the cus_ID.
- Then I
$session = \Stripe\Checkout\Session::create([
'customer' => 'cus_ID',
'success_url' => 'https://example.com/success.html?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => 'https://example.com/canceled.html',
'mode' => 'subscription',
'line_items' => [[
'price' => $priceId,
// For metered billing, do not pass quantity
'quantity' => 1,
]],
]); - Then he pays, I receive the callback and by this cus_
- Then he pays, I receive the callback and by this cusID I know who he is and able to update the subscription...
Oh and 2.1. I update the cus_ID for this customer on my local db
But how do I know which subscription did he bought? Will I have lookup_key in the callback?
When a Subscription is created there are several Events that fire, and they describe the different objects involved (the Subscription itself, the first Invoice, etc.). You can listen for the Events you want in order to get the information you need: https://stripe.com/docs/billing/subscriptions/overview#subscription-events
Ok!