skip to Main Content

I’m building a marketplace (you can imagine it like an Ebay where there are buyer and seller). And i want to get an items that bought customer (and vice versa with seller).

Here is my checkout session service:

async charge(userId: number, dto: CreateChargeDto) {
        //Get an item by id
        const item = await this.prisma.item.findUnique({
          where: {
            id: dto.itemId,
          },
        });

    if (!item) {
      throw new BadRequestException('There is no item with this id');
    }
    // Search for user
    const user = await this.prisma.user.findUnique({
      where: {
        id: userId,
      },
    });
    //update customer
    await this.updateCustomer(user.stripeCustomerId, dto);

    const session = await this.stripe.checkout.sessions.create({
      success_url: 'http://localhost:3000/success?message=Succesful+payment',
      cancel_url: 'http://localhost:3000/404?message=Payment+canceled',
      mode: 'payment',
      currency: 'pln',
      customer: user.stripeCustomerId,
      customer_update: {
        address: 'auto',
      },
      metadata: {
        seller_id: item.userId, // here is where i save seller id
      },
      line_items: [
        {
          quantity: 1,
          price_data: {
            currency: 'pln',
            unit_amount: item.price * 100,
            product_data: {
              name: item.name,
              images: item.images,
              metadata: {
                id: item.id, 
              },
            },
          },
        },
      ],
    });

    return session.id;
  }

  async getCheckoutList(userId: number) {
    const user = await this.prisma.user.findUnique({
      where: {
        id: userId,
      },
    });

    const checkouts = await this.stripe.

    

    return checkouts.data;
  }

And now i wanna filter this session checkouts so i can display them in buyer (or seller) personal page.

const checkouts = await this.stripe.checkout.sessions.list({
      where: {
        metadata: {
          seller_id: // seller id
        }
      }

How can i do it?

2

Answers


  1. Chosen as BEST ANSWER

    So the best in my opinion in this situation is to use a webhook.

    The perfect example in Nestjs you can read here: https://wanago.io/2021/07/05/api-nestjs-stripe-events-webhooks/


  2. There’s currently no way to filter for Checkout Sessions by metadata.

    Some other options are :

    To filter for Checkout Sessions by Customer, you can use https://stripe.com/docs/api/checkout/sessions/list#list_checkout_sessions-customer

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search