|
| 1 | +import { Include, Meta, UnitConfig, UnitResponse } from "../types/common" |
| 2 | +import { CheckPayment, CheckPaymentStatus, ApproveCheckPaymentRequest, ReturnCheckPaymentRequest, BaseCheckPaymentListParams } from "../types/checkPayment" |
| 3 | +import { BaseResource } from "./baseResource" |
| 4 | +import { Account, Customer, Transaction } from "../types" |
| 5 | +import {responseEncoding, ResponseType} from "axios" |
| 6 | + |
| 7 | +export class CheckPayments extends BaseResource { |
| 8 | + constructor(token: string, basePath: string, config?: UnitConfig) { |
| 9 | + super(token, basePath + "/check-payments", config) |
| 10 | + } |
| 11 | + |
| 12 | + public async get(id: string, include?: string): Promise<UnitResponse<CheckPayment>> { |
| 13 | + return this.httpGetWithInclude<UnitResponse<CheckPayment> & Include<Account[] | Customer[] | Transaction[]> >(`/${id}`, include) |
| 14 | + } |
| 15 | + |
| 16 | + public async return(request: ReturnCheckPaymentRequest): Promise<UnitResponse<CheckPayment>> { |
| 17 | + return this.httpPost<UnitResponse<CheckPayment>>(`/${request.id}/return`, {data: request.data}) |
| 18 | + } |
| 19 | + |
| 20 | + public async approve(request: ApproveCheckPaymentRequest): Promise<UnitResponse<CheckPayment>> { |
| 21 | + return this.httpPost<UnitResponse<CheckPayment>>(`/${request.id}/approve`, {data: request.data}) |
| 22 | + } |
| 23 | + |
| 24 | + public async getImage(id: string, front = true, responseEncoding: responseEncoding = "binary", responseType: ResponseType = "blob"): Promise<string> { |
| 25 | + const p = front ? "front" : "back" |
| 26 | + return this.httpGet<string>(`/${id}/${p}`, {responseEncoding, responseType}) |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | + public async list(params?: CheckPaymentListParams): Promise<UnitResponse<CheckPayment[]> & Meta> { |
| 31 | + const parameters: any = { |
| 32 | + "page[limit]": (params?.limit ? params.limit : 100), |
| 33 | + "page[offset]": (params?.offset ? params.offset : 0), |
| 34 | + ...(params?.accountId && { "filter[accountId]": params.accountId }), |
| 35 | + ...(params?.customerId && { "filter[customerId]": params.customerId }), |
| 36 | + ...(params?.since && { "filter[since]": params.since }), |
| 37 | + ...(params?.until && { "filter[until]": params.until }), |
| 38 | + ...(params?.fromAmount && { "filter[fromAmount]": params.fromAmount }), |
| 39 | + ...(params?.toAmount && { "filter[toAmount]": params.toAmount }), |
| 40 | + ...(params?.checkNumber && { "filter[checkNumber]": params.checkNumber }), |
| 41 | + ...(params?.tags && { "tags": params.tags }), |
| 42 | + ...(params?.sort && { "sort": params.sort }), |
| 43 | + ...(params?.include && { "sort": params.include }) |
| 44 | + } |
| 45 | + |
| 46 | + if (params?.status) |
| 47 | + params.status.forEach((s, idx) => { |
| 48 | + parameters[`filter[status][${idx}]`] = s |
| 49 | + }) |
| 50 | + |
| 51 | + return this.httpGet<UnitResponse<CheckPayment[]> & Meta>("", { params: parameters }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +export interface CheckPaymentListParams extends BaseCheckPaymentListParams { |
| 56 | + /** |
| 57 | + * Optional. Filter by status (Processed, PendingReview, MarkedForReturn or Returned). Usage example: filter[status][0]=Processed |
| 58 | + */ |
| 59 | + status?: CheckPaymentStatus[] |
| 60 | + |
| 61 | + /** |
| 62 | + * Optional. A comma-separated list of related resources to include in the response. Related resources include: customer, account, transaction. |
| 63 | + * See [Getting Related Resources](https://docs.unit.co/about-jsonapi/#intro-getting-related-resources) |
| 64 | + */ |
| 65 | + include?: string |
| 66 | +} |
0 commit comments