/// <summary>
/// Processes the confirmation.
/// </summary>
/// <param name="errorMessage">The error message.</param>
/// <returns></returns>
private bool ProcessConfirmation( out string errorMessage )
{
var rockContext = new RockContext();
errorMessage = string.Empty;
if ( string.IsNullOrWhiteSpace( TransactionCode ) )
{
if ( Gateway == null )
{
errorMessage = "There was a problem creating the payment gateway information";
return false;
}
var personService = new PersonService( rockContext );
var transactionService = new FinancialScheduledTransactionService( rockContext );
var transactionDetailService = new FinancialScheduledTransactionDetailService( rockContext );
FinancialScheduledTransaction scheduledTransaction = null;
if ( ScheduledTransactionId.HasValue )
{
scheduledTransaction = transactionService.Get( ScheduledTransactionId.Value );
}
if ( scheduledTransaction == null )
{
errorMessage = "There was a problem getting the transaction information";
return false;
}
if ( scheduledTransaction.AuthorizedPerson == null )
{
errorMessage = "There was a problem determining the person associated with the transaction";
return false;
}
var changeSummary = new StringBuilder();
// Get the payment schedule
scheduledTransaction.TransactionFrequencyValueId = btnFrequency.SelectedValueAsId().Value;
changeSummary.Append( DefinedValueCache.Read( scheduledTransaction.TransactionFrequencyValueId, rockContext ) );
if ( dtpStartDate.SelectedDate.HasValue && dtpStartDate.SelectedDate > RockDateTime.Today )
{
scheduledTransaction.StartDate = dtpStartDate.SelectedDate.Value;
changeSummary.AppendFormat( " starting {0}", scheduledTransaction.StartDate.ToShortDateString() );
}
else
{
scheduledTransaction.StartDate = DateTime.MinValue;
}
changeSummary.AppendLine();
PaymentInfo paymentInfo = GetPaymentInfo( personService, scheduledTransaction );
if ( paymentInfo == null )
{
errorMessage = "There was a problem creating the payment information";
return false;
}
else
{
}
// If transaction is not active, attempt to re-activate it first
if ( !scheduledTransaction.IsActive )
{
if ( !transactionService.Reactivate( scheduledTransaction, out errorMessage ) )
{
return false;
}
}
if ( Gateway.UpdateScheduledPayment( scheduledTransaction, paymentInfo, out errorMessage ) )
{
if ( paymentInfo.CurrencyTypeValue != null )
{
changeSummary.Append( paymentInfo.CurrencyTypeValue.Value );
scheduledTransaction.CurrencyTypeValueId = paymentInfo.CurrencyTypeValue.Id;
DefinedValueCache creditCardTypeValue = paymentInfo.CreditCardTypeValue;
if ( creditCardTypeValue != null )
{
changeSummary.AppendFormat( " - {0}", creditCardTypeValue.Value );
scheduledTransaction.CreditCardTypeValueId = creditCardTypeValue.Id;
}
else
{
scheduledTransaction.CreditCardTypeValueId = null;
}
changeSummary.AppendFormat( " {0}", paymentInfo.MaskedNumber );
changeSummary.AppendLine();
}
var selectedAccountIds = SelectedAccounts
.Where( a => a.Amount > 0 )
.Select( a => a.Id ).ToList();
var deletedAccounts = scheduledTransaction.ScheduledTransactionDetails
.Where( a => !selectedAccountIds.Contains( a.AccountId ) ).ToList();
foreach ( var deletedAccount in deletedAccounts )
{
scheduledTransaction.ScheduledTransactionDetails.Remove( deletedAccount );
transactionDetailService.Delete( deletedAccount );
}
foreach ( var account in SelectedAccounts )
{
var detail = scheduledTransaction.ScheduledTransactionDetails
.Where( d => d.AccountId == account.Id ).FirstOrDefault();
if ( detail == null )
{
detail = new FinancialScheduledTransactionDetail();
detail.AccountId = account.Id;
scheduledTransaction.ScheduledTransactionDetails.Add( detail );
}
detail.Amount = account.Amount;
changeSummary.AppendFormat( "{0}: {1:C2}", account.Name, account.Amount );
changeSummary.AppendLine();
}
rockContext.SaveChanges();
// Add a note about the change
var noteTypeService = new NoteTypeService( rockContext );
var noteType = noteTypeService.Get( scheduledTransaction.TypeId, "Note" );
var noteService = new NoteService( rockContext );
var note = new Note();
note.NoteTypeId = noteType.Id;
note.EntityId = scheduledTransaction.Id;
note.Caption = "Updated Transaction";
note.Text = changeSummary.ToString();
noteService.Add( note );
rockContext.SaveChanges();
ScheduleId = scheduledTransaction.GatewayScheduleId;
TransactionCode = scheduledTransaction.TransactionCode;
if (transactionService.GetStatus( scheduledTransaction, out errorMessage ))
{
rockContext.SaveChanges();
}
}
else
{
return false;
}
tdTransactionCode.Description = TransactionCode;
tdTransactionCode.Visible = !string.IsNullOrWhiteSpace( TransactionCode );
tdScheduleId.Description = ScheduleId;
tdScheduleId.Visible = !string.IsNullOrWhiteSpace( ScheduleId );
return true;
}
else
{
pnlDupWarning.Visible = true;
return false;
}
}