refactor: enhance logging in StripeProvider to improve security and clarity by hiding sensitive information
This commit is contained in:
parent
b27d8cc505
commit
4277970074
@ -87,9 +87,7 @@ export class StripeProvider implements PaymentProvider {
|
|||||||
// user does not exist, update user with customer id
|
// user does not exist, update user with customer id
|
||||||
// in case you deleted user in database, but forgot to delete customer in Stripe
|
// in case you deleted user in database, but forgot to delete customer in Stripe
|
||||||
if (!userId) {
|
if (!userId) {
|
||||||
console.log(
|
console.log('User does not exist, update with customer id (hidden)');
|
||||||
`User ${email} does not exist, update with customer id ${customerId}`
|
|
||||||
);
|
|
||||||
await this.updateUserWithCustomerId(customerId, email);
|
await this.updateUserWithCustomerId(customerId, email);
|
||||||
}
|
}
|
||||||
return customerId;
|
return customerId;
|
||||||
@ -134,9 +132,9 @@ export class StripeProvider implements PaymentProvider {
|
|||||||
.returning({ id: user.id });
|
.returning({ id: user.id });
|
||||||
|
|
||||||
if (result.length > 0) {
|
if (result.length > 0) {
|
||||||
console.log(`Updated user ${email} with customer ID ${customerId}`);
|
console.log('Updated user with customer ID (hidden)');
|
||||||
} else {
|
} else {
|
||||||
console.log(`No user found with email ${email}`);
|
console.log('No user found with given email');
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Update user with customer ID error:', error);
|
console.error('Update user with customer ID error:', error);
|
||||||
@ -164,7 +162,7 @@ export class StripeProvider implements PaymentProvider {
|
|||||||
if (result.length > 0) {
|
if (result.length > 0) {
|
||||||
return result[0].id;
|
return result[0].id;
|
||||||
}
|
}
|
||||||
console.warn(`No user found with customerId ${customerId}`);
|
console.warn('No user found with given customerId');
|
||||||
|
|
||||||
return undefined;
|
return undefined;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -522,26 +520,20 @@ export class StripeProvider implements PaymentProvider {
|
|||||||
private async onCreateSubscription(
|
private async onCreateSubscription(
|
||||||
stripeSubscription: Stripe.Subscription
|
stripeSubscription: Stripe.Subscription
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
console.log(
|
console.log('>> Create payment record for Stripe subscription');
|
||||||
`>> Create payment record for Stripe subscription ${stripeSubscription.id}`
|
|
||||||
);
|
|
||||||
const customerId = stripeSubscription.customer as string;
|
const customerId = stripeSubscription.customer as string;
|
||||||
|
|
||||||
// get priceId from subscription items (this is always available)
|
// get priceId from subscription items (this is always available)
|
||||||
const priceId = stripeSubscription.items.data[0]?.price.id;
|
const priceId = stripeSubscription.items.data[0]?.price.id;
|
||||||
if (!priceId) {
|
if (!priceId) {
|
||||||
console.warn(
|
console.warn('No priceId found for subscription');
|
||||||
`<< No priceId found for subscription ${stripeSubscription.id}`
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get userId from metadata, we add it in the createCheckout session
|
// get userId from metadata, we add it in the createCheckout session
|
||||||
const userId = stripeSubscription.metadata.userId;
|
const userId = stripeSubscription.metadata.userId;
|
||||||
if (!userId) {
|
if (!userId) {
|
||||||
console.warn(
|
console.warn('No userId found for subscription');
|
||||||
`<< No userId found for subscription ${stripeSubscription.id}`
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -581,13 +573,9 @@ export class StripeProvider implements PaymentProvider {
|
|||||||
.returning({ id: payment.id });
|
.returning({ id: payment.id });
|
||||||
|
|
||||||
if (result.length > 0) {
|
if (result.length > 0) {
|
||||||
console.log(
|
console.log('<< Created new payment record for Stripe subscription');
|
||||||
`<< Created new payment record ${result[0].id} for Stripe subscription ${stripeSubscription.id}`
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
console.warn(
|
console.warn('<< No payment record created for Stripe subscription');
|
||||||
`<< No payment record created for Stripe subscription ${stripeSubscription.id}`
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Conditionally handle credits after subscription creation
|
// Conditionally handle credits after subscription creation
|
||||||
@ -607,16 +595,12 @@ export class StripeProvider implements PaymentProvider {
|
|||||||
private async onUpdateSubscription(
|
private async onUpdateSubscription(
|
||||||
stripeSubscription: Stripe.Subscription
|
stripeSubscription: Stripe.Subscription
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
console.log(
|
console.log('>> Update payment record for Stripe subscription');
|
||||||
`>> Update payment record for Stripe subscription ${stripeSubscription.id}`
|
|
||||||
);
|
|
||||||
|
|
||||||
// get priceId from subscription items (this is always available)
|
// get priceId from subscription items (this is always available)
|
||||||
const priceId = stripeSubscription.items.data[0]?.price.id;
|
const priceId = stripeSubscription.items.data[0]?.price.id;
|
||||||
if (!priceId) {
|
if (!priceId) {
|
||||||
console.warn(
|
console.warn('No priceId found for subscription');
|
||||||
`<< No priceId found for subscription ${stripeSubscription.id}`
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -674,9 +658,7 @@ export class StripeProvider implements PaymentProvider {
|
|||||||
.returning({ id: payment.id });
|
.returning({ id: payment.id });
|
||||||
|
|
||||||
if (result.length > 0) {
|
if (result.length > 0) {
|
||||||
console.log(
|
console.log('<< Updated payment record for Stripe subscription');
|
||||||
`<< Updated payment record ${result[0].id} for Stripe subscription ${stripeSubscription.id}`
|
|
||||||
);
|
|
||||||
|
|
||||||
// Add credits for subscription renewal
|
// Add credits for subscription renewal
|
||||||
const currentPayment = payments[0];
|
const currentPayment = payments[0];
|
||||||
@ -690,25 +672,18 @@ export class StripeProvider implements PaymentProvider {
|
|||||||
if (pricePlan?.credits?.enable) {
|
if (pricePlan?.credits?.enable) {
|
||||||
try {
|
try {
|
||||||
await addSubscriptionCredits(currentPayment.userId, priceId);
|
await addSubscriptionCredits(currentPayment.userId, priceId);
|
||||||
console.log(
|
console.log('<< Added renewal credits for user');
|
||||||
`<< Added renewal credits for user ${currentPayment.userId}, priceId: ${priceId}`
|
|
||||||
);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(
|
console.error('<< Failed to add renewal credits for user:', error);
|
||||||
`<< Failed to add renewal credits for user ${currentPayment.userId}:`,
|
|
||||||
error
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log(
|
console.log(
|
||||||
`<< No renewal credits added for user ${currentPayment.userId}, isRenewal: ${isRenewal}`
|
'<< No renewal credits added for user, isRenewal: ' + isRenewal
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.warn(
|
console.warn('<< No payment record found for Stripe subscription');
|
||||||
`<< No payment record found for Stripe subscription ${stripeSubscription.id}`
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -719,9 +694,7 @@ export class StripeProvider implements PaymentProvider {
|
|||||||
private async onDeleteSubscription(
|
private async onDeleteSubscription(
|
||||||
stripeSubscription: Stripe.Subscription
|
stripeSubscription: Stripe.Subscription
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
console.log(
|
console.log('>> Mark payment record for Stripe subscription as canceled');
|
||||||
`>> Mark payment record for Stripe subscription ${stripeSubscription.id} as canceled`
|
|
||||||
);
|
|
||||||
const db = await getDb();
|
const db = await getDb();
|
||||||
const result = await db
|
const result = await db
|
||||||
.update(payment)
|
.update(payment)
|
||||||
@ -735,12 +708,10 @@ export class StripeProvider implements PaymentProvider {
|
|||||||
.returning({ id: payment.id });
|
.returning({ id: payment.id });
|
||||||
|
|
||||||
if (result.length > 0) {
|
if (result.length > 0) {
|
||||||
console.log(
|
console.log('<< Marked payment record for subscription as canceled');
|
||||||
`<< Marked payment record for subscription ${stripeSubscription.id} as canceled`
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
console.warn(
|
console.warn(
|
||||||
`<< No payment record found to cancel for Stripe subscription ${stripeSubscription.id}`
|
'<< No payment record found to cancel for Stripe subscription'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -753,12 +724,12 @@ export class StripeProvider implements PaymentProvider {
|
|||||||
session: Stripe.Checkout.Session
|
session: Stripe.Checkout.Session
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const customerId = session.customer as string;
|
const customerId = session.customer as string;
|
||||||
console.log(`>> Handle onetime payment for customer ${customerId}`);
|
console.log('>> Handle onetime payment for customer');
|
||||||
|
|
||||||
// get userId from session metadata, we add it in the createCheckout session
|
// get userId from session metadata, we add it in the createCheckout session
|
||||||
const userId = session.metadata?.userId;
|
const userId = session.metadata?.userId;
|
||||||
if (!userId) {
|
if (!userId) {
|
||||||
console.warn(`<< No userId found for checkout session ${session.id}`);
|
console.warn('No userId found for checkout session');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -766,7 +737,7 @@ export class StripeProvider implements PaymentProvider {
|
|||||||
// const priceId = session.line_items?.data[0]?.price?.id;
|
// const priceId = session.line_items?.data[0]?.price?.id;
|
||||||
const priceId = session.metadata?.priceId;
|
const priceId = session.metadata?.priceId;
|
||||||
if (!priceId) {
|
if (!priceId) {
|
||||||
console.warn(`<< No priceId found for checkout session ${session.id}`);
|
console.warn('No priceId found for checkout session');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -790,14 +761,10 @@ export class StripeProvider implements PaymentProvider {
|
|||||||
.returning({ id: payment.id });
|
.returning({ id: payment.id });
|
||||||
|
|
||||||
if (result.length === 0) {
|
if (result.length === 0) {
|
||||||
console.warn(
|
console.warn('<< Failed to create one-time payment record for user');
|
||||||
`<< Failed to create one-time payment record for user ${userId}`
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log(
|
console.log('Created one-time payment record for user');
|
||||||
`<< Created one-time payment record for user ${userId}, price: ${priceId}`
|
|
||||||
);
|
|
||||||
|
|
||||||
// Conditionally handle credits after one-time payment
|
// Conditionally handle credits after one-time payment
|
||||||
if (websiteConfig.credits?.enableCredits) {
|
if (websiteConfig.credits?.enableCredits) {
|
||||||
@ -816,10 +783,7 @@ export class StripeProvider implements PaymentProvider {
|
|||||||
const amount = session.amount_total ? session.amount_total / 100 : 0;
|
const amount = session.amount_total ? session.amount_total / 100 : 0;
|
||||||
await sendNotification(session.id, customerId, userId, amount);
|
await sendNotification(session.id, customerId, userId, amount);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(
|
console.error('onOnetimePayment error for session: ' + session.id, error);
|
||||||
`<< onOnetimePayment error for session ${session.id}:`,
|
|
||||||
error
|
|
||||||
);
|
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -832,33 +796,33 @@ export class StripeProvider implements PaymentProvider {
|
|||||||
session: Stripe.Checkout.Session
|
session: Stripe.Checkout.Session
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const customerId = session.customer as string;
|
const customerId = session.customer as string;
|
||||||
console.log(`>> Handle credit purchase for customer ${customerId}`);
|
console.log('>> Handle credit purchase for customer');
|
||||||
|
|
||||||
// get userId from session metadata, we add it in the createCheckout session
|
// get userId from session metadata, we add it in the createCheckout session
|
||||||
const userId = session.metadata?.userId;
|
const userId = session.metadata?.userId;
|
||||||
if (!userId) {
|
if (!userId) {
|
||||||
console.warn(`<< No userId found for checkout session ${session.id}`);
|
console.warn('No userId found for checkout session');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get packageId from session metadata
|
// get packageId from session metadata
|
||||||
const packageId = session.metadata?.packageId;
|
const packageId = session.metadata?.packageId;
|
||||||
if (!packageId) {
|
if (!packageId) {
|
||||||
console.warn(`<< No packageId found for checkout session ${session.id}`);
|
console.warn('No packageId found for checkout session');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get credits from session metadata
|
// get credits from session metadata
|
||||||
const credits = session.metadata?.credits;
|
const credits = session.metadata?.credits;
|
||||||
if (!credits) {
|
if (!credits) {
|
||||||
console.warn(`<< No credits found for checkout session ${session.id}`);
|
console.warn('No credits found for checkout session');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get credit package
|
// get credit package
|
||||||
const creditPackage = getCreditPackageById(packageId);
|
const creditPackage = getCreditPackageById(packageId);
|
||||||
if (!creditPackage) {
|
if (!creditPackage) {
|
||||||
console.warn(`<< Credit package ${packageId} not found`);
|
console.warn('Credit package ' + packageId + ' not found');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -874,14 +838,9 @@ export class StripeProvider implements PaymentProvider {
|
|||||||
expireDays: creditPackage.expireDays,
|
expireDays: creditPackage.expireDays,
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(
|
console.log('Added ' + credits + ' credits to user');
|
||||||
`<< Added ${credits} credits to user ${userId} for $${amount}${creditPackage.expireDays ? ` (expires in ${creditPackage.expireDays} days)` : ' (no expiration)'}`
|
|
||||||
);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(
|
console.error('onCreditPurchase error for session: ' + session.id, error);
|
||||||
`<< onCreditPurchase error for session ${session.id}:`,
|
|
||||||
error
|
|
||||||
);
|
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user