示例#1
0
        public async Task CancelSubscriptionAtPeriodEnd(Domain.Scheduling.Billing.Customer customer)
        {
            if (customer.Subscription == null)
            {
                throw new InvalidOperationException("No subscription to cancel.");
            }

            var opts = new SubscriptionUpdateOptions {
                CancelAtPeriodEnd = true
            };

            await subscriptionService.UpdateAsync(customer.Subscription.BillingReference.BillingId, opts);

            customer.Subscription.CancellingAtPeriodEnd = true;
        }
示例#2
0
        public async Task UndoCancellingSubscription(Domain.Scheduling.Billing.Customer customer)
        {
            if (customer.Subscription == null)
            {
                throw new InvalidOperationException("No subscription to cancel.");
            }

            if (!customer.Subscription.CancellingAtPeriodEnd)
            {
                throw new InvalidOperationException("Cannot undo what has not been done");
            }

            var opts = new SubscriptionUpdateOptions {
                CancelAtPeriodEnd = false
            };

            await subscriptionService.UpdateAsync(customer.Subscription.BillingReference.BillingId, opts);

            customer.Subscription.CancellingAtPeriodEnd = false;
        }
示例#3
0
        public async Task <Domain.Scheduling.Billing.Customer> GetByBillingId(string billingId)
        {
            var stripeCustomer = await customerService.GetAsync(billingId);

            var customer = new Domain.Scheduling.Billing.Customer(
                Guid.Parse(stripeCustomer.Metadata["Id"]),
                Guid.Parse(stripeCustomer.Metadata["UserId"]),
                BillingReference.Customer(stripeCustomer.Id)
                );

            if (stripeCustomer.Subscriptions.Data.Count > 0)
            {
                var stripeSubscription = stripeCustomer.Subscriptions.Data[0];

                customer.Subscription = new Domain.Scheduling.Billing.Subscription(
                    Guid.Parse(stripeSubscription.Metadata["Id"]),
                    Domain.Scheduling.Billing.Subscription.ParseStatus(stripeSubscription.Status),
                    new Domain.Scheduling.Billing.Period(
                        stripeSubscription.TrialStart ?? throw new NullReferenceException(),
                        stripeSubscription.TrialEnd ?? throw new NullReferenceException()
                        ),
                    new Domain.Scheduling.Billing.Period(
                        stripeSubscription.CurrentPeriodStart,
                        stripeSubscription.CurrentPeriodEnd
                        ),
                    stripeSubscription.CancelAtPeriodEnd,
                    new SubscriptionPlanReference(
                        Guid.Parse(stripeSubscription.Metadata["PlanId"]),
                        stripeSubscription.Metadata["PriceBillingId"]
                        ),
                    BillingReference.Subscription(stripeSubscription.Id)
                    );
            }

            /*
             * Payment sources on customer from CustomerService.GetAsync() will always be empty.
             * Need to get them manually instead.
             */
            var paymentMethods = await paymentMethodService.ListAsync(new Stripe.PaymentMethodListOptions()
            {
                Customer = stripeCustomer.Id,
                Type     = "card"
            });

            for (int i = 0; i < paymentMethods.Data.Count; i++)
            {
                var paymentMethod = paymentMethods.Data[i];

                // Add our id to the card if this is the first time we've seen it.
                if (!paymentMethod.Metadata.ContainsKey("Id"))
                {
                    paymentMethod = await paymentMethodService.UpdateAsync(paymentMethod.Id, new PaymentMethodUpdateOptions()
                    {
                        Metadata = new Dictionary <string, string>(new[] {
                            KeyValuePair.Create("Id", Guid.NewGuid().ToString())
                        })
                    });
                }

                var card = paymentMethod.Card;

                customer.PaymentMethods.Add(new Domain.Scheduling.Billing.PaymentMethod(
                                                Guid.Parse(paymentMethod.Metadata["Id"]),
                                                card.Brand,
                                                card.Last4,
                                                // Stripe won't set default unless there is more than 1 payment method on a customer
                                                paymentMethods.Data.Count == 1 ? true : stripeCustomer.InvoiceSettings.DefaultPaymentMethodId == paymentMethod.Id,
                                                new ExpirationDate(card.ExpMonth.ToString(), card.ExpYear.ToString()),
                                                BillingReference.PaymentMethod(paymentMethod.Id)
                                                ));
            }

            return(customer);
        }
示例#4
0
 public async Task Delete(Domain.Scheduling.Billing.Customer customer)
 {
     await customerService.DeleteAsync(customer.BillingReference.BillingId);
 }