예제 #1
0
        public void ShouldBeAbleToConnectToTheApiAndPostChanges()
        {
            var vendGetRequest = new VendRequest(this.Url, this.Username, this.Password);
            var getResponse    = vendGetRequest.Get("/api/products");
            var products       = getResponse.FromJson <ProductList>();

            Assert.IsNotNull(products);

            var    vendRequest   = new VendRequest(this.Url, this.Username, this.Password);
            var    tshirt        = products.Products.First(p => p.Handle == "tshirt");
            string previousValue = tshirt.Description;

            tshirt.Description = tshirt.Description == "My Company" ? "Someone Else" : "My Company";
            var response = vendRequest.Post("/api/products", tshirt.ToJson());

            Assert.IsNotNull(response);
            var responseProducts = response.FromJson <ProductWrapper>();

            Assert.IsNotNull(responseProducts);
            Assert.IsNotNull(responseProducts.Product);

            var tshirtResponse       = vendRequest.Get("/api/products/" + tshirt.Id);
            var tshirtFromWebService = tshirtResponse.FromJson <ProductList>();

            Assert.AreNotEqual(previousValue, tshirtFromWebService.Products[0].Description);
        }
예제 #2
0
        public void ShouldBeAbleToConnectToTheApiAndRequestRecentSales()
        {
            var vendRequest = new VendRequest(this.Url, this.Username, this.Password);
            var response    = vendRequest.Get("/api/products");

            Assert.IsNotNull(response);
        }
        public static VendResponse GetFailureResponse(VendRequest req)
        {
            VendResponse response = new VendResponse
            {
                DealerCode      = req?.DealerCode,
                Amount          = req == null ? 0 : req.Amount,
                Success         = false,
                ResponseCode    = "ZZ",
                ResponseMessage = $"Unable to complete your request. Please try again later."
            };

            return(response);
        }
예제 #4
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));
        }
예제 #5
0
        public void ShouldBeAbleToConvertProductRequestsIntoProductObjects()
        {
            var vendGetRequest = new VendRequest(this.Url, this.Username, this.Password);
            var getResponse    = vendGetRequest.Get("/api/products");
            var products       = getResponse.FromJson <ProductList>();

            Assert.IsNotNull(products);
            Assert.IsNotNull(products.Products);
            var tshirt = products.Products.First(p => p.Handle == "tshirt");

            Assert.IsNotNull(tshirt);
            Assert.AreEqual(0m, tshirt.Inventory[0].Count);
            Assert.AreEqual(4.5m, tshirt.PriceBookEntries[0].Price);
        }
        public static RechargeTransaction GetTransaction(VendRequest request, RechargeType type, string serviceCode)
        {
            if (request == null)
            {
                return(null);
            }

            RechargeTransaction transaction = new RechargeTransaction
            {
                Amount        = request.Amount,
                DealerCode    = request.DealerCode,
                PhoneNumber   = request.PhoneNumber,
                RechargeType  = type,
                RequestType   = RequestType.Request,
                ServiceCode   = serviceCode,
                Status        = TransactionStatus.INITIATED,
                TransactionId = Utility.GetRequestId(),
                TransDate     = DateTime.Now
            };

            return(transaction);
        }