Create {Payment|Setup}Intents Endpoint
const stripe = require("stripe")(process.env.STRIPE_SECRET_TEST_KEY);
const express = require("@runkit/runkit/express-endpoint/1.0.0");
const app = express(exports);
const bodyParser = require('body-parser');
const webhookSecret = process.env.WEBHOOK_SECRET;
const jsonParser = bodyParser.json();
const rawParser = bodyParser.raw({type: 'application/json'});
//app.use(bodyParser.json());
app.use("/", function(req, res, next) {
// Allow requests from localhost.
const allowedOrigins = ['http://localhost:8080'];
const origin = req.headers.origin;
if (allowedOrigins.indexOf(origin) > -1) {
res.setHeader("Access-Control-Allow-Headers", "*");
res.setHeader('Access-Control-Allow-Origin', origin);
}
next();
});
app.post("/payment_intents", jsonParser, async (req, res) => {
const { options } = req.body;
const paymentIntent = await stripe.paymentIntents.create(options);
res.json(paymentIntent);
});
app.post("/setup_intents", jsonParser, async (req, res) => {
const { options } = req.body;
const setupIntent = await stripe.setupIntents.create(options);
res.json(setupIntent);
});
app.post("/webhooks", rawParser, async (req, res) => {
const sig = req.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(req.body, sig, webhookSecret);
} catch (err) {
// On error, return the error message
return res.status(400).send(`Webhook Error: ${err.message}`);
}
// Do something with event
console.log('Success:', event);
// Return a response to acknowledge receipt of the event
res.json({received: true});
});
no comments