mirror of
https://github.com/stripe/ai.git
synced 2026-09-14 18:39:59 +08:00
4ab044a33b
* README + gitignore * Add code
114 lines
3.4 KiB
JavaScript
114 lines
3.4 KiB
JavaScript
const express = require('express');
|
|
const app = express();
|
|
const { resolve } = require('path');
|
|
// Replace if using a different env file or config
|
|
require('dotenv').config({ path: resolve(__dirname, './.env') });
|
|
|
|
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY, {
|
|
apiVersion: '2020-08-27',
|
|
});
|
|
|
|
app.use(express.static(process.env.STATIC_DIR));
|
|
app.use(
|
|
express.json({
|
|
// We need the raw body to verify webhook signatures.
|
|
// Let's compute it only when hitting the Stripe webhook endpoint.
|
|
verify: function(req, res, buf) {
|
|
if (req.originalUrl.startsWith('/webhook')) {
|
|
req.rawBody = buf.toString();
|
|
}
|
|
}
|
|
})
|
|
);
|
|
|
|
app.get('/', (req, res) => {
|
|
const path = resolve(process.env.STATIC_DIR + '/index.html');
|
|
res.sendFile(path);
|
|
});
|
|
|
|
app.get('/config', (req, res) => {
|
|
res.send({
|
|
publishableKey: process.env.STRIPE_PUBLISHABLE_KEY,
|
|
});
|
|
});
|
|
|
|
app.get('/products', (req, res) => {
|
|
|
|
// Needs to be dynamically fetched from the database
|
|
res.send([
|
|
{ id: 'fr_hike', price: 5000, currency: 'eur', name: 'French Alps hike' },
|
|
{ id: 'gb_hike', price: 2000, currency: 'gbp', name: 'Hadrians wall hike' },
|
|
{ id: 'painters_way', price: 2500, currency: 'eur', name: "Painter's Way hike" }
|
|
]);
|
|
});
|
|
|
|
|
|
app.post('/create-payment-intent', async (req, res) => {
|
|
const { currency } = req.body;
|
|
|
|
// Create a PaymentIntent with the amount, currency, and a payment method type.
|
|
// See the documentation [0] for the full list of supported parameters.
|
|
// [0] https://stripe.com/docs/api/payment_intents/create
|
|
try {
|
|
const paymentIntent = await stripe.paymentIntents.create({
|
|
amount: 1999,
|
|
currency: currency,
|
|
});
|
|
|
|
// Send publishable key and PaymentIntent details to client
|
|
res.send({
|
|
clientSecret: paymentIntent.client_secret
|
|
});
|
|
|
|
} catch(e) {
|
|
return res.status(400).send({
|
|
error: {
|
|
message: e.message
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
// Expose a endpoint as a webhook handler for asynchronous events.
|
|
// Configure your webhook in the stripe developer dashboard
|
|
// https://dashboard.stripe.com/test/webhooks
|
|
app.post('/webhook', async (req, res) => {
|
|
let data, eventType;
|
|
|
|
// Check if webhook signing is configured.
|
|
if (process.env.STRIPE_WEBHOOK_SECRET) {
|
|
// Retrieve the event by verifying the signature using the raw body and secret.
|
|
let event;
|
|
let signature = req.headers['stripe-signature'];
|
|
try {
|
|
event = stripe.webhooks.constructEvent(
|
|
req.rawBody,
|
|
signature,
|
|
process.env.STRIPE_WEBHOOK_SECRET
|
|
);
|
|
} catch (err) {
|
|
console.log(`⚠️ Webhook signature verification failed.`);
|
|
return res.sendStatus(400);
|
|
}
|
|
data = event.data;
|
|
eventType = event.type;
|
|
} else {
|
|
// Webhook signing is recommended, but if the secret is not configured in `config.js`,
|
|
// we can retrieve the event data directly from the request body.
|
|
data = req.body.data;
|
|
eventType = req.body.type;
|
|
}
|
|
|
|
if (eventType === 'payment_intent.succeeded') {
|
|
// Funds have been captured
|
|
// Fulfill any orders, e-mail receipts, etc
|
|
// To cancel the payment after capture you will need to issue a Refund (https://stripe.com/docs/api/refunds)
|
|
console.log('💰 Payment captured!');
|
|
} else if (eventType === 'payment_intent.payment_failed') {
|
|
console.log('❌ Payment failed.');
|
|
}
|
|
res.sendStatus(200);
|
|
});
|
|
|
|
app.listen(4242, () => console.log(`Node server listening at http://localhost:4242`));
|