-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
93 lines (79 loc) · 2.75 KB
/
server.js
File metadata and controls
93 lines (79 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import express from "express";
import Stripe from "stripe";
import path from "path";
import { fileURLToPath } from "url";
const app = express();
app.use(express.static("public"));
app.use(express.json());
// Resolve __dirname for ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Load API key from environment
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
// TODO: Replace with an existing test customer you created in Stripe
const CUSTOMER_ID = "cus_SOME_CUSTOMER_ID";
const mandateReference = `mandate_${CUSTOMER_ID}_${Date.now()}`;
const stripeAmountDue = 1500000; // example: 15000 INR
const startDate = Math.floor(Date.now() / 1000); // unix timestamp
// -------------------------------
// Create SetupIntent endpoint
// -------------------------------
app.post("/create-setup-intent", async (req, res) => {
try {
const setupIntent = await stripe.setupIntents.create({
customer: CUSTOMER_ID,
payment_method_types: ["card"],
usage: "off_session",
payment_method_options: {
card: {
mandate_options: {
reference: mandateReference,
description: `Test mandate for recurring payments`,
amount: stripeAmountDue,
amount_type: "maximum",
currency: "usd",
interval: "sporadic",
start_date: startDate,
supported_types: ["india"]
}
}
}
});
res.json({
clientSecret: setupIntent.client_secret,
});
} catch (err) {
console.error("Error creating SetupIntent:", err);
res.status(400).send({ error: { message: err.message } });
}
});
// -------------------------------
// Update customer default payment method
// -------------------------------
app.post("/update-customer-payment-method", async (req, res) => {
try {
const { paymentMethodId } = req.body;
if (!paymentMethodId) {
return res.status(400).send({ error: { message: "paymentMethodId is required" } });
}
const customer = await stripe.customers.update(CUSTOMER_ID, {
invoice_settings: { default_payment_method: paymentMethodId },
});
res.json({ success: true, customer });
} catch (err) {
console.error("Error updating customer:", err);
res.status(400).send({ error: { message: err.message } });
}
});
// -------------------------------
// Serve the HTML page
// -------------------------------
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public/index.html"));
});
// -------------------------------
app.listen(4242, () => {
console.log("Server running at http://localhost:4242");
console.log("Customer dashboard link:");
console.log(`https://dashboard.stripe.com/test/customers/${CUSTOMER_ID}`);
});