From 2e953d4b95a2614c5c67ded6dd4426db7e4edd08 Mon Sep 17 00:00:00 2001 From: Uzo-Felix Date: Wed, 31 Jul 2024 23:19:39 +0100 Subject: [PATCH 1/7] fix: payment --- src/controllers/PaymentController.ts | 12 ++++++------ src/models/payment.ts | 10 ++++++---- src/services/payment/flutter.service.ts | 16 +++++++++++++--- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/controllers/PaymentController.ts b/src/controllers/PaymentController.ts index 4c181497..faadfd86 100644 --- a/src/controllers/PaymentController.ts +++ b/src/controllers/PaymentController.ts @@ -24,13 +24,13 @@ export class PaymentController { const { transactionId } = req.params; const verificationResponse = await verifyPayment(transactionId); - const paymentRepository = dataSource.getRepository(Payment); - const payment = await paymentRepository.findOneBy({ id: transactionId }); + // const paymentRepository = dataSource.getRepository(Payment); + // const payment = await paymentRepository.findOneBy({ id: transactionId }); - if (payment) { - // payment.status = verificationResponse.status === 'successful' ? 'completed' : 'failed'; - await paymentRepository.save(payment); - } + // if (payment) { + // // payment.status = verificationResponse.status === 'successful' ? 'completed' : 'failed'; + // await paymentRepository.save(payment); + // } return res.json(verificationResponse); } catch (error) { diff --git a/src/models/payment.ts b/src/models/payment.ts index c0a66b93..895cd17c 100644 --- a/src/models/payment.ts +++ b/src/models/payment.ts @@ -21,6 +21,12 @@ export class Payment { @Column() currency: string; + @Column() + productId: string; + + @Column() + paymentServiceId: string; + @Column({ type: "enum", enum: ["pending", "completed", "failed"], @@ -47,10 +53,6 @@ export class Payment { }) organization: Organization | null; - @Column({ nullable: true }) - @IsEmail() - payer_email: string; - @Column({ nullable: true }) description: string; diff --git a/src/services/payment/flutter.service.ts b/src/services/payment/flutter.service.ts index f93f4aa5..327740ed 100644 --- a/src/services/payment/flutter.service.ts +++ b/src/services/payment/flutter.service.ts @@ -49,6 +49,7 @@ export const initializePayment = async ( description: `Payment of ${detailsWithoutUserId.amount} ${detailsWithoutUserId.currency} via Flutterwave`, metadata: { tx_ref, flutterwave_response: response }, // status: response.data.status, + id: response.data.metadata.data.id, status: "completed", provider: "flutterwave", }); @@ -72,10 +73,12 @@ export const verifyPayment = async (transactionId: string): Promise => { const paymentStatus = response.data.status === "successful" ? "completed" : "failed"; - await updatePaymentStatus(transactionId, paymentStatus); + await updatePaymentStatus(transactionIdNumber, paymentStatus); + console.log(response); return response; } catch (error) { + console.log(error); throw error; } }; @@ -99,9 +102,16 @@ async function saveTransactionToDatabase( * @param {'completed' | 'failed'} status - The new status of the payment. */ async function updatePaymentStatus( - transactionId: string, + transactionIdNumber: number, status: "completed" | "failed", ): Promise { const paymentRepository = AppDataSource.getRepository(Payment); - await paymentRepository.update({ id: transactionId }, { status }); + await paymentRepository + .createQueryBuilder() + .update(Payment) + .set({ status }) + .where(`metadata->'data'->>'id' = :transactionIdNumber`, { + transactionIdNumber, + }) + .execute(); } From 49cafae90b0d985a918d2d5b2567091ad14bca4b Mon Sep 17 00:00:00 2001 From: Uzo-Felix Date: Thu, 1 Aug 2024 01:21:26 +0100 Subject: [PATCH 2/7] feat: billing plan entity --- src/models/billing-plan.ts | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/models/billing-plan.ts diff --git a/src/models/billing-plan.ts b/src/models/billing-plan.ts new file mode 100644 index 00000000..63639a0c --- /dev/null +++ b/src/models/billing-plan.ts @@ -0,0 +1,44 @@ +import { + Entity, + PrimaryGeneratedColumn, + Column, + ManyToOne, + OneToMany, +} from "typeorm"; +import { Organization } from "./organization"; +import { Payment } from "./payment"; + +@Entity() +export class BillingPlan { + @PrimaryGeneratedColumn("uuid") + id: string; + + @Column("uuid") + organizationId: string; + + @Column() + name: string; + + @Column("decimal", { precision: 10, scale: 2 }) + price: number; + + @Column() + currency: string; + + @Column() + duration: string; + + @Column({ nullable: true }) + description: string; + + @Column("simple-array") + features: string[]; + + @ManyToOne(() => Organization, (organization) => organization.billingPlans, { + onDelete: "CASCADE", + }) + organization: Organization; + + @OneToMany(() => Payment, (payment) => payment.billingPlan) + payments: Payment[]; +} From a6be6eaadfc24f4f91728e739ca7db33bc82b7ab Mon Sep 17 00:00:00 2001 From: Uzo-Felix Date: Thu, 1 Aug 2024 01:22:55 +0100 Subject: [PATCH 3/7] fix: update the organization entity --- src/models/organization.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/models/organization.ts b/src/models/organization.ts index e49727b0..773d1893 100644 --- a/src/models/organization.ts +++ b/src/models/organization.ts @@ -11,6 +11,7 @@ import { User } from "."; import { v4 as uuidv4 } from "uuid"; import { UserOrganization } from "./user-organisation"; import ExtendedBaseEntity from "./extended-base-entity"; +import { BillingPlan } from "./billing-plan"; import { Payment } from "./payment"; @Entity() @@ -66,6 +67,9 @@ export class Organization extends ExtendedBaseEntity { @OneToMany(() => Payment, (payment) => payment.organization) payments: Payment[]; + @OneToMany(() => BillingPlan, (billingPlan) => billingPlan.organization) + billingPlans: BillingPlan[]; + @BeforeInsert() generateSlug() { this.slug = uuidv4(); From c60919bb9ee2b0ce5ed91a943e472364488ae8b7 Mon Sep 17 00:00:00 2001 From: Uzo-Felix Date: Thu, 1 Aug 2024 01:23:39 +0100 Subject: [PATCH 4/7] fix: update the user entity --- src/models/user.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/models/user.ts b/src/models/user.ts index 85334b53..fa8602eb 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -19,7 +19,6 @@ import { UserRole } from "../enums/userRoles"; import { getIsInvalidMessage } from "../utils"; import ExtendedBaseEntity from "./extended-base-entity"; import { Like } from "./like"; -import { Payment } from "./payment"; import { UserOrganization } from "./user-organisation"; @Entity() @@ -82,9 +81,6 @@ export class User extends ExtendedBaseEntity { @OneToMany(() => Sms, (sms) => sms.sender, { cascade: true }) sms: Sms[]; - @OneToMany(() => Payment, (payment) => payment.user) - payments: Payment[]; - @ManyToMany(() => Organization, (organization) => organization.users, { cascade: true, }) From 987a34d8837ce646a954057d2853c0a79efc01bf Mon Sep 17 00:00:00 2001 From: Uzo-Felix Date: Thu, 1 Aug 2024 01:24:17 +0100 Subject: [PATCH 5/7] fix: update the payment entity --- src/models/payment.ts | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/models/payment.ts b/src/models/payment.ts index 895cd17c..2ee631e4 100644 --- a/src/models/payment.ts +++ b/src/models/payment.ts @@ -6,15 +6,17 @@ import { UpdateDateColumn, ManyToOne, } from "typeorm"; -import { IsEmail } from "class-validator"; -import { User } from "./user"; import { Organization } from "./organization"; +import { BillingPlan } from "./billing-plan"; @Entity() export class Payment { @PrimaryGeneratedColumn("uuid") id: string; + @Column("uuid") + billingPlanId: string; + @Column("decimal", { precision: 10, scale: 2 }) amount: number; @@ -22,10 +24,7 @@ export class Payment { currency: string; @Column() - productId: string; - - @Column() - paymentServiceId: string; + paymentServiceId: string | null; @Column({ type: "enum", @@ -39,12 +38,6 @@ export class Payment { }) provider: "stripe" | "flutterwave" | "lemonsqueezy" | "paystack"; - @Column("uuid", { nullable: true }) - userId: string | null; - - @ManyToOne(() => User, (user) => user.payments, { nullable: true }) - user: User | null; - @Column("uuid", { nullable: true }) organizationId: string | null; @@ -53,6 +46,11 @@ export class Payment { }) organization: Organization | null; + @ManyToOne(() => BillingPlan, (billingPlan) => billingPlan.payments, { + onDelete: "CASCADE", + }) + billingPlan: BillingPlan; + @Column({ nullable: true }) description: string; From 5db5fd4538ec3d3d6499fb6a7f095f3fd964fad5 Mon Sep 17 00:00:00 2001 From: Uzo-Felix Date: Thu, 1 Aug 2024 01:25:03 +0100 Subject: [PATCH 6/7] fix: update the lemonsqueezy controller --- src/controllers/PaymentLemonSqueezyController.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/controllers/PaymentLemonSqueezyController.ts b/src/controllers/PaymentLemonSqueezyController.ts index 47f530d4..6d0c174d 100644 --- a/src/controllers/PaymentLemonSqueezyController.ts +++ b/src/controllers/PaymentLemonSqueezyController.ts @@ -117,7 +117,6 @@ export const LemonSqueezyWebhook = async (req: Request, res: Response) => { currency, status: mappedStatus, provider: "lemonsqueezy", - payer_email, created_at, updated_at, }); From ab4f0875115e8896316f5f9d02e966f85dc18c6a Mon Sep 17 00:00:00 2001 From: Uzo-Felix Date: Thu, 1 Aug 2024 09:23:37 +0100 Subject: [PATCH 7/7] fix: modified payment schemas --- src/models/payment.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/models/payment.ts b/src/models/payment.ts index 2ee631e4..43300021 100644 --- a/src/models/payment.ts +++ b/src/models/payment.ts @@ -14,7 +14,7 @@ export class Payment { @PrimaryGeneratedColumn("uuid") id: string; - @Column("uuid") + @Column("uuid", { nullable: true }) billingPlanId: string; @Column("decimal", { precision: 10, scale: 2 }) @@ -23,7 +23,7 @@ export class Payment { @Column() currency: string; - @Column() + @Column({ nullable: true }) paymentServiceId: string | null; @Column({