protected async override Task Handle(DeletePaymentMethod request, CancellationToken cancellationToken)
        {
            var account = await _account.FindAccountByIdAsync(request.AccountId);

            var gateway = _braintreeConfiguration.GetGateway();

            if (account.SubscriptionId == null)
            {
                gateway.PaymentMethod.Delete(request.PaymentMethodId);

                await _account.SaveChangesAsync();
            }
            else
            {
                var currentSubscription = await gateway.Subscription.FindAsync(account.SubscriptionId);

                var planId = PlanTiers.ConvertPlanNameToInt(request.Plan);

                if (request.Plan != "Free")
                {
                    account.PaymentMethodDeletedPlan = request.Plan;
                    await _downgradeRepository.Add(request.AccountId, currentSubscription.BillingPeriodEndDate.GetValueOrDefault(), planId);
                }

                account.RemovePaymentMethodId();

                gateway.PaymentMethod.Delete(request.PaymentMethodId);

                await _account.SaveChangesAsync();
            }
        }
Beispiel #2
0
        public async Task <bool> Handle(ChangePlan request, CancellationToken cancellationToken)
        {
            var account = await _accountRepository.FindAccountByIdAsync(request.AccountId);

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

            var gateway             = _braintreeConfiguration.GetGateway();
            var currentSubscription = await gateway.Subscription.FindAsync(account.SubscriptionId);

            var planId      = PlanTiers.ConvertPlanNameToInt(request.Plan);
            var downgrading = accountPlan.IsNewPlanLessThanCurrentPlan(planId);

            if (downgrading)
            {
                var downgrade = await _downgradeRepository.GetDowngradeByAccountIdAsync(account.Id);

                if (downgrade != null)
                {
                    downgrade.BillingCycleEnd = currentSubscription.BillingPeriodEndDate.GetValueOrDefault();
                    downgrade.PlanId          = planId;
                }
                else
                {
                    await _downgradeRepository.Add(
                        accountId : request.AccountId,
                        billingCycleEnd : currentSubscription.BillingPeriodEndDate.GetValueOrDefault(),
                        planId : planId);
                }
            }
            else
            {
                accountPlan.ChangePlan(planId);
            }

            await _accountPlan.SaveChangesAsync();

            return(true);
        }