diff --git a/resources/bills.ts b/resources/bills.ts new file mode 100644 index 00000000..1eae31a0 --- /dev/null +++ b/resources/bills.ts @@ -0,0 +1,14 @@ +import { UnitConfig, UnitResponse } from "../types/common" +import { Bill } from "../types/bill" +import { BaseResource } from "./baseResource" + +export class Bills extends BaseResource { + + constructor(token: string, basePath: string, config?: UnitConfig) { + super(token, basePath + "/bills", config) + } + + public async get(id: string): Promise> { + return await this.httpGet>(`/${id}`) + } +} diff --git a/resources/index.ts b/resources/index.ts index b53f092c..bbb9b326 100644 --- a/resources/index.ts +++ b/resources/index.ts @@ -31,3 +31,4 @@ export * from "./webhooks" export * from "./chargeback" export * from "./taxForms" export * from "./creditApplication" +export * from "./bills" diff --git a/tests/bills.spec.ts b/tests/bills.spec.ts new file mode 100644 index 00000000..3881b14b --- /dev/null +++ b/tests/bills.spec.ts @@ -0,0 +1,56 @@ +import { Bill, Unit } from "../unit" + +import dotenv from "dotenv" +dotenv.config() +const unit = new Unit(process.env.UNIT_TOKEN || "test", process.env.UNIT_API_URL || "test") + +describe("Test Bills", () => { + test("Test Bill Resource", () => { + const bill: Bill = { + "type": "bill", + "id": "1", + "attributes": { + "status": "Pending", + "version": 1, + "createdAt": "2023-01-01T00:00:00.000Z", + "updatedAt": "2023-01-01T00:00:00.000Z", + "currency": "USD", + "total": 10000, + "description": "Electric bill payment" + }, + "relationships": { + "org": { + "data": { + "type": "org", + "id": "1" + } + }, + "vendor": { + "data": { + "type": "vendor", + "id": "100" + } + } + } + } + + expect(bill.type).toBe("bill") + expect(bill.attributes.status).toBe("Pending") + expect(bill.attributes.total).toBe(10000) + }) + + test("Get Bill", async () => { + const billId = process.env.TEST_BILL_ID + if (billId) { + const res = await unit.bills.get(billId) + expect(res.data.type).toBe("bill") + expect(res.data.id).toBe(billId) + expect(typeof res.data.attributes.status).toBe("string") + expect(typeof res.data.attributes.version).toBe("number") + expect(typeof res.data.attributes.createdAt).toBe("string") + expect(typeof res.data.attributes.updatedAt).toBe("string") + expect(res.data.relationships.org.data.type).toBe("org") + expect(typeof res.data.relationships.org.data.id).toBe("string") + } + }) +}) diff --git a/types/bill.ts b/types/bill.ts new file mode 100644 index 00000000..76d2e058 --- /dev/null +++ b/types/bill.ts @@ -0,0 +1,214 @@ +import { Relationship, Tags } from "./common" + +export type BillStatus = + | "Draft" + | "Pending" + | "Scheduled" + | "Paid" + | "PaymentInProgress" + | "FundsPushed" + | "DeductionPaymentFailed" + | "VendorPaymentFailed" + | "RefundInitiated" + | "Refunded" + | "CancellationInitiated" + | "Canceled" + | "Archived" + +export interface BillLineItem { + /** + * Optional. Reference identifier for the line item. + */ + reference?: string + + /** + * Optional. Description of the line item. + */ + description?: string + + /** + * Optional. Category identifier for the line item. + */ + categoryId?: string + + /** + * Quantity of the line item. + */ + quantity: number + + /** + * Unit price of the line item in cents. + */ + unitPrice: number +} + +export interface PlatformPaymentMethod { + /** + * Date only. The deduction date. + * RFC3339 format. For more information: https://en.wikipedia.org/wiki/ISO_8601#RFCs + */ + deductionDate?: string + + /** + * Date only. The expected payment date. + * RFC3339 format. For more information: https://en.wikipedia.org/wiki/ISO_8601#RFCs + */ + expectedDate?: string + + /** + * Optional. Fee amount in cents. Positive values represent fees, negative values represent rebates. + */ + feeAmount?: number +} + +export interface ExternalPaymentMethod { + /** + * Date only. The date the payment was made. + * RFC3339 format. For more information: https://en.wikipedia.org/wiki/ISO_8601#RFCs + */ + paymentDate?: string +} + +export type BillPaymentMethod = + | { ach: PlatformPaymentMethod; } + | { sameDayAch: PlatformPaymentMethod; } + | { check: PlatformPaymentMethod; } + | { wire: PlatformPaymentMethod; } + | { external: ExternalPaymentMethod; } + +export interface Bill { + /** + * Identifier of the bill resource. + */ + id: string + + /** + * Type of the bill resource. The value is always bill. + */ + type: "bill" + + /** + * JSON object representing the bill data. + */ + attributes: { + /** + * One of Draft, Pending, Scheduled, Paid, PaymentInProgress, FundsPushed, DeductionPaymentFailed, + * VendorPaymentFailed, RefundInitiated, Refunded, CancellationInitiated, Canceled, Archived. + */ + status: BillStatus + + /** + * Version of the bill resource. + */ + version: number + + /** + * The date and time the resource was created. + * RFC3339 format. For more information: https://en.wikipedia.org/wiki/ISO_8601#RFCs + */ + createdAt: string + + /** + * The date and time the resource was last updated. + * RFC3339 format. For more information: https://en.wikipedia.org/wiki/ISO_8601#RFCs + */ + updatedAt: string + + /** + * Optional. ISO 4217 currency code (e.g. "USD"). + */ + currency?: string + + /** + * Optional. Line items that make up the bill. + */ + lineItems?: BillLineItem[] + + /** + * Date only. The date of the bill. + * RFC3339 format. For more information: https://en.wikipedia.org/wiki/ISO_8601#RFCs + */ + billDate?: string + + /** + * Date only. The due date of the bill. + * RFC3339 format. For more information: https://en.wikipedia.org/wiki/ISO_8601#RFCs + */ + dueDate?: string + + /** + * Optional. Invoice number for the bill. + */ + invoiceNumber?: string + + /** + * Optional. Description of the bill. + */ + description?: string + + /** + * Optional. Tax percentage applied to the bill (0-100). + */ + tax?: number + + /** + * Optional. Total amount of the bill in cents. + */ + total?: number + + /** + * Optional. The payment method used or scheduled for this bill. + */ + paymentMethod?: BillPaymentMethod + + /** + * Optional. See [Tags](https://developers.unit.co/#tags). + */ + tags?: Tags + + /** + * Optional. Reason for payment failure, present when status is DeductionPaymentFailed or VendorPaymentFailed. + */ + failureReason?: string + } + + /** + * Describes relationships between the bill resource and other resources. + */ + relationships: { + /** + * The organization this bill belongs to. + */ + org: Relationship + + /** + * Optional. The vendor this bill is for. + */ + vendor?: Relationship + + /** + * Optional. The customer associated with this bill. + */ + customer?: Relationship + + /** + * Optional. The uploaded bill file. + */ + billFile?: Relationship + + /** + * Optional. The fee associated with this bill. + */ + fee?: Relationship + + /** + * Optional. The payment associated with this bill. + */ + payment?: Relationship + + /** + * Optional. The linked account for this bill. + */ + linkedAccount?: Relationship + } +} diff --git a/types/index.ts b/types/index.ts index 6d603b87..66b0f48f 100644 --- a/types/index.ts +++ b/types/index.ts @@ -29,4 +29,5 @@ export * from "./webhooks" export * from "./chargeback" export * from "./taxForms" export * from "./creditApplication" -export * from "./threadApplication" \ No newline at end of file +export * from "./threadApplication" +export * from "./bill" \ No newline at end of file diff --git a/unit.ts b/unit.ts index 3117df83..f4112a38 100644 --- a/unit.ts +++ b/unit.ts @@ -34,6 +34,7 @@ import { CashDeposits } from "./resources/cashDeposits" import { CreditApplications } from "./resources/creditApplication" import { RecurringRepayments } from "./resources/recurringRepayments" import { Migrations } from "./resources/migrations" +import { Bills } from "./resources/bills" export class Unit { public applications: Applications @@ -73,6 +74,7 @@ export class Unit { public creditApplications: CreditApplications public recurringRepayments: RecurringRepayments public migrations: Migrations; + public bills: Bills constructor(token: string, basePath: string, config?: UnitConfig) { // remove all trailing slashes from user-provided basePath @@ -114,6 +116,7 @@ export class Unit { this.creditApplications = new CreditApplications(token, basePath, config) this.recurringRepayments = new RecurringRepayments(token, basePath, config) this.migrations = new Migrations(token, basePath, config) + this.bills = new Bills(token, basePath, config) this.helpers = helpers }