public async Task <bool> Handle(RedeemCoupon request, CancellationToken cancellationToken)
        {
            if (await _accountDiscount.GetUnredeemedDiscountByAccountIdAsync(request.AccountId) == null)
            {
                var gateway = new CodeJarGateway();

                var response = await gateway.RedeemCodeAsync(request.CouponCode);

                if (response.IsSuccess())
                {
                    var discountName = response.PromotionType;
                    var discount     = await _discount.GetDiscountByNameAsync(discountName);

                    if (discount == null)
                    {
                        return(false);
                    }

                    var accountDiscount = new AccountDiscount
                    {
                        Id         = _accountDiscount.NextId(),
                        AccountId  = request.AccountId,
                        DiscountId = discount.Id
                    };

                    await _accountDiscount.AddAsync(accountDiscount);

                    await _accountDiscount.SaveChangesAsync();

                    return(true);
                }
            }

            return(false);
        }
Example #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);
        }