public async Task <IActionResult> BorrowPower([FromBody] BuyPowerModel buyPower) { if (!ModelState.IsValid) { return(Ok(new FailureModel { ResponseCode = "90", Message = "Bad request" })); } var refChecker = _context.Transactions.Where(x => x.reference == buyPower.reference).FirstOrDefault(); if (refChecker != null) { return(Ok(new FailureModel { Message = refChecker.respDescription, ResponseCode = "94" })); } string path = "paelyt/PowerTransact/BorrowPower"; string refNumber = Guid.NewGuid().ToString().Substring(0, 15); var Power = new BuyPowerRequestModel { accountType = buyPower.accountType, customerId = buyPower.customerId, amount = buyPower.amount, customerName = buyPower.customerName, emailAddress = buyPower.emailAddress, phoneNumber = buyPower.phoneNumber, MerchantFK = buyPower.MerchantFK, hashValue = Utilities.GenerateHash(config.Value.AgentId + config.Value.AgentKey + buyPower.customerId + buyPower.amount), refNumber = refNumber }; var stringContent = new StringContent(JsonConvert.SerializeObject(Power), Encoding.UTF8, "application/json"); var SId = HttpContext.Session.GetString("SessionId"); if (SId == null) { SId = await Utilities.GetSessionId(config); HttpContext.Session.SetString("SessionId", SId); } var resp = await Utilities.MakeCallPost(path, config, stringContent, SId); var res = JsonConvert.DeserializeObject <BuyPowerResponseModel>(await resp.Content.ReadAsStringAsync()); res.refNumber = buyPower.reference; Transaction trans = Convert(res); trans.UserId = User.FindFirstValue("Id"); trans.CustomerMeterID = Power.customerId; trans.AccountType = buyPower.accountType; trans.reference = buyPower.reference; _context.Transactions.Add(trans); _context.SaveChanges(); if (res.respCode != "00") { return(Ok(new FailureModel { Message = res.respDescription, ResponseCode = "99" })); } return(Ok(new SuccessModel { ResponseCode = "00", Message = res.respDescription, Data = res })); }
public async Task <IActionResult> BuyPower([FromBody] BuyPowerModel buyPower) { if (!ModelState.IsValid) { return(Ok(new FailureModel { ResponseCode = "90", Message = "Bad request" })); } var UserId = User.FindFirstValue("Id"); var Reseller = _context.Users.Where(x => x.UserModelId == UserId).FirstOrDefault(); if (Reseller == null) { return(Unauthorized()); } var inst = Guid.NewGuid().ToString(); var Log = new Logger { CustomerId = buyPower.customerId, date = DateTime.Now, Id = Guid.NewGuid().ToString(), MerchantFk = buyPower.MerchantFK.ToString(), InstanceId = inst, Status = "Connected to my endpoint" }; _context.Logs.Add(Log); var refChecker = _context.Transactions.Where(x => x.reference == buyPower.reference).FirstOrDefault(); bool isRequery = false; string refNumber; if (refChecker != null && refChecker.value != "AWAITING_SERVICE_PROVIDER") { isRequery = false; return(Ok(new SuccessModel { Message = refChecker.respDescription, ResponseCode = "94", Data = refChecker })); } else if (refChecker != null & refChecker.value == "AWAITING_SERVICE_PROVIDER") { //Perform a call to the details transactions endpoint here var response = await TransactionDetails(new TransactionDetailsModel { amount = refChecker.amount, MerchantFK = 1, refNumber = refChecker.refNumber }); return(response); } refNumber = Guid.NewGuid().ToString().Substring(0, 8); if (Reseller.Subscription == SubscriptionType.Prepaid && Reseller.Balance < System.Convert.ToDouble(buyPower.amount)) { return(Ok(new FailureModel { Message = "Account Balance not Sufficient", ResponseCode = "93" })); } string path = "paelyt/PowerTransact/BuyPower"; var Power = new BuyPowerRequestModel { accountType = buyPower.accountType, customerId = buyPower.customerId, amount = buyPower.amount, customerName = buyPower.customerName, emailAddress = buyPower.emailAddress, phoneNumber = buyPower.phoneNumber, MerchantFK = buyPower.MerchantFK, hashValue = Utilities.GenerateHash(buyPower.customerId + buyPower.amount + config.Value.AgentId + config.Value.AgentKey), refNumber = refNumber, }; var stringContent = new StringContent(JsonConvert.SerializeObject(Power), Encoding.UTF8, "application/json"); var SId = HttpContext.Session.GetString("SessionId"); if (SId == null) { SId = await Utilities.GetSessionId(config); HttpContext.Session.SetString("SessionId", SId); } var resp = await Utilities.MakeCallPost(path, config, stringContent, SId); var res = JsonConvert.DeserializeObject <BuyPowerResponseModel>(await resp.Content.ReadAsStringAsync()); res.Status = inst.ToString(); //Deduct amount from wallet balance if account is Postpaid. if (!isRequery) { double ResellerBalance = Reseller.Balance; Reseller.Balance = ResellerBalance - System.Convert.ToDouble(buyPower.amount); _context.Entry(Reseller).State = Microsoft.EntityFrameworkCore.EntityState.Modified; } var Logg = new Logger { CustomerId = buyPower.customerId, date = DateTime.Now, Id = Guid.NewGuid().ToString(), MerchantFk = buyPower.MerchantFK.ToString(), InstanceId = inst, Status = "Returned response" }; _context.Logs.Add(Logg); res.refNumber = buyPower.reference; Transaction trans = Convert(res); trans.UserId = User.FindFirstValue("Id"); trans.CustomerMeterID = Power.customerId; trans.AccountType = buyPower.accountType; var refff = _context.Transactions.Where(x => x.reference == buyPower.reference).ToList(); if (isRequery) { refChecker.value = trans.value; refChecker.value1 = trans.value; refChecker.description = "success"; _context.Entry(refChecker).State = Microsoft.EntityFrameworkCore.EntityState.Modified; } else { _context.Transactions.Add(trans); } trans.ResellerPercent = Reseller.Percentage; trans.refNumber = refNumber; if (res.respCode.Length > res.respDescription.Length) { var code = res.respDescription; res.respDescription = res.respCode; res.respCode = code; } _context.SaveChanges(); res.refNumber = buyPower.reference; if (res.respCode != "00") { return(Ok(new FailureModel { Message = res.respDescription, ResponseCode = "99" })); } return(Ok(new SuccessModel { Message = res.respDescription, ResponseCode = "00", Data = res })); }