#DOWNTOWN-tax-computation
1 messages ยท Page 1 of 1 (latest)
So you'll need to add a separate function call inside that if statement (line 13) which creates a Price object: https://stripe.com/docs/api/prices/create#create_price-tax_behavior
Then you'll need to pass in the Price's ID to line_items.price in params so that your checkout session is created with the new Price.
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',
});
Yeah, you have to create the Price object in order to set tax_behavior and resolve the error you're getting
but I need to still set things like unit_amount, etc?
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
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?
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
๐ stepping in here. Give me a moment to catch up
To be a bit more clear about what I'm doing here, I have a product component which uses use-shopping-cart to add the Sanity CMS item (incl. name, price, description) to cart as an object, then cartSummary handles checkout (see screenshot) with redirectToCheckout, passing in the sessionId
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:
if it helps at all, the project follows the following structure: https://www.sanity.io/guides/building-ecommerce-sites-with-the-stripe-api
With the following config, I still get this error:
Okay yep so looks like the way you are creating your Price is the issue.
here's the repo also : https://github.com/AndrewNow/Expose-Noir
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
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
Yeah I mean that is completely up to you. What does your line_items variable contain?
I'd assume that is your cart data?
yeah, it would be, although for some reason my console.log result isn't appearing
on line 28
why would that be?
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
That's because the Price creation is failing
That is the error you are seeing
Stating you need to specify a product or product_data
right, right
Can you add a log for line_items before you create the Price?
You might need to await your validateCartItems but I have no idea what that function is doing so can't really say....
Ah thanks
Hmm okay in that case let's log out sanityData and cartItems individually then
And see what those contain
same thing, for some reason they don't log out
We should see an undefined or something before the error though?
Where did you add the logs?
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?
Did you add the log right after you created those variables? Like:
console.log(cartItems)```?
And comment out the Checkout session creation?
Yes
Comment that out
Let's see if you are getting your data at all
Without these other errors coming into play
commenting out 52 and 54 just gets rid of the errors but doesn't show the console.logs
@timid mountain ๐ taking over but there's a ton of words and screenshot of code. What's still blocking you exactly?
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
Do you have an example price id?
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
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?
I could try and explain but it's probably best I just link you this, since this is where I got the project structure from: https://www.sanity.io/guides/building-ecommerce-sites-with-the-stripe-api
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
cart.js is the api for the checkout session, it's definitely being called because prior to trying to add the price object I was able to complete payments successfully just fine
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
Correct to create a Price you need a Product id. It's all documented as https://stripe.com/docs/api/prices/create
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?
sorry you sahre so many pictures of code it's hard to parse
Sorry but what part is your question?
See first paragraph of my last message
Okay I had to step away for a few but now I'm back @timid mountain, give me a moment
Sorry guys, I know this is all confusing, I really do sincerely appreciate your patience
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
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
Okay can you comment out everything in the try block then except for const cartItems = req.body console.log(cartItems)
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
Ok so that is the place to start debugging then
What is triggering that endpoint to be hit?
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
So you said this was working previously, yes? Before you started playing with auto_tax?
yeah correct, 100% working just fine
Do you want to see the result?
it's multiple pages long though
Is there a specific error in the response?
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
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?
It's the response I'm getting within vscode so yeah
I still have everything within the try commented out
I'm sorry, I'm not sure what you mean by this exactly?
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?
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.
Perfect so if you do log out req.body within the handler you get the above body that you screenshotted, correct?
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
Alright great
๐
Sorry, I should've thought to check the terminal. I didn't know it would log something different from the dev tools
All good
So now you need to parse the data in those variables and pass the necessary pieces to create the Price
So as we noted, you need to pass currency product_data unit_amount and tax_behavior when you create the price: https://stripe.com/docs/api/prices/create
So you will want to pull out the necessary properties from your cartItems to do this
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?
Are you able to do cartItems[0].currency in this case? I actually don't remember if that will work or not here
"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?
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 ?
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
right
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
is this not what happens on line 39?
38*
It does, but the issue is you are attempting to use auto_tax which means you need a tax_behavior set for the Price
which is on line 25 no?
So if you look at the above logged out variable for price_data there is no tax_behavior property
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?
oh ok ok
Really up to you
Except
You don't pass quantity there
You pass quantity to the Checkout Session
You do need the line_items param
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
๐
lol glad we figured it out!
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?
Yep that's totally fine to set line_items as a variable ahead of time