#Mike.7189-details

1 messages Β· Page 1 of 1 (latest)

young peak
frozen sparrow
#

Hi πŸ™‚

young peak
#

@frozen sparrow it depends a little. How do you integrate and accept the payments? CheckoutSessions?

frozen sparrow
#

it's stripe checkout

#

like this

#

And I don't really want to store this data in the database

young peak
#

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)

frozen sparrow
#

alright, but how do I send it my frontend?

young peak
#

you don't, since it's collected in the Checkout page for you, right?

#

or sorry you mean send it to your frontend

#

it shows how you can get infromation from the CheckoutSession and render it into the success page

frozen sparrow
#

yes, so I can show something like:

Thanks for your purchase, we'll ship it to shipping_address

young peak
#

same exact concept

frozen sparrow
#

same for /cancel, yes?

young peak
#

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

frozen sparrow
#
    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);
young peak
#

you don't need it do it that way

#

{CHECKOUT_SESSION_ID} is a magic string

frozen sparrow
#

yeah I got it.. πŸ™‚

young peak
#

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.

frozen sparrow
#

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,
                },
            ],
young peak
#

hard to say since I can't see what your variant.stock variable is

frozen sparrow
#

so the remaining quantity (variant.stock) is 1

young peak
#

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

frozen sparrow
#

wth, can't I sell the last item I have? πŸ˜„

young peak
#

you can, just not with adjustable quantity enabled

frozen sparrow
#

so adjustable_quantity can't be 1?

young peak
#

seems that way, it won't let you do min:1 and max:1, as the error implies

frozen sparrow
#
            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

young peak
#

πŸ‘€

#

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"
        });
frozen sparrow
#

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

young peak
#

it's easiest to do it through the dashboard, that's how I usually do it.

frozen sparrow
#

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)

young peak
frozen sparrow
#

annnnd which one will be used as a product image?

young peak
#

the first one I think. I don't know why it accepts multiple

#

I've never used this part of the API unfortunately, it's a good question