#windy_applepay-trial

1 messages ยท Page 1 of 1 (latest)

young flowerBOT
glossy atlasBOT
#

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.

young flowerBOT
#

๐Ÿ‘‹ 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/1238173251096805408

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

worldly sage
#

windy_applepay-trial

#

@rocky frigate can you share the code that creates the Subscription server-side?

rocky frigate
#

Sure

#
const customer = await handleCreateCustomer(user);
    console.log('created customer:', customer.id);

    const product = await getStripeProduct(membershipPlan.stripeProductId);
    console.log('got product:', product.id);

    const metadata: CreateSubscriptionMetadata = {
      userId: userToken.uid,
      // This is needed for plan upgrades
      membershipPlanId: membershipPlan.id as string,
      paymentCardType: cardType,
    };

    // should check if product has default_price on stripe dashboard
    const subscription = await createSubscription(
      customer.id,
      product.default_price as string,
      initializeTrial,
      metadata
    );
    console.log('created subscription:', subscription.id);

    let clientSecret: string | null;
    if (initializeTrial) {
      clientSecret = (subscription.pending_setup_intent as Stripe.SetupIntent)
        ?.client_secret;
    } else {
      clientSecret = (
        (subscription?.latest_invoice as Stripe.Invoice)
          ?.payment_intent as Stripe.PaymentIntent
      )?.client_secret;
    }

    console.log(
      `client secret (initializeTrial=${initializeTrial}):`,
      clientSecret
    );

    return res.status(200).send({
      status: 'success',
      data: {
        subscriptionId: subscription.id,
        clientSecret,
      },
    });
worldly sage
#

yeah sorry that's not what I want

#

Just the exact code that calls the Create Subscription API

rocky frigate
#

sec

worldly sage
#

but wait if (initializeTrial) { clientSecret = (subscription.pending_setup_intent as Stripe.SetupIntent) ?.client_secret; }

#

you have this, so you already understand the concept of pending_setup_intent right?

rocky frigate
#
const createSubscription = async (
  customerId: string,
  priceId: string,
  initializeTrial: boolean,
  metadata: CreateSubscriptionMetadata
): Promise<Stripe.Response<Stripe.Subscription>> => {
  const subscription = await stripe.subscriptions.create({
    customer: customerId,
    items: [
      {
        price: priceId,
      },
    ],
    metadata,
    trial_period_days: initializeTrial
      ? SEVEN_DAYS_FREE_TRIAL_LENGTH
      : undefined,
    payment_behavior: 'default_incomplete',
    payment_settings: { save_default_payment_method: 'on_subscription' },
    expand: ['latest_invoice.payment_intent'],
  });

  return subscription;
};
worldly sage
#

ah yeah here we go! expand: ['latest_invoice.payment_intent'],

worldly sage
#

this is the bug. Here you expand the latest Invoice's PaymentIntent to get its client_secret.
But you do not expand the pending_setup_intent so you are getting back pending_setup_intent: 'seti_123' as a string so your code I quoted above fails to find the client_secret
Change to expand: ['latest_invoice.payment_intent', 'pending_setup_intent'], and it should now work

rocky frigate
#

Amazing, thank you. Let me update it and test my app

young flowerBOT