Esempio n. 1
0
        public int GetDunningAttemptsCount(string accountId, string sharedClientKey)
        {
            // Ensure the clients are certified.
            if (sharedClientKey != Sahara.Core.Platform.Requests.RequestManager.SharedClientKey)
            {
                return(-1);
            }

            var account = AccountManager.GetAccount(accountId);

            return(PlatformBillingManager.GetAutomaticDunningAttemptCount(accountId, account.StoragePartition));
        }
        public static DataAccessResponseType ProcessFailedStripeRecurringChargeEvent(string stripeCustomerID, string stripeChargeId, string amount, string failureMessage, string stripeEventId)
        {
            /* FROM STRIPE DOCS (why we only focus on the charge failure since we ensure a card is always on file:
             *
             * We'll let you know about this case with an 'invoice.payment_failed' event.
             * The payment could have failed either because your customer did not have a card on file or because the charge was declined;
             * if it was due to a decline, we'll also emit a 'charge.failed event'.
             *
             * You'll also see a 'customer.updated' event to set the 'delinquent' flag,
             * an 'invoice.updated' event to track the payment attempts, and 'customer.subscription.updated'
             * or 'customer.subscription.deleted' depending on your retry settings.
             *
             * */

            var response = new DataAccessResponseType();

            // 1. Get the account
            var account = AccountManager.GetAccount(stripeCustomerID);

            // 2. Update account Delinquent state
            AccountManager.UpdateAccountDelinquentStatus(account.AccountID.ToString(), true);

            // 3. Determine number of previous autmatic failures, and adjust messaging severity accordingly
            int previousFailureCount = PlatformBillingManager.GetAutomaticDunningAttemptCount(account.AccountID.ToString(), account.StoragePartition);

            //Pull count of past failures from table storage rows
            // Details shoul include:
            // TieStamp
            // Charge amount
            // Stripe ID's (charge, event, etc)


            // 4. Get all owners for the account:
            var accountOwnerEmails = AccountManager.GetAccountOwnerEmails(stripeCustomerID);

            if (previousFailureCount == 0)
            {
                //First attempt, light messagng
                //Email all account owners
                //email all account owners a copy of the associated invoice
                EmailManager.Send(
                    accountOwnerEmails,
                    Settings.Endpoints.Emails.FromBilling,
                    Settings.Copy.EmailMessages.InvoicedChargeFailed_FirstDunningAttempt.FromName,
                    String.Format(Settings.Copy.EmailMessages.InvoicedChargeFailed_FirstDunningAttempt.Subject),
                    String.Format(Settings.Copy.EmailMessages.InvoicedChargeFailed_FirstDunningAttempt.Body, account.AccountName, account.AccountNameKey, failureMessage),
                    true);
            }
            else if (previousFailureCount == 1)
            {
                //This was the second attempt, email is more sever and includes platform admins
                //Email all account owners

                //email all account owners a copy of alert email
                EmailManager.Send(
                    accountOwnerEmails,
                    Settings.Endpoints.Emails.FromBilling,
                    Settings.Copy.EmailMessages.InvoicedChargeFailed_SecondDunningAttempt.FromName,
                    String.Format(Settings.Copy.EmailMessages.InvoicedChargeFailed_SecondDunningAttempt.Subject),
                    String.Format(Settings.Copy.EmailMessages.InvoicedChargeFailed_SecondDunningAttempt.Body, account.AccountName, account.AccountNameKey, failureMessage),
                    true);


                //Email Platform Admins
                EmailManager.Send(
                    Settings.Endpoints.Emails.PlatformEmailAddresses,
                    Settings.Endpoints.Emails.FromPlatform,
                    "Second Failed Charge Warning",
                    "An account has been sent a second warning about failed charge",
                    "AccountName: <b>" + account.AccountName + "</b><br/><br/>AccountID: <b>" + account.AccountID + "</b><br/><br/>Failure Message: <b>" + failureMessage + "</b><br/>",
                    true);
            }
            else if (previousFailureCount == 2)
            {
                //This was the third attempt, email is more sever and includes platform admins
                //Email all account owners

                //email all account owners a copy of alert email
                EmailManager.Send(
                    accountOwnerEmails,
                    Settings.Endpoints.Emails.FromBilling,
                    Settings.Copy.EmailMessages.InvoicedChargeFailed_ThirdDunningAttempt.FromName,
                    String.Format(Settings.Copy.EmailMessages.InvoicedChargeFailed_ThirdDunningAttempt.Subject),
                    String.Format(Settings.Copy.EmailMessages.InvoicedChargeFailed_ThirdDunningAttempt.Body, account.AccountName, account.AccountNameKey, failureMessage),
                    true);


                //Email Platform Admins
                EmailManager.Send(
                    Settings.Endpoints.Emails.PlatformEmailAddresses,
                    Settings.Endpoints.Emails.FromPlatform,
                    "Third Failed Charge Warning",
                    "An account has been sent a third warning about failed charge",
                    "AccountName: <b>" + account.AccountName + "</b><br/><br/>AccountID: <b>" + account.AccountID + "</b><br/><br/>Failure Message: <b>" + failureMessage + "</b><br/>",
                    true);

                //Send alert notification with a 7 day expiration to the account owners (or do this on the second attempt?)

                /* NOTIFICATIONS TURNED OFF
                 * NotificationsManager.SendNotificationToAccount(
                 *  NotificationType.Alert,
                 *  account.AccountID.ToString(),
                 *  Settings.Copy.NotificationMessages.ChargeFailure_Automatic.NotificationMessage,
                 *  10080, //<-- 7 Days (Length of time until Stripe makes final attempt)
                 *  true
                 *  );
                 */

                // Send email to Platform Admins
            }
            else if (previousFailureCount == 3)
            {
                //This was the fourth and final attempt, after this Stripe has marked the Subscritption as 'unpaid'. Messaing is a dire warning about account closure
                //Email all account owners

                //email all account owners a copy of alert email
                EmailManager.Send(
                    accountOwnerEmails,
                    Settings.Endpoints.Emails.FromBilling,
                    Settings.Copy.EmailMessages.InvoicedChargeFailed_FinalDunningAttempt.FromName,
                    String.Format(Settings.Copy.EmailMessages.InvoicedChargeFailed_FinalDunningAttempt.Subject),
                    String.Format(Settings.Copy.EmailMessages.InvoicedChargeFailed_FinalDunningAttempt.Body, account.AccountName),
                    true);



                //Email Platform Admins
                EmailManager.Send(
                    Settings.Endpoints.Emails.PlatformEmailAddresses,
                    Settings.Endpoints.Emails.FromPlatform,
                    "FINAL Failed Charge Warning",
                    "An account has been sent a final warning about failed charge",
                    "AccountName: <b>" + account.AccountName + "</b><br/><br/>AccountID: <b>" + account.AccountID + "</b><br/><br/>Failure Message: <b>" + failureMessage + "</b><br/>",
                    true);
            }


            //Store failure (increase failure count by 1)
            PlatformBillingManager.StoreAutomaticDunningAttempt(account.AccountID.ToString(), account.StoragePartition, stripeChargeId, amount, account.StripeSubscriptionID, stripeEventId, failureMessage);


            response.isSuccess = true;

            return(response);
        }