using DealerSelection.Api.CommonUtil; using DealerSelection.Api.Infrastructure.CCAvenue; using DealerSelection.Api.Interface; using DealerSelection.Common.Configuration; using Microsoft.Extensions.Logging; using Newtonsoft.Json.Linq; using Newtonsoft.Json; using System.Collections.Generic; using System.Collections.Specialized; using System.Net; using CCA.Util; using DealerSelection.Api.Models; using FastExpressionCompiler; using JasperFx.Core; using System.Security.Cryptography; public class CCAvenueApi : ICCAvenueApi { private readonly ILogger _logger; private IRepository Repository { get; } private IMulesoftApi MulesoftApi { get; } private string? CCAvenueApiPayUrl = ConfigurationHelper.GetSetting("CCAvenueApiPayUrl", true); public CCAvenueApi(ILogger logger, IRepository repository) { _logger = logger; Repository = repository; } public async Task RefundOrder(string bookingId, string transactionId) { Dto customerInfo = await Repository.GetBookingDetail(bookingId, transactionId); //Dto customerInfo = new Dto() //{ // AmountPaid = "4499", // BuId = 1, // RecordId = 800 //}; RefundApiResponse response = null; if (customerInfo != null) { CustomCfg cfg = CustomCfg.GetCustomCfg(customerInfo.BuId); try { int rnd = RandomNumberGenerator.GetInt32(100000,999999); string refundRefNo= cfg.BuCode + customerInfo.RecordId + rnd; string orderStatusQueryJson = "{\"reference_no\":\"" + transactionId + "\",\"refund_amount\": \"" + customerInfo.AmountPaid + "\"," + "\"refund_ref_no\": \"" + refundRefNo + "\" }"; string encJson = ""; string queryUrl = CCAvenueApiPayUrl + "/apis/servlet/DoWebTrans?"; CCACrypto ccaCrypto = new CCACrypto(); encJson = ccaCrypto.Encrypt(orderStatusQueryJson, cfg.CCAvenueWorkingKey); // string authQueryUrlParam = "enc_request=" + encJson + "&access_code=" + cfg.CCAvenueAccessCode + "&command=refundOrder&request_type=JSON&response_type=JSON"; string message = await SendRequestToGateWay(queryUrl, authQueryUrlParam); NameValueCollection param = GetResponseCollection(message); string status = ""; string encResJson = ""; if (param != null && param.Count == 2) { //for (int i = 0; i < param.Count; i++) //{ // if ("status".Equals(param.Keys[i])) // { // status = param[i]; // } // if ("enc_response".Equals(param.Keys[i])) // { // encResJson = param[i]; // } //} if (message.Contains("status") && message.Contains("enc_response")) { status = param.Get("status"); encResJson = param.Get("enc_response"); } string ResJson = ""; // response.Status = status; RefundStatusResponse _refundStatusResponse = new(); if (!"".Equals(status) && status.Equals("0")) { ResJson = ccaCrypto.Decrypt(encResJson, cfg.CCAvenueWorkingKey); dynamic objResult = JObject.Parse(ResJson); _refundStatusResponse.reason = objResult.Refund_Order_Result.reason; _refundStatusResponse.refund_status = objResult.Refund_Order_Result.refund_status; _refundStatusResponse.error_code = objResult.Refund_Order_Result.error_code; //_refundStatusResponse = JsonConvert.DeserializeObject(ResJson); _logger.LogInformation($"DS Api:-BU:- {customerInfo.BuId} + Refund Api Response for bookingId:- {bookingId} and Response:- {ResJson}"); //if (_refundStatusResponse != null) //{ // response.refundStatusResponse = _refundStatusResponse; //} } else if (!"".Equals(status) && status.Equals("1")) { _refundStatusResponse.reason = encResJson; } await Repository.UpdateRefundStatus(customerInfo.BuId,bookingId, transactionId, refundRefNo, _refundStatusResponse); response = new RefundApiResponse(status, _refundStatusResponse); } return response; } catch (Exception ex) { _logger.LogError($"DS Api:-BU:- {customerInfo.BuId} + RefundOrder Error for bookingId:- {bookingId} - {ex.StackTrace}"); throw ex; } } else { RefundStatusResponse _refundStatusResponse = new(); { _refundStatusResponse.refund_status = 1; _refundStatusResponse.reason = "No record found"; }; response = new RefundApiResponse("1", _refundStatusResponse); response.Status = "1"; } return response; } private static async Task SendRequestToGateWay(string queryUrl, string urlParam) { string message = ""; try { StreamWriter myWriter = null;// it will open a http connection with provided url WebRequest objRequest = WebRequest.Create(queryUrl);//send data using objxmlhttp object objRequest.Method = "POST"; objRequest.ContentType = "application/x-www-form-urlencoded";//to set content type myWriter = new StreamWriter(objRequest.GetRequestStream()); myWriter.Write(urlParam);//send data myWriter.Close();//closed the myWriter object // Getting Response HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();//receive the responce from objxmlhttp object using (StreamReader sr = new StreamReader(objResponse.GetResponseStream())) { message = sr.ReadToEnd(); } } catch (Exception ex) { throw ex; } return message; } private static NameValueCollection GetResponseCollection(string message) { NameValueCollection Params = new NameValueCollection(); try { if (message != null || !"".Equals(message)) { string[] segments = message.Split('&'); foreach (string seg in segments) { string[] parts = seg.Split('='); if (parts.Length > 0) { string Key = parts[0].Trim(); string Value = parts[1].Trim(); Params.Add(Key, Value); } } } } catch (Exception ex) { throw ex; } return Params; } }