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.

429 lines
12 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography.X509Certificates;
  5. using System.Text;
  6. using Swift.API.com.global.www;
  7. using System.Net;
  8. using System.Data;
  9. namespace Swift.API.GlobalBank
  10. {
  11. public class GlobalBankAPI
  12. {
  13. private static X509Certificate2 cert;
  14. private static bool foundCertificate = false;
  15. private RemoteRemit gblApi = new RemoteRemit();
  16. private string ProviderName = "Global Bank";
  17. private String username;
  18. private String password;
  19. private String payingBranchCd;
  20. public GlobalBankAPI()
  21. {
  22. username = Utility.GetgblUserid();
  23. password = Utility.GetgblPassword();
  24. payingBranchCd = Utility.GetgblpayoutCode();
  25. /*
  26. * Application to Production Only
  27. */
  28. ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
  29. ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) => true;
  30. X509Certificate2 c = GetCertificateFromFile();
  31. gblApi.ClientCertificates.Add(c);
  32. }
  33. public DbResult GetStatus(string user, string pinNo)
  34. {
  35. var dr = new DbResult();
  36. var id = "0";
  37. try
  38. {
  39. id = Utility.LogRequest(user, ProviderName, "GetRemoteStatus", pinNo, "Control No:" + pinNo).Id;
  40. var res = gblApi.GetRemoteStatus(username, password, pinNo);
  41. if (res == null)
  42. Utility.LogResponse(id, Utility.ArrayToXML(res));
  43. else
  44. Utility.LogResponse(id, Utility.ArrayToXML(res), res[0], res[1]);
  45. if (res == null)
  46. {
  47. dr.SetError("1", "Thirdparty API Server could not process request.", pinNo);
  48. }
  49. else if (string.IsNullOrWhiteSpace(res[0]))
  50. {
  51. dr.SetError("1", "Thirdparty API Server could not process request.", pinNo);
  52. }
  53. else
  54. {
  55. if (res[0].Equals("S007") || res[0].Equals("S008") || res[0].Equals("S009"))
  56. {
  57. dr.SetError("0", res[1], pinNo);
  58. }
  59. else
  60. {
  61. dr.SetError("1", res[1], pinNo);
  62. }
  63. }
  64. return dr;
  65. }
  66. catch (Exception ex)
  67. {
  68. dr.SetError("1", ex.Message, pinNo);
  69. Utility.LogResponse(id, ex.Message);
  70. return dr;
  71. }
  72. }
  73. public DbResult CancelTransaction(string user, string pinNo)
  74. {
  75. var dr = new DbResult();
  76. var id = "0";
  77. try
  78. {
  79. id = Utility.LogRequest(user, ProviderName, "CancelRemoteRemit", pinNo, "Control No:" + pinNo).Id;
  80. var res = gblApi.CancelRemoteRemit(username, password, pinNo);
  81. if (res == null)
  82. Utility.LogResponse(id, Utility.ArrayToXML(res));
  83. else
  84. Utility.LogResponse(id, Utility.ArrayToXML(res), res[0], res[1]);
  85. if (res == null)
  86. {
  87. dr.SetError("1", "Thirdparty API Server could not process request.", pinNo);
  88. }
  89. else if (string.IsNullOrWhiteSpace(res[0]))
  90. {
  91. dr.SetError("1", "Thirdparty API Server could not process request.", pinNo);
  92. }
  93. else
  94. {
  95. if (res[0].Equals("C000"))
  96. {
  97. dr.SetError("0", res[1], pinNo);
  98. }
  99. else
  100. {
  101. dr.SetError("1", res[1], pinNo);
  102. }
  103. }
  104. return dr;
  105. }
  106. catch (Exception ex)
  107. {
  108. dr.SetError("1", ex.Message, pinNo);
  109. Utility.LogResponse(id, ex.Message);
  110. return dr;
  111. }
  112. }
  113. public DbResult UpdateReceiverName(string user, string pinNo, string accNo, string newBenefName, string oldBenefName)
  114. {
  115. var dr = new DbResult();
  116. var id = "0";
  117. try
  118. {
  119. id = Utility.LogRequest(user, ProviderName, "AmendRemoteTxn", pinNo, "Control No:" + pinNo).Id;
  120. var res = gblApi.AmendRemoteTxn(username, password, pinNo,accNo,newBenefName,oldBenefName);
  121. if (res == null)
  122. Utility.LogResponse(id, Utility.ArrayToXML(res));
  123. else
  124. Utility.LogResponse(id, Utility.ArrayToXML(res), res[0], res[1]);
  125. if (res == null)
  126. {
  127. dr.SetError("1", "Thirdparty API Server could not process request.", pinNo);
  128. }
  129. else if (string.IsNullOrWhiteSpace(res[0]))
  130. {
  131. dr.SetError("1", "Thirdparty API Server could not process request.", pinNo);
  132. }
  133. else
  134. {
  135. if (res[0].Equals("C000"))
  136. {
  137. dr.SetError("0", res[1], pinNo);
  138. }
  139. else
  140. {
  141. dr.SetError("1", res[1], pinNo);
  142. }
  143. }
  144. return dr;
  145. }
  146. catch (Exception ex)
  147. {
  148. dr.SetError("1", ex.Message, pinNo);
  149. Utility.LogResponse(id, ex.Message);
  150. return dr;
  151. }
  152. }
  153. public DbResult CreateTransaction(string user, GblSendTransactionRequest input, string processId)
  154. {
  155. var dr = new DbResult();
  156. var id = Utility.LogRequest(user, ProviderName, "AddRemoteRemit", input.ControlNo, Utility.ObjectToXML(input), processId).Id;
  157. //Validate XML Request for Duplicate Checking
  158. var validateRes = Utility.ValidateRequest(user, input.ControlNo);
  159. if (validateRes.ErrorCode != "0")
  160. {
  161. Utility.LogResponse(id, validateRes.Msg, validateRes.ErrorCode, validateRes.Msg);
  162. dr.SetError(validateRes.ErrorCode, validateRes.Msg, validateRes.Id);
  163. return dr;
  164. }
  165. try
  166. {
  167. var res = gblApi.AddRemoteRemit(
  168. username
  169. , password
  170. , Utility.BlankIfNull(input.ControlNo)
  171. , Utility.BlankIfNull(input.BenefName)
  172. , Utility.BlankIfNull(input.BenefAdddress)
  173. , Utility.BlankIfNull(input.BenefTel)
  174. , Utility.BlankIfNull(input.BenefMobile)
  175. , Utility.BlankIfNull(input.BenefIdType)
  176. , Utility.BlankIfNull(input.BenefAccIdNo)
  177. , Utility.BlankIfNull(input.SenderName)
  178. , Utility.BlankIfNull(input.SenderAddress)
  179. , Utility.BlankIfNull(input.SenderTel)
  180. , Utility.BlankIfNull(input.SenderMobile)
  181. , Utility.BlankIfNull(input.SenderIdType)
  182. , Utility.BlankIfNull(input.SenderIdNo)
  183. , Utility.BlankIfNull(input.Purpose)
  184. , Utility.BlankIfNull(input.RemitType)
  185. , Utility.BlankIfNull(input.PayingBankBranchCd)
  186. , Utility.BlankIfNull(input.RCurrency)
  187. , Utility.BlankIfNull(input.LocalAmount)
  188. , Utility.BlankIfNull(input.Amount)
  189. , Utility.BlankIfNull(input.ServiceCharge)
  190. , Utility.BlankIfNull(input.RCommission)
  191. , Utility.BlankIfNull(input.ExchangeRate)
  192. , Utility.BlankIfNull(input.RefNo)
  193. , Utility.BlankIfNull(input.Remarks)
  194. , Utility.BlankIfNull(input.Source)
  195. , Utility.BlankIfNull(input.NewAccount)
  196. , Utility.BlankIfNull(input.customerDOB)
  197. );
  198. if (res == null)
  199. Utility.LogResponse(id, Utility.ArrayToXML(res));
  200. else
  201. {
  202. Utility.LogResponse(id, Utility.ArrayToXML(res), res[0], res[1]);
  203. dr.TpErrorCode = res[0];
  204. }
  205. if (res == null)
  206. {
  207. dr.SetError("1", "Thirdparty API Server could not process request.", input.ControlNo);
  208. }
  209. else if (string.IsNullOrWhiteSpace(res[0]))
  210. {
  211. dr.SetError("1", "Thirdparty API Server could not process request.", input.ControlNo);
  212. }
  213. else
  214. {
  215. if (res[0].Equals("R000"))
  216. {
  217. dr.SetError("0", res[1], res[3]);
  218. dr.Extra = res[2];
  219. }
  220. else
  221. {
  222. dr.SetError("1", res[1], input.ControlNo);
  223. }
  224. }
  225. return dr;
  226. }
  227. catch (Exception ex)
  228. {
  229. dr.SetError("1", ex.Message, input.ControlNo);
  230. Utility.LogResponse(id, ex.Message);
  231. return dr;
  232. }
  233. }
  234. public DbResult GetTransactions(string user, string date, out string[] res)
  235. {
  236. date = DateTime.Parse(date).ToString("yyyy-MM-dd");
  237. var id = Utility.LogRequest(user, ProviderName, "GetRemoteTransactionDetail", date, "Date : " + date).Id;
  238. var dr = new DbResult();
  239. try
  240. {
  241. var tmpDate = Utility.GetDateToSqlDate(date);
  242. res = gblApi.GetRemoteTransactionDetail(username, password, tmpDate);
  243. Utility.LogResponse(id, Utility.ArrayToXML(res));
  244. if (res == null)
  245. {
  246. dr.SetError("1", "No transaction found", "");
  247. }
  248. else if (res.Length.Equals(0))
  249. {
  250. dr.SetError("1", "No transaction found", "");
  251. }
  252. else if (res[0].StartsWith("ERROR------->"))
  253. {
  254. dr.SetError("1", res[0], "");
  255. }
  256. else
  257. {
  258. dr.SetError("0", "Success", "");
  259. }
  260. return dr;
  261. }
  262. catch (Exception ex)
  263. {
  264. dr.SetError("1", ex.Message, "");
  265. Utility.LogResponse(id, ex.Message);
  266. res = null;
  267. return dr;
  268. }
  269. }
  270. public DbResult GetAccountDetail(string user, string accNo)
  271. {
  272. var id = Utility.LogRequest(user, ProviderName, "GetRemoteAccountDetail", accNo, "Account No:" + accNo).Id;
  273. var dr = new DbResult();
  274. try
  275. {
  276. var res = gblApi.GetRemoteAccountDetail(username, password, accNo);
  277. Utility.LogResponse(id, Utility.ArrayToXML(res));
  278. if (res == null)
  279. {
  280. dr.SetError("1", "Thirdparty API Server could not process request.", accNo);
  281. }
  282. else if (string.IsNullOrWhiteSpace(res[0]))
  283. {
  284. dr.SetError("1", "Thirdparty API Server could not process request.", accNo);
  285. }
  286. else
  287. {
  288. if (res[0].Equals("A000"))
  289. {
  290. if (string.IsNullOrWhiteSpace(res[3]) || string.IsNullOrWhiteSpace(res[2]))
  291. {
  292. dr.SetError("1", "Could not find Account Details.", accNo);
  293. }
  294. else
  295. {
  296. dr.SetError("0", res[3], res[2]);
  297. }
  298. }
  299. else
  300. {
  301. dr.SetError("1", res[1], accNo);
  302. }
  303. }
  304. return dr;
  305. }
  306. catch (Exception ex)
  307. {
  308. dr.SetError("1", ex.Message, accNo);
  309. Utility.LogResponse(id, ex.Message);
  310. return dr;
  311. }
  312. }
  313. public DbResult GetReconcileReport(string user, string trnDate, out DataTable dt)
  314. {
  315. var dr = new DbResult();
  316. dt = new DataTable();
  317. var id = "0";
  318. try
  319. {
  320. dt.Columns.Add("Control No");
  321. dt.Columns.Add("Status");
  322. dt.Columns.Add("SendOn");
  323. dt.Columns.Add("Sender");
  324. dt.Columns.Add("Receiver");
  325. dt.Columns.Add("Amount");
  326. dt.Columns.Add("PaidOn");
  327. trnDate = DateTime.Parse(trnDate).ToString("yyyy-MM-dd");
  328. var requestXml = trnDate;
  329. id = Utility.LogRequest(user, "Global Bank", "GetRemoteTransactionDetail", trnDate, requestXml).Id;
  330. var res = gblApi.GetRemoteTransactionDetail(username, password, trnDate);
  331. var responseXml = Utility.ObjectToXML(res);
  332. Utility.LogResponse(id, responseXml, "0", "Success");
  333. if (res == null || res.Length.Equals(0))
  334. {
  335. dr.SetError("1", "API Server Could Not Process Your Request.", trnDate);
  336. Utility.LogResponse(id, dr.Msg, dr.ErrorCode, dr.Msg);
  337. return dr;
  338. }
  339. if (res[0].Split('|').Length < 8)
  340. {
  341. var msg = "API Server Returned Invalid Data.";
  342. if (res.Length > 1)
  343. {
  344. msg = res[1];
  345. }
  346. dr.SetError("1", msg, trnDate);
  347. Utility.LogResponse(id, dr.Msg, dr.ErrorCode, dr.Msg);
  348. }
  349. else
  350. {
  351. foreach (var row in res)
  352. {
  353. var cols = row.Split('|');
  354. var newRow = dt.NewRow();
  355. for (var i = 1; i < 8; i++)
  356. {
  357. var iData = cols[i];
  358. if (iData.ToUpper().Equals("NULL"))
  359. iData = "";
  360. newRow[i - 1] = iData;
  361. }
  362. dt.Rows.Add(newRow);
  363. }
  364. dr.SetError("0", "Success", trnDate);
  365. }
  366. }
  367. catch (Exception ex)
  368. {
  369. Utility.LogResponse(id, ex.Message, "999", "Internal Error");
  370. dr.SetError("1", ex.Message, "");
  371. }
  372. return dr;
  373. }
  374. private static X509Certificate2 GetCertificateFromFile()
  375. {
  376. if (!foundCertificate)
  377. {
  378. var gblCertPath = Utility.GetgblCertPath();
  379. var gblCertPwd = Utility.GetgblCertPwd();
  380. var c = new X509Certificate2(gblCertPath, gblCertPwd, X509KeyStorageFlags.MachineKeySet);
  381. cert = c;
  382. foundCertificate = true;
  383. }
  384. return cert;
  385. }
  386. }
  387. }