-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanpay.cs
206 lines (184 loc) · 6.14 KB
/
scanpay.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json;
namespace Scanpay
{
public class Client
{
private const string HOSTNAME = "api.scanpay.dk";
private readonly string apikey;
public Client(string _apikey)
{
apikey = _apikey;
}
private Tres request<Tdata, Tres>(string url, Tdata data, Options opts)
{
var hostname = (opts == null || opts.hostname == null) ? HOSTNAME : opts.hostname;
var _apikey = (opts == null || opts.auth == null) ? apikey : opts.auth;
var req = (HttpWebRequest)WebRequest.Create("https://" + hostname + url);
var auth = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(_apikey));
req.Method = "GET";
req.ContentType = "application/json";
req.Headers["X-SDK"] = ".NET-0.1.0";
req.Headers[HttpRequestHeader.Authorization] = auth;
if (opts != null && opts.headers != null)
{
foreach(KeyValuePair<string, string> hdr in opts.headers)
{
req.Headers[hdr.Key] = hdr.Value;
}
}
req.AutomaticDecompression = DecompressionMethods.GZip;
if (data != null)
{
req.Method = "POST";
using (var sw = new StreamWriter(req.GetRequestStream()))
using (JsonTextWriter jtw = new JsonTextWriter(sw))
{
JsonSerializer.Create().Serialize(jtw, data);
jtw.Flush();
}
}
using (var res = (HttpWebResponse)req.GetResponse())
using (var stream = res.GetResponseStream())
using (var sr = new StreamReader(stream))
using (var jtr = new JsonTextReader(sr))
{
return JsonSerializer.Create().Deserialize<Tres>(jtr);
}
}
public string newURL(NewURLReq data, Options opts=null)
{
var resobj = request<NewURLReq, NewURLRes>("/v1/new", data, opts);
return resobj.url;
}
public SeqRes seq(ulong seqnum, Options opts=null)
{
return request<NullReq, SeqRes>("/v1/seq/" + seqnum, null, opts);
}
private bool constTimeEquals(string a, string b)
{
uint diff = (uint)a.Length ^ (uint)b.Length;
for (int i = 0; i < a.Length && i < b.Length; i++) {
diff |= (uint)(a[i] ^ b[i]);
}
return diff == 0;
}
public Ping handlePing(byte[] body, string signature, Options opts=null)
{
var _apikey = (opts == null || opts.auth == null) ? apikey : opts.auth;
var binApikey = Encoding.UTF8.GetBytes(_apikey);
var hasher = new System.Security.Cryptography.HMACSHA256(binApikey);
var binDigest = hasher.ComputeHash(body);
var digest = Convert.ToBase64String(binDigest);
if (!constTimeEquals(digest, signature)) {
throw new Exception("invalid ping signature");
}
using (var ms = new MemoryStream(body))
using (var sr = new StreamReader(ms))
using (var jtr = new JsonTextReader(sr)) {
return JsonSerializer.Create().Deserialize<Ping>(jtr);
}
}
public Ping handlePing(string body, string signature, Options opts=null)
{
return handlePing(Encoding.UTF8.GetBytes(body), signature);
}
internal class NullReq{}
}
public class NewURLReq
{
public string orderid;
public string successurl;
public Item[] items;
public Billing billing;
public Shipping shipping;
public string language;
public bool autocapture;
public string lifetime;
}
public class Item
{
public string name;
public uint quantity;
public string price;
public string sku;
}
public class Billing
{
public string name;
public string company;
public string vatin;
public string gln;
public string email;
public string phone;
public string[] address;
public string city;
public string zip;
public string state;
public string country;
}
public class Shipping
{
public string name;
public string company;
public string email;
public string phone;
public string[] address;
public string city;
public string zip;
public string state;
public string country;
}
public class NewURLRes
{
public string url;
}
public class SeqRes
{
public ulong seq;
public Change[] changes;
}
public class Change
{
public ulong id;
public uint rev;
public string orderid;
public string error;
public Time time;
public Act[] acts;
public Totals totals;
}
public class Time
{
public long created;
public long authorized;
}
public class Act
{
public string act;
public long time;
public string total;
}
public class Totals
{
public string authorized;
public string captured;
public string refunded;
public string left;
}
public class Ping
{
public ulong shopid;
public ulong seq;
}
public class Options
{
public string hostname;
public string auth;
public Dictionary<string, string> headers;
}
}