-
Notifications
You must be signed in to change notification settings - Fork 37
/
CurrencyCloudCookbook.java
243 lines (221 loc) · 10.6 KB
/
CurrencyCloudCookbook.java
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
package com.currencycloud.examples;
import com.currencycloud.client.CurrencyCloudClient;
import com.currencycloud.client.backoff.BackOff;
import com.currencycloud.client.backoff.BackOffResult;
import com.currencycloud.client.exception.CurrencyCloudException;
import com.currencycloud.client.model.*;
import java.math.BigDecimal;
import java.util.*;
/**
* This is a Java SDK implementation of the examples in the
* <a href="https://connect.currencycloud.com/documentation/getting-started/cookbook">Currency Cloud API v2.0 Cookbook</a>.
* All API calls are wrapped in try/catch blocks and executed using an exponential backoff-and-retry policy.
*
* The default parameters used are:
* - BackOff.<T>builder().withMaxAttempts - Maximum number of retries set to 7
* - BackOff.<T>builder().withBase - Minimum wait time in milliseconds set to a random value between 125 and 750
* - BackOff.<T>builder().withCap - Maximum wait time in milliseconds set to a random value between 60000 and 90000
* - BackOff.<T>builder().withExceptionType(TooManyRequestsException.class) - TooManyRequestsException. All other
* exceptions are rethrown
*
* Please see BackOffTest.java for a comprehensive set of test cases
*/
public class CurrencyCloudCookbook {
public static void main(String[] args) throws Exception {
// Please provide your login id and api key here to run this example.
runCookBook("development@currencycloud.com", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
}
public static void runCookBook(String loginId, String apiKey) {
/*
* 1. Generate an authentication token. This authentication token will be used in all subsequent calls and
* will expire after 30mins of inactivity after login. Token requests are limited to 10 calls/min. Individual
* contacts will be locked out of the account after 4 unsuccessful login attempts.
*
* Create CurrencyCloudClient(<Environment>, <LoginId>, <ApiKey>) with default HTTP client config.
* Create CurrencyCloudClient(<Environment>, <LoginId>, <ApiKey>, <HttpClientConfiguration>) with custom HTTP client config.
*/
CurrencyCloudClient client = new CurrencyCloudClient(
CurrencyCloudClient.Environment.demo,
loginId,
apiKey,
CurrencyCloudClient.HttpClientConfiguration.builder()
.httpConnTimeout(15000)
.httpReadTimeout(35000)
.build());
try {
final BackOffResult<Void> authenticateResult = BackOff.<Void>builder()
.withTask(() -> {
client.authenticate();
return null;
})
.execute();
} catch (Exception e) {
e.printStackTrace();
}
/*
* 2. Get a quote for the requested currency based on the spread table of the currently logged in contact. If
* delivery date is not supplied it will default to a deal which settles in 2 working days.
*/
DetailedRate detailedRate = null;
try {
final BackOffResult<DetailedRate> detailedRateResult = BackOff.<DetailedRate>builder()
.withTask(() -> client.detailedRates(
"EUR",
"GBP",
"buy",
new BigDecimal("12345.67"),
null,
null
)
)
.execute();
detailedRate = detailedRateResult.data.orElse(null);
System.out.println("Single Detailed Rate: " + detailedRate.toString());
} catch (Exception e) {
e.printStackTrace();
}
/*
* 3. We are happy with the rate and now wish to create the conversion. A successful response means that the
* currency conversion has been executed and the amount of sold funds need to arrive at Currencycloud by the
* settlement_date. The funds will be available for payment after the conversion has settled on the conversion_date.
*/
Conversion conversion = null;
try {
final BackOffResult<Conversion> conversionResult = BackOff.<Conversion>builder()
.withTask(() -> {
Conversion conversionTemp = Conversion.create();
conversionTemp.setBuyCurrency("EUR");
conversionTemp.setSellCurrency("GBP");
conversionTemp.setFixedSide("buy");
conversionTemp.setAmount(new BigDecimal("12345.67"));
conversionTemp.setReason("Invoice Payment");
conversionTemp.setTermAgreement(true);
return client.createConversion(conversionTemp);
})
.execute();
conversion = conversionResult.data.orElse(null);
} catch (CurrencyCloudException e) {
e.printStackTrace();
}
System.out.println(conversion.toString());
/*
* 4. Create a new beneficiary. Some of the optional parameters may be required depending on the currency and
* the country of the beneficiary and beneficiary bank. Please use the /reference/beneficiary_required_details
* call to know which fields would be required.
*/
List<Map<String, String>> beneficiaryRequiredDetails = null;
try {
final BackOffResult<List<Map<String, String>>> beneficiaryRequiredDetailsResult = BackOff.<List<Map<String, String>>>builder()
.withTask(() -> client.beneficiaryRequiredDetails("EUR", "IT", "IT"))
.execute();
beneficiaryRequiredDetails = beneficiaryRequiredDetailsResult.data.orElse(null);
} catch (CurrencyCloudException e) {
e.printStackTrace();
}
System.out.println(beneficiaryRequiredDetails.toString());
/*
* We know the IBAN and BIC/SWIFT numbers for the beneficiary, so we can use these details.
*/
Beneficiary beneficiary = null;
try {
final BackOffResult<Beneficiary> beneficiaryResult = BackOff.<Beneficiary>builder()
.withTask(() -> {
Beneficiary beneficiaryObj = Beneficiary.create("Antica Salumeria Pane 1864", "IT", "EUR", "Fortunato Pane");
beneficiaryObj.setBeneficiaryCountry("IT");
beneficiaryObj.setBicSwift("BKRAITMM");
beneficiaryObj.setIban("IT40L2798279187CC4WJAU999QH");
List<String> beneficiaryAddress = new ArrayList<String>();
beneficiaryAddress.add("Via Luigi Settembrini n° 111");
beneficiaryAddress.add("80138, Naples, Italy");
beneficiaryObj.setBeneficiaryAddress(beneficiaryAddress);
return client.createBeneficiary(beneficiaryObj);
})
.execute();
beneficiary = beneficiaryResult.data.orElse(null);
} catch (CurrencyCloudException e) {
e.printStackTrace();
}
System.out.println(beneficiary.toString());
/*
* Validate this beneficiary before we attempt a payment to avoid payment failures.
*/
final Beneficiary beneficiaryTemp = beneficiary;
try {
final BackOffResult<Void> validateBeneficiaryResult = BackOff.<Void>builder()
.withTask(() -> {
System.out.println(client.validateBeneficiary(beneficiaryTemp).toString());
return null;
})
.execute();
} catch (CurrencyCloudException e) {
e.printStackTrace();
}
/*
* 5. Provide details of the Payer and Pay
*/
Payer payer = null;
try {
final BackOffResult<Payer> payerResult = BackOff.<Payer>builder()
.withTask(() -> {
List<String> payerAddress = new ArrayList<String>();
payerAddress.add("12 Steward St");
payerAddress.add("London E1 6FQ");
return Payer.create(
"individual",
"Currencycloud Ltd.",
"Guido",
"Bianco",
payerAddress,
"London",
"GB",
new Date()
);
})
.execute();
payer = payerResult.data.orElse(null);
} catch (Exception e) {
e.printStackTrace();
}
/*
* Finally, we create a payment to send the funds to the beneficiary. Currencycloud will execute the payment
* when the related conversion settles.
*/
final Payer payerTemp = payer;
final Payment paymentTemp = Payment.create(
"EUR",
beneficiary.getId(),
new BigDecimal("12345.67"),
"Invoice Payment",
"Invoice 1234",
null,
"regular",
conversion.getId(),
null
);
Payment payment = null;
try {
final BackOffResult<Payment> paymentResult = BackOff.<Payment>builder()
.withTask(() -> client.createPayment(paymentTemp, payerTemp))
.execute();
payment = paymentResult.data.orElse(null);
} catch (CurrencyCloudException e) {
e.printStackTrace();
}
System.out.println(payment.toString());
/*
* 6. All sessions must come to an end, either manually using this call, or the session will automatically
* timeout after 30 minutes of inactivity. If the session is no longer required, it is best practice to close
* the session rather than leaving it to time-out. A successful response will return a 200 code with a blank body.
*/
try {
final BackOffResult<Void> endSessionResult = BackOff.<Void>builder()
.withTask(() -> {
client.endSession();
return null;
})
.execute();
} catch (CurrencyCloudException e) {
e.printStackTrace();
}
}
}