Exemplo n.º 1
0
        /// <summary>
        /// Adds a new payment
        /// </summary>
        public Common.Models.Payment StorePayment(Common.Models.Payment paymentRequest)
        {
            using (var context = new PaymentsDbContext(ContextOptions))
            {
                try
                {
                    var dbPayment = context.Payments.FirstOrDefault(c => c.Id == paymentRequest.PaymentId);
                    if (dbPayment == null || string.IsNullOrWhiteSpace(dbPayment.Id.ToString()))
                    {
                        Log.LogInformation($"Adding the payment to the database: {paymentRequest.PaymentId}");
                        var newPayment = MyMapper.Map <Models.Payment>(paymentRequest);
                        context.Payments.Add(newPayment);
                        context.SaveChanges();
                    }
                    else
                    {
                        Log.LogWarning($"Duplicate Payment: {paymentRequest.PaymentId}");
                        paymentRequest.Status       = Common.Enums.PaymentStatus.DuplicateRequest;
                        paymentRequest.IsSuccessful = false;
                        paymentRequest.Message      = $"Duplicate payment request";
                    }

                    return(paymentRequest);
                }
                catch (Exception ex)
                {
                    Log.LogError(ex, "Failed to store payment");
                    paymentRequest.Status = PaymentStatus.PaymentNotStored;
                    return(paymentRequest);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets a payment by PAymentId
        /// </summary>
        /// <param name="paymentId"></param>
        /// <returns></returns>
        public Payment GetPayment(Guid paymentId)
        {
            using (var context = new PaymentsDbContext(ContextOptions))
            {
                try
                {
                    var defaultResponse = new Common.Models.Payment
                    {
                        PaymentId = paymentId,
                        Status    = PaymentStatus.RequestDoesNotExist,
                        Message   = $"The payment: {paymentId} does not exist"
                    };
                    var payment = (from p in context.Payments
                                   join c in context.Cards on p.CardId equals c.Id
                                   join u in context.Users on c.UserId equals u.Id
                                   where p.Id == paymentId
                                   select new Common.Models.Payment
                    {
                        Amount = p.Amount,
                        BankPaymentId = p.BankPaymentId,
                        CurrencyCode = p.CurrencyCode,
                        FullName = u.Fullname,
                        IsSuccessful = p.IsSuccessful,
                        Message = p.Message,
                        PaymentId = p.Id,
                        RequestCompleted = p.RequestCompleted,
                        RequestDate = p.RequestDate,
                        Status = p.PaymentStatusId,
                        CardExpiryDate = c.ExpiryDate,
                        CardNumber = c.CardNumber,
                        CVC = c.CVC
                    }).FirstOrDefault();

                    if (payment == null)
                    {
                        payment = defaultResponse;
                    }

                    return(payment);
                }
                catch (Exception ex)
                {
                    Log.LogError(ex, $"Failed to get payment information {paymentId}");
                    return(new Payment
                    {
                        PaymentId = paymentId,
                        IsSuccessful = false,
                        Status = PaymentStatus.Error,
                        Message = ex.Message
                    });
                }
            }
        }