public async Task<HttpResponseMessage> Post(ClientPaymentModel model)
        {
            // Get company for user
            DomainCompany company = await _companyService.FindByUserAsync(UserId);

            var chargeCreateOptions = new CompanyChargeOptions
            {
                Id = company.Id,
                AmountInCents = model.AmountInCents,
                Currency = DomainCurrencies.Default,
                Description = model.Description,
                TokenId = model.TokenId
            };

            // Charging

            // balance will be updated after callback (webhook) from billing system
            await _companyService.ChargeAsync(chargeCreateOptions);

            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created);
            return response;
        }
Exemplo n.º 2
0
        public async Task ChargeAsync(CompanyChargeOptions charge)
        {
            CompanyEntity company = await _companyRepository.GetAsync(charge.Id);
            if (company == null)
            {
                throw new NotFoundException(string.Format("Could not find company by id {0}", charge.Id));
            }

            if (company.State == (int)ResourceState.Blocked)
            {
                throw new ForbiddenException(string.Format("Company {0} is blocked", company.Id));
            }
            if (company.State == (int)ResourceState.Deleted)
            {
                throw new NotFoundException(string.Format("Company {0} is deleted", company.Id));
            }

            // Creating customer card
            var cardCreateOptions = new DomainCardCreateOptions
            {
                CustomerId = company.BillingCustomerId,
                TokenId = charge.TokenId
            };

            DomainCard billingCard;
            try
            {
                billingCard = await _billingCardService.AddAsync(cardCreateOptions);
            }
            catch (BillingException e)
            {
                throw new BadRequestException(string.Format("Failed to register card for company {0}: {1}", company.Id, e));
            }


            // Charging card
            var chargeCreateOptions = new DomainChargeCreateOptions
            {
                AmountInCents = charge.AmountInCents,
                CustomerId = company.BillingCustomerId,
                Currency = charge.Currency,
                Description = charge.Description,
                Card = billingCard.Id
            };

            // Balance will be updated automatically after callback (webhook) from billing system
            DomainCharge billingCharge;
            try
            {
                billingCharge = await _billingChargeService.AddAsync(chargeCreateOptions);
            }
            catch (BillingException e)
            {
                throw new BadRequestException(string.Format("Failed to charge billing customer {0} for company {1}: {2}", company.BillingCustomerId, company.Id, e));
            }

            if (!billingCharge.IsPaid)
            {
                // payment failed
                throw new PaymentRequiredException(string.Format("Payment failed for company {0}", company.Id));
            }
        }