#Mike.7189-details
1 messages Β· Page 1 of 1 (latest)
Hi π
@frozen sparrow it depends a little. How do you integrate and accept the payments? CheckoutSessions?
https://fns-accessories.web.app/en/catalog
hit buy now
it's stripe checkout
like this
And I don't really want to store this data in the database
so you'd handle the webhook for checkout.session.completed at least (https://stripe.com/docs/payments/checkout/fulfill-orders)
and then the shipping comes from the shipping object on the CheckoutSession object https://stripe.com/docs/payments/checkout/shipping#handling-transactions
https://stripe.com/docs/api/checkout/sessions/object#checkout_session_object-shipping-address
I know you mentioned billing_details in your question but you are collecting a shipping address and sending stuff there, so presumably you explicitly want that, and not the billing address of the payment method(which might be different)
alright, but how do I send it my frontend?
you don't, since it's collected in the Checkout page for you, right?
or sorry you mean send it to your frontend
I mean that depends how your frontend works, but for example look at https://stripe.com/docs/payments/checkout/custom-success-page
it shows how you can get infromation from the CheckoutSession and render it into the success page
yes, so I can show something like:
Thanks for your purchase, we'll ship it to
shipping_address
same exact concept
same for /cancel, yes?
yep, you can add {CHECKOUT_SESSION_ID} to the cancel URL and do similar things
obviously there won't be any address to work with in that case though
and where am I supposed to get the session.id?
const session = await stripe.checkout.sessions.create({ ... })
res.status(200).send(session.url) // for redirect on frontend
So in my case it'll be session.id?
but I can't
I can't access session before initialization
const session = await stripe.checkout.sessions.create({
payment_method_types: ["card"],
shipping_rates: ["shr_1JrLmoKuBnFIZzDsZ26cDNtp"],
shipping_address_collection: {
allowed_countries: ["IT", "FR", "ES"],
},
line_items: [
{
price: variant.stripeProductId,
adjustable_quantity: {
enabled: true,
minimum: 1,
maximum: parseInt(variant.stock),
},
quantity: 1,
},
],
mode: "payment",
success_url: `${process.env.SUCCESS_URL}?session_id=${session.id}`,
cancel_url: `${process.env.CANCEL_URL}?session_id=${session.id}`,
});
res.status(200).send(session.url);
yeah I got it.. π
https://stripe.com/docs/payments/checkout/custom-success-page
Add the {CHECKOUT_SESSION_ID} template variable to the success_url when you create the Checkout Session. Note that this is a literal string and must be added exactly as you see it here. Do not substitute it with a Checkout Session IDβthis happens automatically after your customer pays and is redirected to the success page.
alright, lemme try to access this data and I'll move to another question π
alright, running into problem
StripeInvalidRequestError: line_items[0][adjustable_quantity][minimum] (1) must be less than line_items[0][adjustable_quantity][maximum] (1).
line_items: [
{
price: variant.stripeProductId,
adjustable_quantity: {
enabled: true,
minimum: 1,
maximum: parseInt(variant.stock),
},
quantity: 1,
},
],
hard to say since I can't see what your variant.stock variable is
so the remaining quantity (variant.stock) is 1
then you probably want to write some code to special case that
if it's 1, then don't use adjustable_quantity and omit it from the request
wth, can't I sell the last item I have? π
you can, just not with adjustable quantity enabled
so adjustable_quantity can't be 1?
seems that way, it won't let you do min:1 and max:1, as the error implies
line_items:
parseInt(variant.stock) === 1
? [
{
price: variant.stripeProductId,
quantity: 1,
},
]
: [
{
price: variant.stripeProductId,
adjustable_quantity: {
enabled: true,
minimum: 1,
maximum: parseInt(variant.stock),
},
quantity: 1,
},
],
this is ugly
can I pass empty object instead of adjustable_quantity?
or I can just do enabled: false, right?
StripeInvalidRequestError: You cannot use `line_items[0][adjustable_quantity][maximum]` if `line_items[0][adjustable_quantity][enabled]` is false.
wtf is this error?
enabled is false, but I still can't use it
π
your code seems correct and for example this works for me with both stock 1 or stock 3
let stock = 1;
//let stock = 3;
const session = await stripe.checkout.sessions.create({
payment_method_types: ["card"],
line_items:
parseInt(stock) === 1
? [
{
price: "price_1JiKBfJoUivz182DeeQGbB0L",
quantity: 1,
},
]
: [
{
price: "price_1JiKBfJoUivz182DeeQGbB0L",
adjustable_quantity: {
enabled: true,
minimum: 1,
maximum: stock,
},
quantity: 1,
},
],
mode: "payment",
success_url: "https://example.com",
cancel_url: "https://example.com"
});
if you have a request ID req_xxx for that error (https://support.stripe.com/questions/finding-the-id-for-an-api-request) I can try to look at that
alright, it's just a bit ugly
enabled: parseInt(variant.stock) === 1 ? false : true
this would look much better and shorter, but doesn't work for some reason
alright, seems all good.
Now I have a question about product creation
particularly about image(s)
as far as I remember api call receives images, and that is an array
so I am a big confused how can I set product image
I know it's easiest π
I need it through api
cos I am creating CMS
admin creates new item > images uploaded to cloudinary > links to cloudinary used to create stripe product + price > the whole product saved on mongoDB (with stripe_price_id and links to cloudinary)
then it's https://stripe.com/docs/api/products/create#create_product-images (you pass an array of strings, the strings are URLs to where you've hosted the images)
annnnd which one will be used as a product image?