UNPKG

@wepublish/api

Version:
128 lines (127 loc) 4.53 kB
import { Invoice, InvoiceItem, MetadataProperty, Payment, PaymentState, PrismaClient, Subscription } from '@prisma/client'; import { NextHandleFunction } from 'connect'; import express from 'express'; import DataLoader from 'dataloader'; export type InvoiceWithItems = Invoice & { items: InvoiceItem[]; }; export type InvoicesByID = DataLoader<string, InvoiceWithItems | null>; export type PaymentsByID = DataLoader<string, Payment | null>; export interface WebhookForPaymentIntentProps { req: express.Request; } export interface IntentState { paymentID: string; state: PaymentState; paidAt?: Date; paymentData?: string; customerID?: string; } export interface CreatePaymentIntentProps { paymentID: string; invoice: InvoiceWithItems; saveCustomer: boolean; customerID?: string; successURL?: string; failureURL?: string; } export interface CheckIntentProps { intentID: string; } export interface UpdateRemoteSubscriptionAmountProps { newAmount: number; subscription: Subscription & { properties: MetadataProperty[]; }; } export interface CancelRemoteSubscriptionProps { subscription: Subscription & { properties: MetadataProperty[]; }; } export interface CreateRemoteInvoiceProps { subscription: Subscription & { properties: MetadataProperty[]; }; invoice: Invoice & { items: InvoiceItem[]; }; } export interface UpdatePaymentWithIntentStateProps { intentState: IntentState; paymentClient: PrismaClient['payment']; paymentsByID: PaymentsByID; invoicesByID: InvoicesByID; subscriptionClient: PrismaClient['subscription']; userClient: PrismaClient['user']; invoiceClient: PrismaClient['invoice']; subscriptionPeriodClient: PrismaClient['subscriptionPeriod']; invoiceItemClient: PrismaClient['invoiceItem']; } export interface WebhookUpdatesProps { payment: Payment; body: any; headers: any; } export interface GetInvoiceIDFromWebhookProps { body: any; headers: any; } export interface Intent { intentID: string; intentSecret: string; state: PaymentState; paidAt?: Date; intentData?: string; paymentData?: string; errorCode?: string; } export type WebhookResponse = { status: number; message?: string; paymentStates?: IntentState[]; }; export interface PaymentProvider { id: string; name: string; offSessionPayments: boolean; remoteManagedSubscription: boolean; incomingRequestHandler: NextHandleFunction; webhookForPaymentIntent(props: WebhookForPaymentIntentProps): Promise<WebhookResponse>; createIntent(props: CreatePaymentIntentProps): Promise<Intent>; checkIntentStatus(props: CheckIntentProps): Promise<IntentState>; updatePaymentWithIntentState(props: UpdatePaymentWithIntentStateProps): Promise<Payment>; updateRemoteSubscriptionAmount(props: UpdateRemoteSubscriptionAmountProps): Promise<void>; cancelRemoteSubscription(props: CancelRemoteSubscriptionProps): Promise<void>; createRemoteInvoice(props: CreateRemoteInvoiceProps): Promise<void>; } export interface PaymentProviderProps { id: string; name: string; offSessionPayments: boolean; incomingRequestHandler?: NextHandleFunction; } export declare abstract class BasePaymentProvider implements PaymentProvider { readonly id: string; readonly name: string; readonly offSessionPayments: boolean; readonly remoteManagedSubscription: boolean; readonly incomingRequestHandler: NextHandleFunction; protected constructor(props: PaymentProviderProps); abstract webhookForPaymentIntent(props: WebhookForPaymentIntentProps): Promise<WebhookResponse>; abstract createIntent(props: CreatePaymentIntentProps): Promise<Intent>; abstract checkIntentStatus(props: CheckIntentProps): Promise<IntentState>; updateRemoteSubscriptionAmount(props: UpdateRemoteSubscriptionAmountProps): Promise<void>; cancelRemoteSubscription(props: CancelRemoteSubscriptionProps): Promise<void>; createRemoteInvoice(props: CreateRemoteInvoiceProps): Promise<void>; updatePaymentWithIntentState({ intentState, paymentClient, paymentsByID, invoicesByID, subscriptionClient, userClient, invoiceClient }: UpdatePaymentWithIntentStateProps): Promise<Payment>; /** * adding or updating paymentProvider customer ID for user * * @param userClient * @param subscription * @param customerID * @private */ private updatePaymentProvider; }