#saar_code

1 messages ¡ Page 1 of 1 (latest)

dapper sorrelBOT
#

👋 Welcome to your new thread!

⏲️ We'll be here soon! We typically respond in a few minutes, but in some cases we might need a bit more time (e.g., server's busy, you've got a complex question, etc.).

⏱️ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can 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/1248174285546328146

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

deep saddle
#

there are 2 different stripe customer ids being shown in the dashboard
Could you share the example subscription (sub_xxx) which has two customer IDs, and where you see two customers from?

One subscription should only have one customer.

boreal gorge
#

this is how it's showing up on the customers tab @deep saddle.

Let me know if you need any other info

deep saddle
#

Can you share the customer IDs (cus_xxx) for the customer with the same name in the screenshot, so that I can take a look how your customer and checkout session were created?

boreal gorge
#

So, I am creating a customer id when the user signs up on the website through a backend api call.

Customer creation:

import { NextRequest, NextResponse } from "next/server";
import Stripe from "stripe";

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
  apiVersion: "2024-04-10",
});

export async function POST(req: NextRequest) {
  try {
    const { email, name } = await req.json();

    const customer = await stripe.customers.create({
      email: email,
      name: name,
    });

    return NextResponse.json(
      {
        message: "Created Stripe customer successfully",
        status: true,
        data: customer,
      },
      {
        status: 201,
      },
    );
  } catch (err: any) {
    console.error(err.message);

    return NextResponse.json(
      {
        message: "Could not create Stripe customer",
        data: null,
        status: false,
      },
      { status: 500 },
    );
  }
}

In the DB, I now have:

Name: Prakhar Kapoor
Stripe Customer Id: cus_QF1J2ti0VeME4U

In the screenshot sent above, the top 2 customers are under the same name Prakhar Kapoor with 2 seprate customer ids:

First being:

Name: Prakhar Kapoor
Stripe Customer Id: cus_QF1MKbWfOtP0ET

Second being:

Name: Prakhar Kapoor
Stripe Customer Id: cus_QF1J2ti0VeME4U

I believe the second record mentioned is being created during sign up and the first record is being created during the payment

deep saddle
#

You should create the customer (cus_xxx) first, and set in it customer field of Checkout Session creation requests, so that new customer won't be created

In https://dashboard.stripe.com/test/logs/req_YOr2Kj01GmcskM, customer field wasn't set in the request, so new customer will be created

#

I'd recommend creating Stripe customer (cus_xxx) for your own customer and save it into your database. During the Checkout Session creation, the corresponding customer ID associated to your customer identifier/email should be set in customer field to avoid new customer being created

dapper sorrelBOT
boreal gorge
#

i'm doing that already though.

in the code above, i'm sending the customer id and then creating the session.

Here's the code for that route:

import Stripe from "stripe";
import { NextRequest, NextResponse } from "next/server";
import { getStripeUrls } from "@/utils/stripe";

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
  apiVersion: "2024-04-10",
});

export async function POST(req: NextRequest) {
  try {
    const body = await req.json();
    const {
      // needed for stripe checkout session
      priceId,
      planName,
      customerId,
      customerName,
      customerAddress,
      // needed for creating workspace
      workspaceName,
      userEmail
    } = body;

    const checkoutSession = await stripe.checkout.sessions.create({
      mode: "subscription",
      customer: customerId,
      payment_method_types: ["card"],
      line_items: [
        {
          price: priceId,
          quantity: 1,
        },
      ],
      success_url: getStripeUrls().successUrl,
      cancel_url: getStripeUrls().cancelUrl,
      metadata: {
        user_email: userEmail,
      },
      subscription_data: {
      description: planName,
        metadata: {
          customer_name: customerName,
          customer_address: customerAddress,
          workspace_name: workspaceName,
          customer_id: customerId,
          user_email: userEmail,
        },
      },
      billing_address_collection: "required",
      client_reference_id: customerId,
    });

    if (!checkoutSession?.url) {
      throw new Error("Failed to create checkout session");
    }

    return NextResponse.json(
      {
        status: "success",
        message: "Checkout session created successfully",
        data: {
          url: checkoutSession,
        },
      },
      {
        status: 201,
      },
    );
  } catch (err: any) {...}
}

orchid oak
#

Hey! Taking over for my colleague. Let me catch up.

boreal gorge
#

Hey. Sure thing. Thank you to both of you for taking out the time btw

orchid oak
#

Have you had the chance to debug your endpoint and see if you are passing a correct value ?