UNPKG

@lalit.fullstackdev/stripe-wrapper

Version:

Simplified Stripe wrapper for Node.js with payments, subscriptions, invoices, products, webhooks, and mobile integration

171 lines (124 loc) 4.28 kB
# Stripe Wrapper for Node.js A **simplified Stripe wrapper** for Node.js that provides **full Stripe functionality** including: * Customers management * Products and prices * Subscriptions (recurring payments) * Invoices * Payment Intents (one-time payments) * Payment history per customer * Webhook handling * Mobile app payment integration This package simplifies working with Stripe in Node.js and provides an easy-to-use interface for developers, including beginners. --- ## Installation ```bash npm install @lalit.fullstackdev/stripe-wrapper ``` --- ## Environment Setup Create a `.env` file in the project root: ```env STRIPE_SECRET_KEY=sk_test_your_secret_key STRIPE_WEBHOOK_SECRET=whsec_your_webhook_secret ``` > Do **not** commit your `.env` file. > Use `.env.example` for reference. --- ## Basic Usage ```js import StripeWrapper from "@lalit.fullstackdev/stripe-wrapper"; import dotenv from "dotenv"; dotenv.config(); const stripe = new StripeWrapper(process.env.STRIPE_SECRET_KEY); // Create a customer const customer = await stripe.createCustomer({ email: "user@example.com", name: "Test User" }); // Create a product const product = await stripe.createProduct({ name: "Pro Plan" }); // Create a recurring price const price = await stripe.createPrice({ productId: product.id, amount: 2000, currency: "usd", recurring: "month" }); // Create a subscription const subscription = await stripe.createSubscription({ customerId: customer.id, priceId: price.id }); // Create an invoice const invoice = await stripe.createInvoice({ customerId: customer.id }); // List invoices const invoices = await stripe.listInvoices(customer.id); // Create a payment intent (one-time payment) const paymentIntent = await stripe.createPaymentIntent({ amount: 5000, currency: "usd" }); // List payment history for a customer const paymentHistory = await stripe.listPayments(customer.id); ``` --- ## Mobile Payment Flow **Client (Mobile App using Stripe SDK)** ```js const paymentMethod = await stripe.createPaymentMethod({ card: cardElement }); await fetch("https://yourserver.com/pay", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ paymentMethodId: paymentMethod.id, amount: 5000 }) }); ``` **Server (Node.js with StripeWrapper)** ```js import express from "express"; import StripeWrapper from "@lalit.fullstackdev/stripe-wrapper"; import dotenv from "dotenv"; dotenv.config(); const stripe = new StripeWrapper(process.env.STRIPE_SECRET_KEY); const app = express(); app.use(express.json()); app.post("/pay", async (req, res) => { try { const { paymentMethodId, amount } = req.body; const paymentIntent = await stripe.createPaymentIntent({ amount, currency: "usd", payment_method: paymentMethodId, confirm: true }); res.json({ clientSecret: paymentIntent.client_secret }); } catch (err) { res.status(400).json({ error: err.message }); } }); // Webhook handling app.post("/webhook", stripe.handleWebhook(async (event) => { console.log("Webhook event received:", event.type); // Update DB, notify frontend/mobile app via push or sockets })); app.listen(3000, () => console.log("Server running")); ``` > Mobile apps never send raw card data to your server. The server only receives `PaymentMethod` IDs or `PaymentIntent` IDs. --- ## Webhook Handling ```js app.post("/webhook", stripe.handleWebhook(async (event) => { switch(event.type) { case "invoice.paid": await db.updateInvoice(event.data.object); frontend.notify("invoice_paid", event.data.object); break; case "customer.subscription.created": await db.addSubscription(event.data.object); frontend.notify("subscription_created", event.data.object); break; default: console.log("Unhandled event:", event.type); } })); ``` * Single function to handle all webhook events * Automatic Stripe signature verification * User callback handles DB, frontend, or notifications --- ## Testing 1. Copy `.env.example` to `.env` and fill in test API keys. 2. Run tests: ```bash npm install npm test ``` > Tests use Stripe test mode. If raw card creation is blocked, the test suite falls back to mock PaymentMethods. --- ## License MIT License