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
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/
There’s currently no way to filter for Checkout Sessions by metadata.
Some other options are :
Listen for the
checkout.session.completed
webhook event, save the data to your DB and then perform your own filtering. See https://stripe.com/docs/webhooks for more information on handling webhooks.Add the metadata to the PaymentIntent also using the following parameter : https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-payment_intent_data-metadata. You can then use the Search API to filter for the
corresponding PaymentIntent with https://stripe.com/docs/search. Once
you have the PaymentIntent, retrieve the corresponding
Checkout Session with
https://stripe.com/docs/api/checkout/sessions/list#list_checkout_sessions-payment_intent
To filter for Checkout Sessions by Customer, you can use https://stripe.com/docs/api/checkout/sessions/list#list_checkout_sessions-customer