-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRecipientGateway.ts
136 lines (118 loc) · 3.39 KB
/
RecipientGateway.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { Configuration } from "./Configuration";
import { Recipient } from './Recipient';
import { Gateway } from "./Gateway";
import * as types from "./types";
import { buildURL } from './util';
import * as querystring from 'querystring';
export interface RecipientInput {
referenceId?: string;
email?: string;
name?: string;
firstName?: string;
lastName?: string;
type?: "individual" | "business";
dob?: string;
ssn?: string;
governmentId?: string;
passport?: string;
language?: string;
address?: {
phone?: string;
street1?: string;
street2?: string;
city?: string;
postalCode?: string;
region?: string;
country?: string;
};
account?: any;
}
export class RecipientGateway {
/**
* @hidden
*/
private gateway: Gateway;
/**
* @hidden
*/
private config: Configuration;
/**
* @param gateway gateway object
* @private
*/
constructor(gateway: Gateway) {
this.gateway = gateway;
this.config = this.gateway.config;
}
/**
* Find a specific recipient by their Payment Rails recipient ID
* ```
* const recipient = await client.recipient.find('R-1234');
* ```
* @param recipientId The Payment Rails recipient ID (e.g. R-xyzzy)
*/
async find(recipientId: string) {
const endPoint = buildURL('recipients', recipientId);
const result = await this.gateway.client.get<types.Recipient.Response>(endPoint);
return Recipient.factory(result.recipient);
}
/**
* Create a given recipient
* ```
* const recipient = await client.recipient.create({
* type: "individual",
* firstName: "Tom",
* lastName: "Jones",
* email: "[email protected]",
* address: {
* street1: "123 Main St",
* country: "US",
* }
* });
* ```
* @param body The recipient information to create
*/
async create(body: RecipientInput) {
const endPoint = buildURL('recipients');
const result = await this.gateway.client.post<types.Recipient.Response>(endPoint, body);
return Recipient.factory(result.recipient);
}
/**
* Update the given recipient
* ```
* const recipient = await client.recipient.update('R-1234', {
* firstName: "Carl",
* });
* ```
* @param recipientId The Payment Rails recipient ID (e.g. R-xyzzy)
* @param body the changes to make to the recipient
*/
async update(recipientId: string, body: RecipientInput) {
const endPoint = buildURL('recipients', recipientId);
const result = await this.gateway.client.patch<types.Recipient.Response>(endPoint, body);
return true;
}
/**
* Delete the given recipient.
* ```
* const status = await client.recipient.remove('R-123');
* ```
* @param recipientId The Payment Rails recipient ID (e.g. R-xyzzy)
*/
async remove(recipientId: string) {
const endPoint = buildURL('recipients', recipientId);
const result = await this.gateway.client.remove<{ ok: boolean }>(endPoint);
return true;
}
async search(page: number, pageSize: number, term: string) {
// tslint:disable-next-line:max-line-length
const endPoint = buildURL('recipients');
const query = querystring.stringify({
page,
pageSize,
search: term,
});
const result = await this.gateway.client.get<types.Recipient.ListResponse>(`${endPoint}?${query}`);
return result.recipients.map(r => Recipient.factory(r));
}
}