Example #1
0
        public async Task <Domain.Scheduling.Billing.Customer> CreateTrialCustomer(User user, SubscriptionPlan trialPlan)
        {
            var customerId = Guid.NewGuid();

            var custCreateOpts = new Stripe.CustomerCreateOptions {
                Email    = user.Email,
                Metadata = new Dictionary <string, string>(new[] {
                    KeyValuePair.Create("Id", customerId.ToString()),
                    KeyValuePair.Create("UserId", user.Id.ToString()),
                })
            };

            var customer = await customerService.CreateAsync(custCreateOpts);

            var price = trialPlan.Prices.Find(p => p.BillingReference.BillingId == config.DefaultPrice) !;
            var subId = Guid.NewGuid();

            var subCreateOpts = new Stripe.SubscriptionCreateOptions {
                Customer = customer.Id,
                Items    = new List <Stripe.SubscriptionItemOptions> {
                    new Stripe.SubscriptionItemOptions {
                        Price = price.BillingReference.BillingId
                    }
                },
                TrialPeriodDays = config.TrialPeriod,
                Metadata        = new Dictionary <string, string>(new[] {
                    KeyValuePair.Create("Id", subId.ToString()),
                    KeyValuePair.Create("PlanId", trialPlan.Id.ToString()),
                    KeyValuePair.Create("PriceBillingId", price.BillingReference.BillingId)
                })
            };

            var subscription = await subscriptionService.CreateAsync(subCreateOpts);

            return(new Domain.Scheduling.Billing.Customer(
                       customerId,
                       user.Id,
                       BillingReference.Customer(customer.Id),
                       new Domain.Scheduling.Billing.Subscription(
                           subId,
                           Domain.Scheduling.Billing.Subscription.ParseStatus(subscription.Status),
                           new Domain.Scheduling.Billing.Period(
                               subscription.TrialStart ?? throw new NullReferenceException(),
                               subscription.TrialEnd ?? throw new NullReferenceException()
                               ),
                           new Domain.Scheduling.Billing.Period(
                               subscription.CurrentPeriodStart,
                               subscription.CurrentPeriodEnd
                               ),
                           subscription.CancelAtPeriodEnd,
                           new SubscriptionPlanReference(
                               trialPlan.Id,
                               price.BillingReference.BillingId
                               ),
                           BillingReference.Subscription(subscription.Id)
                           )
                       ));
        }
Example #2
0
 public virtual Task <Subscription> CreateAsync(SubscriptionCreateOptions options, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(this.CreateEntityAsync(options, requestOptions, cancellationToken));
 }
Example #3
0
 public virtual Subscription Create(SubscriptionCreateOptions options, RequestOptions requestOptions = null)
 {
     return(this.CreateEntity(options, requestOptions));
 }