#Erik Magnusson

1 messages · Page 1 of 1 (latest)

hoary horizonBOT
proven mason
#
  1. How are you creating your subscriptions on the back-end?
agile valve
#

It's an exact replica of the example I referenced to

proven mason
#

Do you have a Subscription ID I can review?

agile valve
#

pi_3MgdZKGkDZZoZCPj0eowKHHL

proven mason
#

That's a payment intent. I thought you were asking about the Subscription part?

agile valve
#

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], });

proven mason
#

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

agile valve
#

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?

proven mason
#

You expand the latest_invoice.payment_intent when creating the Subscription.

agile valve
#

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?

lilac prawn
#

👋 Taking over this thread, catching up now

#

Automatically, it seems not to.
You'd have to add expand parameter and set latest_invoice.payment_intent as 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_intent and 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_intent and use client secret of Payment Intent
agile valve
#

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?

lilac prawn
agile valve
#

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?

lilac prawn
#

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).

agile valve
#

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}

lilac prawn
#

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

agile valve
# lilac prawn > If the customer does not have payment method, I can't create a subscription. ...

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.

lilac prawn
agile valve
#

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.

lilac prawn
agile valve
#

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?

lilac prawn
#

metadata should be a key-value pair where value only accepts string. For example,

metadata: {
  'product_identifier': 'id_12345',
}
agile valve
#

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.

  1. does it trigger either a charge or paymentIntent on every interval, lets say month? Or is it only one of them?
  2. How do I get the metadata that's attached to the subscription from the that event when using the stripe webhook?
lilac prawn
#
  1. 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.
  1. 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 the price_data, you can listen to invoice.paid event when a payment is completed every cycle. Metadata only stays within the object you set and isn't populated to relevant objects.
agile valve
#

Oh! And the invoice.paid will also trigger every month, just like the charge or payment intent?

lilac prawn
#

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

agile valve
#

Amazing! You’ve been a lifesaver!
Thank you !