/// <summary> /// Charges the specified payment info. /// </summary> /// <param name="financialGateway"></param> /// <param name="paymentInfo">The payment info.</param> /// <param name="errorMessage">The error message.</param> /// <returns></returns> public override FinancialTransaction Charge(FinancialGateway financialGateway, PaymentInfo paymentInfo, out string errorMessage) { var financialTransaction = base.Charge(financialGateway, paymentInfo, out errorMessage); // Handle issues with the "Original transaction ID not found" error more gracefully if (paymentInfo is ReferencePaymentInfo && errorMessage == "[19] Original transaction ID not found") { // First delete the saved account--it is worthless var rockContext = new RockContext(); var savedAccountService = new FinancialPersonSavedAccountService(rockContext); var savedAccount = savedAccountService.Queryable() .Where(s => s.TransactionCode == (( ReferencePaymentInfo )paymentInfo).TransactionCode && s.FinancialGatewayId.HasValue && s.FinancialGatewayId.Value == financialGateway.Id) .FirstOrDefault(); if (savedAccount != null) { savedAccountService.Delete(savedAccount); rockContext.SaveChanges(); errorMessage = "The previously saved payment method is no longer valid. Please select a different method."; } } return(financialTransaction); }
/// <summary> /// Event when the user clicks to delete the saved accounts /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void rptSavedAccounts_Delete(object sender, CommandEventArgs e) { var bankAccountGuid = e.CommandArgument.ToStringSafe().AsGuid(); var rockContext = new RockContext(); var financialPersonSavedAccountService = new FinancialPersonSavedAccountService(rockContext); var financialPersonSavedAccount = financialPersonSavedAccountService.Get(bankAccountGuid); if (financialPersonSavedAccount != null) { string errorMessage; if (!financialPersonSavedAccountService.CanDelete(financialPersonSavedAccount, out errorMessage)) { mdWarningAlert.Show(errorMessage, ModalAlertType.Information); return; } financialPersonSavedAccountService.Delete(financialPersonSavedAccount); rockContext.SaveChanges(); } ShowDetail(); }
/// <summary> /// Handles the Delete event of the gSavedAccounts control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RowEventArgs"/> instance containing the event data.</param> protected void gSavedAccounts_Delete(object sender, RowEventArgs e) { var rockContext = new RockContext(); var service = new FinancialPersonSavedAccountService(rockContext); var savedAccount = service.Get(e.RowKeyId); string errorMessage; if (savedAccount == null) { return; } if (!service.CanDelete(savedAccount, out errorMessage)) { mdGridWarning.Show(errorMessage, ModalAlertType.Information); return; } service.Delete(savedAccount); rockContext.SaveChanges(); BindGrid(); }
/// <summary> /// Removes any expired saved accounts (if <see cref="AttributeKey.RemovedExpiredSavedAccountDays"/> is set) /// </summary> /// <param name="context">The context.</param> private RemoveExpiredSavedAccountsResult RemoveExpiredSavedAccounts(IJobExecutionContext context) { var dataMap = context.JobDetail.JobDataMap; int?removedExpiredSavedAccountDays = dataMap.GetString(AttributeKey.RemovedExpiredSavedAccountDays).AsIntegerOrNull(); if (!removedExpiredSavedAccountDays.HasValue) { return(new RemoveExpiredSavedAccountsResult()); } var financialPersonSavedAccountQry = new FinancialPersonSavedAccountService(new RockContext()).Queryable() .Where(a => a.FinancialPaymentDetail.ExpirationMonthEncrypted != null) .Where(a => a.PersonAliasId.HasValue || a.GroupId.HasValue) .Where(a => a.FinancialPaymentDetailId.HasValue) .Where(a => a.IsSystem == false) .OrderBy(a => a.Id); var savedAccountInfoList = financialPersonSavedAccountQry.Select(a => new SavedAccountInfo { Id = a.Id, FinancialPaymentDetail = a.FinancialPaymentDetail }).ToList(); DateTime now = DateTime.Now; int currentMonth = now.Month; int currentYear = now.Year; var result = new RemoveExpiredSavedAccountsResult() { // if today is 3/16/2020 and removedExpiredSavedAccountDays is 90, only delete card if it expired before 12/17/2019 DeleteIfExpiredBeforeDate = RockDateTime.Today.AddDays(-removedExpiredSavedAccountDays.Value) }; foreach (var savedAccountInfo in savedAccountInfoList) { int?expirationMonth = savedAccountInfo.FinancialPaymentDetail.ExpirationMonth; int?expirationYear = savedAccountInfo.FinancialPaymentDetail.ExpirationYear; if (!expirationMonth.HasValue || !expirationMonth.HasValue) { continue; } if (expirationMonth.Value < 1 || expirationMonth.Value > 12 || expirationYear <= DateTime.MinValue.Year || expirationYear >= DateTime.MaxValue.Year) { // invalid month (or year) continue; } // a credit card with an expiration of April 2020 would be expired on May 1st, 2020 var cardExpirationDate = new DateTime(expirationYear.Value, expirationMonth.Value, 1).AddMonths(1); /* Example: * Today's Date: 2020-3-16 * removedExpiredSavedAccountDays: 90 * Expired Before Date: 2019-12-17 (Today (2020-3-16) - removedExpiredSavedAccountDays) * Cards that expired before 2019-12-17 should be deleted * Delete 04/20 (Expires 05/01/2020) card? No * Delete 05/20 (Expires 06/01/2020) card? No * Delete 01/20 (Expires 03/01/2020) card? No * Delete 12/19 (Expires 01/01/2020) card? No * Delete 11/19 (Expires 12/01/2019) card? Yes * Delete 10/19 (Expires 11/01/2019) card? Yes * * Today's Date: 2020-3-16 * removedExpiredSavedAccountDays: 0 * Expired Before Date: 2019-03-16 (Today (2020-3-16) - 0) * Cards that expired before 2019-03-16 should be deleted * Delete 04/20 (Expires 05/01/2020) card? No * Delete 05/20 (Expires 06/01/2020) card? No * Delete 01/20 (Expires 03/01/2020) card? Yes * Delete 12/19 (Expires 01/01/2020) card? Yes * Delete 11/19 (Expires 12/01/2019) card? Yes * Delete 10/19 (Expires 11/01/2019) card? Yes */ if (cardExpirationDate >= result.DeleteIfExpiredBeforeDate) { // We want to only delete cards that expired more than X days ago, so if this card expiration day is after that, skip continue; } // Card expiration date is older than X day ago, so delete it. // Wrapping the following in a try/catch so a single deletion failure doesn't end the process for all deletion candidates. try { using (var savedAccountRockContext = new RockContext()) { var financialPersonSavedAccountService = new FinancialPersonSavedAccountService(savedAccountRockContext); var financialPersonSavedAccount = financialPersonSavedAccountService.Get(savedAccountInfo.Id); if (financialPersonSavedAccount != null) { if (financialPersonSavedAccountService.CanDelete(financialPersonSavedAccount, out _)) { financialPersonSavedAccountService.Delete(financialPersonSavedAccount); savedAccountRockContext.SaveChanges(); result.AccountsDeletedCount++; } } } } catch (Exception ex) { // Provide better identifying context in case the caught exception is too vague. var exception = new Exception($"Unable to delete FinancialPersonSavedAccount (ID = {savedAccountInfo.Id}).", ex); result.AccountRemovalExceptions.Add(exception); } } return(result); }