-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlipPayWebClient.cs
More file actions
443 lines (391 loc) · 15 KB
/
Copy pathFlipPayWebClient.cs
File metadata and controls
443 lines (391 loc) · 15 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
using FlipPayApiLibrary.Models.Common;
using FlipPayApiLibrary.Models.Direct;
using FlipPayApiLibrary.Models.General;
using FlipPayApiLibrary.Models.Link;
using FlipPayApiLibrary.Models.Onboard;
using FlipPayApiLibrary.Models.PayLater;
using FlipPayApiLibrary.Models.PayNow;
using Microsoft.Extensions.Logging;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
namespace FlipPayApiLibrary;
// API Documentation: https://api-docs.flippay.com.au/v2.html
public class FlipPayWebClient : IFlipPayWebClient
{
private readonly HttpClient _httpClient;
private readonly ILogger<FlipPayWebClient> _logger;
#region Constructors
public FlipPayWebClient(HttpClient httpClient, ILogger<FlipPayWebClient> logger)
{
_httpClient = httpClient;
_logger = logger;
}
public FlipPayWebClient(FlipPayConfig config, ILogger<FlipPayWebClient> logger)
{
_httpClient = new();
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Bearer",
config.Token
);
_httpClient.BaseAddress = new Uri(config.IsDemo ? config.DemoUrl : config.ProductionUrl);
_logger = logger;
}
#endregion Constructors
#region Introducers
#region Onboard
/// <summary>
/// Create an onboarding request for a merchant.
/// </summary>
/// <param name="onboardPostRequest"></param>
/// <returns>OnboardPostResponse</returns>
public async Task<OnboardPostResponse?> CreateAnOnboardingRequest(
OnboardPostRequest onboardPostRequest
)
{
return await PostAsync<OnboardPostResponse>(
$"onboard",
onboardPostRequest,
nameof(CreateAnOnboardingRequest)
);
}
/// <summary>
/// Retrieve the status of an onboarding request.
/// </summary>
/// <param name="onboardingId"></param>
public async Task<OnboardGetResponse?> RetrieveAnOnboardingRequest(string onboardingId)
{
return await GetAsync<OnboardGetResponse>(
$"onboard/{onboardingId}",
nameof(RetrieveAnOnboardingRequest)
);
}
/// <summary>
/// Cancel an onboarding request.
/// </summary>
/// <param name="onboardingId"></param>
public async Task CancelAnOnboardingRequest(string onboardingId)
{
await DeleteAsync($"onboard/{onboardingId}", nameof(CancelAnOnboardingRequest));
}
#endregion Onboard
#region Link
/// <summary>
/// Request a link between an integrated partner and a merchant account. Notifications received (if enabled) are for the linked and not-linked statuses
/// - note that if accounts are linked, then one party removes the link some time later, a not-linked notification will be sent to the original webhook url.
/// </summary>
/// <param name="linkPostRequest"></param>
public async Task RequestAnAccountLink(LinkPostRequest linkPostRequest)
{
_ = await PostAsync<object>($"link", linkPostRequest, nameof(RequestAnAccountLink));
}
/// <summary>
/// Retrieve the status of a link between an integrated partner and a merchant account. Statuses returned are:
/// - pending: link request has been sent but not actioned by the merchant
/// - linked: accounts are linked
/// - not-linked: accounts are not linked
/// </summary>
/// <param name="merchantId"></param>
/// <returns>LinkGetResponse</returns>
public async Task<LinkGetResponse?> RetrieveTheStatusOfAnAccountLink(string merchantId)
{
return await GetAsync<LinkGetResponse>(
$"link/{merchantId}",
nameof(RetrieveTheStatusOfAnAccountLink)
);
}
/// <summary>
/// Remove a link between an integrated partner and a merchant account.
/// </summary>
/// <param name="merchantId"></param>
public async Task RemoveAnAccountLink(string merchantId)
{
await DeleteAsync($"link/{merchantId}", nameof(RemoveAnAccountLink));
}
#endregion Link
#endregion Introducers
#region Merchant
#region Pay Later
/// <summary>
/// Create a payment request between a merchant and customer, enabled with "pay later" payment options. Refer to the integration guide for specific products to confirm product field format requirements (e.g. dates, currency, etc).
/// </summary>
/// <param name="payLaterPostRequest"></param>
/// <returns>PayLaterPostResponse</returns>
public async Task<PayLaterPostResponse?> CreateAPayLaterEnabledRequest(
PayLaterPostRequest payLaterPostRequest
)
{
return await PostAsync<PayLaterPostResponse>(
$"paylater",
payLaterPostRequest,
nameof(CreateAPayLaterEnabledRequest)
);
}
/// <summary>
/// Update a payment request between a merchant and customer, enabled with "pay later" payment options
/// Refer to the integration guide to confirm specific field format requirements(e.g.dates, currency, etc).
/// </summary>
/// <param name="prId"></param>
/// <param name="payLaterPatchRequest"></param>
public async Task UpdateAPayLaterEnabledRequest(
string prId,
PayLaterPatchRequest payLaterPatchRequest
)
{
await PatchAsync(
$"paylater/{prId}",
payLaterPatchRequest,
nameof(UpdateAPayLaterEnabledRequest)
);
}
/// <summary>
/// Retrieve a payment request created between a merchant and customer, enabled with "pay later" payment options.
/// Refer to the integration guide to confirm specific field format requirements(e.g.dates, currency, etc).
/// Note that if a payment request has not yet been activated, and was enabled with multiple products to offer,
/// multiple products will be returned.If a payment request has been activated, only the product that was approved will be returned.
/// </summary>
/// <param name="prId"></param>"
/// <returns>PayLaterGetResponse</returns>
public async Task<PayLaterGetResponse?> RetrieveAPayLaterEnabledRequest(string prId)
{
return await GetAsync<PayLaterGetResponse>(
$"paylater/{prId}",
nameof(RetrieveAPayLaterEnabledRequest)
);
}
/// <summary>
/// Cancel a payment request between a merchant and customer, enabled with "pay later" payment options
/// Refer to the integration guide to confirm specific field format requirements(e.g.dates, currency, etc).
/// </summary>
/// <param name="prId"></param>
public async Task CancelAPayLaterEnabledRequest(string prId)
{
await DeleteAsync($"paylater/{prId}", nameof(CancelAPayLaterEnabledRequest));
}
#endregion Pay Later
#region Pay Now
/// <summary>
/// Create a payment request between a merchant and customer, enabled with immediate card payment functionality only.
/// </summary>
/// <param name="payNowPostRequest"></param>
/// <returns>PayNowPostResponse</returns>
public async Task<PayNowPostResponse?> CreateAPayNowEnabledRequest(
PayNowPostRequest payNowPostRequest
)
{
return await PostAsync<PayNowPostResponse>(
$"paynow",
payNowPostRequest,
nameof(CreateAPayNowEnabledRequest)
);
}
/// <summary>
/// Retrieve a pay now enabled request
/// </summary>
/// <param name="prId"></param>
/// <returns>PayNowGetResponse</returns>
public async Task<PayNowGetResponse?> RetrieveAPayNowEnabledRequest(string prId)
{
return await GetAsync<PayNowGetResponse>(
$"paynow/{prId}",
nameof(RetrieveAPayNowEnabledRequest)
);
}
/// <summary>
/// Cancel a payment request between a merchant and customer, enabled with immediate card payment functionality only.
/// </summary>
/// <param name="prId"></param>
public async Task DeleteAPayNowEnabledRequest(string prId)
{
await DeleteAsync($"paynow/{prId}", nameof(DeleteAPayNowEnabledRequest));
}
#endregion Pay Now
#region Direct
/// <summary>
/// Create a B2B funding request between an onboarded entity and FlipPay.
/// </summary>
/// <param name="directPostRequest"></param>
/// <returns>DirectPostResponse</returns>
public async Task<DirectPostResponse?> CreateADirectFundingRequest(
DirectPostRequest directPostRequest
)
{
return await PostAsync<DirectPostResponse>(
$"direct",
directPostRequest,
nameof(CreateADirectFundingRequest)
);
}
/// <summary>
/// Update a direct funding request between an onboarded entity and FlipPay.
/// </summary>
/// <param name="prId">The unique ID of the payment request to be updated</param>
/// <param name="productFields">Product fields to update on the PR</param>
public async Task UpdateADirectFundingRequest(string prId, List<ProductField> productFields)
{
await PatchAsync($"direct/{prId}", productFields, nameof(UpdateADirectFundingRequest));
}
/// <summary>
/// Retrieve a direct funding request
/// </summary>
/// <param name="prId">The unique ID of the payment request to be retrieved.</param>
/// <returns>DirectGetResponse</returns>
public async Task<DirectGetResponse?> RetrieveADirectFundingRequest(string prId)
{
return await GetAsync<DirectGetResponse>(
$"direct/{prId}",
nameof(RetrieveADirectFundingRequest)
);
}
/// <summary>
/// Cancel a B2B funding request between an onboarded entity and FlipPay.
/// </summary>
/// <param name="prId">The unique ID of the payment request to be cancelled.</param>
public async Task CancelADirectFundingRequest(string prId)
{
await DeleteAsync($"direct/{prId}", nameof(CancelADirectFundingRequest));
}
/// <summary>
/// Retrieve a filtered list of direct funding requests.
/// - When authenticating as a merchant, no single parameter is mandatory; all are optional.
/// - When authenticating as an integrated partner, merchantId is mandatory (the service will only provide records for a single merchant).
/// </summary>
/// <param name="queryParameters">Query parameters to filter the list of direct funding requests.</param>
/// <returns>List of DirectGetListResponseItem</returns>
public async Task<List<DirectGetListResponseItem>?> RetrieveAListOfDirectFundingRequests(
string queryParameters
)
{
return await GetAsync<List<DirectGetListResponseItem>>(
$"direct?{queryParameters}",
nameof(RetrieveAListOfDirectFundingRequests)
);
}
#endregion Direct
#region General
/// <summary>
/// Retrieve bank accounts enabled on a merchant account
/// </summary>
/// <param name="merchantId"></param>
/// <returns>GetBankAccountsResponse</returns>
public async Task<GetBankAccountsResponse?> RetrieveBankAccounts(string merchantId)
{
return await GetAsync<GetBankAccountsResponse>(
$"bankaccounts/{merchantId}",
nameof(RetrieveBankAccounts)
);
}
/// <summary>
/// Retrieve products enabled on a merchant account.
/// </summary>
/// <param name="merchantId"></param>
/// <returns>GetProductsResponse</returns>
public async Task<GetProductsResponse?> RetrieveProductsOnAMerchantAccount(string merchantId)
{
return await GetAsync<GetProductsResponse>(
$"products/{merchantId}",
nameof(RetrieveProductsOnAMerchantAccount)
);
}
#endregion General
#endregion Merchant
#region Helper Methods
private const string contentType = "application/json";
private async Task<T?> GetAsync<T>(string url, string methodName)
where T : class
{
try
{
var response = await _httpClient.GetAsync(url).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return JsonSerializer.Deserialize<T>(content);
}
catch (HttpRequestException e)
{
HandleError(e, $"Error fetching data in {methodName}: {e.Message}");
}
catch (JsonException ex)
{
HandleError(ex, $"Error using JSON in {methodName}: {ex.Message}");
}
catch (Exception ex)
{
HandleError(ex, $"Unexpected error in {methodName}: {ex.Message}");
}
return null;
}
private async Task<T?> PostAsync<T>(string url, object payload, string methodName)
where T : class
{
try
{
var jsonPayload = JsonSerializer.Serialize(payload);
var response = await _httpClient
.PostAsync(url, new StringContent(jsonPayload, Encoding.UTF8, contentType))
.ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return JsonSerializer.Deserialize<T>(content);
}
catch (HttpRequestException e)
{
HandleError(e, $"Error posting data in {methodName}: {e.Message}");
}
catch (JsonException ex)
{
HandleError(ex, $"Error using JSON in {methodName}: {ex.Message}");
}
catch (Exception ex)
{
HandleError(ex, $"Unexpected error in {methodName}: {ex.Message}");
}
return null;
}
private async Task PatchAsync(string url, object payload, string methodName)
{
try
{
var jsonPayload = JsonSerializer.Serialize(payload);
var response = await _httpClient
.PatchAsync(url, new StringContent(jsonPayload, Encoding.UTF8, contentType))
.ConfigureAwait(false);
response.EnsureSuccessStatusCode();
_logger.LogInformation($"{methodName} successfully executed.");
}
catch (HttpRequestException e)
{
HandleError(e, $"Error patching data in {methodName}: {e.Message}");
}
catch (JsonException ex)
{
HandleError(ex, $"Error using JSON in {methodName}: {ex.Message}");
}
catch (Exception ex)
{
HandleError(ex, $"Unexpected error in {methodName}: {ex.Message}");
}
}
private async Task DeleteAsync(string url, string methodName)
{
try
{
var response = await _httpClient.DeleteAsync(url).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
_logger.LogInformation($"{methodName} successfully executed.");
}
catch (HttpRequestException e)
{
HandleError(e, $"Error deleting data in {methodName}: {e.Message}");
}
catch (Exception ex)
{
HandleError(ex, $"Unexpected error in {methodName}: {ex.Message}");
}
}
private void HandleError(Exception ex, string message)
{
_logger.LogError(message);
}
#endregion Helper Methods
}