@lalit.fullstackdev/stripe-wrapper
Version:
Simplified Stripe wrapper for Node.js with payments, subscriptions, invoices, products, webhooks, and mobile integration
76 lines (65 loc) • 2.22 kB
JavaScript
export default function createPayments(stripe) {
return {
async createPaymentIntent({ amount, currency = "usd", metadata = {}, description }) {
return stripe.paymentIntents.create({
amount,
currency,
metadata,
description,
automatic_payment_methods: { enabled: true },
});
},
async retrievePaymentIntent(id) {
return stripe.paymentIntents.retrieve(id);
},
async confirmPaymentIntent(id, options = {}) {
return stripe.paymentIntents.confirm(id, options);
},
async cancelPaymentIntent(id) {
return stripe.paymentIntents.cancel(id);
},
async capturePaymentIntent(id) {
return stripe.paymentIntents.capture(id);
},
async refundPayment({ charge, payment_intent, amount, reason }) {
return stripe.refunds.create({ charge, payment_intent, amount, reason });
},
async listPaymentHistory(customerId, { limit = 10, startDate, endDate } = {}) {
if (!customerId) throw new Error("customerId is required");
const created = {};
if (startDate) created.gte = Math.floor(new Date(startDate).getTime() / 1000);
if (endDate) created.lte = Math.floor(new Date(endDate).getTime() / 1000);
const paymentIntents = await stripe.paymentIntents.list({
customer: customerId,
limit,
...(Object.keys(created).length ? { created } : {}),
});
const charges = await stripe.charges.list({
customer: customerId,
limit,
...(Object.keys(created).length ? { created } : {}),
});
const history = [
...paymentIntents.data.map(pi => ({
id: pi.id,
type: "payment_intent",
amount: pi.amount,
currency: pi.currency,
status: pi.status,
created: pi.created,
description: pi.description,
})),
...charges.data.map(ch => ({
id: ch.id,
type: "charge",
amount: ch.amount,
currency: ch.currency,
status: ch.status,
created: ch.created,
description: ch.description,
})),
];
return history.sort((a, b) => b.created - a.created);
}
};
}