-
Notifications
You must be signed in to change notification settings - Fork 26
/
pay_beneficiary.js
278 lines (251 loc) · 8.29 KB
/
pay_beneficiary.js
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
* This is JavaScript implementation of the
* {@link https://www.currencycloud.com/developers/cookbooks/ Currency Cloud API v2.0 Cookbook} example.
* Additional documentation for each API endpoint can be found at {@link https://www.currencycloud.com/developers/overview}.
* If you have any queries or you require support, please contact our Support team at {@link [email protected]}.
*/
'use strict';
let currencyCloud = require('../lib/currency-cloud');
const uuid4 = require('uuid/v4');
const opts = {
retries: 3,
factor: 2,
minTimeout: Math.random() * 750,
maxTimeout: Math.random() * 30000 + 30000,
randomize: true,
log: true
};
let payBeneficiary = {
getBalance: {
currency: "EUR"
},
getQuote: {
buyCurrency: "EUR",
sellCurrency: "GBP",
amount: 10000,
fixedSide: "buy"
},
conversion: {
buyCurrency: "EUR",
sellCurrency: "GBP",
amount: 10000,
fixedSide: "buy",
reason: "Top up Euros balance",
termAgreement: true
},
beneficiary: {
name: "Acme GmbH",
bankAccountHolderName: "Acme GmbH",
currency: "EUR",
beneficiaryCountry: "DE",
bankCountry: "DE",
bicSwift: "COBADEFF",
iban: "DE89370400440532013000"
},
payment: {
reason: "Invoice Payment",
reference: "2018-014",
paymentType: "regular",
uniqueRequestId: uuid4()
}
};
/**
* Pay a beneficiary using funds in a different currency.
*
* In this cookbook, you will:
*
* 1. Check how much money you hold in various foreign currencies in your Currencycloud account.
* 2. Top up your Euros balance by trading some Pound Sterling.
* 3. Make a payment in Euros to a beneficiary in Germany.
*/
/**
* 1. Authenticate using valid credentials.
* If you do not have a valid Login ID and API Key, you can get one by registering at
* {@link https://www.currencycloud.com/developers/register-for-an-api-key/}
*/
let login = () => {
return currencyCloud.retry(
() => {
return currencyCloud.authentication.login({
environment: 'demo',
loginId: '[email protected]',
apiKey: 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef'
});
},
opts,
"currencyCloud.authentication.login"
);
};
/**
* 2. Check available balances
* To find out how many Euros you have, call the Get Balance endpoint, passing EUR as the third URI path parameter.
*/
let getBalance = () => {
return currencyCloud.retry(
() => {
return currencyCloud.balances.get(payBeneficiary.getBalance)
.then((res) => {
console.log('getBalance: ' + JSON.stringify(res, null, 2) + '\n');
});
},
opts
);
};
/**
* You can also check the balances for all foreign currencies that you hold in your Currencycloud account by calling the
* Find Balances endpoint
*/
let findBalances = () => {
return currencyCloud.retry(
() => {
return currencyCloud.balances.find()
.then((res) => {
console.log('findBalance: ' + JSON.stringify(res, null, 2) + '\n');
});
});
};
/**
* 3. Top up
* Check how much it will cost to buy 10,000 Euros using funds from your Pound Sterling balance, by making a call to the
* Get Detailed Rates endpoint.
*/
let getQuote = () => {
return currencyCloud.retry(
() => {
return currencyCloud.rates.get(payBeneficiary.getQuote)
.then((res) => {
console.log('getQuote: ' + JSON.stringify(res, null, 2) + '\n');
});
},
opts,
"currencyCloud.rates.get"
);
};
/**
* If you’re happy with the quote, you may authorize the conversion by calling the Create Conversion endpoint.
*/
let createConversion = () => {
return currencyCloud.retry(
() => {
return currencyCloud.conversions.create(payBeneficiary.conversion)
.then((res) => {
payBeneficiary.payment.conversionId = res.id;
console.log('createConversion: ' + JSON.stringify(res, null, 2) + '\n');
});
},
opts
);
};
/**
* On success, the payload of the response message will contain full details of the conversion as recorded against your
* Currencycloud account. Note the unique conversion id (the id field). You’ll need this to complete the conversion.
*/
/**
* 4. Check payment requirements
* You want to make a regular payment to a supplier based in Germany. First, check what details are required
* to make a regular payment in Euros to a beneficiary with a bank account in Germany. To do that, call the
* Get Beneficiary Requirements endpoint.
*/
let getBeneficiaryRequiredDetails = () => {
return currencyCloud.retry(
() => {
return currencyCloud.reference.getBeneficiaryRequiredDetails({
currency: payBeneficiary.beneficiary.currency,
bankAccountCountry: payBeneficiary.beneficiary.bankCountry
})
.then((res) => {
console.log('getBeneficiaryRequiredDetails: ' + JSON.stringify(res, null, 2) + '\n');
});
});
};
/**
* The response tells us that, to make a regular payment to a German bank account in Euros, we need two pieces of
* information: the IBAN and BIC/SWIFT numbers for the beneficiary. The beneficiary could be either a company or
* an individual. Either way, the same information is required.
*/
/**
* 5. Add a beneficiary
* If you know the required details, you can go ahead and create a record for the beneficiary via the
* Create Beneficiary endpoint.
*/
let createBeneficiary = () => {
return currencyCloud.retry(
() => {
return currencyCloud.beneficiaries.create(payBeneficiary.beneficiary)
.then((res) => {
payBeneficiary.payment.beneficiaryId = res.id;
console.log('createBeneficiary: ' + JSON.stringify(res, null, 2) + '\n');
});
},
opts,
"currencyCloud.beneficiaries.create"
);
};
/**
* If the beneficiary is successfully created, the response message will contain full details about the beneficiary as
* recorded in your Currencycloud account. Note the beneficiary’s unique ID (id). You’ll need this to make a payment to
* the beneficiary, in the next step.
*/
/**
* 6. Make a payment
* Authorize a payment by calling the Create Payment endpoint. Optionally, you may provide an idempotency key (via the
* unique_request_id parameter). This helps protect against accidental duplicate payments.
*/
let createPayment = () => {
return currencyCloud.retry(
() => {
return currencyCloud.payments.create({
currency: payBeneficiary.conversion.buyCurrency,
beneficiaryId: payBeneficiary.payment.beneficiaryId,
amount: payBeneficiary.conversion.amount,
reason: payBeneficiary.payment.reason,
reference: payBeneficiary.payment.reference,
conversionId: payBeneficiary.payment.conversionId,
paymentType: payBeneficiary.payment.paymentType,
uniqueRequestId: payBeneficiary.payment.uniqueRequestId
})
.then((res) => {
console.log('createPayment: ' + JSON.stringify(res, null, 2) + '\n');
});
},
opts
);
};
/**
* If the payment is successfully queued, the response payload will contain all the information about the payment as
* recorded in your Currencycloud account. This does not mean that the payment was made. It just means that it is ready
* for processing.
*
* Payments are processed asynchronously. Currencycloud will process payments on the payment_date specified, provided
* you hold enough money in the relevant currency at the time. It is possible to instruct payments even if you don’t hold
* enough money in the relevant currency. The payments will be queued in the normal way but will not be processed until
* your account balance is topped up.
*/
/**
* 7. Logout
* It is good security practice to retire authentication tokens when they are no longer needed, rather than let them
* expire. Send a request to the Logout endpoint to terminate an authentication token immediately.
*/
let logout = () => {
return currencyCloud.authentication.logout()
.then(() => {
console.log('logout\n');
});
};
login()
.then(getQuote)
.then(getBalance)
.then(findBalances)
.then(createConversion)
.then(getBeneficiaryRequiredDetails)
.then(createBeneficiary)
.then(createPayment)
.then(logout)
.catch((err) => {
if (err instanceof currencyCloud.APIerror) {
console.log(err.toYAML());
}
else {
console.log(err);
}
});