Beispiel #1
0
 public BillingUpcomingInvoice(Braintree.Subscription sub)
 {
     Amount = sub.NextBillAmount.GetValueOrDefault() + sub.Balance.GetValueOrDefault();
     if (Amount < 0)
     {
         Amount = 0;
     }
     Date = sub.NextBillingDate;
 }
 /// <summary>
 /// Save updated data and status of a subscription from a notification
 /// of the gateway.
 /// </summary>
 /// <param name="subscription"></param>
 public void UpdatedAtGateway(Braintree.Subscription subscription, Braintree.WebhookKind notification)
 {
     if (subscriptionID != subscription.Id)
     {
         throw new Exception(String.Format("Subscription IDs don't match, record '{0}', input '{1}'", subscriptionID, subscription.Id));
     }
     // Update record with information from the gateway
     paymentPlanLastChangedDate = subscription.UpdatedAt.Value;
     nextPaymentDueDate         = subscription.NextBillingDate;
     nextPaymentAmount          = subscription.NextBillAmount;
     daysPastDue = subscription.DaysPastDue ?? 0;
     // New status
     planStatus = subscription.Status.ToString();
     // Detect when a subscription ended
     if (subscription.Status == Braintree.SubscriptionStatus.CANCELED ||
         subscription.Status == Braintree.SubscriptionStatus.EXPIRED)
     {
         subscriptionEndDate = subscription.BillingPeriodEndDate ?? DateTimeOffset.Now;
     }
 }
Beispiel #3
0
            public BillingSubscription(Braintree.Subscription sub, Braintree.Plan plan)
            {
                Status = sub.Status.ToString();

                if (sub.HasTrialPeriod.GetValueOrDefault() && sub.CreatedAt.HasValue && sub.TrialDuration.HasValue)
                {
                    TrialStartDate = sub.CreatedAt.Value;
                    if (sub.TrialDurationUnit == Braintree.SubscriptionDurationUnit.DAY)
                    {
                        TrialEndDate = TrialStartDate.Value.AddDays(sub.TrialDuration.Value);
                    }
                    else
                    {
                        TrialEndDate = TrialStartDate.Value.AddMonths(sub.TrialDuration.Value);
                    }
                }

                PeriodStartDate = sub.BillingPeriodStartDate;
                PeriodEndDate   = sub.BillingPeriodEndDate;

                CancelAtEndDate = !sub.NeverExpires.GetValueOrDefault();
                Cancelled       = sub.Status == Braintree.SubscriptionStatus.CANCELED;
                if (Cancelled)
                {
                    CancelledDate = sub.UpdatedAt.Value;
                }

                var items = new List <BillingSubscriptionItem>();

                items.Add(new BillingSubscriptionItem(plan));
                if (sub.AddOns != null)
                {
                    items.AddRange(sub.AddOns.Select(a => new BillingSubscriptionItem(plan, a)));
                }

                if (items.Count > 0)
                {
                    Items = items;
                }
            }
        /// <summary>
        /// Save updated data and status of a subscription from a change at the gateway.
        /// A comparision between saved status and new one will detect the kind of notification
        /// </summary>
        /// <param name="subscription"></param>
        public void UpdatedAtGateway(Braintree.Subscription subscription)
        {
            // Detect kind of change/notification
            // Status livecycle at official docs: https://developers.braintreepayments.com/guides/recurring-billing/overview#subscription-statuses
            // Cannot detect Braintree.WebhookKind.SUBSCRIPTION_TRIAL_ENDED here but is
            // not important really, because the status of the subscrition is Active already in trial period;
            // the important change (to update some data) is when the paymend was done (that happens at same
            // time as the trial_ended for the first one, and one update for the same data and status is enought).
            var kind = Braintree.WebhookKind.UNRECOGNIZED;

            if (planStatus != subscription.Status.ToString())
            {
                // Status change, detect which one
                // - Can change from Pending, Active or PastDue to Canceled
                if (subscription.Status == Braintree.SubscriptionStatus.CANCELED)
                {
                    kind = Braintree.WebhookKind.SUBSCRIPTION_CANCELED;
                }
                // - Can change from Active to Canceled
                else if (subscription.Status == Braintree.SubscriptionStatus.EXPIRED)
                {
                    kind = Braintree.WebhookKind.SUBSCRIPTION_EXPIRED;
                }
                // - Change change from Pending, PastDue
                else if (subscription.Status == Braintree.SubscriptionStatus.ACTIVE)
                {
                    kind = Braintree.WebhookKind.SUBSCRIPTION_WENT_ACTIVE;
                }
                // - Change change from Pending, Active
                else if (subscription.Status == Braintree.SubscriptionStatus.PAST_DUE)
                {
                    kind = Braintree.WebhookKind.SUBSCRIPTION_WENT_PAST_DUE;
                }
                // - Impossible to have a change from 'other' status to Pending, is not allowed
                //   in the Braintree subscription livecycle
            }

            UpdatedAtGateway(subscription, kind);
        }