#tobi_code

1 messages Ā· Page 1 of 1 (latest)

light spokeBOT
#

šŸ‘‹ 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/1318586915729051690

šŸ“ Have more to share? Add more details, code, screenshots, videos, etc. below.

languid lion
#

Here is my checkout code:

    const session = await stripe.checkout.sessions.create({
      customer: user.stripeCustomerId,
      mode: "subscription",
      line_items: [
        {
          price: id,
          quantity: 1,
        },
      ],
      subscription_data: {
        trial_settings: {
          end_behavior: {
            missing_payment_method: "cancel",
          },
        },
        trial_period_days: 7,
      },
      success_url: new URL(
        `/success?session={CHECKOUT_SESSION_ID}`,
        basepath
      ).toString(),
      cancel_url: new URL(
        "/cancel?session={CHECKOUT_SESSION_ID}",
        basepath
      ).toString(),
      metadata: {
        vendorId: vendorId,
      },
      allow_promotion_codes: true,
      locale: "pt-BR",
      client_reference_id: user.id,
    });
spring inlet
#

Hi, let me help you with this.

#

How are you exporting the payment data?

languid lion
#

In the dashboard, payments tab

#

I get this

#

I tried adding all columns and I still get only this metadata

#

That comes from the checkout

#

But when the billing cycle restarts it creates a new id without the metadata, and I don't see a way to add it to it

#

This is how I update the metadata in the payment intent

async function onPaymentIntentSucceed(event: Stripe.Event) {
  const paymentIntent = event.data.object as Stripe.PaymentIntent;

  try {
    // Retrieve the associated customer
    const customer = await stripe.customers.retrieve(
      paymentIntent.customer as string
    );

    if (customer.deleted) {
      console.warn(`Customer ${customer.id} has been deleted.`);

      return;
    }

    // Get vendorId from customer metadata
    const vendorId = customer.metadata?.vendorId;

    if (!vendorId) {
      console.warn("No affiliate ID found in customer metadata");
      return;
    }

    // Update the PaymentIntent metadata
    await stripe.paymentIntents.update(paymentIntent.id, {
      metadata: {
        vendorId: vendorId,
      },
    });
  } catch (error) {
    console.error("Error updating PaymentIntent metadata:", error);
  }
}

As you can see in the image, it does work, but I can't see this data in the export file.

spring inlet
#

How are you setting this metadata exactly?

languid lion
#

Right now when I receive the payment_intent.succeeded event through the webhook

languid lion
spring inlet
#

Why are you not setting it on the other PaymentIntents?

light spokeBOT
languid lion
#

Oh well, I guess it wasn't working before for some reason

#

Now it does update

#

I actually don't need this metadata in the checkout

#

Thanks. I thought that this was the metadata from the checkout, but apparently it was the PaymentIntent, and I rushed things so I forgot to test it again šŸ¤¦ā€ā™‚ļø

spring inlet
#

Are you able to copy the metadata every time a new PaymentIntent is created?

languid lion
#

Yeah