-
Notifications
You must be signed in to change notification settings - Fork 16
/
refundService.ts
197 lines (163 loc) · 5.59 KB
/
refundService.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict';
import { Api } from '../api/api';
import * as Types from '../types/Types';
interface RefundResponse extends Types.Refund, Types.APIResponse {}
interface RefundListResponse extends Types.APIResponse {
refunds: Types.Refund[];
meta: Types.ListMeta;
}
interface RefundCreateRequest {
// Amount in minor unit (e.g. pence in GBP, cents in EUR).
amount: string;
// Resources linked to this Refund.
links: Types.RefundCreateRequestLinks;
// Key-value store of custom data. Up to 3 keys are permitted, with key names up
// to 50 characters and values up to 500 characters.
metadata?: Types.JsonMap;
// An optional reference that will appear on your customer's bank statement. The
// character limit for this reference is dependent on the scheme.<br />
// <strong>ACH</strong> - 10 characters<br /> <strong>Autogiro</strong> - 11
// characters<br /> <strong>Bacs</strong> - 10 characters<br />
// <strong>BECS</strong> - 30 characters<br /> <strong>BECS NZ</strong> - 12
// characters<br /> <strong>Betalingsservice</strong> - 30 characters<br />
// <strong>PAD</strong> - 12 characters<br /> <strong>SEPA</strong> - 140
// characters<br /> Note that this reference must be unique (for each merchant)
// for the BECS scheme as it is a scheme requirement. <p
// class='restricted-notice'><strong>Restricted</strong>: You can only specify a
// payment reference for Bacs payments (that is, when collecting from the UK) if
// you're on the <a href='https://gocardless.com/pricing'>GoCardless Plus, Pro
// or Enterprise packages</a>.</p>
reference?: string;
// Total expected refunded amount in minor unit (e.g. pence/cents/öre). If there
// are
// other partial refunds against this payment, this value should be the sum of
// the
// existing refunds plus the amount of the refund being created.
//
// Must be supplied if `links[payment]` is present.
//
total_amount_confirmation?: string;
}
interface RefundListRequest {
// Cursor pointing to the start of the desired set.
after?: string;
// Cursor pointing to the end of the desired set.
before?: string;
// The creation date of this Refund.
created_at?: Types.CreatedAtFilter;
// Number of records to return.
limit?: string;
// Unique identifier, beginning with "MD". Note that this prefix may not apply
// to mandates created before 2016.
mandate?: string;
// Unique identifier, beginning with "PM".
payment?: string;
// Whether a refund was issued against a mandate or a payment. One of:
// <ul>
// <li>`payment`: <em>default</em> returns refunds created against payments
// only</li>
// <li>`mandate`: returns refunds created against mandates only</li>
// </ul>
refund_type?: Types.RefundRefundType;
}
interface RefundUpdateRequest {
// Key-value store of custom data. Up to 3 keys are permitted, with key names up
// to 50 characters and values up to 500 characters.
metadata?: Types.JsonMap;
}
export class RefundService {
private api: Api;
constructor(api) {
this.api = api;
}
async create(
requestParameters: RefundCreateRequest,
idempotencyKey = '',
customHeaders: Types.JsonMap = {}
): Promise<RefundResponse> {
const urlParameters = [];
const requestParams = {
path: '/refunds',
method: 'post',
urlParameters,
requestParameters,
payloadKey: 'refunds',
idempotencyKey,
customHeaders,
fetch: async identity => this.find(identity),
};
const response = await this.api.request(requestParams);
const formattedResponse: RefundResponse = {
...response.body['refunds'],
__response__: response.__response__,
};
return formattedResponse;
}
async list(
requestParameters: RefundListRequest
): Promise<RefundListResponse> {
const urlParameters = [];
const requestParams = {
path: '/refunds',
method: 'get',
urlParameters,
requestParameters,
payloadKey: null,
fetch: null,
};
const response = await this.api.request(requestParams);
const formattedResponse: RefundListResponse = {
...response.body,
__response__: response.__response__,
};
return formattedResponse;
}
async *all(
requestParameters: RefundListRequest
): AsyncGenerator<Types.Refund, void, unknown> {
let cursor = undefined;
do {
const list = await this.list({ ...requestParameters, after: cursor });
for (const refund of list.refunds) {
yield refund;
}
cursor = list.meta.cursors.after;
} while (cursor);
}
async find(identity: string): Promise<RefundResponse> {
const urlParameters = [{ key: 'identity', value: identity }];
const requestParams = {
path: '/refunds/:identity',
method: 'get',
urlParameters,
payloadKey: null,
fetch: null,
};
const response = await this.api.request(requestParams);
const formattedResponse: RefundResponse = {
...response.body['refunds'],
__response__: response.__response__,
};
return formattedResponse;
}
async update(
identity: string,
requestParameters: RefundUpdateRequest
): Promise<RefundResponse> {
const urlParameters = [{ key: 'identity', value: identity }];
const requestParams = {
path: '/refunds/:identity',
method: 'put',
urlParameters,
requestParameters,
payloadKey: 'refunds',
fetch: null,
};
const response = await this.api.request(requestParams);
const formattedResponse: RefundResponse = {
...response.body['refunds'],
__response__: response.__response__,
};
return formattedResponse;
}
}