#cinbx

1 messages · Page 1 of 1 (latest)

craggy lionBOT
plush scaffold
#

Hello

#

Are you using a Webhook endpoint to ingest data here after a successful Session?

warm haven
#

Nah. All I'm using with this is Sanity CMS.. that's where my products are coming from.

plush scaffold
#

Okay well you likely will want to implement a Webhook endpoint to handle fulfillment and get your product information. We show you how to do this here: https://stripe.com/docs/payments/checkout/fulfill-orders -- the key is that when you receive the checkout.session.completed Event you want to retrieve the Checkout Session and expand line_items and that will provide you with the necessary Product information you are looking for.

craggy lionBOT
warm haven
#

Hm would this be the case even if I already have a line_items object when I create the checkout session?

ember bloom
#

Hi there 👋 jumping in as my teammate needs to step away soon. Can you tell me a bit more about how you're creating the Checkout Sessions. Are you leveraging price_data and product_data to create adhoc objects, and are then interested in trying to update those?

warm haven
#

Thanks for stepping in. Sure. I'm using 'use-shopping-cart' package to handle the items that I pull in from Sanity.

I'll show you the POST request I have where I use the create the checkout session

#

export async function POST(request) {
const cartDetails = await request.json();
const storeItems = await client.fetch(
*[_type == "storeItem"]
);
const lineItems = validateCartItems(storeItems, cartDetails);
const origin = request.headers.get('origin');

const session = await stripe.checkout.sessions.create({
submit_type: 'pay',
mode: 'payment',
payment_method_types: ['card'],
line_items: lineItems,
shipping_address_collection: {
allowed_countries: ['US'],
},
billing_address_collection: 'auto',
success_url: ${origin}/success?session_id={CHECKOUT_SESSION_ID},
cancel_url: ${origin}/cart,
});

return NextResponse.json(session);
}

ember bloom
#

What is lineItems getting set to? Do you have a sample request where you created a Checkout Session that I can look at?

warm haven
#

Sure, 1 sec

ember bloom
#

I'm still a little fuzzy on what you're actually trying to do is though. Was I correct earlier that you're looking to edit existing Product or Price objects in Stripe? Rereading your message it seems like my teammate may have been going down more of the right path if you're trying to retrieve data instead.

warm haven
#

Here's an example of lineItems when the user tries to buy 3 items:

lineItems: [
{
price_data: { currency: 'USD', unit_amount: 3500, product_data: [Object] },
quantity: 1
},
{
price_data: { currency: 'USD', unit_amount: 20000, product_data: [Object] },
quantity: 1
},
{
price_data: { currency: 'USD', unit_amount: 16000, product_data: [Object] },
quantity: 1
}
]

warm haven
#

Looking into it now, I see that the product_data object has the name and description of the product.

{
name: 'product 1',
description: "description of product 1"
}

#

So anyways, after I go through the purchase session, I have a component that retrieves the user data from the purchase:

  const checkoutSession = await stripe.checkout.sessions.retrieve(sessionId);
  const customerDetails = checkoutSession?.customer_details;
#

So the user details is fine, but was wondering if the product details were also available for retrieval

ember bloom
#

They'll be the same details you provided us, but you should be able to retrieve it. You'll want to find the Price and Product object IDs within the structure of the Checkout Session, and then use those to retrieve the related objects.

#

I think the easiest way to do that, is actually to retrieve the Checkout Session's Line Items:
https://stripe.com/docs/api/checkout/sessions/line_items

While using expand to expand ['data.price.id', 'data.price.product']
https://stripe.com/docs/api/expanding_objects

warm haven
#

Ok let me look into that. Thank's Toby

ember bloom
#

Any time, let me know if that doesn't do what you're hoping!

warm haven
#

Excellent, that command worked! Unfortunately they seem to have their own stripe-generated IDs so I can't use their ID to query against their Sanity counterparts like I wanted to.. I can use the 'name' field though.

ember bloom
#

Ah dang.