Files
rentall-app/backend/services/stripeService.js
2025-10-06 15:41:48 -04:00

183 lines
4.6 KiB
JavaScript

const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
class StripeService {
static async getCheckoutSession(sessionId) {
try {
return await stripe.checkout.sessions.retrieve(sessionId, {
expand: ['setup_intent', 'setup_intent.payment_method']
});
} 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: "express",
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;
}
}
static async createRefund({
paymentIntentId,
amount,
metadata = {},
reason = "requested_by_customer",
}) {
try {
const refund = await stripe.refunds.create({
payment_intent: paymentIntentId,
amount: Math.round(amount * 100), // Convert to cents
metadata,
reason,
});
return refund;
} catch (error) {
console.error("Error creating refund:", error);
throw error;
}
}
static async getRefund(refundId) {
try {
return await stripe.refunds.retrieve(refundId);
} catch (error) {
console.error("Error retrieving refund:", error);
throw error;
}
}
static async chargePaymentMethod(paymentMethodId, amount, customerId, metadata = {}) {
try {
// Create a payment intent with the stored payment method
const paymentIntent = await stripe.paymentIntents.create({
amount: Math.round(amount * 100), // Convert to cents
currency: "usd",
payment_method: paymentMethodId,
customer: customerId, // Include customer ID
confirm: true, // Automatically confirm the payment
return_url: `${process.env.FRONTEND_URL || 'http://localhost:3000'}/payment-complete`,
metadata,
});
return {
paymentIntentId: paymentIntent.id,
status: paymentIntent.status,
clientSecret: paymentIntent.client_secret,
};
} catch (error) {
console.error("Error charging payment method:", error);
throw error;
}
}
static async createCustomer({ email, name, metadata = {} }) {
try {
const customer = await stripe.customers.create({
email,
name,
metadata,
});
return customer;
} catch (error) {
console.error("Error creating customer:", error);
throw error;
}
}
static async createSetupCheckoutSession({ customerId, metadata = {} }) {
try {
const session = await stripe.checkout.sessions.create({
customer: customerId,
payment_method_types: ['card', 'us_bank_account', 'link'],
mode: 'setup',
ui_mode: 'embedded',
redirect_on_completion: 'never',
setup_intent_data: {
usage: 'off_session'
},
metadata: {
type: 'payment_method_setup',
...metadata
}
});
return session;
} catch (error) {
console.error("Error creating setup checkout session:", error);
throw error;
}
}
}
module.exports = StripeService;