refund and delayed charge

This commit is contained in:
jackiettran
2025-09-04 16:44:47 -04:00
parent b59fc07fc3
commit b22e4fa910
19 changed files with 1255 additions and 594 deletions

View File

@@ -1,44 +1,12 @@
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);
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;
@@ -115,6 +83,97 @@ class StripeService {
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',
metadata: {
type: 'payment_method_setup',
...metadata
}
});
return session;
} catch (error) {
console.error("Error creating setup checkout session:", error);
throw error;
}
}
}
module.exports = StripeService;