|
| 1 | +import { Configuration } from "./Configuration"; |
| 2 | +import { Gateway } from "./Gateway"; |
| 3 | +import { OfflinePayment } from "./OfflinePayment"; |
| 4 | +import { buildURL } from "./util"; |
| 5 | +import * as querystring from "querystring"; |
| 6 | +import * as types from "./types"; |
| 7 | + |
| 8 | +export class OfflinePaymentGateway { |
| 9 | + /** |
| 10 | + * @hidden |
| 11 | + */ |
| 12 | + gateway: Gateway; |
| 13 | + /** |
| 14 | + * @hidden |
| 15 | + */ |
| 16 | + config: Configuration; |
| 17 | + |
| 18 | + /** |
| 19 | + * @param gateway |
| 20 | + * @hidden |
| 21 | + */ |
| 22 | + constructor(gateway: Gateway) { |
| 23 | + this.gateway = gateway; |
| 24 | + this.config = this.gateway.config; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Create a new offline payment |
| 29 | + * ``` |
| 30 | + * const payment = await client.offlinePayment.create('OP-xx99bb', { |
| 31 | + * taxReportable: true, |
| 32 | + * amount: "10.00", |
| 33 | + * currency: "USD", |
| 34 | + * withholdingAmount: "2.40", |
| 35 | + * withholdingCurrency: "USD", |
| 36 | + * }); |
| 37 | + * ``` |
| 38 | + * @param recipientId Payment Rails recipient id (e.g. "R-xx999bb") |
| 39 | + * @param body Offline Payment information |
| 40 | + */ |
| 41 | + async create(recipientId: string, body: any) { |
| 42 | + const endPoint = buildURL("recipients", recipientId, "offlinePayments"); |
| 43 | + |
| 44 | + const result = await this.gateway.client.post< |
| 45 | + types.OfflinePayment.Response |
| 46 | + >(endPoint, body); |
| 47 | + |
| 48 | + return OfflinePayment.factory(result.offlinePayment); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Update a given offline payment |
| 53 | + * ``` |
| 54 | + * const success = await client.offlinePayment.update('R-aabbccc', 'OP-xx99bb', { |
| 55 | + * taxReportable: true, |
| 56 | + * amount: "10.00", |
| 57 | + * currency: "USD", |
| 58 | + * withholdingAmount: "2.40", |
| 59 | + * withholdingCurrency: "USD", |
| 60 | + * }); |
| 61 | + * ``` |
| 62 | + * @param recipientId Payment Rails recipient id (e.g. "R-aabccc") |
| 63 | + * @param offlinePaymentId Payment Rails offline payment id (e.g. "OP-xx999bb") |
| 64 | + * @param body Payment update information |
| 65 | + */ |
| 66 | + async update(recipientId: string, offlinePaymentId: string, body: any) { |
| 67 | + const endPoint = buildURL( |
| 68 | + "recipients", |
| 69 | + recipientId, |
| 70 | + "offlinePayments", |
| 71 | + offlinePaymentId, |
| 72 | + ); |
| 73 | + |
| 74 | + const result = await this.gateway.client.patch<{ ok: boolean }>( |
| 75 | + endPoint, |
| 76 | + body, |
| 77 | + ); |
| 78 | + |
| 79 | + return true; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Delete a given offline payment |
| 84 | + * ``` |
| 85 | + * const success = await client.offlinePayment.remove('R-aabbccc', 'OP-xx99bb'); |
| 86 | + * ``` |
| 87 | + * @param recipientId Payment Rails recipient id (e.g. "R-aabccc") |
| 88 | + * @param offlinePaymentId Payment Rails offline payment id (e.g. "OP-xx999bb") |
| 89 | + */ |
| 90 | + async remove(recipientId: string, offlinePaymentId: string) { |
| 91 | + const endPoint = buildURL( |
| 92 | + "recipients", |
| 93 | + recipientId, |
| 94 | + "offlinePayments", |
| 95 | + offlinePaymentId, |
| 96 | + ); |
| 97 | + |
| 98 | + const result = await this.gateway.client.remove<{ ok: boolean }>(endPoint); |
| 99 | + |
| 100 | + return true; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Search for offline payments |
| 105 | + * @param query Object containing either search key, usually either "recipientId" |
| 106 | + * @param page Page number (1 based) |
| 107 | + * @param pageSize Page size (0...1000) |
| 108 | + * @param term Any search terms to look for |
| 109 | + */ |
| 110 | + async search( |
| 111 | + query: { [key: string]: string }, |
| 112 | + page: number = 1, |
| 113 | + pageSize: number = 10, |
| 114 | + term: string = "", |
| 115 | + ) { |
| 116 | + // tslint:disable-next-line:max-line-length |
| 117 | + |
| 118 | + const endPoint = !!(query && query.recipientId) |
| 119 | + ? buildURL("recipients", query.recipientId, "offline-payments") |
| 120 | + : buildURL("offline-payments"); |
| 121 | + |
| 122 | + const urlQuery = querystring.stringify({ |
| 123 | + ...query, |
| 124 | + page, |
| 125 | + pageSize, |
| 126 | + search: term, |
| 127 | + }); |
| 128 | + |
| 129 | + const result = await this.gateway.client.get< |
| 130 | + types.OfflinePayment.ListResponse |
| 131 | + >(`${endPoint}?${urlQuery}`); |
| 132 | + |
| 133 | + return result.offlinePayments.map(p => OfflinePayment.factory(p)); |
| 134 | + } |
| 135 | +} |
0 commit comments