public void Fix() { var appender = FixSubOutput.Logger.Repository.GetAppenders().First(x => x.Name.Equals("FixSubOutputLog")); Logger.Info(string.Format("Starting fix subscription process - CSV results will be written to {0}", ((FileAppender)appender).File)); var recurringGifts = _mpContext.RecurringGifts.ToList().Where(gift => !gift.End_Date.HasValue).ToList(); var giftsToProcess = recurringGifts.Count(); Logger.Info(string.Format("Fixing {0} recurring gifts", giftsToProcess)); FixSubOutput.Info("mpRecurringGiftId,stripeSubscriptionId,mpAmount,stripeAmount,mpFrequency,stripeFrequency,mpRepeat,stripeRepeat,success,errorMessage"); var giftsProcessed = 0; foreach (var gift in recurringGifts) { var percentComplete = (int)Math.Round((double)(++giftsProcessed * 100.0) / giftsToProcess); Logger.Info(string.Format("Processing gift #{0} ({1}% complete)", giftsProcessed, percentComplete)); var success = true; var errorMessage = string.Empty; StripeSubscription sub = null; var mpRecurringGiftId = gift.Recurring_Gift_ID; var mpAmount = gift.Amount; var mpFrequency = gift.Frequency.ToString(); var mpRepeat = gift.Frequency == Frequency.Monthly ? gift.Day_Of_Month + "" : gift.DayOfWeek.ToString(); if (gift.DonorAccount == null || string.IsNullOrWhiteSpace(gift.DonorAccount.Processor_ID) || string.IsNullOrWhiteSpace(gift.Subscription_ID)) { success = false; errorMessage = "Recurring gift is missing Stripe processor information"; } else { try { sub = _paymentService.GetSubscription(gift.DonorAccount.Processor_ID, gift.Subscription_ID); if (sub == null || string.IsNullOrWhiteSpace(sub.Id)) { success = false; errorMessage = string.Format("Stripe problem: Nothing returned for subscription {0} for customer {1}", gift.Subscription_ID, gift.DonorAccount.Processor_ID); } } catch (Exception e) { success = false; errorMessage = string.Format("Stripe exception: Could not lookup subscription for customer {0} subscription {1}: {2}", gift.DonorAccount.Processor_ID, gift.Subscription_ID, e.Message); Logger.Error(errorMessage, e); } } DateTime?trialEndDate = null; if (success) { trialEndDate = CalculateTrialEndDate(gift); if (!trialEndDate.HasValue) { success = false; errorMessage = "Trial end date cannot be set this month; run again next month"; } } StripeSubscription newSub = null; if (success) { try { newSub = _paymentService.UpdateSubscriptionTrialEnd(gift.DonorAccount.Processor_ID, gift.Subscription_ID, trialEndDate); } catch (Exception e) { success = false; errorMessage = string.Format("Stripe exception: Could not update subscription for customer {0} subscription {1}: {2}", gift.DonorAccount.Processor_ID, gift.Subscription_ID, e.Message); Logger.Error(errorMessage, e); } } var stripeSubscriptionId = string.IsNullOrWhiteSpace(gift.Subscription_ID) ? Null : gift.Subscription_ID; var stripeAmount = newSub == null || newSub.Plan == null ? 0.00M : (decimal)(newSub.Plan.Amount / 100.00M); var freq = newSub == null || newSub.Plan == null ? null : newSub.Plan.Interval.ToLower().Equals("month") ? Frequency.Monthly : newSub.Plan.Interval.ToLower().Equals("week") ? Frequency.Weekly : (Frequency?)null; var stripeFrequency = freq == null ? Null : freq.ToString(); var startDate = newSub == null || string.IsNullOrWhiteSpace(newSub.CurrentPeriodEnd) ? (DateTime?)null : StripeEpochTime.ConvertEpochToDateTime(long.Parse(newSub.CurrentPeriodEnd)); var stripeRepeat = freq != null && freq == Frequency.Monthly && startDate != null ? startDate.Value.Day + "" : freq != null && freq == Frequency.Weekly && startDate != null?startDate.Value.DayOfWeek.ToString() : Null; LogRecurringGiftUpdate(mpRecurringGiftId, stripeSubscriptionId, mpAmount, stripeAmount, mpFrequency, stripeFrequency, mpRepeat, stripeRepeat, success, errorMessage); } }
public void Verify() { var appender = VerifyOutput.Logger.Repository.GetAppenders().First(x => x.Name.Equals("VerifyOutputLog")); Logger.Info(string.Format("Starting verification process - CSV results will be written to {0}", ((FileAppender)appender).File)); var markerDateTime = new DateTime(2001, 01, 01); var migratedDateTime = new DateTime(2016, 01, 29); var recurringGifts = _mpContext.RecurringGifts.ToList().Where(gift => !gift.End_Date.HasValue || gift.End_Date.Value.Date.Equals(markerDateTime.Date)).ToList(); var giftsToProcess = recurringGifts.Count(); Logger.Info(string.Format("Verifying {0} recurring gifts", giftsToProcess)); VerifyOutput.Info("mpRecurringGiftId,stripeSubscriptionId,mpAmount,stripeAmount,mpFrequency,stripeFrequency,mpRepeat,stripeRepeat,mpDonorAccountType,stripeAccountType,mpAccountLast4,stripeAccountLast4,success,errorMessage"); var giftsProcessed = 0; foreach (var gift in recurringGifts) { var percentComplete = (int)Math.Round((double)(++giftsProcessed * 100.0) / giftsToProcess); Logger.Info(string.Format("Processing gift #{0} ({1}% complete)", giftsProcessed, percentComplete)); var success = true; var errorMessage = string.Empty; StripeSubscription sub = null; var mpRecurringGiftId = gift.Recurring_Gift_ID; var mpAmount = gift.Amount; var mpFrequency = gift.Frequency.ToString(); var mpRepeat = gift.Frequency == Frequency.Monthly ? gift.Day_Of_Month + "" : gift.DayOfWeek.ToString(); var mpDonorAccountType = gift.DonorAccount != null?gift.DonorAccount.AccountType.ToString() : Null; var mpAccountLast4 = gift.DonorAccount != null ? gift.DonorAccount.Account_Number : Null; if (gift.DonorAccount == null || string.IsNullOrWhiteSpace(gift.DonorAccount.Processor_ID) || string.IsNullOrWhiteSpace(gift.Subscription_ID)) { success = false; errorMessage = "Recurring gift is missing Stripe processor information"; } else { try { sub = _paymentService.GetSubscription(gift.DonorAccount.Processor_ID, gift.Subscription_ID); if (sub == null || string.IsNullOrWhiteSpace(sub.Id)) { success = false; errorMessage = string.Format("Stripe problem: Nothing returned for subscription {0} for customer {1}", gift.Subscription_ID, gift.DonorAccount.Processor_ID); } } catch (Exception e) { success = false; errorMessage = string.Format("Stripe exception: Could not lookup subscription for customer {0} subscription {1}: {2}", gift.DonorAccount.Processor_ID, gift.Subscription_ID, e.Message); Logger.Error(errorMessage, e); } } var stripeSubscriptionId = string.IsNullOrWhiteSpace(gift.Subscription_ID) ? Null : gift.Subscription_ID; var stripeAmount = sub == null || sub.Plan == null ? 0.00M : (decimal)(sub.Plan.Amount / 100.00M); var freq = sub == null || sub.Plan == null ? null : sub.Plan.Interval.ToLower().Equals("month") ? Frequency.Monthly : sub.Plan.Interval.ToLower().Equals("week") ? Frequency.Weekly : (Frequency?)null; var stripeFrequency = freq == null ? Null : freq.ToString(); var startDate = sub == null || string.IsNullOrWhiteSpace(sub.CurrentPeriodEnd) ? (DateTime?)null : StripeEpochTime.ConvertEpochToDateTime(long.Parse(sub.CurrentPeriodEnd)); var stripeRepeat = freq != null && freq == Frequency.Monthly && startDate != null ? startDate.Value.Day + "" : freq != null && freq == Frequency.Weekly && startDate != null?startDate.Value.DayOfWeek.ToString() : Null; var stripeAccountType = Null; var stripeAccountLast4 = Null; if (sub != null && !string.IsNullOrWhiteSpace(sub.Customer)) { try { var source = _paymentService.GetDefaultSource(sub.Customer); if ("card".Equals(source.@object)) { stripeAccountType = AccountType.CreditCard.ToString(); stripeAccountLast4 = source.last4; } else if ("bank_account".Equals(source.@object)) { stripeAccountType = AccountType.Bank.ToString(); stripeAccountLast4 = source.bank_last4; } } catch (Exception e) { success = false; errorMessage = string.Format("Could not lookup default source for customer {0}: {1}", sub.Customer, e.Message); Logger.Error(errorMessage, e); } } if (success) { success = mpAmount == stripeAmount && mpFrequency.Equals(stripeFrequency) && mpRepeat.Equals(stripeRepeat) && mpDonorAccountType.Equals(stripeAccountType) && mpAccountLast4.Equals(stripeAccountLast4); if (!success) { errorMessage = "Field mismatch"; } } LogRecurringGiftComparison(mpRecurringGiftId, stripeSubscriptionId, mpAmount, stripeAmount, mpFrequency, stripeFrequency, mpRepeat, stripeRepeat, mpDonorAccountType, stripeAccountType, mpAccountLast4, stripeAccountLast4, success, errorMessage); } }