/// <summary>
        /// Subscribes the user to a Stripe plan. If the user doesn't exist in Stripe, is created
        /// </summary>
        /// <param name="user">Application User</param>
        /// <param name="planId">Plan Id to subscribe the user to</param>
        /// <param name="taxPercent">The tax percent.</param>
        /// <param name="creditCard">The credit card.</param>
        /// <returns>
        /// Subscription
        /// </returns>
        public async Task <Subscription> SubscribeUserAsync
            (SaasEcomUser user, string planId, decimal taxPercent = 0, CreditCard creditCard = null)
        {
            Subscription subscription;

            // If the user isn't created in Stripe
            if (string.IsNullOrEmpty(user.StripeCustomerId))
            {
                // Save the subscription in the DB
                subscription = await _subscriptionDataService.SubscribeUserAsync(user, planId, trialPeriodInDays : null, taxPercent : taxPercent);

                // Create a new customer in Stripe and subscribe him to the plan
                var cardToken  = creditCard == null ? null : creditCard.StripeToken;
                var stripeUser = (StripeCustomer)await _customerProvider.CreateCustomerAsync(user, planId, null, cardToken);

                user.StripeCustomerId = stripeUser.Id; // Add stripe user Id to the user

                // Save Stripe Subscription Id in the DB
                subscription.StripeId = GetStripeSubscriptionIdForNewCustomer(stripeUser);
                await _subscriptionDataService.UpdateSubscriptionAsync(subscription);
            }
            else // Create new subscription in Stripe and DB
            {
                subscription = await this.SubscribeUserAsync(user, planId, creditCard, 0, taxPercent : taxPercent);
            }

            // Update tax percent on stripe
            if (taxPercent > 0)
            {
                await this.UpdateSubscriptionTax(user, subscription.StripeId, taxPercent);
            }

            return(subscription);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a new user in Stripe and database.
        /// </summary>
        /// <param name="user">Application User</param>
        /// <param name="planId">Plan Id to subscribe the user to</param>
        /// <param name="taxPercent">The tax percent.</param>
        /// <returns>
        /// Task
        /// </returns>
        public async Task SubscribeNewUserAsync(SaasEcomUser user, string planId, decimal taxPercent = 0)
        {
            // Subscribe the user to the plan
            var subscription = await _subscriptionDataService.SubscribeUserAsync(user, planId, trialPeriodInDays : null, taxPercent : taxPercent);

            // Create a new customer in Stripe and subscribe him to the plan
            var stripeUser = (StripeCustomer)await _customerProvider.CreateCustomerAsync(user, planId);

            // Add subscription Id to the user
            user.StripeCustomerId = stripeUser.Id;

            // Save subscription Id
            subscription.StripeId = GetStripeSubscriptionId(stripeUser);
            await _subscriptionDataService.UpdateSubscriptionAsync(subscription);

            // Update tax percent on stripe
            if (taxPercent > 0)
            {
                await this.UpdateSubscriptionTax(user, subscription.StripeId, taxPercent);
            }
        }