#Erik Magnusson
1 messages · Page 1 of 1 (latest)
- How are you creating your subscriptions on the back-end?
It's an exact replica of the example I referenced to
Do you have a Subscription ID I can review?
pi_3MgdZKGkDZZoZCPj0eowKHHL
That's a payment intent. I thought you were asking about the Subscription part?
Hmm. I think this is the subscription that relates to the payment intent. sub_1MgdtTGkDZZoZCPjmx3HEefZ
The one thing that is different from the example is that I'm creating a price during tuntime:
const subscriptionItem = { price_data: { unit_amount: amount, currency: 'sek', product: stripeProduct, recurring: { interval: 'month' }, }, }; const subscription = await stripe.subscriptions.create({ customer: customerId, items: [subscriptionItem], });
This susbcription was created without a trial period so it didn't have a pending_setup_intent because payment was already due. In that case the client_secret is in the latest_invoice.payment_intent property
I see! Do I have to retrieve that invoice to get that information? Because on the created subscription latest_invoice is just a string? Or can I ask to expand the subscription a the same time that I'm creating it?
You expand the latest_invoice.payment_intent when creating the Subscription.
Automatically, it seems not to.
Another questions, should I not use the setup intent if I do create subscription without a trial? Should the client expect a payment intent to confirm the subscription?
👋 Taking over this thread, catching up now
Automatically, it seems not to.
You'd have to addexpandparameter and setlatest_invoice.payment_intentas value in order to get the Payment Intent from Subscription.
For example, expand: ['latest_invoice.payment_intent']: https://stripe.com/docs/api/expanding_objects
Another questions, should I not use the setup intent if I do create subscription without a trial? Should the client expect a payment intent to confirm the subscription?
- If the subscription has a trial (amount = $0 for the first invoice), expand
pending_setup_intentand use the client secret of Setup Intent - If the subscription doesn't have trial (amount is non-zero for the first invoice), expand
latest_invoice.payment_intentand use client secret of Payment Intent
Thank you, River.
Is there a way to create a way to create a paymentIntent for subscription without having to create the subscription first?
When creating a paymentIntent for a one-time charge, I can return the intent to the client who can then ask the user to provide a payment method.
I would like do to the same thing for a subscription, is this possible?
No, it's not possible. To complete the payment of a subscription, the Payment Intent has to be from the Invoice of the Subscription itself. You may refer to this guide for the code example about obtaining the Payment Intent from the subscription: https://stripe.com/docs/billing/subscriptions/build-subscriptions?ui=elements#create-subscription
Ouch. So If one of my users wants to sign up for a subscription, I'd need to have a separate process in my client for them to provide a payment method? There's no way to get around the this to give the same experience as for the regular payment intent?
Customer shouldn't notice the difference since it's running behind the scene. Your server will create the subscription first, then pass Payment Intent client secret from the backend to frontend to collect payment method from the customer (This is one single API call). This is similar to where you create a Payment Intent and pass client secret to frontend (One single API call).
Hmm, a couple of more things I'm wondering about.
If the customer does not have payment method, I can't create a subscription.So how do i return the intent to the client?
And another question;
When I pass the client_secret as "paymentIntentClientSecret" that I get from the paymentIntent.create, the payment sheet works fine.
But if I pass the client_secret that I take from the invoices payment_intent. The payment sheet fails with:
{"code": "Failed", "declineCode": null, "localizedMessage": "The operation couldn’t be completed. (Stripe.PaymentSheetError error 0.)", "message": "The operation couldn’t be completed. (Stripe.PaymentSheetError error 0.)", "stripeErrorCode": null, "type": null}
If the customer does not have payment method, I can't create a subscription.
Why? Do you have any specific use case? Otherwise, you can create the subscription without payment method first, then pass payment intent client secret to frontend to collect payment method from the customer.
Payment Intent created from the Subscription is meant for you to create a payment method from the customer
I do not have a specific case. If I try to do this:
const subscription = await stripe.subscriptions.create({ customer: customerId, items: [subscriptionItem], expand: ['latest_invoice.payment_intent'], });
With a user that does not have a payment method attached. The api throws this:
Error: This customer has no attached payment source or default payment method. Please consider adding a default payment method.
Can you share the request ID (req_xxx) where you see this error message? Here’s how you can find it: https://support.stripe.com/questions/finding-the-id-for-an-api-request
req_RXywceKg7CE0ZA
From what it looks like in my dashboard. It looks like the stripe.subscriptions.create goes through to successfully create the subscription. I was of the belief that it would not succeed/be active until the the client confirmed it.
You miss quite a few parameters. Do you follow the guide here? https://stripe.com/docs/billing/subscriptions/build-subscriptions?ui=elements#create-subscription
payment_behavior: 'default_incomplete': creates a subscription without a payment method, and you can use payment intent client secret to collect it laterpayment_settings: { save_default_payment_method: 'on_subscription' }: save payment method (collected later) on the subscription for future recurring payments
Oh my, yeah, that solved it... My bad.
While I have you here. Could I ask you about metadata on items?
I try to create prices by using "price_data" when creating a subscription. And I want to attach metadata to it.
I looks something like this { price_data: { ... }, metadata: { data:[{...}] } };
Can the metadata not accept array as values?
metadata should be a key-value pair where value only accepts string. For example,
metadata: {
'product_identifier': 'id_12345',
}
Thank you
aaaand a last one
I want to store metadata in the subscription so that I want use that to make transfers for every time the subscription interval triggers a new charrge/paymentIntent.
- does it trigger either a charge or paymentIntent on every interval, lets say month? Or is it only one of them?
- How do I get the metadata that's attached to the subscription from the that event when using the stripe webhook?
- does it trigger either a charge or paymentIntent on every interval, lets say month? Or is it only one of them?
Yes, Payment Intent and Charge event will be created if amount is non-zero. However, metadata stored on the price_data won't be populated to Payment Intent.
- How do I get the metadata that's attached to the subscription from the that event when using the stripe webhook?
If you store the metadata on theprice_data, you can listen toinvoice.paidevent when a payment is completed every cycle. Metadata only stays within the object you set and isn't populated to relevant objects.
Oh! And the invoice.paid will also trigger every month, just like the charge or payment intent?
Yes! Regardless the amount of an invoice (zero or non-zero), invoice.paid event will be triggered as long as the invoice is completed successfully every cycle
Amazing! You’ve been a lifesaver!
Thank you !