简体   繁体   中英

Filter checkout session by metadata Stripe

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?

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

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM