Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PaiwisePaymentServices.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
5using Waher.Content;
10
11namespace Paiwise.Internal
12{
17 {
18 private static readonly Dictionary<string, KeyValuePair<IBuyEDalerService[], DateTime>> buyEDalerServices =
19 new Dictionary<string, KeyValuePair<IBuyEDalerService[], DateTime>>();
20 private static readonly Dictionary<string, KeyValuePair<ISellEDalerService[], DateTime>> sellEDalerServices =
21 new Dictionary<string, KeyValuePair<ISellEDalerService[], DateTime>>();
22
26 public const string TokenIdSetting = "Paiwise.Internal.Token";
27
31 public const string HostSetting = "Paiwise.Internal.Host";
32
36 public const string TimeoutMinutesSetting = "Paiwise.Internal.TimeoutMinutes";
37
42 {
43 }
44
48 public Task Start()
49 {
50 return Task.CompletedTask;
51 }
52
56 public Task Stop()
57 {
58 return Task.CompletedTask;
59 }
60
65 public Task<IConfigurablePage[]> GetConfigurablePages()
66 {
67 return Task.FromResult(new IConfigurablePage[]
68 {
69 new ConfigurablePage("Paiwise", "/Settings/Paiwise.md", "Admin.Payments.Paiwise")
70 });
71 }
72
76 public string Id => "paiwise";
77
81 public string Name => "Paiwise";
82
86 public string IconUrl => Gateway.GetUrl("/Images/paiwiseLogo_white.webp");
87
91 public int IconWidth => 190;
92
96 public int IconHeight => 54;
97
104 public async Task<IBuyEDalerService[]> GetServicesForBuyingEDaler(CaseInsensitiveString Currency, CaseInsensitiveString Country)
105 {
106 string Key = Currency.Value.ToUpper() + " " + Country.Value.ToUpper();
107
108 lock (buyEDalerServices)
109 {
110 if (buyEDalerServices.TryGetValue(Key, out KeyValuePair<IBuyEDalerService[], DateTime> P) &&
111 P.Value.Subtract(DateTime.UtcNow).TotalHours < 1)
112 {
113 return P.Key;
114 }
115 }
116
117 string Host = await RuntimeSettings.GetAsync(HostSetting, string.Empty);
118 if (string.IsNullOrEmpty(Host))
119 return new IBuyEDalerService[0];
120
121 string Token = await RuntimeSettings.GetAsync(TokenIdSetting, string.Empty);
122 if (string.IsNullOrEmpty(Token))
123 return new IBuyEDalerService[0];
124
125 StringBuilder Url = new StringBuilder();
126
127 Url.Append("https://");
128 Url.Append(Host);
129 Url.Append("/payment/available-buy-payment-method");
130
131 Dictionary<string, object> Request = new Dictionary<string, object>()
132 {
133 { "currency", Currency.Value },
134 { "country", Country.Value }
135 };
136
137 object Obj = await InternetContent.PostAsync(new Uri(Url.ToString()), Request, Gateway.Certificate,
138 new KeyValuePair<string, string>("Authorization", "Bearer " + Token),
139 new KeyValuePair<string, string>("Accept", JsonCodec.DefaultContentType));
140
141 if (!(Obj is Array Services))
142 return new IBuyEDalerService[0];
143
144 List<IBuyEDalerService> Result = new List<IBuyEDalerService>();
145
146 foreach (object Obj2 in Services)
147 {
148 if (!(Obj2 is Dictionary<string, object> Service &&
149 Service.TryGetValue("id", out Obj) && Obj is string Id &&
150 Service.TryGetValue("displayName", out Obj) && Obj is string DisplayName &&
151 Service.TryGetValue("logo", out Obj) && Obj is Dictionary<string, object> Logo &&
152 Logo.TryGetValue("url", out Obj) && Obj is string LogoUrl &&
153 Logo.TryGetValue("width", out Obj) && Obj is int LogoWidth &&
154 Logo.TryGetValue("height", out Obj) && Obj is int LogoHeight &&
155 Service.TryGetValue("contractId", out object ContractId) &&
156 Service.TryGetValue("fields", out Obj) && Obj is Array Fields &&
157 Service.TryGetValue("endpoint", out Obj) && Obj is Dictionary<string, object> Endpoint &&
158 Endpoint.TryGetValue("method", out Obj) && Obj is string Method &&
159 string.Compare(Method, "POST", true) == 0 &&
160 Endpoint.TryGetValue("url", out Obj) && Obj is string ServiceUrl))
161 {
162 continue;
163 }
164
165 if (!Endpoint.TryGetValue("optionsUrl", out Obj) || !(Obj is string OptionsUrl))
166 OptionsUrl = null;
167
168 List<ServiceField> ServiceFields = new List<ServiceField>();
169
170 foreach (object Obj3 in Fields)
171 {
172 if (!(Obj3 is Dictionary<string, object> Field &&
173 Field.TryGetValue("field", out Obj) && Obj is string FieldId &&
174 Field.TryGetValue("dataType", out Obj) && Obj is string DataType &&
175 Field.TryGetValue("required", out Obj) && Obj is bool Required))
176 {
177 continue;
178 }
179
180 ServiceFields.Add(new ServiceField(FieldId, DataType, Required));
181 }
182
183 Result.Add(new BuyEDalerPaymentService(Id, DisplayName, LogoUrl, LogoWidth, LogoHeight,
184 ContractId?.ToString(), Method, ServiceUrl, OptionsUrl, ServiceFields.ToArray(), Host, this));
185 }
186
187 IBuyEDalerService[] Result2 = Result.ToArray();
188
189 lock (buyEDalerServices)
190 {
191 buyEDalerServices[Key] = new KeyValuePair<IBuyEDalerService[], DateTime>(Result2, DateTime.UtcNow);
192 }
193
194 return Result2;
195 }
196
204 public async Task<IBuyEDalerService> GetServiceForBuyingEDaler(string ServiceId, CaseInsensitiveString Currency, CaseInsensitiveString Country)
205 {
206 foreach (IBuyEDalerService Service in await this.GetServicesForBuyingEDaler(Currency, Country))
207 {
208 if (Service.Id == ServiceId)
209 return Service;
210 }
211
212 return null;
213 }
214
221 public async Task<IPaymentService[]> GetServicesForPayment(CaseInsensitiveString Currency, CaseInsensitiveString Country)
222 {
223 IBuyEDalerService[] Services = await this.GetServicesForBuyingEDaler(Currency, Country);
224 List<IPaymentService> Result = new List<IPaymentService>();
225
226 foreach (IBuyEDalerService Service in Services)
227 {
228 if (Service is BuyEDalerPaymentService PaymentService && PaymentService.CanBeUsedForPayment)
229 Result.Add(PaymentService);
230 }
231
232 return Result.ToArray();
233 }
234
242 public async Task<IPaymentService> GetServiceForPayment(string ServiceId, CaseInsensitiveString Currency, CaseInsensitiveString Country)
243 {
244 IBuyEDalerService Service = await this.GetServiceForBuyingEDaler(ServiceId, Currency, Country);
245 if (Service is BuyEDalerPaymentService PaymentService && PaymentService.CanBeUsedForPayment)
246 return PaymentService;
247 else
248 return null;
249 }
250
257 public async Task<ISellEDalerService[]> GetServicesForSellingEDaler(CaseInsensitiveString Currency, CaseInsensitiveString Country)
258 {
259 string Key = Currency.Value.ToUpper() + " " + Country.Value.ToUpper();
260
261 lock (sellEDalerServices)
262 {
263 if (sellEDalerServices.TryGetValue(Key, out KeyValuePair<ISellEDalerService[], DateTime> P) &&
264 P.Value.Subtract(DateTime.UtcNow).TotalHours < 1)
265 {
266 return P.Key;
267 }
268 }
269
270 string Host = await RuntimeSettings.GetAsync(HostSetting, string.Empty);
271 if (string.IsNullOrEmpty(Host))
272 return new ISellEDalerService[0];
273
274 string Token = await RuntimeSettings.GetAsync(TokenIdSetting, string.Empty);
275 if (string.IsNullOrEmpty(Token))
276 return new ISellEDalerService[0];
277
278 StringBuilder Url = new StringBuilder();
279
280 Url.Append("https://");
281 Url.Append(Host);
282 Url.Append("/payout/available-sell-payment-method");
283
284 Dictionary<string, object> Request = new Dictionary<string, object>()
285 {
286 { "currency", Currency.Value },
287 { "country", Country.Value }
288 };
289
290 object Obj = await InternetContent.PostAsync(new Uri(Url.ToString()), Request, Gateway.Certificate,
291 new KeyValuePair<string, string>("Authorization", "Bearer " + Token),
292 new KeyValuePair<string, string>("Accept", JsonCodec.DefaultContentType));
293
294 if (!(Obj is Array Services))
295 return new ISellEDalerService[0];
296
297 List<ISellEDalerService> Result = new List<ISellEDalerService>();
298
299 foreach (object Obj2 in Services)
300 {
301 if (!(Obj2 is Dictionary<string, object> Service &&
302 Service.TryGetValue("id", out Obj) && Obj is string Id &&
303 Service.TryGetValue("displayName", out Obj) && Obj is string DisplayName &&
304 Service.TryGetValue("logo", out Obj) && Obj is Dictionary<string, object> Logo &&
305 Logo.TryGetValue("url", out Obj) && Obj is string LogoUrl &&
306 Logo.TryGetValue("width", out Obj) && Obj is int LogoWidth &&
307 Logo.TryGetValue("height", out Obj) && Obj is int LogoHeight &&
308 Service.TryGetValue("contractId", out object ContractId) &&
309 Service.TryGetValue("fields", out Obj) && Obj is Array Fields &&
310 Service.TryGetValue("endpoint", out Obj) && Obj is Dictionary<string, object> Endpoint &&
311 Endpoint.TryGetValue("method", out Obj) && Obj is string Method &&
312 string.Compare(Method, "POST", true) == 0 &&
313 Endpoint.TryGetValue("url", out Obj) && Obj is string ServiceUrl))
314 {
315 continue;
316 }
317
318 if (!Endpoint.TryGetValue("optionsUrl", out Obj) || !(Obj is string OptionsUrl))
319 OptionsUrl = null;
320
321 List<ServiceField> ServiceFields = new List<ServiceField>();
322
323 foreach (object Obj3 in Fields)
324 {
325 if (!(Obj3 is Dictionary<string, object> Field &&
326 Field.TryGetValue("field", out Obj) && Obj is string FieldId &&
327 Field.TryGetValue("dataType", out Obj) && Obj is string DataType &&
328 Field.TryGetValue("required", out Obj) && Obj is bool Required))
329 {
330 continue;
331 }
332
333 ServiceFields.Add(new ServiceField(FieldId, DataType, Required));
334 }
335
336 Result.Add(new SellEDalerPaymentService(Id, DisplayName, LogoUrl, LogoWidth, LogoHeight,
337 ContractId?.ToString(), Method, ServiceUrl, OptionsUrl, ServiceFields.ToArray(), Host, this));
338 }
339
340 ISellEDalerService[] Result2 = Result.ToArray();
341
342 lock (sellEDalerServices)
343 {
344 sellEDalerServices[Key] = new KeyValuePair<ISellEDalerService[], DateTime>(Result2, DateTime.UtcNow);
345 }
346
347 return Result2;
348 }
349
357 public async Task<ISellEDalerService> GetServiceForSellingEDaler(string ServiceId, CaseInsensitiveString Currency, CaseInsensitiveString Country)
358 {
359 foreach (ISellEDalerService Service in await this.GetServicesForSellingEDaler(Currency, Country))
360 {
361 if (Service.Id == ServiceId)
362 return Service;
363 }
364
365 return null;
366 }
367
368 }
369}
Reference to a Paiwise payment service.
bool CanBeUsedForPayment
If service can be used for payments.
Payment services made available by Paiwise.
string Name
Displayable name of service provider.
async Task< IBuyEDalerService[]> GetServicesForBuyingEDaler(CaseInsensitiveString Currency, CaseInsensitiveString Country)
Gets available payment services for buying eDaler.
Task< IConfigurablePage[]> GetConfigurablePages()
Gets an array of configurable pages for the module.
async Task< IPaymentService > GetServiceForPayment(string ServiceId, CaseInsensitiveString Currency, CaseInsensitiveString Country)
Gets a payment service.
int IconWidth
Width of icon, if available.
async Task< IPaymentService[]> GetServicesForPayment(CaseInsensitiveString Currency, CaseInsensitiveString Country)
Gets available payment services.
const string TimeoutMinutesSetting
Settings key for Paiwise timeout, in minutes.
async Task< ISellEDalerService > GetServiceForSellingEDaler(string ServiceId, CaseInsensitiveString Currency, CaseInsensitiveString Country)
Gets a payment service for selling eDaler.
PaiwisePaymentServices()
Payment services made available by Paiwise.
async Task< ISellEDalerService[]> GetServicesForSellingEDaler(CaseInsensitiveString Currency, CaseInsensitiveString Country)
Gets available payment services for seller eDaler.
int IconHeight
Height of icon, if available.
string IconUrl
Optional URL to icon of service provider.
const string TokenIdSetting
Settings key for Paiwise token.
const string HostSetting
Settings key for Paiwise host name.
async Task< IBuyEDalerService > GetServiceForBuyingEDaler(string ServiceId, CaseInsensitiveString Currency, CaseInsensitiveString Country)
Gets a payment service for buying eDaler.
Reference to a Paiwise payment service.
Represents a field in a service.
Definition: ServiceField.cs:9
Static class managing encoding and decoding of internet content.
static Task< object > PostAsync(Uri Uri, object Data, params KeyValuePair< string, string >[] Headers)
Posts to a resource, using a Uniform Resource Identifier (or Locator).
const string DefaultContentType
application/json
Definition: JsonCodec.cs:19
Configuration page for a configurable module.
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static X509Certificate2 Certificate
Domain certificate.
Definition: Gateway.cs:2349
static string GetUrl(string LocalResource)
Gets a URL for a resource.
Definition: Gateway.cs:4167
Represents a case-insensitive string.
string Value
String-representation of the case-insensitive string. (Representation is case sensitive....
Static class managing persistent settings.
static async Task< string > GetAsync(string Key, string DefaultValue)
Gets a string-valued setting.
Interface for information about a service provider that users can use to buy eDaler.
Interface for information about a service provider that users can use to buy eDaler.
Interface for information about a service provider that users can use to pay for services.
Interface for information about a service provider that users can use to sell eDaler.
Interface for information about a service provider that users can use to sell eDaler.
string Id
ID of service provider.
Interface for configurable modules.
Configuration page for a configurable module.
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.