Esempio n. 1
0
        public async Task <PaymentResponse> CreatePayment(PaymentRequest paymentRequest)
        {
            if (paymentRequest == null)
            {
                return(null);
            }

            var payment = _mapper.Map <Payment>(paymentRequest);

            payment.Status = PaymentStatus.Captured;

            payment = await _paymentsRepo.CreatePayment(payment);

            var bankRequest = _mapper.Map <BankPaymentRequest>(paymentRequest);

            try
            {
                var bankResponse = await _bankRepo.ProcessCardPost(bankRequest);

                if (bankResponse != null)
                {
                    payment.Approved     = bankResponse.Approved;
                    payment.AuthCode     = bankResponse.AuthCode;
                    payment.Status       = bankResponse.Approved ? PaymentStatus.Authorized : PaymentStatus.Declined;
                    payment.ResponseCode = bankResponse.ResponseCode;
                }

                payment = await _paymentsRepo.UpdatePaymentStatus(payment.Id, payment.Approved, payment.AuthCode, payment.Status, payment.ResponseCode);
            }
            catch (HttpRequestException e)
            {
                _logger.LogError($"Payment Failed - {e.Message}");
                payment.Approved = false;
                payment.Status   = PaymentStatus.Failed;
                payment          = await _paymentsRepo.UpdatePaymentStatus(payment.Id, payment.Approved, payment.AuthCode, payment.Status, null);
            }

            var response = _mapper.Map <PaymentResponse>(payment);

            return(response);
        }