Files
rentall-app/backend/services/stripeService.js
2025-08-29 00:32:06 -04:00

121 lines
2.8 KiB
JavaScript

const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
class StripeService {
static async createCheckoutSession({
item_name,
total,
return_url,
metadata = {},
}) {
try {
const sessionConfig = {
line_items: [
{
price_data: {
currency: "usd",
product_data: {
name: item_name,
},
unit_amount: total * 100,
},
quantity: 1,
},
],
mode: "payment",
ui_mode: "embedded",
return_url: return_url,
metadata: metadata,
};
const session = await stripe.checkout.sessions.create(sessionConfig);
return session;
} catch (error) {
console.error("Error creating checkout session:", error);
throw error;
}
}
static async getCheckoutSession(sessionId) {
try {
return await stripe.checkout.sessions.retrieve(sessionId);
} catch (error) {
console.error("Error retrieving checkout session:", error);
throw error;
}
}
static async createConnectedAccount({ email, country = "US" }) {
try {
const account = await stripe.accounts.create({
type: "standard",
email,
country,
capabilities: {
transfers: { requested: true },
},
});
return account;
} catch (error) {
console.error("Error creating connected account:", error);
throw error;
}
}
static async createAccountLink(accountId, refreshUrl, returnUrl) {
try {
const accountLink = await stripe.accountLinks.create({
account: accountId,
refresh_url: refreshUrl,
return_url: returnUrl,
type: "account_onboarding",
});
return accountLink;
} catch (error) {
console.error("Error creating account link:", error);
throw error;
}
}
static async getAccountStatus(accountId) {
try {
const account = await stripe.accounts.retrieve(accountId);
return {
id: account.id,
details_submitted: account.details_submitted,
payouts_enabled: account.payouts_enabled,
capabilities: account.capabilities,
requirements: account.requirements,
};
} catch (error) {
console.error("Error retrieving account status:", error);
throw error;
}
}
static async createTransfer({
amount,
currency = "usd",
destination,
metadata = {},
}) {
try {
const transfer = await stripe.transfers.create({
amount: Math.round(amount * 100), // Convert to cents
currency,
destination,
metadata,
});
return transfer;
} catch (error) {
console.error("Error creating transfer:", error);
throw error;
}
}
}
module.exports = StripeService;