#xwb89

1 messages · Page 1 of 1 (latest)

quiet scarabBOT
abstract cave
#

Can you show me the code for the call that is getting that error?

gray lodge
#

Hello, this is the code, I am using "stripe" package

export async function createStripeCheckoutSession(req: Request, res: Response) {
  const { priceId, purchaseId, successUrl, cancelUrl, mode, currency } =
    req.body;

  if (!priceId || !purchaseId) {
    res.status(400).json({ message: "Price ID or Purchase ID not specified." });
    return;
  }

  if (mode !== "payment" && mode !== "subscription") {
    res.status(400).json({ message: "Stripe subscription mode not provided." });
    return;
  }

  const config = await admin.remoteConfig().getTemplate();
  const STRIPE_SECRET_KEY = String(
    (config.parameters.STRIPE_SECRET_KEY?.defaultValue as any).value
  );

  if (!STRIPE_SECRET_KEY) {
    res.status(400).send("Stripe key not provided.");
    return;
  }

  const paymentMethods: Stripe.Checkout.SessionCreateParams.PaymentMethodType[] =
    ["card"];

  if (currency) {
    switch (currency) {
      case "EUR": {
        paymentMethods.push("alipay", "wechat_pay");
        break;
      }
      case "BRL": {
        paymentMethods.push("boleto");
      }
    }
  }

  const stripe = new Stripe(STRIPE_SECRET_KEY, {
    apiVersion: "2022-11-15",
    appInfo: {
      name: "Connectour",
    },
  });

  try {
    const checkoutSession = await stripe.checkout.sessions.create({
      success_url: successUrl,
      cancel_url: cancelUrl,
      payment_method_types: paymentMethods,
      mode,
      line_items: [
        {
          price: priceId,
          quantity: 1,
        },
      ],
      ...(mode === "payment" && {
        payment_intent_data: {
          metadata: {
            purchaseId,
          },
        },
      }),
      ...(mode === "subscription" && {
        subscription_data: {
          metadata: {
            purchaseId,
          },
        },
      }),
    });

    res.status(200).json({ url: checkoutSession.url });
  } catch (error) {
    res.status(400).json({ error });
  }
}
abstract cave