Пример #1
0
        public async Task <IActionResult> PutRechargeTransaction([FromRoute] long id, [FromBody] RechargeTransaction rechargeTransaction)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != rechargeTransaction.Id)
            {
                return(BadRequest());
            }

            _context.Entry(rechargeTransaction).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RechargeTransactionExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Пример #2
0
        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));
        }