stripe-multi-payments
v1.2.8
Published
ANode.js module for processing payments via Strapi integration
Downloads
24
Readme
stripe-multi-payments
A Node.js module for simplified payment processing via the Stripe integration in a multi-user environment for transfer to connect accounts with calculation.
Introduction
stripe-multi-payments
streamlines the process of handling payments through the Stripe API in a multi-user application. This package provides a flexible and convenient interface for developers to seamlessly integrate payment processing functionality into their Next.js/Node js projects.
Features
- Multi-User Support: Easily process payments on behalf of individual users in a multi-user system.
- Flexible Configuration: Configure the package using environment variables and your Stripe account settings.
- Automatic Stripe Charges: Automatically calculate and deduct Stripe charges from the payment amount.
Installation
Install the package using npm:
npm install stripe-multi-payments Set Up Your Stripe Account
If you don't have a Stripe account, follow these steps to create one:
- Sign up for a Stripe account
- After signing up, obtain your Stripe secret key from the Stripe Dashboard.
- Create a .env file in the root of your project and add your Stripe secret key:
- Create stripe connect accounts and configure your account for trsanfer seprate charges and obtained connect ids
Usage with Next.js API
Here's an example of using the module in a Next.js API route:
// Add Account IDs to .env File
NEXT_PUBLIC_STRIPE_SECRET_KEY=sk_test_your_actual_stripe_secret_key
NEXT_PUBLIC_STRIPE_CONNECT_ADMIN_ACCOUNT_ID=acct\***\*\*\*\*\*\*\***oB
NEXT_PUBLIC_STRIPE_CONNECT_VENDOR_ACCOUNT_ID=acct\***\*\*\*\*\*\*\***iV
// pages/api/checkout.js
//Import the Module
import stripeMultiPayments from "stripe-multi-payments";
const secretKey = process.env.NEXT_PUBLIC_STRIPE_SECRET_KEY;
export default async function handler(req, res) {
if (req.method === "POST") {
try {
const cartItems = req.body.cartItem;
const transformedItems = cartItems.map((item) => ({
price_data: {
currency: "usd",
product_data: {
name: item.name,
images: [req.headers.origin + item.image],
},
unit_amount: item.price * 100,
},
quantity: item.quantity,
}));
let successUrl = `${req.headers.origin}/success`;
let cancelUrl = `${req.headers.origin}/cancel`;
let paymentMode = "payment";
const paymentResult = await stripeMultiPayments.processPayment(
secretKey,
transformedItems,
successUrl,
cancelUrl,
paymentMode
);
res.json({ success: true, result: paymentResult });
} catch (error) {
console.error("Error processing payment:", error);
res
.status(500)
.json({ success: false, message: "Internal server error" });
}
} else {
res.setHeader("Allow", "POST");
res.status(405).end("Method Not Allowed");
}
}
// page/api/webhook
const stripeMultiPayments = require("stripe-multi-payments");
import { buffer } from "micro";
const secretKey = 'your*stripe_secret_key';
const webhookSecret = 'your_webhook_secret';
let transferGroups = {
"adminAccountId": process.env.NEXT_PUBLIC_STRIPE_CONNECT_ADMIN_ACCOUNT_ID,
"vendorAccountId": process.env.NEXT_PUBLIC_STRIPE_CONNECT_VENDOR_ACCOUNT_ID,
"vendorPayPercentange": 90 // Percentage of the amount to be paid to the vendor
}
export default async (req, res) => {
if (req.method === "POST") {
const sig = req.headers["stripe-signature"];
const body = await buffer(req);
try {
await stripeMultiPayments.handleWebhookAndTransfers(
secretKey,
webhookSecret,
body,
sig,
transferGroups
);
} catch (error) {
return res.status(400).send(`Webhook error: ${error.message}`);
}
} else {
res.setHeader("Allow", ["POST", "GET"]);
res.status(405).end("Method Not Allowed");
}
};
export const config = {
api: {
bodyParser: false,
externalResolver: true,
},
};
Make sure to replace placeholders like 'your_stripe_secret_key', 'your_webhook_secret', and others with your actual values.
Feel free to customize the code based on your project's needs and structure.
License
This project is licensed under the MIT License - see the LICENSE.md file for details.