server.js
Keep the PriceEdge API key on the server. Create a fresh invoice whenever the order total changes, then return the resulting invoice details to the browser.
const PE = "https://priceedge.me/api/invoice"
const key = process.env.PRICEEDGE_API_KEY
const headers = {
"content-type": "application/json",
authorization: `Bearer ${key}`
}
const body = {
"invoice-nbr": order.id,
amt: order.total.toFixed(2)
}
const response = await fetch(PE, {
method: "POST",
headers,
body: JSON.stringify(body)
})
const invoice = await response.json()
return {
invoiceNumber: invoice["invoice-nbr"],
amount: order.total.toFixed(2)
}