Пример #1
0
 public static CreditCard ToCreditCard(PaymentCreationParameters source, CreditCard destination)
 {
     destination.Cardholder  = source.CardHolder;
     destination.CardNumber  = source.CardNumber.Substring(source.CardNumber.Length - 4);
     destination.ExpiryMonth = source.CardExpiryMonth;
     destination.ExpiryYear  = source.CardExpiryYear;
     return(destination);
 }
Пример #2
0
        private void ValidatePaymentCreationParameters(PaymentCreationParameters p)
        {
            if (p.PatientId == default(int))
            {
                throw new ArgumentException("PatientId must be provided.");
            }
            if (p.Amount <= 0)
            {
                throw new ArgumentException("Amount should be greater than zero.");
            }
            switch (p.PaymentType)
            {
            case PaymentType.onetime:
                if (!p.OneTimePaymentDate.HasValue)
                {
                    throw new ArgumentException(nameof(p.OneTimePaymentDate));
                }
                if (p.OneTimePaymentDate.Value.Date < DateTime.Today)
                {
                    throw new ArgumentException("OneTimePaymentDate cannot contain a date in the past.");
                }
                if (p.OneTimePaymentDate.Value.Date > DateTime.Today.AddDays(Configuration.OneTimeTransactionTimeWindow))
                {
                    throw new ArgumentException($"One time transactions can only be scheduled within {Configuration.OneTimeTransactionTimeWindow} days.");
                }
                break;

            case PaymentType.recurring:
                if (!p.RecurringDateStart.HasValue)
                {
                    throw new ArgumentException(nameof(p.RecurringDateStart));
                }
                if (!p.RecurringDateEnd.HasValue)
                {
                    throw new ArgumentException(nameof(p.RecurringDateEnd));
                }
                if (p.RecurringDateStart.Value.Date < DateTime.Today)
                {
                    throw new ArgumentException("RecurringDateStart cannot contain a date in the past.");
                }
                if (p.RecurringDateStart.Value.Date > DateTime.Today.AddMonths(Configuration.RecurringTransactionTimeWindow))
                {
                    throw new ArgumentException($"Recurring transactions can only be scheduled within {Configuration.RecurringTransactionTimeWindow} months.");
                }
                if (p.RecurringDateStart.Value.Date > p.RecurringDateEnd.Value.Date)
                {
                    throw new ArgumentException("RecurringDateEnd cannot be greater than RecurringDateStart.");
                }
                break;
            }
        }
Пример #3
0
 public static Payment ToPayment(PaymentCreationParameters source, Payment destination)
 {
     destination.PaymentType = source.PaymentType;
     destination.PatientId   = source.PatientId;
     destination.Amount      = source.Amount;
     if (source.PaymentType == PaymentType.onetime)
     {
         destination.OneTimePaymentDate = source.OneTimePaymentDate;
     }
     else if (source.PaymentType == PaymentType.recurring)
     {
         destination.RecurringFrequency = source.RecurringFrequency;
         destination.RecurringDateEnd   = source.RecurringDateEnd;
         destination.RecurringDateStart = source.RecurringDateStart;
     }
     return(destination);
 }
Пример #4
0
        public OperationResult SavePayment(PatientLoginInfo patientLoginInfo, PaymentCreationParameters paymentCreationParameters, int?managementUserId)
        {
            if (patientLoginInfo == null)
            {
                throw new ArgumentNullException(nameof(patientLoginInfo));
            }
            if (paymentCreationParameters == null)
            {
                throw new ArgumentNullException(nameof(paymentCreationParameters));
            }
            ValidatePaymentCreationParameters(paymentCreationParameters);

            try
            {
                //save payment data locally
                var paymentDb = PaymentRepository.Create();
                Mapper.ToPayment(paymentCreationParameters, paymentDb);
                paymentDb.Active           = true;
                paymentDb.LoginId          = patientLoginInfo.ID;
                paymentDb.ManagementUserId = managementUserId;
                PaymentRepository.Insert(paymentDb);

                //create the customer in stripe
                var r = PaymentGateway.Customer(patientLoginInfo.ID, patientLoginInfo.FirstName + " " + patientLoginInfo.LastName, patientLoginInfo.Email);
                if (r["error"] != null)
                {
                    throw new Exception(r.error.message.ToString());
                }

                //create the card in stripe
                var c = PaymentGateway.CreateCard(patientLoginInfo.ID, paymentCreationParameters.CardHolder, paymentCreationParameters.CardNumber, paymentCreationParameters.CardExpiryMonth, paymentCreationParameters.CardExpiryYear, paymentCreationParameters.CardSecurityCode);
                if (c["error"] != null)
                {
                    throw new Exception(r.error.message.ToString());
                }

                //save cc data locally
                var card = CreditCardRepository.Create();
                Mapper.ToCreditCard(paymentCreationParameters, card);
                card.LoginId       = patientLoginInfo.ID;
                card.GatewayType   = PaymentGateway.GetId();
                card.GatewayCardId = c["id"];
                card.CardType      = c["brand"];
                CreditCardRepository.Insert(card);

                //update payment with cc data
                paymentDb.CreditCardId = card.Id;
                PaymentRepository.Save(paymentDb);

                if (paymentCreationParameters.PaymentType == PaymentType.recurring)
                {
                    //create instance of a recurring payment
                    var start = paymentCreationParameters.RecurringDateStart.Value;
                    var end   = paymentCreationParameters.RecurringDateEnd.Value;
                    var dates = DatesGenerator.GetDates(start, end, paymentCreationParameters.RecurringFrequency.Value).Select((m, i) => new { date = m, index = i });
                    if (dates.Count() == 0)
                    {
                        throw new InvalidOperationException("No payments to schedule.");
                    }
                    var schedules = new List <PaymentSchedule>();
                    foreach (var d in dates)
                    {
                        var schedule = PaymentScheduleRepository.Create();
                        schedule.PaymentId      = paymentDb.Id;
                        schedule.ScheduledDate  = d.date;
                        schedule.ScheduleNumber = d.index + 1;
                        schedules.Add(schedule);
                    }
                    PaymentScheduleRepository.Insert(schedules);
                }
                else
                {
                    if (paymentCreationParameters.OneTimePaymentDate.Value.Date == DateTime.Today)
                    {
                        //charge to cc since this is a onetime payment scheduled for today
                        var chargeParameters = new PaymentChargeParameters
                        {
                            Description   = "One Time Charge -  Payment Id " + paymentDb.Id.ToString("D5"),
                            Amount        = paymentCreationParameters.Amount,
                            CustomerId    = patientLoginInfo.ID,
                            GatewayCardId = card.GatewayCardId,
                            PaymentId     = paymentDb.Id,
                            CardId        = card.Id,
                            ReferenceType = "Payment",
                            ReferenceId   = paymentDb.Id
                        };
                        return(Charge(chargeParameters));
                    }
                    else
                    {
                        var schedule = PaymentScheduleRepository.Create();
                        schedule.PaymentId      = paymentDb.Id;
                        schedule.ScheduledDate  = paymentCreationParameters.OneTimePaymentDate.Value.Date;
                        schedule.ScheduleNumber = 1;
                        PaymentScheduleRepository.Insert(schedule);
                    }
                }
            }
            catch (Exception e)
            {
                return(new OperationResult {
                    Success = false, Message = e.Message
                });
            }
            return(new OperationResult {
                Success = true
            });
        }