This repository has been archived by the owner on Jun 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NimbusSharp.cs
107 lines (96 loc) · 4.48 KB
/
NimbusSharp.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Net.Http;
using System.Threading.Tasks;
using NimbusSharp.Data;
namespace NimbusSharp
{
public class NimbusSharp
{
private readonly string _merchantId;
private readonly string _apiKey;
public NimbusSharp(string merchantId, string apiKey)
{
_merchantId = merchantId;
_apiKey = apiKey;
}
public async Task<NimbusResult> Execute(NimbusFunction function)
{
CheckRequiredProperties(function);
var functionString =
function.GetType().GetTypeInfo().GetCustomAttributes(true).Cast<Function>().FirstOrDefault()?.Name;
var propertyNames = function.GetType()
.GetRuntimeProperties()
.ToDictionary(p => p.Name, p => GetPropertyFunctionString(function, p));
var data = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("func", functionString),
new KeyValuePair<string, string>("Cmn", _merchantId),
new KeyValuePair<string, string>("key", _apiKey)
};
foreach (var propertyName in propertyNames)
{
try
{
var property = propertyName.Key;
var propString = propertyName.Value;
var propInfo = function.GetType().GetRuntimeProperties().First(pi => pi.Name == property);
var propValue = propInfo.GetValue(function);
switch (property)
{
case "Test":
data.Add(new KeyValuePair<string, string>(propString, (bool) propValue ? "1" : "0"));
continue;
case "Manual":
data.Add(new KeyValuePair<string, string>(propString, (bool) propValue ? "2" : "1"));
continue;
case "CardholderName":
data.Add(new KeyValuePair<string, string>(propString, propValue.ToString()));
data.Add(new KeyValuePair<string, string>("cn", propValue.ToString()));
continue;
}
data.Add(new KeyValuePair<string, string>(propString, propValue.ToString()));
}
catch
{
// ignored
}
}
var client = new HttpClient();
var httpContent = new HttpRequestMessage(HttpMethod.Post, "http://ws.nimbusprocessing.com/testgate.php ")
{
Content = new FormUrlEncodedContent(data)
};
var response = await client.SendAsync(httpContent);
var result = await response.Content.ReadAsStringAsync();
return new NimbusResult(result);
}
private static void CheckRequiredProperties(NimbusFunction function)
{
//Check for Required Data
var properties = function.GetType().GetRuntimeProperties().ToDictionary(a => a.Name, a => a);
// ReSharper disable once LoopCanBePartlyConvertedToQuery
foreach (var property in properties)
{
var prop = properties.FirstOrDefault(f => f.Key == property.Key).Value;
if (!prop.GetCustomAttributes(true).Cast<Property>().FirstOrDefault().Required) continue;
var propInfo = function.GetType().GetRuntimeProperties().First(pi => pi.Name == property.Key);
var propValue = propInfo.GetValue(function);
if (propValue == null)
{
throw new Exception(
$"[NimbusSharp] [Execute] The Property : {property.Key} is required, please populate this variable and try again.");
}
}
}
private static string GetPropertyFunctionString(NimbusFunction func, MemberInfo property)
{
var properties = func.GetType().GetRuntimeProperties().ToDictionary(a => a.Name, a => a);
var prop = properties.FirstOrDefault(f => f.Key == property.Name).Value;
return prop.GetCustomAttributes(true).Cast<Property>().FirstOrDefault()?.Name;
}
}
}