#phu_api

1 messages · Page 1 of 1 (latest)

supple torrentBOT
#

👋 Welcome to your new thread!

⏲️ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

⏱️ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

🔗 This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1370095661554335856

📝 Have more to share? Add more details, code, screenshots, videos, etc. below.

buoyant cairn
brave badger
#

I am trying this right now I will get back to you in a minute!

#

function:
const createSubscriptionWithPaymentIntent = async (customerId, priceId, userId, spaceId, quantity = 1) => {
try {
// Create the subscription with expand parameter to get the payment intent directly
const subscription = await stripe.subscriptions.create({
customer: customerId,
items: [
{
price: priceId,
quantity: quantity,
},
],
metadata: {
user_id: userId,
space_id: spaceId
},
payment_behavior: 'default_incomplete',
collection_method: 'charge_automatically',
payment_settings: {
save_default_payment_method: 'on_subscription',
payment_method_types: ['card']
},
expand: ['latest_invoice.payments.data.payment.payment_intent']
});

buoyant cairn
#

Yup, gimme a sec.

supple torrentBOT
grim quarry
#

👋 stepping in

#

Why are you trying to access the PaymentIntent here?

supple torrentBOT
brave badger
#

hello!

#

I think it is to retrieve the client_secret and return it to the client so we can process the payment

grim quarry
#

So you only need to expand latest_invoice here

brave badger
#

here is the updated function:
const createSubscriptionWithPaymentIntent = async (customerId, priceId, userId, spaceId, quantity = 1) => {
  try {
    // Create the subscription with expand parameter to get the payment intent directly
    const subscription = await stripe.subscriptions.create({
      customer: customerId,
      items: [
        {
          price: priceId,
          quantity: quantity,
        },
      ],
      metadata: {
        user_id: userId,
        space_id: spaceId
      },
      payment_behavior: 'default_incomplete',
      collection_method: 'charge_automatically',
      payment_settings: {
        save_default_payment_method: 'on_subscription',
        payment_method_types: ['card']
      },
      expand: ['latest_invoice']
    });
    
    if (!subscription.latest_invoice || !subscription.latest_invoice.confirmation_secret) {
      throw new Error('No confirmation secret was found on the latest invoice');
    }
    
    const clientSecret = subscription.latest_invoice.confirmation_secret.client_secret;
    
    if (!clientSecret) {
      throw new Error('No client secret was found in the confirmation secret');
    }
    
    // Return the client secret from the payment intent
    return {
      subscriptionId: subscription.id,
      clientSecret: clientSecret
    };
  } catch (error) {
    console.error('Error creating subscription with payment intent:', error);
    throw error;
  }
};

#

And here is the new error:
Error creating subscription with payment intent: Error: No confirmation secret was found on the latest invoice
at Object.createSubscriptionWithPaymentIntent (/Users/arvihaxhillari/Documents/Code/mention/backend/services/stripeService.js:214:13)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async createSubscription (/Users/arvihaxhillari/Documents/Code/mention/backend/controllers/billing/billingController.js:46:20)

#

We keep getting stuck 😅

grim quarry
#

What API version are you using here?

#

And can you share the request ID from your test?

#

If you are not on the newest API version then you would just use latest_invoice.payment_intent.client_secret

brave badger
#

the version is : 18.0.0

grim quarry
#

That looks to be a SDK version

#

You are using the node SDK though it looks like

#

Can you share the Subscription ID that you created here in your test?

brave badger
#

the stripe version is 2025-03-31.basil

#

we are getting the other ids

#

here is the subscription id:  sub_1RMZjqGKpk2qFBnGk1NvO6eg

grim quarry
#

Thanks, give me a sec

brave badger
#

we think this is the requestId: req_r9iXYtcCFika3O

grim quarry
#

Ah okay sorry @brave badger you actually need to expand latest_invoice.confirmation_secret

#

As otherwise confirmation_secret is null here

brave badger
#

On it

#

I love you bismarck

#

this worked!