/// <summary> /// Adds the credit card asynchronous. /// </summary> /// <param name="user">The user.</param> /// <param name="card">The card.</param> /// <returns></returns> public async Task AddAsync(SaasEcomUser user, CreditCard card) { // Save to Stripe var stripeCustomerId = user.StripeCustomerId; AddCardToStripe(card, stripeCustomerId); // Save to storage card.SaasEcomUserId = user.Id; await _cardDataService.AddAsync(card); }
/// <summary> /// Subscribes the user natural month asynchronous. /// </summary> /// <param name="user">The user.</param> /// <param name="planId">The plan identifier.</param> /// <param name="card">The card.</param> /// <param name="description">The description.</param> /// <param name="taxPercent">The tax percent.</param> /// <returns></returns> public async Task <string> SubscribeUserNaturalMonthAsync(SaasEcomUser user, string planId, CreditCard card, string description, decimal taxPercent = 0) { var trialEnds = GetStartNextMonth(); if (string.IsNullOrEmpty(user.StripeCustomerId)) { // Create a new customer in Stripe and save card var stripeUser = (StripeCustomer)await _customerProvider.CreateCustomerAsync (user, null, trialEnds, card.StripeToken); // Add subscription Id to the user user.StripeCustomerId = stripeUser.Id; await _cardDataService.AddAsync(card); } else if (!string.IsNullOrEmpty(card.StripeToken)) { // Update the default card for the user _customerProvider.UpdateCustomer(user, card); card.SaasEcomUserId = user.Id; await _cardDataService.AddOrUpdateDefaultCardAsync(user.Id, card); } // Create pro-rata charge for the remaining of the month var amountInCents = await CalculateProRata(planId); var planCurrency = await GetPlanCurrency(planId); // If Charge succeeds -> Create Subscription string error; if (this._chargeProvider.CreateCharge(amountInCents, planCurrency, description, user.StripeCustomerId, out error)) { // Create Subscription var subscriptionId = _subscriptionProvider.SubscribeUser(user, planId, trialEnds, taxPercent); var subscription = await _subscriptionDataService.SubscribeUserAsync (user, planId, trialEnds, taxPercent, subscriptionId); // Update tax percent on stripe if (subscription.TaxPercent > 0) { await this.UpdateSubscriptionTax(user, subscription.StripeId, subscription.TaxPercent); } } else { return(error); } return(null); }
/// <summary> /// Subscribes the user, with a billing cycle that goes from the 1st of the month asynchronous. /// Creates the user in Stripe if doesn't exist already. /// Saves de Subscription data in the database if the subscription suceeds. /// </summary> /// <param name="user">The user.</param> /// <param name="planId">The plan identifier.</param> /// <param name="card">The card.</param> /// <param name="taxPercent">The tax percent.</param> /// <returns></returns> public async Task SubscribeUserNaturalMonthAsync(SaasEcomUser user, string planId, CreditCard card, decimal taxPercent = 0) { if (string.IsNullOrEmpty(user.StripeCustomerId)) { // Create a new customer in Stripe and save card var stripeUser = (StripeCustomer)await _customerProvider.CreateCustomerAsync(user, cardToken : card.StripeToken); user.StripeCustomerId = stripeUser.Id; card.SaasEcomUserId = user.Id; await _cardDataService.AddAsync(card); } else if (card != null && !string.IsNullOrEmpty(card.StripeToken)) { // Update the default card for the user var customer = (StripeCustomer)_customerProvider.UpdateCustomer(user, card); card.SaasEcomUserId = user.Id; card.StripeId = customer.DefaultSourceId; await _cardDataService.AddOrUpdateDefaultCardAsync(user.Id, card); } var stripeSubscription = (StripeSubscription)_subscriptionProvider.SubscribeUserNaturalMonth(user, planId, GetStartNextMonth(), taxPercent); await _subscriptionDataService.SubscribeUserAsync(user, planId, (int?)null, taxPercent, stripeSubscription.Id); }