Skip to content

Instantly share code, notes, and snippets.

@dayhaysoos
Created April 22, 2021 22:07
Show Gist options
  • Save dayhaysoos/82e27a1b6e22129a3276d9bd727061fa to your computer and use it in GitHub Desktop.
Save dayhaysoos/82e27a1b6e22129a3276d9bd727061fa to your computer and use it in GitHub Desktop.
app.post("/charge", async function (req, res) {
const {
email,
stripeToken,
address,
name,
city,
state,
zip,
country,
} = req.body;
const { itemId } = req.query;
try {
const result = await stripe.charges.create({
amount: ITEM_DATA[itemId].amount,
currency: "usd",
description: ITEM_DATA[itemId].title,
source: stripeToken,
receipt_email: email,
shipping: {
name,
address: {
line1: address,
city,
country,
state,
postal_code: zip,
},
},
});
var formatter = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
});
res.render("success", {
amount: formatter.format(result.amount / 100),
receipt_email: result.receipt_email,
description: result.description,
receipt_url: result.receipt_url,
chargeId: result.id,
});
} catch (error) {
console.log(error);
res.send(error);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment