#sergixel04
1 messages · Page 1 of 1 (latest)
Hello sergixel04, we'll be with you shortly! Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.
• sergixel04, 1 hour ago, 13 messages
• sergixel04, 2 days ago, 23 messages
• sergixel04, 5 days ago, 37 messages
• sergixel04, 5 days ago, 78 messages
$createdSubscription = $stripe->subscriptions->create([
'customer' => $user->stripe_id,
'items' => [
['price' => $priceId],
],
'description' => 'Extra business: ' . $request->name,
]);
if ($createdSubscription->status !== 'active') {
return back()->with('error', 'The payment could not be completed. Please try again or contact support.');
}
Where did you find this code snippet?
Got payment_intent.canceled, but the customer has a payment method available
I did it
Could you please share an example Subscription ID?
Sure! pi_3O5SemKYINwhn6n419QHa8QH
In my log I got
{"error":"This customer has no attached payment source or default payment method. Please consider adding a default payment method. For more information, visit https://stripe.com/docs/billing/subscriptions/payment-methods-setting#payment-method-priority."}
But don't understand this error. Because the user has a payment source
cus_OtF3QA4Ab2HJNn
Even this test customer has one subscription. Just trying to add another different subscription to this customer
Sources is a legacy concept, you need to either set Customer.invoice_settings.default_payment_method, or set Subscription.default_payment_method when creating it.
the problem is that the first subscription is always using Stripe Checkout interface, don't know how to handle to automatically set the card the user used to default payment method
I think a solution maybe is, in this code, to detect if a user has a payment method, if not, catch every payment source, and the first one select it as default payment method
Do you know how can I get every payment source of a user?
And how to set a default payment method?
What do you mean by "first subscription"?
When you register in the SaaS you have to suscribe for an acount (first sub).
If u want more accounts, you suscribe for more
This sounds like you need a single Subscription with different quantity of items: https://stripe.com/docs/api/subscriptions/create#create_subscription-items-quantity
It has to be independent subscriptions and invoices. So if a user wants 8 accounts, there is going to be a main subscription and 7 other subscriptions
But don't know why I am getting that error. In the past, I had it with subscription quantities, and when updating the subscription and charging I didn't get any error of payment intent
👋 stepping in as vanya needs to step away
When you create a Subscription with Checkout, that sets the default_payment_method on the Subscription itself. You are asking how to then set this PaymentMethod as the Customer's default PM overall so it is used for other Subscriptions?
yes!
So it sounds like what you mostly want to do in that case is listen for checkout.session.completed and then grab the PaymentMethod that is associated with that Checkout Session and update the Customer to set the invoice_settings.default_payment_method
which field in checkout.session.completed webhook is the default payment method?
Well, I mean the payment inetntion to make it default
You can retrieve the Checkout Session and expand the Subscription
To see the PaymentMethod that was set as the default_payment_method on the Subscription
what API I have to use to do that?
For example I have this checkout sesssion id (not sure if I can share this) cs_test_b14DllO9WoghxJqsE5UKdpJd8i92aMIFfDSUoUfVmGLJSEZh5m7eMYqEZW
Yes it is fine to share object IDs. They are useless without your secret key
You would use this API: https://stripe.com/docs/api/checkout/sessions/retrieve
And here are docs on how expanding responses work: https://stripe.com/docs/expand
Thank you. Alright I just see a problem and the checkout.session.completed the payment_intent ID is null. The response I am looking at, is the one of the first subscription I told you (with Stripe Checkout). Here it is a part of it:
"mode": "subscription",
"payment_intent": null,
"payment_link": null,
"payment_method_collection": "always",
"payment_method_configuration_details": null,
"payment_method_options": null,
"payment_method_types": [
"card"
],
"payment_status": "paid",
"phone_number_collection": {
"enabled": false
},
"recovered_from": null,
"setup_intent": null,
"shipping": null,
"shipping_address_collection": null,
"shipping_options": [
],
"shipping_rate": null,
"status": "complete",
"submit_type": null,
"subscription": "sub_1O5SZsKYINwhn6n4ijoOswXY",
Yep so see how it has a Subscription ID?
When you retrieve the Checkout Session you want to use expand: ['subscription']
Then instead of just seeing the ID there you will see the entire Subscription object
This is what is explained in https://stripe.com/docs/expand
return $stripe->checkout->sessions->retrieve(
'cs_test_b14DllO9WoghxJqsE5UKdpJd8i92aMIFfDSUoUfVmGLJSEZh5m7eMYqEZW',
['expand' => ['subscription']]
);
"subscription": {
"id": "sub_1O5SZsKYINwhn6n4ijoOswXY",
"object": "subscription",
"application": null,
"application_fee_percent": null,
"automatic_tax": {
"enabled": false
},
"billing_cycle_anchor": 1698322940,
"billing_thresholds": null,
"cancel_at": null,
"cancel_at_period_end": false,
"canceled_at": null,
"cancellation_details": {
"comment": null,
"feedback": null,
"reason": null
},
"collection_method": "charge_automatically",
"created": 1698322940,
"currency": "eur",
"current_period_end": 1701001340,
"current_period_start": 1698322940,
"customer": "cus_OtF3QA4Ab2HJNn",
"days_until_due": null,
"default_payment_method": "pm_1O5SZrKYINwhn6n4AWSKC8WM",
"default_source": null,
Alright I think there it is. Now I would have to take that default payment method and use establish it as defauly payment method of user right, so other subscription will use it too?
Yep
You update the Customer and set it here: https://stripe.com/docs/api/customers/update#update_customer-invoice_settings-default_payment_method
$checkoutSession = $stripe->checkout->sessions->retrieve(
'cs_test_b14DllO9WoghxJqsE5UKdpJd8i92aMIFfDSUoUfVmGLJSEZh5m7eMYqEZW',
['expand' => ['subscription']]
);
$defaultPaymentMethod = $checkoutSession['subscription']['default_payment_method'];
$stripe->customers->update(
'cus_Is8yXZukeWcRTO',
['invoice_settings' => ['default_payment_method' => $defaultPaymentMethod]]
);
Like this?
I will test it, thank you very very much @oak flume !