Exemplo n.º 1
0
        public HttpResponseMessage SubmitAccountPayment(string AuthToken, int UserId, int AccountTypeId, string Nonce, string DiscountCode = null)
        {
            try
            {
                //authenticate user
                var User = UserRepository.RefreshAuthToken(AuthToken);

                if (UserId != User.Id)
                {
                    throw new Exception("Unauthorized");
                }

                decimal Amount = AccountPaymentsRepository.GetAmount(AccountTypeId, DiscountCode);

                //create new account payment entity
                AccountPayment AccountPayment = new AccountPayment();
                AccountPayment.UserId         = UserId;
                AccountPayment.Amount         = Amount;
                AccountPayment.IdempotencyKey = Guid.NewGuid().ToString();
                AccountPayment.Nonce          = Nonce;

                //bill
                AccountPayment.SquarePaymentId = Payments.Bill(AccountPayment.IdempotencyKey, Nonce, (long)Amount * 100);

                //successful billing
                AccountPayment.PaymentDate = DateTime.Now;

                //set expiratin date
                //AccountPayment.ExpirationDate = DateTime.Today.AddMonths(1).AddDays(1);

                //add to database
                AccountPaymentsRepository.AddAccountPayment(AccountPayment);

                //change account type
                UserRepository.ChangeAccountType(UserId, AccountTypeId);

                return(OKResponse(true));
            }
            catch (Exception ex)
            {
                return(ErrorResponse(ex));
            }
        }