#DOWNTOWN-tax-computation

1 messages ยท Page 1 of 1 (latest)

hidden goblet
timid mountain
#

Hey, thanks for getting back to me!

you'll need to add a separate function call inside that if statement (line 13) which creates a Price object

I'm a little confused about what belongs inside the Price object in this case, because all my product information is created on line 20 based on the sanityData.

Based on the docs, this is what you're referring to by a Prices object, right?

const price = await stripe.prices.create({
  unit_amount: 2000,
  currency: 'cad',
  recurring: {interval: 'month'},
  product: 'prod_LbDkBM6vTf5VDw',
});
hidden goblet
#

Yeah, you have to create the Price object in order to set tax_behavior and resolve the error you're getting

timid mountain
#

but I need to still set things like unit_amount, etc?

hidden goblet
#

I can't really help you with the Sanity part unfortunately. If your Prices are being created there, then you might want to check to see if tax_behavior can be set on their side. Otherwise, create the Price before creating and redirecting to your Checkout Session

timid mountain
#

I see, I'll try to sort that out, but could you maybe tell me what the successful behavior would be in this case if all is configured properly? I'm assuming at checkout, the screen would display the total price of the items + the associated tax rate depending on what I set in https://dashboard.stripe.com/test/settings/tax , is that correct?

hidden goblet
#

Sorry for the wait. Let me check

#

I think whether it displays the tax amount will depend on configuration but let me dig a bit

uneven crescent
#

๐Ÿ‘‹ stepping in here. Give me a moment to catch up

timid mountain
#

So i'm a little confused about which stage I should be adding the price object, which I've currently placed in cart.js , see here as of line 22:

timid mountain
uneven crescent
#

Okay yep so looks like the way you are creating your Price is the issue.

timid mountain
uneven crescent
#

So you are only passing tax_behavior and currency

#

You are going to need to also pass either a product or product_data there (product_data will create the new product when you create the price)

#

And you are going to need a unit_amount which will be the actual amount you charge

timid mountain
#

is there any way i can retrieve this from line_items/cartItems though? I'll need the information to match the information that is coming from Sanity

#

I'm still a little new to node.js, sorry about this lol

uneven crescent
#

Yeah I mean that is completely up to you. What does your line_items variable contain?

#

I'd assume that is your cart data?

timid mountain
#

yeah, it would be, although for some reason my console.log result isn't appearing

timid mountain
#

why would that be?

uneven crescent
#

Instead of doing final_line_items = Object.assign(line_items, price) you will want to pass the relevant line_items data when you create the Price within Stripe. The other way to do this is to just create the price and product when you create the Session

uneven crescent
#

That is the error you are seeing

#

Stating you need to specify a product or product_data

timid mountain
#

right, right

uneven crescent
#

Can you add a log for line_items before you create the Price?

timid mountain
#

sure, one sec

#

yeah so nothing logs still

#

w/ the following code

uneven crescent
#

You might need to await your validateCartItems but I have no idea what that function is doing so can't really say....

timid mountain
#

comes from here ^

uneven crescent
#

Ah thanks

#

Hmm okay in that case let's log out sanityData and cartItems individually then

#

And see what those contain

timid mountain
#

same thing, for some reason they don't log out

uneven crescent
#

We should see an undefined or something before the error though?

#

Where did you add the logs?

timid mountain
#

this is the full log

#

the first few are from a privacy browser extension, so disregard those

#

would i have to call these outside of line 11?

uneven crescent
#

Did you add the log right after you created those variables? Like:

  console.log(cartItems)```?
timid mountain
#

yeah to no effect

#

๐Ÿ˜ฆ

uneven crescent
#

And comment out the Checkout session creation?

timid mountain
#

on line 52?

uneven crescent
#

Yes

#

Comment that out

#

Let's see if you are getting your data at all

#

Without these other errors coming into play

timid mountain
#

commenting out 52 and 54 just gets rid of the errors but doesn't show the console.logs

subtle snow
#

@timid mountain ๐Ÿ‘‹ taking over but there's a ton of words and screenshot of code. What's still blocking you exactly?

timid mountain
#

Hey! Sorry, I know this is a little confusing lol

#

let me try and summarize

#

the main issue: I'm trying to add automatic_tax at checkout, but I'm having issues with the Price object

how I'm creating my products: products come from Sanity CMS entries (which have fields for Name, Price, Description.., then items get added to cart with use-shopping-cart

subtle snow
#

Do you have an example price id?

timid mountain
#

in cart.js, where i create the checkout session, I'm unable to console.log() out my sanityData object for some reason. I'm trying to get values such as price, quantity and product name from this object and merge them with the Stripe price object so that I can add tax_behavior

subtle snow
#

in cart.js, where i create the checkout session, I'm unable to console.log()
What is cart.js? Is the code even being called? Does it run client-side?

timid mountain
subtle snow
#

Also taking a step back, have you tried just using Payment Links instead if you're this confused? This might help you just have a direct link to the right Checkout Session?

#

Yeah I skimmed it but that's hours of work to try and parse such a complex tutorial using multiple third-parties

timid mountain
#

sure sure

#

1 sec

timid mountain
#

the issue only started once i tried to create the price object, with errors saying I'm missing certain parameters like product etc. I need these parameters to come from cartItems seen on line 22 in the last screenshot

subtle snow
timid mountain
#

Right, the product ID is a piece of data I'm trying to retrieve from the product that was added to cart via use-shopping-cart. The object you see here in the first screenshot below is a representation of what I should be seeing when I console.log cartItems. I'm trying to get these values and append price to line_items, as seen on line 29 here, which in theory should allow for a successful checkout, no?

Prior to creating price, I would just pass line_items into params which would allow for a successful checkout.

#

Am I understanding this correctly?

subtle snow
#

sorry you sahre so many pictures of code it's hard to parse

#

Sorry but what part is your question?

timid mountain
#

See first paragraph of my last message

uneven crescent
#

Okay I had to step away for a few but now I'm back @timid mountain, give me a moment

timid mountain
#

Sorry guys, I know this is all confusing, I really do sincerely appreciate your patience

uneven crescent
#

Okay so we need to back up and understand why sanityData and cartItems are null

#

So next step would be to log out the req when your handler endpoint is hit

#

And ensure you are receiving data

timid mountain
#

well I can tell that I'm receiving data because prior to trying to integrate price, I was able to execute payments successfully

#

I'm assuming the reason that those items are null is because of my catch, on line 55, which throws a 500 status error

uneven crescent
#

Okay can you comment out everything in the try block then except for const cartItems = req.body console.log(cartItems)

timid mountain
#

sure

#

no errors now, but also no logs

uneven crescent
#

Okay then you aren't making it inside that try block if there are no logs

#

Let's log req right after the handler

#

So before the if statement

#

Just to ensure you actually are seeing stuff there

#

Feel free to just log req.body

#

If you don't want to deal with the huge return from logging req

timid mountain
#

so yeah, still nothing

#

console.log(req.body) on line 12

uneven crescent
#

Ok so that is the place to start debugging then

#

What is triggering that endpoint to be hit?

timid mountain
#

see here, it's from this

#

I've attached the relevant section

#

In the code above, you'll notice we send API requests to api/checkout_sessions/cart in the fetchPostJSON(). This is the serverless API that will manage our interactions with Stripe.

#

So in this component that I linked the article to, (cartSummary), I've added a console.log for the response on line 34

uneven crescent
#

So you said this was working previously, yes? Before you started playing with auto_tax?

timid mountain
#

and I'm getting a big response in my terminal but not in my dev tools

timid mountain
timid mountain
#

it's multiple pages long though

uneven crescent
#

Is there a specific error in the response?

timid mountain
#

I'm not sure, but if you look at the very last line here:

#

aha wait! see the body: {} object

#

that's the expected result for const cartItems = req.body;

#

here's the entire log

uneven crescent
#

Okay great so that has the body.

#

But that is the response you are getting on your client right

#

You want to be passing those cartDetails to your server

#

Why is the server sending back the body that you want to be used on your server?

timid mountain
#

I still have everything within the try commented out

timid mountain
uneven crescent
#

Oh I'm sorry. That log you sent is from your server

#

Not your client

#

Was thrown off because you said you added console.log(response on your client-side

#

Okay so you are seeing that big long log from logging out req in your Handler(), correct?

timid mountain
# timid mountain

I'm getting that big log by logging it out here, in cartSummary, not cart

#

do you want to see fetchPostJSON ? I feel like that might be useful for you to see

#

I reference it on line 29

#

Sorry, I was wrong

#

This is what I get when I console.log(response) on cartSummary.

#

The big long log is indeed within the handler() as you said.

uneven crescent
#

Perfect so if you do log out req.body within the handler you get the above body that you screenshotted, correct?

timid mountain
#

yes

#

correct

#

When i log cartItems, it also shows the same log

#

this is the result of cartItems and sanityData respectively

#

sanityData represents the three products my store has

uneven crescent
#

Alright great

timid mountain
#

๐Ÿ˜„

#

Sorry, I should've thought to check the terminal. I didn't know it would log something different from the dev tools

uneven crescent
#

All good

#

So now you need to parse the data in those variables and pass the necessary pieces to create the Price

#

So you will want to pull out the necessary properties from your cartItems to do this

timid mountain
#

so the objects within cartItems are nested within that ID, where does that ID (dc143c49-3689-4856-aa08-9ca730a4a390) come from?

#

I guess what I'm asking is how can i access cartItems.[big long id].currency for instance?

uneven crescent
#

Are you able to do cartItems[0].currency in this case? I actually don't remember if that will work or not here

timid mountain
#

"Cannot read property 'currency' of undefined"

#

I'm assuming this ID is going to be variable, so I can't hard-code it in?

#

aha wait, when I log const validate_line_items = validateCartItems(sanityData, cartItems);

the output is the following:

#

I can just use this instead, right?

uneven crescent
#

lol

#

Yes

#

That is exactly what you want

timid mountain
#

awesome lol

#

I'm nearly done here, but I'm just confused on the very last part, which is merging price with these objects

#

are they supposed to be appended to one another ?

uneven crescent
#

So you have two options

#

You either create the price and then pass the price ID to the Checkout Session

#

Or you just create the price in-line in the Checkout Session itself.

#

So right now you are attempting to create it in-line in the Checkout Session

timid mountain
#

right

uneven crescent
#

So really the only thing you need to add is the price_data.tax_behavior

#

So the easiest way to do this in my opinion is to take your line_items variable and modify it prior to passing to the Checkout Session

timid mountain
#

38*

uneven crescent
#

It does, but the issue is you are attempting to use auto_tax which means you need a tax_behavior set for the Price

timid mountain
#

which is on line 25 no?

uneven crescent
#

So if you look at the above logged out variable for price_data there is no tax_behavior property

timid mountain
#

just as an aside, would it not be better for me to just add all these variables directly into price like so :

      const price = await stripe.prices.create({
        tax_behavior: "exclusive",
        currency: priceData.currency,
        product_data: priceData.product_data,
        unit_amount: priceData.unit_amount,
        quantity: validate_line_items[0].quantity,
      });

instead of trying to use Object.assign() ?

#

and then where line_items is within the params just use price instead? or does stripe expect line_items as a keyword?

uneven crescent
#

That is the other option I mentioned

#

And is totally fine!

timid mountain
#

oh ok ok

uneven crescent
#

Really up to you

#

Except

#

You don't pass quantity there

#

You pass quantity to the Checkout Session

timid mountain
#

so with that integrated

uneven crescent
#

You do need the line_items param

timid mountain
#

it should look like this?

#

oh

uneven crescent
#
{price: price.id, quantity: 3}
],```
#

That's what it would look like for you

timid mountain
#

I'm now getting the following error when I proceed to hit checkout

#

Oh nvm sorry, one sec

#

oh wow, a checkout screen ๐Ÿ’• ๐Ÿ˜ finally lol

uneven crescent
#

๐ŸŽ‰

timid mountain
#

wowwww

#

you should see my smile

#

lol, thank you SO MUCH

uneven crescent
#

lol glad we figured it out!

timid mountain
#

one very very last question, just for peace of mind, I don't actually need to pass in price to params right? just line_items with the price.id??

#

so like this?

uneven crescent
#

Yep that's totally fine to set line_items as a variable ahead of time

timid mountain
#

amazing

#

you've been incredible, thank you so much for your patience - I learned so much ๐Ÿ™‚