UNPKG

@wepublish/api

Version:
146 lines (145 loc) 7.48 kB
import { Invoice, MemberPlan, PrismaClient, Subscription, User, UserAddress } from '@prisma/client'; import Bexio, { ContactsStatic, InvoicesStatic } from 'bexio'; import { BasePaymentProvider, CheckIntentProps, CreatePaymentIntentProps, CreateRemoteInvoiceProps, Intent, IntentState, PaymentProviderProps, WebhookForPaymentIntentProps, WebhookResponse } from '../payment-provider'; export interface BexioPaymentProviderProps extends PaymentProviderProps { apiKey: string; userId: number; countryId: number; invoiceTemplateNewMembership: string; invoiceTemplateRenewalMembership: string; unitId: number; taxId: number; accountId: number; invoiceTitleNewMembership: string; invoiceTitleRenewalMembership: string; invoiceMailSubjectNewMembership: string; invoiceMailBodyNewMembership: string; invoiceMailSubjectRenewalMembership: string; invoiceMailBodyRenewalMembership: string; markInvoiceAsOpen: boolean; prisma: PrismaClient; } export declare class BexioPaymentProvider extends BasePaymentProvider { readonly bexio: Bexio; private apiKey; private userId; private countryId; private invoiceTemplateNewMembership; private invoiceTemplateRenewalMembership; private unitId; private taxId; private accountId; private invoiceTitleNewMembership; private invoiceTitleRenewalMembership; private invoiceMailSubjectNewMembership; private invoiceMailBodyNewMembership; private invoiceMailSubjectRenewalMembership; private invoiceMailBodyRenewalMembership; private markInvoiceAsOpen; private prisma; constructor(props: BexioPaymentProviderProps); webhookForPaymentIntent(props: WebhookForPaymentIntentProps): Promise<WebhookResponse>; /** * Creates a payment intent based on the provided invoice details. * If the invoice has an associated subscription ID, it throws an error. * Otherwise, it proceeds to create a payment intent using Bexio's API. * * @param {CreatePaymentIntentProps} props - The properties required to create the payment intent. * @returns {Promise<Intent>} A promise that resolves to the created payment intent object. * @throws {NoSubscriptionIdInInvoice} Throws an error if the invoice contains a subscription ID. */ createIntent(props: CreatePaymentIntentProps): Promise<Intent>; /** * This function calls the `bexioCreate` method to create an invoice in Bexio. * It is designed to handle the creation of remote invoices, specifically for renewals. * * @async * @function * @param {CreateRemoteInvoiceProps} props - The properties required to create the remote invoice. * @param {string} props.invoice.id - The ID of the invoice. * * @returns {Promise<void>} Resolves when the remote invoice is successfully created. */ createRemoteInvoice(props: CreateRemoteInvoiceProps): Promise<void>; /** * Checks the status of a payment intent in Bexio. * It makes a direct fetch request to the Bexio API because the Bexio library * does not return the status when querying for an invoice. * * @param {CheckIntentProps} CheckIntentProps - An object with the intentID to check the status. * @returns {Promise<IntentState>} A promise that resolves to the status of the intent. * @throws {ResponseNOK} Throws an error if the response status from Bexio is not 200. * @throws {UnknownIntentState} Throws an error if the intent status is not recognized. */ checkIntentStatus({ intentID }: CheckIntentProps): Promise<IntentState>; /** * Creates an invoice in Bexio based on a provided invoice ID and optionally marks it as a renewal. * * This function performs several operations: * 1. Fetches the invoice details from the database using Prisma. * 2. Searches for the associated contact in Bexio or creates/updates the contact. * 3. Creates a new invoice in Bexio for the contact. * * @async * @function * @param {string} invoiceId - The ID of the invoice in the local database. * @param {boolean} isRenewal - Indicates whether the invoice is a renewal or a new invoice. * @param {string} successURL - The url the client gets redirected after successful invoice creation * * @throws {InvoiceNotFoundError} If the invoice or related data cannot be found in the local database. * @throws {Error} For any other generic error. * * @returns {Promise<Intent>} An object containing: * - intentID: The ID of the newly created invoice in Bexio. * - intentSecret: An empty string (as there's no secret provided in the function). * - intentData: A JSON string representation of the new invoice from Bexio. * - state: The state of the payment, set as 'submitted'. */ bexioCreate(invoiceId: string, isRenewal: boolean, successURL?: string): Promise<Intent>; /** * Creates a new contact in Bexio or updates an existing contact based on provided data. * * The function determines whether to create or update a contact based on the presence * of the `contact` parameter. If a contact is provided, it will be updated; otherwise, * a new contact will be created. The function utilizes user data, especially the user's * address, to populate or overwrite contact details in Bexio. * * @async * @function * @param {ContactsStatic.ContactSmall} contact - The existing contact details from Bexio. * If this parameter is not provided, a new contact will be created. * @param {User & {address: UserAddress | null}} user - The user's details, including the associated address. * * @returns {Promise<ContactsStatic.ContactFull>} The created or updated contact object from Bexio. */ createOrUpdateContact(contact: ContactsStatic.ContactSmall, user: User & { address: UserAddress | null; }): Promise<ContactsStatic.ContactFull>; /** * Creates and sends an invoice in Bexio based on provided contact, invoice, and renewal status. * * This function performs several operations: * 1. Validates the provided invoice and associated data. * 2. Creates a string replace map based on the subscription, user, and memberPlan. * 3. Constructs a new Bexio invoice object and creates the invoice in Bexio. * 4. Sends the created Bexio invoice to the associated contact. * * @async * @function * @param {ContactsStatic.ContactFull} contact - The contact associated with the invoice in Bexio. * @param {Invoice & {subscription: (Subscription & {memberPlan: MemberPlan; user: User}) | null}} invoice - * The invoice data and its associated subscription, member plan, and user details. * @param {boolean} isRenewal - Indicates whether the invoice is a renewal or a new invoice. * * @throws {InvoiceNotFoundError} If the provided invoice or related data is not valid. * @throws {SendingInvoiceError} If there's an issue sending the created invoice. * * @returns {Promise<InvoicesStatic.Invoice>} The created invoice object from Bexio. */ createInvoice(contact: ContactsStatic.ContactFull, invoice: Invoice & { subscription: (Subscription & { memberPlan: MemberPlan; user: User; }) | null; }, isRenewal: boolean): Promise<InvoicesStatic.Invoice>; }