I’m trying to use square hosted checkout for my pizza business. It looks as though the data I’m sending is valid, as I had various issues I fixed with validity before this one. I was getting 401 errors for a bit, so changed my access token, and now I’m getting 404s.
Any help would be appreciated,
Cucumberbob
const uuid = require("uuid");
const { Client, Environment } = require("square");
const locationId = "LV9V6ND34HHBS";
const squareAccessToken =
process.env.squareAccessToken ||
"redacted"
const client = new Client({
environment: Environment.Sandbox,
accessToken: squareAccessToken
});
app.post("/api/checkout", async (req, res) => {
const pizzas = req.body.pizzas;
const response = await client.checkoutApi.createCheckout(
locationId,
convertPizzasToCheckoutRequest(pizzas)
);
console.log(response.json());
return res.status(200).send("OK");
});
function convertPizzasToCheckoutRequest(pizzas) {
const lineItems = pizzas.map((pizza) => {
const itemName = { sm: "Small", lg: "Large" }[pizza.size].concat(" Pizza");
const prunedToppings = pizza.toppings.filter((t) => t !== "Mozzarella");
//todo fix pluralisation
const variation = `With ${prunedToppings.length} toppings`;
const note = prunedToppings.reduce((toppingList, topping) => {
if (toppingList === "") return topping;
return toppingList.concat(`, ${topping}`);
}, "");
const basePrice =
{ sm: 3, lg: 6 }[pizza.size] +
{ sm: 0.5, lg: 1 }[pizza.size] * prunedToppings.length;
return {
idempotencyKey: uuid.v4(),
quantity: 1,
name: itemName,
variation_name: variation,
note,
base_price_money: { amount: basePrice, currency: "GBP" }
};
});
return {
order: { locationId, line_items: lineItems },
idempotencyKey: uuid.v4(),
ask_for_shipping_address: true
};
}