update payment system

This commit is contained in:
Emily
2025-04-01 19:46:07 +02:00
parent 7e093251fa
commit 70c15238a0
10 changed files with 88 additions and 106 deletions

View File

@@ -1,7 +1,7 @@
import type Event from 'stripe';
import StripeService from '../services/StripeService';
import { getPlanFromPrice, PLAN_DATA } from '../shared/data/PLANS';
import { getPlanFromPrice, getPlanFromTag, PLAN_DATA } from '../shared/data/PLANS';
import { PremiumModel } from '../shared/schema/PremiumSchema';
import { UserLimitModel } from '../shared/schema/UserLimitSchema';
@@ -29,6 +29,27 @@ async function addSubscriptionToUser(user_id: string, plan: PLAN_DATA, subscript
}
export async function onPaymentFailed(event: Event.InvoicePaymentFailedEvent) {
if (event.data.object.attempt_count == 0) return { received: true, warn: 'attempt_count = 0' }
//TODO: Send emails
const customer_id = event.data.object.customer as string;
const premiumData = await PremiumModel.findOne({ customer_id });
if (!premiumData) return { error: 'customer not found' }
const subscription_id = event.data.object.subscription as string;
await StripeService.deleteSubscription(subscription_id);
const freeSub = await StripeService.createFreeSubscription(customer_id);
await PremiumModel.updateOne({ customer_id }, { subscription_id: freeSub.id });
await addSubscriptionToUser(premiumData.user_id.toString(), getPlanFromTag('FREE'), subscription_id, event.data.object.period_start, event.data.object.period_end);
return { ok: true }
}
export async function onPaymentSuccess(event: Event.InvoicePaidEvent) {

View File

@@ -2,13 +2,16 @@ import express from 'express';
import StripeService from './services/StripeService'
import { webhookRouter } from './routers/WebhookRouter';
import { paymentRouter } from './routers/PaymentRouter';
import { connectDatabase } from './shared/services/DatabaseService';
const STRIPE_PRIVATE_KEY = process.env.STRIPE_PRIVATE_KEY;
const STRIPE_WEBHOOK_SECRET = process.env.STRIPE_WEBHOOK_SECRET;
const STRIPE_TESTMODE = process.env.STRIPE_TESTMODE === 'true';
const MONGO_CONNECTION_STRING = process.env.MONGO_CONNECTION_STRING;
StripeService.init(STRIPE_PRIVATE_KEY, STRIPE_WEBHOOK_SECRET, STRIPE_TESTMODE);
connectDatabase(MONGO_CONNECTION_STRING);
console.log('Stripe started in', STRIPE_TESTMODE ? 'TESTMODE' : 'LIVEMODE');

View File

@@ -21,12 +21,12 @@ webhookRouter.get('/', json(), async (req, res) => {
const response = await WebhookController.onPaymentSuccess(eventData);
return sendJson(res, 200, response);
}
if (eventData.type === 'invoice.payment_failed') {
const response = await WebhookController.onPaymentFailed(eventData);
return sendJson(res, 200, response);
}
// if (eventData.type === 'payment_intent.succeeded') return await onPaymentOnetimeSuccess(eventData);
// if (eventData.type === 'invoice.payment_failed') return await onPaymentFailed(eventData);
// if (eventData.type === 'customer.subscription.deleted') return await onSubscriptionDeleted(eventData);
// if (eventData.type === 'customer.subscription.created') return await onSubscriptionCreated(eventData);
// if (eventData.type === 'customer.subscription.updated') return await onSubscriptionUpdated(eventData);
} catch (ex) {
res.status(500).json({ error: ex.message });

View File

@@ -1,5 +1,6 @@
import Stripe from "stripe";
import { getPlanFromTag } from "../shared/data/PLANS";
@@ -186,22 +187,21 @@ class StripeService {
// return subscription;
// }
// async createFreeSubscription(customer_id: string) {
// if (this.disabledMode) return;
// if (!this.stripe) throw Error('Stripe not initialized');
async createFreeSubscription(customer_id: string) {
if (!this.stripe) throw Error('Stripe not initialized');
// const FREE_PLAN = getPlanFromTag('FREE');
const FREE_PLAN = getPlanFromTag('FREE');
// const subscription = await this.stripe.subscriptions.create({
// customer: customer_id,
// items: [
// { price: this.testMode ? FREE_PLAN.PRICE_TEST : FREE_PLAN.PRICE, quantity: 1 }
// ]
// });
const subscription = await this.stripe.subscriptions.create({
customer: customer_id,
items: [
{ price: this.testMode ? FREE_PLAN.PRICE_TEST : FREE_PLAN.PRICE, quantity: 1 }
]
});
// return subscription;
return subscription;
// }
}
}