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.

200 lines
7.5 KiB

1 year ago
  1. using Common;
  2. using Common.Language;
  3. using Common.Model.AutoRefund;
  4. using Common.Model.Enum;
  5. using Common.Model.KwangjuBank;
  6. using log4net;
  7. using Newtonsoft.Json;
  8. using PushNotification;
  9. using Repository.AutoRefund;
  10. using System;
  11. using System.Runtime.Remoting.Messaging;
  12. using System.Threading.Tasks;
  13. namespace Business.AutoRefund
  14. {
  15. public class AutoRefundBusiness : IAutoRefundBusiness
  16. {
  17. private readonly IAutoRefundRepository _repo;
  18. private static readonly ILog Log = LogManager.GetLogger(typeof(AutoRefundBusiness));
  19. public AutoRefundBusiness(IAutoRefundRepository repo)
  20. {
  21. _repo = repo;
  22. }
  23. public JsonRxResponse UpdateAutoRefund(KwangjuAutoRefundModel model)
  24. {
  25. return _repo.SendAutoRefund(model);
  26. }
  27. public CustomerInfo GetCustomerInfoForKwangju(string customerId)
  28. {
  29. return _repo.GetCustomerInfoForKwangju(customerId);
  30. }
  31. public JsonRxResponse GetRefundRequirement(string userName)
  32. {
  33. var jsonRx = new JsonRxResponse() { ErrorCode = "1", Msg = "Cannot find the requirements." };
  34. var res = _repo.GetRefundRequirement(userName);
  35. if (res == null)
  36. {
  37. return jsonRx;
  38. }
  39. var minAmt = Convert.ToDouble(res.MinimumAmount) + Convert.ToDouble(res.RefundCharge);
  40. var curAmt = Convert.ToDouble(res.CurrentBalance);
  41. if (curAmt < minAmt)
  42. {
  43. jsonRx.SetResponse("1", "You donot have sufficient amount.");
  44. return jsonRx;
  45. }
  46. jsonRx.SetResponse("0", "Success");
  47. jsonRx.Data = res;
  48. return jsonRx;
  49. }
  50. public JsonRxResponse ProcessAutoRefund(AutoRefundRequestModel model)
  51. {
  52. JsonRxResponse JsonRx = new JsonRxResponse { ErrorCode = "1", Msg = "Refund Request cannot proceed further. Please contact GME head office." };
  53. KwangjuAutoRefundModel kjModel = new KwangjuAutoRefundModel
  54. {
  55. Flag = "REQ",
  56. CustomerId = model.UserId,
  57. CustomerSummary = "",
  58. Amount = model.Amount,
  59. Action = "REQ",
  60. ActionDate = "",
  61. ActionBy = model.Username
  62. };
  63. //Laster remove this for demo purpose only now
  64. // INSERT REQUEST INTO DB AND GENERATE VOUCHER / DIDUCT BALANCE
  65. var res = _repo.SendAutoRefund(kjModel);
  66. if (res.ErrorCode != "0")
  67. {
  68. JsonRx.ErrorCode = res.ErrorCode;
  69. JsonRx.Msg = res.Msg;
  70. return JsonRx;
  71. }
  72. /*
  73. * KJ_AUTO_REFUND의 RowId를 Update한다.
  74. * */
  75. kjModel.RowId = res.Id;
  76. //GET CUSTOMER INFORMATION FOR KJ BANK API TO REFUND BALANCE IN CUSTOMER PRIMARY ACCOUNT
  77. var custDetail = _repo.GetCustomerInfoForKwangju(model.UserId);
  78. //kjModel.Amount = (Convert.ToDecimal(model.Amount) - Convert.ToDecimal(model.ChargeAmount)).ToString();
  79. AmountTransferToBank req = new AmountTransferToBank
  80. {
  81. obpId = "", // "001-90010001-000001-6000001", //GME OBPID
  82. accountNo = "KJ",// "1107020345626", //GME 계좌코드
  83. accountPassword = "", // "1212", //GME 패스워드
  84. receiveInstitution = custDetail.BankCode.ToString(),
  85. receiveAccountNo = custDetail.BankAccountNo.ToString(),
  86. amount = (Convert.ToDecimal(model.Amount) - 1000).ToString(),
  87. bankBookSummary = String.Format("REFUND{0}", custDetail.BankAccountNo.ToString()),
  88. transactionSummary = "GME Refund"
  89. };
  90. //FOR TEST
  91. //req.receiveInstitution = "034";
  92. //req.receiveAccountNo = "145121636083";
  93. var body = JsonConvert.SerializeObject(req);
  94. var resp = KwangjuBankApi.TransferAmount(body);
  95. var dbApiRes = JsonConvert.DeserializeObject<JsonRxResponse>(resp);
  96. if (!dbApiRes.ErrorCode.Equals("0"))
  97. {
  98. if (dbApiRes.ErrorCode.Equals(ErrorCode.One))
  99. {
  100. var kjMsg = JsonConvert.DeserializeObject<KwangjuResult>(dbApiRes.Msg);
  101. var eCode = kjMsg.error.code.ToString();
  102. var eMessage = kjMsg.error.message.ToString();
  103. dbApiRes.Msg = "Something is wrong. " + eCode + " " + eMessage;
  104. }
  105. kjModel.Flag = "FAIL";
  106. kjModel.Action = "FAIL";
  107. }
  108. else
  109. {
  110. kjModel.Flag = "SUCCESS";
  111. kjModel.Action = "SUCCESS";
  112. }
  113. var rs = _repo.SendAutoRefund(kjModel);
  114. if (rs.ErrorCode.Equals("0"))
  115. {
  116. var fcmId = Convert.ToString(CallContext.GetData(Constants.FcmId));
  117. var lang = Convert.ToString(CallContext.GetData(Constants.Language));
  118. Task.Run(() => FcmNotifier.Notify(fcmId, Languages.GetMessage("autorefund_success", lang), Languages.GetTitle("autorefund", lang)));
  119. }
  120. rs.Data = new { Message = rs.Msg, Extra = rs.Extra };
  121. return rs;
  122. }
  123. public JsonRxResponse AutoDebitGiveBackDeposit(AutoDebitRefund refundInfo, AmountTransferToBank transferToBank)
  124. {
  125. var jsonRx = new JsonRxResponse { ErrorCode = "1", Msg = "Failed!" };
  126. _repo.LogFailTransactionForAutoDebit(refundInfo.RowId, refundInfo.ErrorCode, refundInfo.ErrorMsg);
  127. try
  128. {
  129. KwangjuAutoRefundModel kj = new KwangjuAutoRefundModel()
  130. {
  131. Flag = "Autodebit_REQ",
  132. CustomerId = refundInfo.CustomerId,
  133. CustomerSummary = "",
  134. Amount = transferToBank.amount,
  135. Action = "REQ",
  136. ActionDate = "",
  137. ActionBy = refundInfo.Username,
  138. BankCode = transferToBank.receiveInstitution,
  139. BankAccountNo = transferToBank.receiveAccountNo
  140. };
  141. var res = _repo.SendAutoRefund(kj);
  142. kj.RowId = res.Id;
  143. var json = JsonConvert.SerializeObject(transferToBank);
  144. var resp = KwangjuBankApi.TransferAmount(json);
  145. jsonRx = JsonConvert.DeserializeObject<JsonRxResponse>(resp);
  146. if (!jsonRx.ErrorCode.Equals("0"))
  147. {
  148. if (jsonRx.ErrorCode.Equals(ErrorCode.One))
  149. {
  150. var kjMsg = JsonConvert.DeserializeObject<KwangjuResult>(jsonRx.Msg);
  151. var eCode = kjMsg.error.code.ToString();
  152. var eMessage = kjMsg.error.message.ToString();
  153. jsonRx.Msg = "Something is wrong. " + eCode + " " + eMessage;
  154. }
  155. kj.Flag = "Autodebit_FAIL";
  156. kj.Action = "FAIL";
  157. }
  158. else
  159. {
  160. kj.Flag = "SUCCESS";
  161. kj.Action = "SUCCESS";
  162. }
  163. jsonRx = _repo.SendAutoRefund(kj);
  164. if (jsonRx.ErrorCode.Equals("0"))
  165. {
  166. jsonRx.Data = new { Message = jsonRx.Msg };
  167. }
  168. return jsonRx;
  169. }
  170. catch (Exception ex)
  171. {
  172. return jsonRx;
  173. }
  174. }
  175. }
  176. }