Beispiel #1
0
        public async Task <IActionResult> Post([FromBody] CreateChargeRequest request)
        {
            // Note that explicitly checking ModelState is not required for controllers with the [ApiController]
            // attribute, so there is no code here for that here.

            var entity = new Charge
            {
                IdempotentKey = request.IdempotentKey,
                CreatedOn     = DateTimeOffset.UtcNow,
                Status        = ChargeStatus.Pending,
                Amount        = request.Amount,
                Currency      = request.Currency,
                Description   = request.Description,
                CardNumber    = request.CardNumber,
                Cvv           = request.Cvv,
                ExpiryMonth   = request.ExpiryMonth,
                ExpiryYear    = request.ExpiryYear
            };

            _db.Charges.Add(entity);

            try
            {
                await _db.SaveChangesAsync();
            }
            catch (DbUpdateException exception) when(exception.IsViolationOfUniqueIndex())
            {
                // This handles the case where someone is double-posting a charge with the same IdempotentKey.

                _logger.LogWarning("A charge with idempotency key {IdempotentKey} attempted to double post.", request.IdempotentKey);
                return(Conflict());
            }

            var acquiringBankRequest = new AcquiringBankRequest
            {
                Amount      = entity.Amount,
                Currency    = entity.Currency,
                CardNumber  = entity.CardNumber,
                Cvv         = entity.Cvv,
                ExpiryMonth = entity.ExpiryMonth,
                ExpiryYear  = entity.ExpiryYear
            };

            var success = await _bank.TrySendAsync(acquiringBankRequest, out var bankChargeId);

            if (!success)
            {
                _logger.LogError("The acquiring bank for charge id {Id} rejected the payment.", entity.Id);
            }

            entity.Status       = success ? ChargeStatus.Success : ChargeStatus.Failed;
            entity.BankChargeId = bankChargeId;

            await _db.SaveChangesAsync();

            var result = new GetChargeResponse(entity);

            return(CreatedAtAction("Get", new { id = entity.Id }, result));
        }
Beispiel #2
0
        public async Task <IActionResult> Get(Guid id)
        {
            var entity = await _db.Charges.FindAsync(id);

            if (entity is null)
            {
                _logger.LogWarning("A charge with id {Id} was not found.", id);
                return(NotFound());
            }

            var result = new GetChargeResponse(entity);

            return(Ok(result));
        }
Beispiel #3
0
        public GetChargeResponse GetById(int id)
        {
            try
            {
                var response = new GetChargeResponse();
                var bc       = new ChargeComponent();

                response.Charge = bc.GetById(id);

                return(response);
            }
            catch (Exception ex)
            {
                var httpError = new HttpResponseMessage()
                {
                    StatusCode   = (HttpStatusCode)422,
                    ReasonPhrase = ex.Message
                };

                throw new HttpResponseException(httpError);
            }
        }