public async Task <bool> Handle(CreateSubscription request, CancellationToken cancellationToken)
        {
            var account = await _account.FindAccountByIdAsync(request.AccountId);

            if (account.HasSubscription())
            {
                return(true);
            }

            var gateway        = _braintreeConfiguration.GetGateway();
            var planId         = SubscriptionHelper.ConvertPlanToBrainTreeType(request.Plan);
            var subscriptionId = Guid.NewGuid().ToString();

            var createSubscriptionRequest = new SubscriptionRequest
            {
                PaymentMethodToken = account.PaymentMethodId,
                PlanId             = planId,
                Id = subscriptionId,
            };

            var createSubscriptionResult = gateway.Subscription.Create(createSubscriptionRequest);

            if (createSubscriptionResult.IsSuccess())
            {
                account.UpdateSubscriptionId(createSubscriptionRequest.Id);

                await _account.SaveChangesAsync();

                return(true);
            }

            return(false);
        }
示例#2
0
        public async Task <bool> Handle(ChangeSubscription request, CancellationToken cancellationToken)
        {
            var account = await _account.FindAccountByIdAsync(request.AccountId);

            if (account.HasPaymentMethod())
            {
                var gateway = _braintreeConfiguration.GetGateway();
                var plans   = await gateway.Plan.AllAsync();

                var plan            = (from p in plans where p.Name == request.Plan select p).FirstOrDefault();
                var planId          = SubscriptionHelper.ConvertPlanToBrainTreeType(request.Plan);
                var accountDiscount = await _accountDiscountRepository.GetUnredeemedDiscountByAccountIdAsync(request.AccountId);

                var updateSubscriptionRequest = new SubscriptionRequest
                {
                    PaymentMethodToken = account.PaymentMethodId,
                    PlanId             = planId,
                    Price = plan.Price,
                };

                if (accountDiscount != null)
                {
                    var discount = await _discountRepository.GetDiscountByIdAsync(accountDiscount.DiscountId);

                    updateSubscriptionRequest.Discounts = new DiscountsRequest
                    {
                        Add = new AddDiscountRequest[]
                        {
                            new AddDiscountRequest
                            {
                                InheritedFromId = discount.Id.ToString(),
                                Amount          = DiscountCalculator.CalculateDiscount(
                                    price: plan.Price.GetValueOrDefault(),
                                    percentage: discount.Percentage),
                                NumberOfBillingCycles = discount.BillingCycles
                            }
                        },
                    };

                    accountDiscount.ApplyDiscountToSubscription();
                }

                var updateSubscriptionResult = await gateway.Subscription.UpdateAsync(account.SubscriptionId, updateSubscriptionRequest);

                if (updateSubscriptionResult.IsSuccess())
                {
                    await _accountDiscountRepository.SaveChangesAsync();

                    return(true);
                }
            }

            return(false);
        }
示例#3
0
        protected async override Task Handle(UpdateSubscriptionPaymentMethod request, CancellationToken cancellationToken)
        {
            var account = await _account.FindAccountByIdAsync(request.AccountId);

            var accountPlan = await _accountPlan.FindAccountPlanByAccountIdAsync(request.AccountId);

            var gateway        = _braintreeConfiguration.GetGateway();
            var subscriptionId = Guid.NewGuid().ToString();

            if (account.HasPaymentMethodDeletedPlan())
            {
                var downgrade = await _downgradeRepository.GetDowngradeByAccountIdAsync(request.AccountId);

                var createSubscriptionRequest = new SubscriptionRequest
                {
                    PaymentMethodToken = account.PaymentMethodId,
                    PlanId             = SubscriptionHelper.ConvertPlanToBrainTreeType(account.PaymentMethodDeletedPlan),
                    Id = subscriptionId,
                };

                var createSubscriptionResult = gateway.Subscription.Create(createSubscriptionRequest);

                account.UpdateSubscriptionId(createSubscriptionRequest.Id);

                account.RemovePaymentMethodDeletedPlan();

                await _downgradeRepository.Remove(downgrade);

                await _account.SaveChangesAsync();
            }
            else
            {
                var subscriptionUpdatedResult = await gateway.Subscription.UpdateAsync(account.SubscriptionId, new SubscriptionRequest
                {
                    PaymentMethodToken = account.PaymentMethodId,
                });
            }
        }