I've been trying to build an Ecommerce website using Commerce JS. I'm stuck at 'Capturing Order' phase. Here is my orderData code that I'm sending to Commerce JS to capture, but it returns an error code of 401 & 422. 1st error is this:
{
"status_code": 401,
"error": {
"message": "You did not provide an API key. You need to provide your API key in the X-Authorization header (e.g. 'X-Authorization: YOUR_API_KEY').",
"type": "authentication_error"
}
}
Now if I've not provided API key then how am I able to use other functions of Commerce JS.
2nd error is: 'Line item ID not found', on the contrary line item ID is present with each item and it is maintained by Commmerce.js itself, I can see the line item ID in my console.
line_items : checkoutToken.live.line_items,
customer: {
firstname : shippingData.firstName,
lastname: shippingData.lastName,
email: shippingData.email
},
shipping: {
name :"Primary",
street: shippingData.address1,
town_city: shippingData.city,
county_state: shippingData.shippingSubdivision,
postal_zip_code: shippingData.zip,
country: shippingData.shippingCountry
},
fulfillment: {
shipping_method: shippingData.shippingOption
},
billing:{
"name": "John Doe",
"street": "234 Fake St",
"town_city": "San Francisco",
"county_state": "US-CA",
"postal_zip_code": "94103",
"country": "US"
},
payment : {
gateway: 'stripe',
stripe: {
payment_method_id: paymentMethod.id
}
}

You haven't shown any code that actually uses Commerce.js here. Your first problem is simply that your API key is not being provided to Commerce.js when you construct it:
It's likely that you're using an environment variable here, and it's possible that the environment variable isn't being loaded correctly. In this case please ensure that the
.envfile in your project is in the project root folder (same level as package.json), and that it has the environment variable defined correctly as referenced by your code.Regarding the second error, the code you've shared with us that relates to this error is
line_items : checkoutToken.live.line_items. It looks like you are passing your line items list directly from the live object into the capture method. This does not provide the information in the way Commerce.js expects it.This is what your
line_itemsobject should look like:The
quantityargument is optional, and will use your line item's stored quantity if you don't provided it (see your checkout token to find out what the quantity is - it's 1 by default).variantsis also optional. If your product has no variants, or you've previously used the "check variant" helper, then you may not need to provide this. Nevertheless it's always good to provide this information for clarity if you can.