failed payment method handling
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
|
||||
const logger = require("../utils/logger");
|
||||
const { parseStripeError, PaymentError } = require("../utils/stripeErrors");
|
||||
|
||||
class StripeService {
|
||||
|
||||
@@ -184,8 +185,21 @@ class StripeService {
|
||||
amountCharged: amount, // Original amount in dollars
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error("Error charging payment method", { error: error.message, stack: error.stack });
|
||||
throw error;
|
||||
// Parse Stripe error into structured format
|
||||
const parsedError = parseStripeError(error);
|
||||
|
||||
logger.error("Payment failed", {
|
||||
code: parsedError.code,
|
||||
ownerMessage: parsedError.ownerMessage,
|
||||
originalError: parsedError._originalMessage,
|
||||
stripeCode: parsedError._stripeCode,
|
||||
paymentMethodId,
|
||||
customerId,
|
||||
amount,
|
||||
stack: error.stack,
|
||||
});
|
||||
|
||||
throw new PaymentError(parsedError);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,6 +219,15 @@ class StripeService {
|
||||
}
|
||||
|
||||
|
||||
static async getPaymentMethod(paymentMethodId) {
|
||||
try {
|
||||
return await stripe.paymentMethods.retrieve(paymentMethodId);
|
||||
} catch (error) {
|
||||
logger.error("Error retrieving payment method", { error: error.message, paymentMethodId });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
static async createSetupCheckoutSession({ customerId, metadata = {} }) {
|
||||
try {
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
|
||||
Reference in New Issue
Block a user