From 891df974a1ec03154159602e6d403941cbfa04ac Mon Sep 17 00:00:00 2001 From: Will Franklin Date: Thu, 16 May 2024 16:29:24 +0100 Subject: [PATCH] Move lots of payment related types to @type --- src/api/dto/PaymentFlowDto.ts | 2 +- src/core/providers/payment-flow/GCProvider.ts | 6 +-- .../providers/payment-flow/StripeProvider.ts | 11 +++-- src/core/providers/payment-flow/index.ts | 49 +++++-------------- src/core/providers/payment/GCProvider.ts | 12 +++-- src/core/providers/payment/ManualProvider.ts | 12 +++-- src/core/providers/payment/StripeProvider.ts | 12 +++-- src/core/providers/payment/index.ts | 15 +++--- src/core/services/ContactsService.ts | 4 +- src/core/services/PaymentFlowService.ts | 20 ++++---- src/core/services/PaymentService.ts | 14 +++--- src/core/utils/index.ts | 7 --- src/core/utils/payment.ts | 4 +- src/core/utils/payment/gocardless.ts | 3 +- src/core/utils/payment/stripe.ts | 2 +- src/models/JoinForm.ts | 3 +- src/type/completed-payment-flow-data.ts | 7 +++ src/type/completed-payment-flow.ts | 7 +++ src/type/index.ts | 7 +++ src/type/payment-flow-data.ts | 5 ++ src/type/payment-flow-params.ts | 4 ++ src/type/payment-flow.ts | 6 +++ src/type/payment-form.ts | 8 +++ src/type/update-contribution-result.ts | 4 ++ 24 files changed, 128 insertions(+), 96 deletions(-) create mode 100644 src/type/completed-payment-flow-data.ts create mode 100644 src/type/completed-payment-flow.ts create mode 100644 src/type/payment-flow-data.ts create mode 100644 src/type/payment-flow-params.ts create mode 100644 src/type/payment-flow.ts create mode 100644 src/type/payment-form.ts create mode 100644 src/type/update-contribution-result.ts diff --git a/src/api/dto/PaymentFlowDto.ts b/src/api/dto/PaymentFlowDto.ts index 9f5b689da..dca5df637 100644 --- a/src/api/dto/PaymentFlowDto.ts +++ b/src/api/dto/PaymentFlowDto.ts @@ -1,6 +1,6 @@ import { IsOptional, IsString } from "class-validator"; -import { PaymentFlowParams } from "@core/providers/payment-flow"; +import { PaymentFlowParams } from "@type/index"; export class GetPaymentFlowDto implements PaymentFlowParams { @IsOptional() diff --git a/src/core/providers/payment-flow/GCProvider.ts b/src/core/providers/payment-flow/GCProvider.ts index a4b5851e9..9cc187bb5 100644 --- a/src/core/providers/payment-flow/GCProvider.ts +++ b/src/core/providers/payment-flow/GCProvider.ts @@ -3,13 +3,13 @@ import { log as mainLogger } from "@core/logging"; import JoinFlow from "@models/JoinFlow"; +import { PaymentFlowProvider } from "."; import { CompletedPaymentFlow, CompletedPaymentFlowData, PaymentFlow, - PaymentFlowData, - PaymentFlowProvider -} from "."; + PaymentFlowData +} from "@type/index"; const log = mainLogger.child({ app: "gc-payment-flow-provider" }); diff --git a/src/core/providers/payment-flow/StripeProvider.ts b/src/core/providers/payment-flow/StripeProvider.ts index f43658c78..fe2618580 100644 --- a/src/core/providers/payment-flow/StripeProvider.ts +++ b/src/core/providers/payment-flow/StripeProvider.ts @@ -2,14 +2,15 @@ import { stripe } from "@core/lib/stripe"; import { log as mainLogger } from "@core/logging"; import { paymentMethodToStripeType } from "@core/utils/payment/stripe"; +import { PaymentFlowProvider } from "."; + +import JoinFlow from "@models/JoinFlow"; + import { CompletedPaymentFlow, CompletedPaymentFlowData, - PaymentFlow, - PaymentFlowProvider -} from "."; - -import JoinFlow from "@models/JoinFlow"; + PaymentFlow +} from "@type/index"; const log = mainLogger.child({ app: "stripe-payment-flow-provider" }); diff --git a/src/core/providers/payment-flow/index.ts b/src/core/providers/payment-flow/index.ts index b9d76f68a..9ed466811 100644 --- a/src/core/providers/payment-flow/index.ts +++ b/src/core/providers/payment-flow/index.ts @@ -1,46 +1,23 @@ import JoinFlow from "@models/JoinFlow"; -import JoinForm from "@models/JoinForm"; - -import { Address } from "@type/address"; - -export interface PaymentFlow { - id: string; - params: PaymentFlowParams; -} - -export interface PaymentFlowParams { - clientSecret?: string; - redirectUrl?: string; -} - -export interface PaymentFlowData { - email: string; - firstname?: string; - lastname?: string; -} - -export interface CompletedPaymentFlow { - joinForm: JoinForm; - customerId: string; - mandateId: string; -} - -export interface CompletedPaymentFlowData { - firstname?: string; - lastname?: string; - billingAddress?: Address; -} - -export interface PaymentFlowProvider { - createPaymentFlow( +import { + CompletedPaymentFlow, + CompletedPaymentFlowData, + PaymentFlow, + PaymentFlowData +} from "@type/index"; + +export abstract class PaymentFlowProvider { + abstract createPaymentFlow( joinFlow: JoinFlow, completeUrl: string, data: PaymentFlowData ): Promise; - completePaymentFlow(joinFlow: JoinFlow): Promise; + abstract completePaymentFlow( + joinFlow: JoinFlow + ): Promise; - getCompletedPaymentFlowData( + abstract getCompletedPaymentFlowData( completedPaymentFlow: CompletedPaymentFlow ): Promise; } diff --git a/src/core/providers/payment/GCProvider.ts b/src/core/providers/payment/GCProvider.ts index ed59bafc0..6f867c4c6 100644 --- a/src/core/providers/payment/GCProvider.ts +++ b/src/core/providers/payment/GCProvider.ts @@ -4,7 +4,7 @@ import moment from "moment"; import gocardless from "@core/lib/gocardless"; import { log as mainLogger } from "@core/logging"; -import { getActualAmount, PaymentForm } from "@core/utils"; +import { getActualAmount } from "@core/utils"; import { updateSubscription, createSubscription, @@ -13,8 +13,7 @@ import { } from "@core/utils/payment/gocardless"; import { calcRenewalDate } from "@core/utils/payment"; -import { PaymentProvider, UpdateContributionResult } from "."; -import { CompletedPaymentFlow } from "@core/providers/payment-flow"; +import { PaymentProvider } from "."; import Contact from "@models/Contact"; @@ -22,7 +21,12 @@ import NoPaymentMethod from "@api/errors/NoPaymentMethod"; import config from "@config"; -import { ContributionInfo } from "@type/contribution-info"; +import { + CompletedPaymentFlow, + ContributionInfo, + PaymentForm, + UpdateContributionResult +} from "@type/index"; const log = mainLogger.child({ app: "gc-payment-provider" }); diff --git a/src/core/providers/payment/ManualProvider.ts b/src/core/providers/payment/ManualProvider.ts index 7fc4ef1b5..0da90bc76 100644 --- a/src/core/providers/payment/ManualProvider.ts +++ b/src/core/providers/payment/ManualProvider.ts @@ -1,8 +1,12 @@ -import { PaymentForm } from "@core/utils"; import Contact from "@models/Contact"; -import { PaymentProvider, UpdateContributionResult } from "."; -import { CompletedPaymentFlow } from "../payment-flow"; -import { ContributionInfo } from "@type/contribution-info"; +import { PaymentProvider } from "."; + +import { + CompletedPaymentFlow, + ContributionInfo, + PaymentForm, + UpdateContributionResult +} from "@type/index"; export default class ManualProvider extends PaymentProvider { async canChangeContribution(useExistingMandate: boolean): Promise { diff --git a/src/core/providers/payment/StripeProvider.ts b/src/core/providers/payment/StripeProvider.ts index faa7aeccf..37f8b8644 100644 --- a/src/core/providers/payment/StripeProvider.ts +++ b/src/core/providers/payment/StripeProvider.ts @@ -2,12 +2,11 @@ import { ContributionType, PaymentSource } from "@beabee/beabee-common"; import { add } from "date-fns"; import Stripe from "stripe"; -import { PaymentProvider, UpdateContributionResult } from "."; -import { CompletedPaymentFlow } from "@core/providers/payment-flow"; +import { PaymentProvider } from "."; import { stripe } from "@core/lib/stripe"; import { log as mainLogger } from "@core/logging"; -import { getActualAmount, PaymentForm } from "@core/utils"; +import { getActualAmount } from "@core/utils"; import { calcRenewalDate, getChargeableAmount } from "@core/utils/payment"; import { createSubscription, @@ -22,7 +21,12 @@ import NoPaymentMethod from "@api/errors/NoPaymentMethod"; import config from "@config"; -import { ContributionInfo } from "@type/contribution-info"; +import { + CompletedPaymentFlow, + ContributionInfo, + PaymentForm, + UpdateContributionResult +} from "@type/index"; const log = mainLogger.child({ app: "stripe-payment-provider" }); diff --git a/src/core/providers/payment/index.ts b/src/core/providers/payment/index.ts index a620fa376..1371acade 100644 --- a/src/core/providers/payment/index.ts +++ b/src/core/providers/payment/index.ts @@ -1,19 +1,16 @@ import { PaymentMethod } from "@beabee/beabee-common"; import { getRepository } from "@core/database"; -import { PaymentForm } from "@core/utils"; - -import { CompletedPaymentFlow } from "@core/providers/payment-flow"; import Contact from "@models/Contact"; import ContactContribution from "@models/ContactContribution"; -import { ContributionInfo } from "@type/contribution-info"; - -export interface UpdateContributionResult { - startNow: boolean; - expiryDate: Date; -} +import { + CompletedPaymentFlow, + ContributionInfo, + PaymentForm, + UpdateContributionResult +} from "@type/index"; export abstract class PaymentProvider { protected readonly data: ContactContribution; diff --git a/src/core/services/ContactsService.ts b/src/core/services/ContactsService.ts index d19d061c3..d2d7b64a9 100644 --- a/src/core/services/ContactsService.ts +++ b/src/core/services/ContactsService.ts @@ -8,7 +8,7 @@ import { FindManyOptions, FindOneOptions, FindOptionsWhere, In } from "typeorm"; import { createQueryBuilder, getRepository } from "@core/database"; import { log as mainLogger } from "@core/logging"; -import { cleanEmailAddress, isDuplicateIndex, PaymentForm } from "@core/utils"; +import { cleanEmailAddress, isDuplicateIndex } from "@core/utils"; import { generatePassword, isValidPassword } from "@core/utils/auth"; import { generateContactCode } from "@core/utils/contact"; @@ -35,6 +35,8 @@ import { LOGIN_CODES } from "@enums/login-codes"; import { RESET_SECURITY_FLOW_TYPE } from "@enums/reset-security-flow-type"; import { RESET_SECURITY_FLOW_ERROR_CODE } from "@enums/reset-security-flow-error-code"; +import { PaymentForm } from "@type/index"; + export type PartialContact = Pick & Partial; diff --git a/src/core/services/PaymentFlowService.ts b/src/core/services/PaymentFlowService.ts index 259712b65..800b17303 100644 --- a/src/core/services/PaymentFlowService.ts +++ b/src/core/services/PaymentFlowService.ts @@ -12,14 +12,7 @@ import JoinFlow from "@models/JoinFlow"; import JoinForm from "@models/JoinForm"; import Contact from "@models/Contact"; -import { - CompletedPaymentFlow, - CompletedPaymentFlowData, - PaymentFlow, - PaymentFlowData, - PaymentFlowParams, - PaymentFlowProvider -} from "@core/providers/payment-flow"; +import { PaymentFlowProvider } from "@core/providers/payment-flow"; import StripeProvider from "@core/providers/payment-flow/StripeProvider"; import GCProvider from "@core/providers/payment-flow/GCProvider"; @@ -27,8 +20,15 @@ import DuplicateEmailError from "@api/errors/DuplicateEmailError"; import { RESET_SECURITY_FLOW_TYPE } from "@enums/reset-security-flow-type"; -import { Address } from "@type/address"; -import { CompleteUrls } from "@type/complete-urls"; +import { + Address, + CompleteUrls, + CompletedPaymentFlow, + CompletedPaymentFlowData, + PaymentFlow, + PaymentFlowData, + PaymentFlowParams +} from "@type/index"; const paymentProviders = { [PaymentMethod.StripeCard]: StripeProvider, diff --git a/src/core/services/PaymentService.ts b/src/core/services/PaymentService.ts index 7af457c4c..316bdee7c 100644 --- a/src/core/services/PaymentService.ts +++ b/src/core/services/PaymentService.ts @@ -2,23 +2,23 @@ import { MembershipStatus, PaymentMethod } from "@beabee/beabee-common"; import { getRepository } from "@core/database"; import { log as mainLogger } from "@core/logging"; -import { PaymentForm } from "@core/utils"; import { calcRenewalDate } from "@core/utils/payment"; import Contact from "@models/Contact"; import Payment from "@models/Payment"; import ContactContribution from "@models/ContactContribution"; -import { - PaymentProvider, - UpdateContributionResult -} from "@core/providers/payment"; +import { PaymentProvider } from "@core/providers/payment"; import GCProvider from "@core/providers/payment/GCProvider"; import ManualProvider from "@core/providers/payment/ManualProvider"; import StripeProvider from "@core/providers/payment/StripeProvider"; -import { CompletedPaymentFlow } from "@core/providers/payment-flow"; -import { ContributionInfo } from "@type/contribution-info"; +import { + CompletedPaymentFlow, + ContributionInfo, + PaymentForm, + UpdateContributionResult +} from "@type/index"; const log = mainLogger.child({ app: "payment-service" }); diff --git a/src/core/utils/index.ts b/src/core/utils/index.ts index df692106e..f84f8e5c6 100644 --- a/src/core/utils/index.ts +++ b/src/core/utils/index.ts @@ -3,13 +3,6 @@ import { AuthInfo } from "@type/auth-info"; import { NextFunction, Request, RequestHandler, Response } from "express"; import { QueryFailedError } from "typeorm"; -export interface PaymentForm { - monthlyAmount: number; - period: ContributionPeriod; - payFee: boolean; - prorate: boolean; -} - export function getActualAmount( amount: number, period: ContributionPeriod diff --git a/src/core/utils/payment.ts b/src/core/utils/payment.ts index a7b4654c4..3a61eda0e 100644 --- a/src/core/utils/payment.ts +++ b/src/core/utils/payment.ts @@ -7,7 +7,9 @@ import { import config from "@config"; import Contact from "@models/Contact"; import { addMonths, getYear, setYear, sub, differenceInMonths } from "date-fns"; -import { getActualAmount, PaymentForm } from "."; +import { getActualAmount } from "."; + +import { PaymentForm } from "@type/index"; /** * Calculate the contact's membership renewal date based on their membership diff --git a/src/core/utils/payment/gocardless.ts b/src/core/utils/payment/gocardless.ts index 94869edf5..cd25f3984 100644 --- a/src/core/utils/payment/gocardless.ts +++ b/src/core/utils/payment/gocardless.ts @@ -14,11 +14,12 @@ import moment from "moment"; import { log as mainLogger } from "@core/logging"; import gocardless from "@core/lib/gocardless"; -import { PaymentForm } from "@core/utils"; import { getChargeableAmount } from "@core/utils/payment"; import config from "@config"; +import { PaymentForm } from "@type/index"; + const log = mainLogger.child({ app: "gc-utils" }); function getGCChargeableAmount(paymentForm: PaymentForm): string { diff --git a/src/core/utils/payment/stripe.ts b/src/core/utils/payment/stripe.ts index 19c4dc1f6..5b2078cfa 100644 --- a/src/core/utils/payment/stripe.ts +++ b/src/core/utils/payment/stripe.ts @@ -10,10 +10,10 @@ import OptionsService from "@core/services/OptionsService"; import { stripe, Stripe } from "@core/lib/stripe"; import { log as mainLogger } from "@core/logging"; -import { PaymentForm } from "@core/utils"; import { getChargeableAmount } from "@core/utils/payment"; import config from "@config"; +import { PaymentForm } from "@type/index"; const log = mainLogger.child({ app: "stripe-utils" }); diff --git a/src/models/JoinForm.ts b/src/models/JoinForm.ts index fabdae268..8280ca21c 100644 --- a/src/models/JoinForm.ts +++ b/src/models/JoinForm.ts @@ -1,10 +1,9 @@ import { ContributionPeriod, PaymentMethod } from "@beabee/beabee-common"; import { Column } from "typeorm"; -import { PaymentForm } from "@core/utils"; import Password from "./Password"; -import { Address } from "@type/address"; +import { PaymentForm } from "@type/index"; export interface ReferralGiftForm { referralGift?: string | null; diff --git a/src/type/completed-payment-flow-data.ts b/src/type/completed-payment-flow-data.ts new file mode 100644 index 000000000..253e2d6ce --- /dev/null +++ b/src/type/completed-payment-flow-data.ts @@ -0,0 +1,7 @@ +import { Address } from "@type/address"; + +export interface CompletedPaymentFlowData { + firstname?: string; + lastname?: string; + billingAddress?: Address; +} diff --git a/src/type/completed-payment-flow.ts b/src/type/completed-payment-flow.ts new file mode 100644 index 000000000..3fb5e3fb4 --- /dev/null +++ b/src/type/completed-payment-flow.ts @@ -0,0 +1,7 @@ +import JoinForm from "@models/JoinForm"; + +export interface CompletedPaymentFlow { + joinForm: JoinForm; + customerId: string; + mandateId: string; +} diff --git a/src/type/index.ts b/src/type/index.ts index 337dac97c..396a8edef 100644 --- a/src/type/index.ts +++ b/src/type/index.ts @@ -3,6 +3,8 @@ export * from "./auth-info.js"; export * from "./callout-map-schema.js"; export * from "./callout-response-view-schema.js"; export * from "./complete-urls.js"; +export * from "./completed-payment-flow-data.js"; +export * from "./completed-payment-flow.js"; export * from "./contact-mfa-secure.js"; export * from "./contribution-info.js"; export * from "./create-contact-mfa-data.js"; @@ -14,4 +16,9 @@ export * from "./passport-local-done-callback.js"; export * from "./passport-local-strategy-options.js"; export * from "./passport-local-verify-options.js"; export * from "./passport-login-info.js"; +export * from "./payment-flow-data.js"; +export * from "./payment-flow-params.js"; +export * from "./payment-flow.js"; +export * from "./payment-form.js"; export * from "./stripe-tax-rate-create-params.js"; +export * from "./update-contribution-result.js"; diff --git a/src/type/payment-flow-data.ts b/src/type/payment-flow-data.ts new file mode 100644 index 000000000..715796549 --- /dev/null +++ b/src/type/payment-flow-data.ts @@ -0,0 +1,5 @@ +export interface PaymentFlowData { + email: string; + firstname?: string; + lastname?: string; +} diff --git a/src/type/payment-flow-params.ts b/src/type/payment-flow-params.ts new file mode 100644 index 000000000..0addf2fa9 --- /dev/null +++ b/src/type/payment-flow-params.ts @@ -0,0 +1,4 @@ +export interface PaymentFlowParams { + clientSecret?: string; + redirectUrl?: string; +} diff --git a/src/type/payment-flow.ts b/src/type/payment-flow.ts new file mode 100644 index 000000000..1c5ed8d69 --- /dev/null +++ b/src/type/payment-flow.ts @@ -0,0 +1,6 @@ +import { PaymentFlowParams } from "."; + +export interface PaymentFlow { + id: string; + params: PaymentFlowParams; +} diff --git a/src/type/payment-form.ts b/src/type/payment-form.ts new file mode 100644 index 000000000..a13f416bb --- /dev/null +++ b/src/type/payment-form.ts @@ -0,0 +1,8 @@ +import { ContributionPeriod } from "@beabee/beabee-common"; + +export interface PaymentForm { + monthlyAmount: number; + period: ContributionPeriod; + payFee: boolean; + prorate: boolean; +} diff --git a/src/type/update-contribution-result.ts b/src/type/update-contribution-result.ts new file mode 100644 index 000000000..7e6dd4cce --- /dev/null +++ b/src/type/update-contribution-result.ts @@ -0,0 +1,4 @@ +export interface UpdateContributionResult { + startNow: boolean; + expiryDate: Date; +}