Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #429 from beabee-communityrm/refactor/payment-types
Browse files Browse the repository at this point in the history
refactor: move lots of payment related types to @type
  • Loading branch information
wpf500 authored May 16, 2024
2 parents 23b5e4f + 891df97 commit 3597c82
Show file tree
Hide file tree
Showing 24 changed files with 128 additions and 96 deletions.
2 changes: 1 addition & 1 deletion src/api/dto/PaymentFlowDto.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
6 changes: 3 additions & 3 deletions src/core/providers/payment-flow/GCProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });

Expand Down
11 changes: 6 additions & 5 deletions src/core/providers/payment-flow/StripeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });

Expand Down
49 changes: 13 additions & 36 deletions src/core/providers/payment-flow/index.ts
Original file line number Diff line number Diff line change
@@ -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<PaymentFlow>;

completePaymentFlow(joinFlow: JoinFlow): Promise<CompletedPaymentFlow>;
abstract completePaymentFlow(
joinFlow: JoinFlow
): Promise<CompletedPaymentFlow>;

getCompletedPaymentFlowData(
abstract getCompletedPaymentFlowData(
completedPaymentFlow: CompletedPaymentFlow
): Promise<CompletedPaymentFlowData>;
}
12 changes: 8 additions & 4 deletions src/core/providers/payment/GCProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -13,16 +13,20 @@ 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";

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" });

Expand Down
12 changes: 8 additions & 4 deletions src/core/providers/payment/ManualProvider.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
Expand Down
12 changes: 8 additions & 4 deletions src/core/providers/payment/StripeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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" });

Expand Down
15 changes: 6 additions & 9 deletions src/core/providers/payment/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/core/services/ContactsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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<Contact, "email" | "contributionType"> &
Partial<Contact>;

Expand Down
20 changes: 10 additions & 10 deletions src/core/services/PaymentFlowService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ 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";

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,
Expand Down
14 changes: 7 additions & 7 deletions src/core/services/PaymentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });

Expand Down
7 changes: 0 additions & 7 deletions src/core/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/core/utils/payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/core/utils/payment/gocardless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils/payment/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });

Expand Down
3 changes: 1 addition & 2 deletions src/models/JoinForm.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/type/completed-payment-flow-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Address } from "@type/address";

export interface CompletedPaymentFlowData {
firstname?: string;
lastname?: string;
billingAddress?: Address;
}
7 changes: 7 additions & 0 deletions src/type/completed-payment-flow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import JoinForm from "@models/JoinForm";

export interface CompletedPaymentFlow {
joinForm: JoinForm;
customerId: string;
mandateId: string;
}
7 changes: 7 additions & 0 deletions src/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
5 changes: 5 additions & 0 deletions src/type/payment-flow-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface PaymentFlowData {
email: string;
firstname?: string;
lastname?: string;
}
Loading

0 comments on commit 3597c82

Please sign in to comment.