public int CreateRecurringGift(string authorizedUserToken, RecurringGiftDto recurringGiftDto, MpContactDonor mpContactDonor) { StripeCustomer customer = null; StripePlan plan = null; StripeSubscription stripeSubscription = null; var donorAccountId = -1; var recurGiftId = -1; try { customer = _paymentService.CreateCustomer(recurringGiftDto.StripeTokenId, string.Format("{0}, Recurring Gift Subscription", mpContactDonor.DonorId)); var source = customer.sources.data.Find(s => s.id == customer.default_source); donorAccountId = _mpDonorService.CreateDonorAccount(source.brand, DonorRoutingNumberDefault, string.IsNullOrWhiteSpace(source.bank_last4) ? source.last4 : source.bank_last4, null, mpContactDonor.DonorId, source.id, customer.id); plan = _paymentService.CreatePlan(recurringGiftDto, mpContactDonor); stripeSubscription = _paymentService.CreateSubscription(plan.Id, customer.id, recurringGiftDto.StartDate); var contact = _mpContactService.GetContactById(mpContactDonor.ContactId); var congregation = contact.Congregation_ID ?? _notSiteSpecificCongregation; recurGiftId = _mpDonorService.CreateRecurringGiftRecord(authorizedUserToken, mpContactDonor.DonorId, donorAccountId, EnumMemberSerializationUtils.ToEnumString(recurringGiftDto.PlanInterval), recurringGiftDto.PlanAmount, recurringGiftDto.StartDate, recurringGiftDto.Program, stripeSubscription.Id, congregation, recurringGiftDto.SourceUrl, recurringGiftDto.PredefinedAmount); SendRecurringGiftConfirmationEmail(authorizedUserToken, _recurringGiftSetupEmailTemplateId, null, recurGiftId); return(recurGiftId); } catch (Exception e) { // "Rollback" any updates _logger.Warn(string.Format("Error setting up recurring gift for donor {0}", mpContactDonor.DonorId), e); if (stripeSubscription != null) { _logger.Debug(string.Format("Deleting Stripe Subscription {0} for donor {1}", stripeSubscription.Id, mpContactDonor.DonorId)); try { _paymentService.CancelSubscription(customer.id, stripeSubscription.Id); } catch (Exception ex) { _logger.Warn(string.Format("Error deleting Stripe Subscription {0} for donor {1}", stripeSubscription.Id, mpContactDonor.DonorId), ex); } } if (plan != null) { _logger.Debug(string.Format("Deleting Stripe Plan {0} for donor {1}", plan.Id, mpContactDonor.DonorId)); try { _paymentService.CancelPlan(plan.Id); } catch (Exception ex) { _logger.Warn(string.Format("Error deleting Stripe Plan {0} for donor {1}", plan.Id, mpContactDonor.DonorId), ex); } } if (customer != null) { _logger.Debug(string.Format("Deleting Stripe Customer {0} for donor {1}", customer.id, mpContactDonor.DonorId)); try { _paymentService.DeleteCustomer(customer.id); } catch (Exception ex) { _logger.Warn(string.Format("Error deleting Stripe Customer {0} for donor {1}", customer.id, mpContactDonor.DonorId), ex); } } if (donorAccountId != -1) { _logger.Debug(string.Format("Deleting Donor Account {0} for donor {1}", donorAccountId, mpContactDonor.DonorId)); try { _mpDonorService.DeleteDonorAccount(authorizedUserToken, donorAccountId); } catch (Exception ex) { _logger.Warn(string.Format("Error deleting Donor Account {0} for donor {1}", donorAccountId, mpContactDonor.DonorId), ex); } } if (recurGiftId != -1) { _logger.Debug(string.Format("Deleting Recurring Gift {0} for donor {1}", recurGiftId, mpContactDonor.DonorId)); try { _mpDonorService.CancelRecurringGift(authorizedUserToken, recurGiftId); } catch (Exception ex) { _logger.Warn(string.Format("Error deleting Recurring Gift {0} for donor {1}", recurGiftId, mpContactDonor.DonorId), ex); } } throw; } }