public async Task <IActionResult> PostTransaction([FromBody] VendRequest req)
        {
            _logger.LogInformation($"{req?.RechargeType} purchase request for {req?.PhoneNumber} NGN{req?.Amount}");
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //persist request
            var rechargeType = req.RechargeType.ParseEnum <RechargeType>();
            var serviceCode  = await _context.ServiceCodes.FirstOrDefaultAsync(x => x.RechargeType == rechargeType &&
                                                                               x.NetworkType == req.NetworkType.ParseEnum <NetworkType>());

            var reqTrans = TransactionService.GetTransaction(req, rechargeType, serviceCode?.Code);

            _context.RechargeTransactions.Add(reqTrans);
            await _context.SaveChangesAsync();

            _logger.LogInformation("Transaction request persisted.");

            //initate service call
            var switchReq = CreditSwitchService.GetRequest(reqTrans, _config);
            CreditSwitchRespose switchResp = null;

            try
            {
                switchResp = await RestHandler.PostJsonAsync <CreditSwitchRespose>(_config.BaseUrl, _config.AirtimePath, switchReq);

                if (switchResp == null)
                {
                    _logger.LogWarning("Switch response is null");
                    return(BadRequest(TransactionService.GetFailureResponse(req)));
                }
            } catch (Exception e)
            {
                _logger.LogError(e, "Web service request failed.");
                return(BadRequest(TransactionService.GetFailureResponse(req)));
            }

            //persist response
            var respTrans = TransactionService.GetTransaction(switchResp, switchReq, rechargeType);

            _context.RechargeTransactions.Add(respTrans);
            await _context.SaveChangesAsync();

            _logger.LogInformation("Response transaction was persisted successfully.");

            var resp = TransactionService.GetResponse(switchResp, switchReq, req.DealerCode, null);

            return(Ok(resp));
        }
        public static VendResponse GetResponse(CreditSwitchRespose resp, CreditSwitchRequest req, string dealerCode, string message)
        {
            if (resp == null)
            {
                return(null);
            }

            VendResponse response = new VendResponse
            {
                TransactionId   = String.IsNullOrWhiteSpace(resp.MReference) ? req.RequestId : resp.MReference,
                PhoneNumber     = String.IsNullOrWhiteSpace(resp.Recipient) ? req.Recipient : resp.Recipient,
                DealerCode      = dealerCode,
                Amount          = Convert.ToInt64(req.Amount),
                Success         = resp.StatusCode == "00",
                ResponseCode    = resp.StatusCode,
                ResponseMessage = message ?? resp.StatusDescription
            };

            return(response);
        }
        public static RechargeTransaction GetTransaction(CreditSwitchRespose resp, CreditSwitchRequest req, RechargeType type)
        {
            if (resp == null)
            {
                return(null);
            }

            RechargeTransaction transaction = new RechargeTransaction
            {
                Amount              = Convert.ToDouble(resp.Amount == 0 ? req.Amount : resp.Amount),
                ResponseCode        = resp.StatusCode,
                ResponseDescription = resp.StatusDescription,
                TransactionId       = String.IsNullOrWhiteSpace(resp.MReference) ? req.RequestId : resp.MReference,
                Reference           = resp.TranxReference,
                PhoneNumber         = String.IsNullOrWhiteSpace(resp.Recipient) ? req.Recipient : resp.Recipient,
                ConfirmCode         = resp.ConfirmCode,
                TransDate           = resp.TranxDate,
                RechargeType        = type,
                RequestType         = RequestType.Response,
                Status              = resp.StatusCode == "00" ? TransactionStatus.SUCCESSFUL : TransactionStatus.FAILED
            };

            return(transaction);
        }