You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

365 lines
14 KiB

  1. using Swift.DAL.OnlineAgent;
  2. using Swift.DAL.SwiftDAL;
  3. using System;
  4. using System.IO;
  5. using System.Net;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Web.Script.Serialization;
  9. namespace Swift.web.Library
  10. {
  11. public static class KJBankAPIConnection
  12. {
  13. private static string GMEWalletApiBaseUrl = GetStatic.ReadWebConfig("KJURL", "");
  14. private static string secretKey = GetStatic.ReadWebConfig("KJsecretKey", "");
  15. private static byte[] CalcHMACSHA256Hash(string plaintext, string salt)
  16. {
  17. var utf8 = Encoding.UTF8;
  18. byte[] key = utf8.GetBytes(plaintext);
  19. byte[] message = utf8.GetBytes(salt);
  20. var hash = new HMACSHA256(key);
  21. return hash.ComputeHash(message);
  22. }
  23. private static string CreateToken(string message, string secret)
  24. {
  25. secret = secret ?? "";
  26. var encoding = new System.Text.ASCIIEncoding();
  27. byte[] keyByte = encoding.GetBytes(secret);
  28. byte[] messageBytes = encoding.GetBytes(message);
  29. using (var hmacsha256 = new HMACSHA256(keyByte))
  30. {
  31. byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
  32. return Convert.ToBase64String(hashmessage);
  33. }
  34. }
  35. public static AuthTokenResponse GetAuthentication()
  36. {
  37. AuthTokenResponse resp = new AuthTokenResponse();
  38. try
  39. {
  40. string body = "grant_type=client_credentials&client_id=" + GetStatic.ReadWebConfig("client_id", "") + "&client_secret=" + GetStatic.ReadWebConfig("KJsecretKey", "") + "&scope=public";
  41. WebRequest req = WebRequest.Create(GMEWalletApiBaseUrl + "/auth/oauth/v2/token");
  42. byte[] send = Encoding.Default.GetBytes(body);
  43. req.Method = "POST";
  44. req.ContentType = "application/x-www-form-urlencoded";
  45. req.ContentLength = send.Length;
  46. Stream sout = req.GetRequestStream();
  47. sout.Write(send, 0, send.Length);
  48. sout.Flush();
  49. sout.Close();
  50. WebResponse res = req.GetResponse();
  51. StreamReader sr = new StreamReader(res.GetResponseStream());
  52. resp = new JavaScriptSerializer().Deserialize<AuthTokenResponse>(sr.ReadToEnd());
  53. res.Close();
  54. }
  55. catch (Exception e)
  56. {
  57. return resp;
  58. }
  59. return resp;
  60. }
  61. public static string GetSHAValue(string plaintext, string sertetKey)
  62. {
  63. string key = CreateToken(plaintext, sertetKey);
  64. return key;
  65. }
  66. public static DbResult PostToKJBank(string body)
  67. {
  68. var dbResult = new DbResult();
  69. try
  70. {
  71. var url = "/api/partnerserviceaccount?body=" + body;
  72. var root = GMEWalletApiBaseUrl + url;
  73. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(root);
  74. req.ContentType = "application/json";
  75. req.Headers.Add("Authorization", GetStatic.ReadWebConfig("KJsecretKey", ""));
  76. req.Headers.Add("HeaderToken", GetStatic.ReadWebConfig("client_id", ""));
  77. //req.Headers.Add("Authorization", auth.token_type + " " + auth.access_token);
  78. //string signatureurl = GetSHAValue("GET&" + url, secretKey);
  79. //req.Headers.Add("x-obp-signature-url", signatureurl);
  80. //req.Headers.Add("x-obp-partnercode", GetStatic.ReadWebConfig("KJpartnercode", ""));
  81. req.Method = "GET";
  82. HttpWebResponse res = (HttpWebResponse)req.GetResponse();
  83. StreamReader sr = new StreamReader(res.GetResponseStream());
  84. var result = sr.ReadToEnd();
  85. dbResult = new JavaScriptSerializer().Deserialize<DbResult>(result);
  86. OnlineCustomerDao onlineCustomerDao = new OnlineCustomerDao();
  87. onlineCustomerDao.RequestLog(new JavaScriptSerializer().Serialize((dbResult)));
  88. res.Close();
  89. return dbResult;
  90. }
  91. catch (Exception e)
  92. {
  93. dbResult.Msg = e.Message;
  94. dbResult.ErrorCode = "1";
  95. return dbResult;
  96. }
  97. }
  98. public static DbResult GetAccountDetailKJBank(string AccountNo, string bankCode)
  99. {
  100. var dbResult = new DbResult();
  101. try
  102. {
  103. var url = "/api/bankaccount/name?institution=" + bankCode + "&no=" + AccountNo;
  104. var root = GMEWalletApiBaseUrl + url;
  105. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(root);
  106. req.Headers.Add("Authorization", GetStatic.ReadWebConfig("KJsecretKey", ""));
  107. req.Headers.Add("HeaderToken", GetStatic.ReadWebConfig("client_id", ""));
  108. req.ContentType = "application/json";
  109. //req.Headers.Add("Authorization", auth.token_type + " " + auth.access_token);
  110. //string signatureurl = GetSHAValue("GET&" + url, secretKey);
  111. //req.Headers.Add("x-obp-signature-url", signatureurl);
  112. //req.Headers.Add("x-obp-partnercode", GetStatic.ReadWebConfig("KJpartnercode", ""));
  113. req.Method = "GET";
  114. HttpWebResponse res = (HttpWebResponse)req.GetResponse();
  115. StreamReader sr = new StreamReader(res.GetResponseStream());
  116. var result = sr.ReadToEnd();
  117. dbResult = new JavaScriptSerializer().Deserialize<DbResult>(result);
  118. res.Close();
  119. return dbResult;
  120. }
  121. catch (Exception e)
  122. {
  123. dbResult.Msg = e.Message;
  124. dbResult.ErrorCode = "1";
  125. return dbResult;
  126. }
  127. }
  128. /// <summary>
  129. /// METHOD USED TO TRANSFER AMOUNT IN BANK ACCOUNT USING KJ BANK
  130. /// </summary>
  131. /// <param name="body">
  132. /// </param>
  133. /// <param name="baseUrl">
  134. /// </param>
  135. /// <param name="kjSecretKey">
  136. /// </param>
  137. /// <param name="clientId">
  138. /// </param>
  139. /// <returns>
  140. /// </returns>
  141. public static DbResult AccountTransferKJBank(string body, string baseUrl, string kjSecretKey, string clientId)
  142. {
  143. var dbResult = new DbResult();
  144. try
  145. {
  146. var url = "/api/moneytransfer/partner?body=" + body;
  147. var root = GMEWalletApiBaseUrl + url;
  148. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(root);
  149. req.ContentType = "application/json";
  150. req.Headers.Add("Authorization", kjSecretKey);
  151. req.Headers.Add("HeaderToken", clientId);
  152. req.Method = "GET";
  153. var _httpResponse = (HttpWebResponse)req.GetResponse();
  154. using (var sr = new StreamReader(_httpResponse.GetResponseStream()))
  155. {
  156. var result = sr.ReadToEnd();
  157. dbResult = new JavaScriptSerializer().Deserialize<DbResult>(result);
  158. _httpResponse.Close();
  159. return dbResult;
  160. }
  161. }
  162. catch (Exception e)
  163. {
  164. return new DbResult() { ErrorCode = "1", Msg = e.Message };
  165. }
  166. }
  167. public static string AccountTransferKJBank(string body)
  168. {
  169. var auth = GetAuthentication();
  170. var root = GMEWalletApiBaseUrl + "/api/moneytransfer/partner";
  171. WebRequest req = WebRequest.Create(root);
  172. req.ContentType = "application/json";
  173. req.Headers.Add("Authorization", auth.token_type + " " + auth.access_token);
  174. req.Headers.Add("scope", "public");
  175. string signatureurl = KJBankAPIConnection.GetSHAValue("POST&/api/moneytransfer/partner", secretKey);
  176. req.Headers.Add("x-obp-signature-url", signatureurl);
  177. req.Headers.Add("x-obp-partnercode", GetStatic.ReadWebConfig("KJpartnercode", ""));
  178. string signaturebody = KJBankAPIConnection.GetSHAValue(body, secretKey);
  179. req.Headers.Add("x-obp-signature-body", signaturebody);
  180. req.Method = "POST";
  181. byte[] send = Encoding.Default.GetBytes(body);
  182. Stream sout = req.GetRequestStream();
  183. sout.Write(send, 0, send.Length);
  184. sout.Flush();
  185. sout.Close();
  186. WebResponse res = req.GetResponse();
  187. StreamReader sr = new StreamReader(res.GetResponseStream());
  188. var result = sr.ReadToEnd();
  189. return result;
  190. }
  191. /*
  192. * @Max - 2018.09
  193. * API
  194. * */
  195. public static DbResult GetRealNameCheck(string body)
  196. {
  197. var dbResult = new DbResult();
  198. try
  199. {
  200. var url = "/api/realname/name?body=" + body;
  201. var root = GMEWalletApiBaseUrl + url;
  202. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(root);
  203. req.ContentType = "application/json";
  204. req.Headers.Add("Authorization", GetStatic.ReadWebConfig("KJsecretKey", ""));
  205. req.Headers.Add("HeaderToken", GetStatic.ReadWebConfig("client_id", ""));
  206. req.Method = "GET";
  207. var _httpResponse = (HttpWebResponse)req.GetResponse();
  208. using (var sr = new StreamReader(_httpResponse.GetResponseStream()))
  209. {
  210. var result = sr.ReadToEnd();
  211. dbResult = new JavaScriptSerializer().Deserialize<DbResult>(result);
  212. _httpResponse.Close();
  213. return dbResult;
  214. }
  215. }
  216. catch (Exception e)
  217. {
  218. return new DbResult();
  219. }
  220. }
  221. public static DbResult CustomerRegistration(string body)
  222. {
  223. var dbResult = new DbResult();
  224. try
  225. {
  226. /*
  227. * @Max-2018.09
  228. *
  229. * */
  230. var url = "/api/partnerserviceaccount_v2?body=" + body;
  231. var root = GMEWalletApiBaseUrl + url;
  232. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(root);
  233. req.ContentType = "application/json";
  234. req.Headers.Add("Authorization", GetStatic.ReadWebConfig("KJsecretKey", ""));
  235. req.Headers.Add("HeaderToken", GetStatic.ReadWebConfig("client_id", ""));
  236. //req.Headers.Add("Authorization", auth.token_type + " " + auth.access_token);
  237. //string signatureurl = GetSHAValue("GET&" + url, secretKey);
  238. //req.Headers.Add("x-obp-signature-url", signatureurl);
  239. //req.Headers.Add("x-obp-partnercode", GetStatic.ReadWebConfig("KJpartnercode", ""));
  240. req.Method = "GET";
  241. HttpWebResponse res = (HttpWebResponse)req.GetResponse();
  242. StreamReader sr = new StreamReader(res.GetResponseStream());
  243. var result = sr.ReadToEnd();
  244. dbResult = new JavaScriptSerializer().Deserialize<DbResult>(result);
  245. OnlineCustomerDao onlineCustomerDao = new OnlineCustomerDao();
  246. onlineCustomerDao.RequestLog(new JavaScriptSerializer().Serialize((dbResult)));
  247. res.Close();
  248. return dbResult;
  249. }
  250. catch (WebException wex)
  251. {
  252. //resp.Msg = ex.Message;
  253. //resp.ErrorCode = "1";
  254. string exMessage = wex.Message;
  255. if (wex.Response != null)
  256. {
  257. using (StreamReader srException = new StreamReader(wex.Response.GetResponseStream()))
  258. {
  259. exMessage = srException.ReadToEnd();
  260. }
  261. }
  262. dbResult.Msg = exMessage;
  263. dbResult.ErrorCode = "1";
  264. return dbResult;
  265. }
  266. }
  267. }
  268. public class AcountTransferToBank
  269. {
  270. public string obpId { get; set; }
  271. public string accountNo { get; set; }
  272. public string accountPassword { get; set; }
  273. public string receiveInstitution { get; set; }
  274. public string receiveAccountNo { get; set; }
  275. public string amount { get; set; }
  276. }
  277. public class PartnerServiceAccountRequest
  278. {
  279. public string processDivision { get; set; }
  280. public string institution { get; set; }
  281. public string depositor { get; set; }
  282. public string no { get; set; }
  283. public string virtualAccountNo { get; set; }
  284. public string obpId { get; set; }
  285. public string partnerServiceKey { get; set; }
  286. public string realNameDivision { get; set; }
  287. public string realNameNo { get; set; }
  288. }
  289. public class PartnerServiceModificationRequest
  290. {
  291. public string processDivision { get; set; }
  292. public string institution { get; set; }
  293. public string depositor { get; set; }
  294. public string no { get; set; }
  295. public string virtualAccountNo { get; set; }
  296. public string obpId { get; set; }
  297. }
  298. public class PartnerServiceAccountResponse
  299. {
  300. public string obpId { get; set; }
  301. public string code { get; set; }
  302. }
  303. public class AuthTokenResponse
  304. {
  305. public string access_token { get; set; }
  306. public string token_type { get; set; }
  307. public string expires_in { get; set; }
  308. public string scope { get; set; }
  309. }
  310. /*
  311. * @Max-2018.09
  312. * API
  313. * */
  314. public class RealNameRequest
  315. {
  316. public string institution { get; set; }
  317. public string no { get; set; }
  318. public string realNameDivision { get; set; }
  319. public string realNameNo { get; set; }
  320. }
  321. }