-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectDebit.cs
More file actions
64 lines (56 loc) · 2 KB
/
DirectDebit.cs
File metadata and controls
64 lines (56 loc) · 2 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace DirectDebitCSharpClient
{
public class DirectDebit
{
private readonly string _userCode;
private readonly string _password;
private readonly string _url;
public DirectDebit(string userCode, string password, bool prod=false)
{
_userCode = userCode;
_password = password;
var subdomain = prod ? "dos" : "dos-dr";
_url = $"https://{subdomain}.directdebit.co.za:31143/v1.1";
}
private HttpClient GenerateHttpClient()
{
var client = new HttpClient();
var token = Convert.ToBase64String(Encoding.GetEncoding("utf-8").GetBytes($"{_userCode}:{_password}"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", token);
return client;
}
// Upload a file to Direct Debit via the API
public async Task UploadFile(string filePath)
{
if (!File.Exists(filePath))
{
throw new ArgumentException($"{filePath} does not exist");
}
var content = new List<byte>();
await using (var fs = File.OpenRead(filePath))
{
var b = new byte[1024];
while (fs.Read(b, 0, b.Length) > 0)
{
content.AddRange(b);
}
}
var hc = GenerateHttpClient();
var postBody = new MultipartFormDataContent
{
{new ByteArrayContent(content.ToArray()), "file_data", Path.GetFileName(filePath)}
};
var resp = await hc.PostAsync(_url + "/batch/eft", postBody);
resp.EnsureSuccessStatusCode();
var r = await resp.Content.ReadAsStringAsync();
Console.WriteLine(r);
}
}
}