/// <summary> /// Handles the SaveClick event of the mdAccount control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void mdAccount_SaveClick(object sender, EventArgs e) { Guid?guid = hfAccountGuid.Value.AsGuidOrNull(); if (guid.HasValue) { var txnDetail = TransactionDetailsState.Where(t => t.Guid.Equals(guid.Value)).FirstOrDefault(); if (txnDetail == null) { txnDetail = new FinancialScheduledTransactionDetail(); TransactionDetailsState.Add(txnDetail); } txnDetail.AccountId = apAccount.SelectedValue.AsInteger(); txnDetail.Amount = tbAccountAmount.Text.AsDecimal(); txnDetail.Summary = tbAccountSummary.Text; txnDetail.LoadAttributes(); Rock.Attribute.Helper.GetEditValues(phAccountAttributeEdits, txnDetail); foreach (var attributeValue in txnDetail.AttributeValues) { txnDetail.SetAttributeValue(attributeValue.Key, attributeValue.Value.Value); } BindAccounts(); } HideDialog(); }
/// <summary> /// Handles the SaveClick event of the mdAccount control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void mdAccount_SaveClick(object sender, EventArgs e) { Guid?guid = hfAccountGuid.Value.AsGuidOrNull(); if (guid.HasValue) { var financialTransactionDetail = TransactionDetailsState.Where(t => t.Guid.Equals(guid.Value)).FirstOrDefault(); if (financialTransactionDetail == null) { financialTransactionDetail = new FinancialScheduledTransactionDetail(); TransactionDetailsState.Add(financialTransactionDetail); } financialTransactionDetail.AccountId = apAccount.SelectedValue.AsInteger(); var feeCoverageAmount = tbAccountFeeCoverageAmount.Value; financialTransactionDetail.Amount = (tbAccountAmountMinusFeeCoverageAmount.Value ?? 0.0M) + (feeCoverageAmount ?? 0.00M); financialTransactionDetail.FeeCoverageAmount = feeCoverageAmount; financialTransactionDetail.Summary = tbAccountSummary.Text; financialTransactionDetail.LoadAttributes(); Rock.Attribute.Helper.GetEditValues(phAccountAttributeEdits, financialTransactionDetail); foreach (var attributeValue in financialTransactionDetail.AttributeValues) { financialTransactionDetail.SetAttributeValue(attributeValue.Key, attributeValue.Value.Value); } BindAccounts(); } HideDialog(); }
/// <summary> /// Handles the DataBound event of the lAccountName 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 lAccountName_DataBound(object sender, RowEventArgs e) { FinancialScheduledTransactionDetail financialScheduledTransactionDetail = e.Row.DataItem as FinancialScheduledTransactionDetail; Literal lAccountName = sender as Literal; lAccountName.Text = FinancialAccountNameLookup.GetValueOrNull(financialScheduledTransactionDetail.AccountId); }
/// <summary> /// Shows the account dialog. /// </summary> /// <param name="guid">The unique identifier.</param> private void ShowAccountDialog(Guid guid) { hfAccountGuid.Value = guid.ToString(); var txnDetail = TransactionDetailsState.Where(d => d.Guid.Equals(guid)).FirstOrDefault(); if (txnDetail != null) { apAccount.SetValue(txnDetail.AccountId); tbAccountAmount.Text = txnDetail.Amount.ToString("N2"); tbAccountSummary.Text = txnDetail.Summary; if (txnDetail.Attributes == null) { txnDetail.LoadAttributes(); } } else { apAccount.SetValue(null); tbAccountAmount.Text = string.Empty; tbAccountSummary.Text = string.Empty; txnDetail = new FinancialScheduledTransactionDetail(); txnDetail.LoadAttributes(); } phAccountAttributeEdits.Controls.Clear(); Helper.AddEditControls(txnDetail, phAccountAttributeEdits, true, mdAccount.ValidationGroup); ShowDialog("ACCOUNT"); }
protected void lbSaveAccounts_Click(object sender, EventArgs e) { using (var rockContext = new RockContext()) { var txn = GetTransaction(rockContext); { decimal totalAmount = TransactionDetailsState.Select(d => d.Amount).ToList().Sum(); if (txn.TotalAmount != totalAmount) { nbError.Title = "Incorrect Amount"; nbError.Text = string.Format("<p>When updating account allocations, the total amount needs to remain the same as the original amount ({0}).</p>", txn.TotalAmount.FormatAsCurrency()); nbError.Visible = true; return; } var txnDetailService = new FinancialScheduledTransactionDetailService(rockContext); var accountService = new FinancialAccountService(rockContext); // Delete any transaction details that were removed var txnDetailsInDB = txnDetailService.Queryable().Where(a => a.ScheduledTransactionId.Equals(txn.Id)).ToList(); var deletedDetails = from txnDetail in txnDetailsInDB where !TransactionDetailsState.Select(d => d.Guid).Contains(txnDetail.Guid) select txnDetail; bool accountChanges = deletedDetails.Any(); deletedDetails.ToList().ForEach(txnDetail => { txnDetailService.Delete(txnDetail); }); var changeSummary = new StringBuilder(); // Save Transaction Details foreach (var editorTxnDetail in TransactionDetailsState) { editorTxnDetail.Account = accountService.Get(editorTxnDetail.AccountId); // Add or Update the activity type var txnDetail = txn.ScheduledTransactionDetails.FirstOrDefault(d => d.Guid.Equals(editorTxnDetail.Guid)); if (txnDetail == null) { accountChanges = true; txnDetail = new FinancialScheduledTransactionDetail(); txnDetail.Guid = editorTxnDetail.Guid; txn.ScheduledTransactionDetails.Add(txnDetail); } else { if (txnDetail.AccountId != editorTxnDetail.AccountId || txnDetail.Amount != editorTxnDetail.Amount || txnDetail.Summary != editorTxnDetail.Summary) { accountChanges = true; } } changeSummary.AppendFormat("{0}: {1}", editorTxnDetail.Account != null ? editorTxnDetail.Account.Name : "?", editorTxnDetail.Amount.FormatAsCurrency()); changeSummary.AppendLine(); txnDetail.AccountId = editorTxnDetail.AccountId; txnDetail.Amount = editorTxnDetail.Amount; txnDetail.Summary = editorTxnDetail.Summary; } if (accountChanges) { // save changes rockContext.SaveChanges(); // Add a note about the change var noteType = NoteTypeCache.Read(Rock.SystemGuid.NoteType.SCHEDULED_TRANSACTION_NOTE.AsGuid()); if (noteType != null) { var noteService = new NoteService(rockContext); var note = new Note(); note.NoteTypeId = noteType.Id; note.EntityId = txn.Id; note.Caption = "Updated Transaction"; note.Text = changeSummary.ToString(); noteService.Add(note); } rockContext.SaveChanges(); } ShowView(txn); } } }
/// <summary> /// Updates the scheduled payment. /// </summary> /// <param name="usePaymentToken">if set to <c>true</c> [use payment token].</param> /// <param name="paymentToken">The payment token.</param> protected void UpdateScheduledPayment(bool usePaymentToken, string paymentToken = null) { var giftTerm = this.GetAttributeValue(AttributeKey.GiftTerm); if (dtpStartDate.SelectedDate <= RockDateTime.Today) { nbUpdateScheduledPaymentWarning.Visible = true; nbUpdateScheduledPaymentWarning.Text = string.Format("When scheduling a {0}, make sure the starting date is in the future (after today)", giftTerm.ToLower()); return; } var rockContext = new RockContext(); var financialScheduledTransactionService = new FinancialScheduledTransactionService(rockContext); int scheduledTransactionId = hfScheduledTransactionId.Value.AsInteger(); var financialScheduledTransaction = financialScheduledTransactionService.Get(scheduledTransactionId); financialScheduledTransaction.StartDate = dtpStartDate.SelectedDate.Value; financialScheduledTransaction.TransactionFrequencyValueId = ddlFrequency.SelectedValue.AsInteger(); ReferencePaymentInfo referencePaymentInfo; var person = financialScheduledTransaction.AuthorizedPersonAlias.Person; string errorMessage; var financialGateway = this.FinancialGateway; var financialGatewayComponent = this.FinancialGatewayComponent; if (usePaymentToken) { referencePaymentInfo = new ReferencePaymentInfo(); referencePaymentInfo.FirstName = person.FirstName; referencePaymentInfo.LastName = person.LastName; referencePaymentInfo.UpdateAddressFieldsFromAddressControl(acBillingAddress); referencePaymentInfo.ReferenceNumber = paymentToken; var customerToken = financialGatewayComponent.CreateCustomerAccount(this.FinancialGateway, referencePaymentInfo, out errorMessage); if (errorMessage.IsNotNullOrWhiteSpace() || customerToken.IsNullOrWhiteSpace()) { nbMessage.NotificationBoxType = NotificationBoxType.Danger; nbMessage.Text = errorMessage ?? "Unknown Error"; nbMessage.Visible = true; return; } referencePaymentInfo.GatewayPersonIdentifier = customerToken; } else { var savedAccountId = ddlPersonSavedAccount.SelectedValue.AsInteger(); var savedAccount = new FinancialPersonSavedAccountService(rockContext).Get(savedAccountId); if (savedAccount != null) { referencePaymentInfo = savedAccount.GetReferencePayment(); } else { throw new Exception("Unable to determine Saved Account"); } } var selectedAccountAmounts = caapPromptForAccountAmounts.AccountAmounts.Where(a => a.Amount.HasValue && a.Amount.Value != 0).Select(a => new { a.AccountId, Amount = a.Amount.Value }).ToArray(); referencePaymentInfo.Amount = selectedAccountAmounts.Sum(a => a.Amount); var successfullyUpdated = financialGatewayComponent.UpdateScheduledPayment(financialScheduledTransaction, referencePaymentInfo, out errorMessage); if (!successfullyUpdated) { nbMessage.NotificationBoxType = NotificationBoxType.Danger; nbMessage.Text = errorMessage ?? "Unknown Error"; nbMessage.Visible = true; return; } financialScheduledTransaction.FinancialPaymentDetail.SetFromPaymentInfo(referencePaymentInfo, financialGatewayComponent as GatewayComponent, rockContext); var selectedAccountIds = selectedAccountAmounts.Select(a => a.AccountId).ToArray(); var deletedTransactionDetails = financialScheduledTransaction.ScheduledTransactionDetails.ToList().Where(a => selectedAccountIds.Contains(a.AccountId)).ToList(); foreach (var deletedTransactionDetail in deletedTransactionDetails) { financialScheduledTransaction.ScheduledTransactionDetails.Remove(deletedTransactionDetail); } foreach (var selectedAccountAmount in selectedAccountAmounts) { var scheduledTransactionDetail = financialScheduledTransaction.ScheduledTransactionDetails.FirstOrDefault(a => a.AccountId == selectedAccountAmount.AccountId); if (scheduledTransactionDetail == null) { scheduledTransactionDetail = new FinancialScheduledTransactionDetail(); scheduledTransactionDetail.AccountId = selectedAccountAmount.AccountId; financialScheduledTransaction.ScheduledTransactionDetails.Add(scheduledTransactionDetail); } scheduledTransactionDetail.Amount = selectedAccountAmount.Amount; } rockContext.SaveChanges(); var mergeFields = LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson, new CommonMergeFieldsOptions { GetLegacyGlobalMergeFields = false }); var finishLavaTemplate = this.GetAttributeValue(AttributeKey.FinishLavaTemplate); mergeFields.Add("Transaction", financialScheduledTransaction); mergeFields.Add("Person", financialScheduledTransaction.AuthorizedPersonAlias.Person); mergeFields.Add("PaymentDetail", financialScheduledTransaction.FinancialPaymentDetail); mergeFields.Add("BillingLocation", financialScheduledTransaction.FinancialPaymentDetail.BillingLocation); pnlPromptForChanges.Visible = false; pnlTransactionSummary.Visible = true; lTransactionSummaryHTML.Text = finishLavaTemplate.ResolveMergeFields(mergeFields); }
/// <summary> /// Processes the confirmation. /// </summary> /// <param name="errorMessage">The error message.</param> /// <returns></returns> private bool ProcessConfirmation( out string errorMessage ) { errorMessage = string.Empty; if ( string.IsNullOrWhiteSpace( TransactionCode ) ) { if ( Gateway == null ) { errorMessage = "There was a problem creating the payment gateway information"; return false; } using ( new UnitOfWorkScope() ) { var personService = new PersonService(); var transactionService = new FinancialScheduledTransactionService(); var transactionDetailService = new FinancialScheduledTransactionDetailService(); 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; } // Get the payment schedule scheduledTransaction.TransactionFrequencyValueId = btnFrequency.SelectedValueAsId().Value; if ( dtpStartDate.SelectedDate.HasValue && dtpStartDate.SelectedDate > DateTime.Today ) { scheduledTransaction.StartDate = dtpStartDate.SelectedDate.Value; } else { scheduledTransaction.StartDate = DateTime.MinValue; } PaymentInfo paymentInfo = GetPaymentInfo( personService, scheduledTransaction ); if ( paymentInfo == null ) { errorMessage = "There was a problem creating the payment information"; return false; } else { } if ( Gateway.UpdateScheduledPayment( scheduledTransaction, paymentInfo, out errorMessage ) ) { 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, CurrentPersonId ); } 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; } transactionService.Save( scheduledTransaction, CurrentPersonId ); ScheduleId = scheduledTransaction.GatewayScheduleId; TransactionCode = scheduledTransaction.TransactionCode; } 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; } }
/// <summary> /// Processes the confirmation. /// </summary> /// <param name="errorMessage">The error message.</param> /// <returns></returns> private bool ProcessConfirmation( out string errorMessage ) { var rockContext = new RockContext(); if ( string.IsNullOrWhiteSpace( TransactionCode ) ) { GatewayComponent gateway = hfPaymentTab.Value == "ACH" ? _achGateway : _ccGateway; if ( gateway == null ) { errorMessage = "There was a problem creating the payment gateway information"; return false; } Person person = GetPerson( true ); if ( person == null ) { errorMessage = "There was a problem creating the person information"; return false; } if ( !person.PrimaryAliasId.HasValue) { errorMessage = "There was a problem creating the person's primary alias"; return false; } PaymentInfo paymentInfo = GetPaymentInfo(); if ( paymentInfo == null ) { errorMessage = "There was a problem creating the payment information"; return false; } else { paymentInfo.FirstName = person.FirstName; paymentInfo.LastName = person.LastName; } if ( paymentInfo.CreditCardTypeValue != null ) { CreditCardTypeValueId = paymentInfo.CreditCardTypeValue.Id; } PaymentSchedule schedule = GetSchedule(); if ( schedule != null ) { schedule.PersonId = person.Id; var scheduledTransaction = gateway.AddScheduledPayment( schedule, paymentInfo, out errorMessage ); if ( scheduledTransaction != null ) { scheduledTransaction.TransactionFrequencyValueId = schedule.TransactionFrequencyValue.Id; scheduledTransaction.AuthorizedPersonAliasId = person.PrimaryAliasId.Value; scheduledTransaction.GatewayEntityTypeId = EntityTypeCache.Read( gateway.TypeGuid ).Id; scheduledTransaction.CurrencyTypeValueId = paymentInfo.CurrencyTypeValue.Id; scheduledTransaction.CreditCardTypeValueId = CreditCardTypeValueId; var changeSummary = new StringBuilder(); changeSummary.AppendFormat( "{0} starting {1}", schedule.TransactionFrequencyValue.Value, schedule.StartDate.ToShortDateString() ); changeSummary.AppendLine(); changeSummary.Append( paymentInfo.CurrencyTypeValue.Value ); if (paymentInfo.CreditCardTypeValue != null) { changeSummary.AppendFormat( " - {0}", paymentInfo.CreditCardTypeValue.Value ); } changeSummary.AppendFormat( " {0}", paymentInfo.MaskedNumber ); changeSummary.AppendLine(); foreach ( var account in SelectedAccounts.Where( a => a.Amount > 0 ) ) { var transactionDetail = new FinancialScheduledTransactionDetail(); transactionDetail.Amount = account.Amount; transactionDetail.AccountId = account.Id; scheduledTransaction.ScheduledTransactionDetails.Add( transactionDetail ); changeSummary.AppendFormat( "{0}: {1:C2}", account.Name, account.Amount ); changeSummary.AppendLine(); } var transactionService = new FinancialScheduledTransactionService( rockContext ); transactionService.Add( scheduledTransaction ); 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 = "Created Transaction"; note.Text = changeSummary.ToString(); noteService.Add( note ); rockContext.SaveChanges(); ScheduleId = scheduledTransaction.GatewayScheduleId; TransactionCode = scheduledTransaction.TransactionCode; } else { return false; } } else { var transaction = gateway.Charge( paymentInfo, out errorMessage ); if ( transaction != null ) { transaction.TransactionDateTime = RockDateTime.Now; transaction.AuthorizedPersonAliasId = person.PrimaryAliasId; transaction.GatewayEntityTypeId = gateway.TypeId; transaction.TransactionTypeValueId = DefinedValueCache.Read( new Guid( Rock.SystemGuid.DefinedValue.TRANSACTION_TYPE_CONTRIBUTION ) ).Id; transaction.CurrencyTypeValueId = paymentInfo.CurrencyTypeValue.Id; transaction.CreditCardTypeValueId = CreditCardTypeValueId; Guid sourceGuid = Guid.Empty; if ( Guid.TryParse( GetAttributeValue( "Source" ), out sourceGuid ) ) { transaction.SourceTypeValueId = DefinedValueCache.Read( sourceGuid ).Id; } foreach ( var account in SelectedAccounts.Where( a => a.Amount > 0 ) ) { var transactionDetail = new FinancialTransactionDetail(); transactionDetail.Amount = account.Amount; transactionDetail.AccountId = account.Id; transaction.TransactionDetails.Add( transactionDetail ); } var batchService = new FinancialBatchService( rockContext ); // Get the batch var batch = batchService.Get( GetAttributeValue( "BatchNamePrefix" ), paymentInfo.CurrencyTypeValue, paymentInfo.CreditCardTypeValue, transaction.TransactionDateTime.Value, gateway.BatchTimeOffset ); batch.ControlAmount += transaction.TotalAmount; transaction.BatchId = batch.Id; batch.Transactions.Add( transaction ); rockContext.SaveChanges(); TransactionCode = transaction.TransactionCode; } else { return false; } } tdTransactionCodeReceipt.Description = TransactionCode; tdTransactionCodeReceipt.Visible = !string.IsNullOrWhiteSpace( TransactionCode ); tdScheduleId.Description = ScheduleId; tdScheduleId.Visible = !string.IsNullOrWhiteSpace( ScheduleId ); tdNameReceipt.Description = paymentInfo.FullName; tdPhoneReceipt.Description = paymentInfo.Phone; tdEmailReceipt.Description = paymentInfo.Email; tdAddressReceipt.Description = string.Format( "{0} {1}, {2} {3}", paymentInfo.Street1, paymentInfo.City, paymentInfo.State, paymentInfo.PostalCode ); rptAccountListReceipt.DataSource = SelectedAccounts.Where( a => a.Amount != 0 ); rptAccountListReceipt.DataBind(); tdTotalReceipt.Description = paymentInfo.Amount.ToString( "C" ); tdPaymentMethodReceipt.Description = paymentInfo.CurrencyTypeValue.Description; tdAccountNumberReceipt.Description = paymentInfo.MaskedNumber; tdWhenReceipt.Description = schedule != null ? schedule.ToString() : "Today"; // If there was a transaction code returned and this was not already created from a previous saved account, // show the option to save the account. if ( !( paymentInfo is ReferencePaymentInfo ) && !string.IsNullOrWhiteSpace( TransactionCode ) ) { cbSaveAccount.Visible = true; pnlSaveAccount.Visible = true; txtSaveAccount.Visible = true; // If current person does not have a login, have them create a username and password phCreateLogin.Visible = !new UserLoginService( rockContext ).GetByPersonId( person.Id ).Any(); } else { pnlSaveAccount.Visible = false; } return true; } else { pnlDupWarning.Visible = true; divActions.Visible = false; errorMessage = string.Empty; return false; } }
/// <summary> /// Handles the Click event of the btnGive control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void btnGive_Click(object sender, EventArgs e) { Person person = FindPerson(); using (new UnitOfWorkScope()) { RockTransactionScope.WrapTransaction(() => { var groupLocationService = new GroupLocationService(); var groupMemberService = new GroupMemberService(); var phoneService = new PhoneNumberService(); var locationService = new LocationService(); var groupService = new GroupService(); GroupLocation groupLocation; Location homeAddress; Group familyGroup; var homeLocationType = DefinedValueCache.Read(Rock.SystemGuid.DefinedValue.LOCATION_TYPE_HOME); var addressList = locationService.Queryable().Where(l => l.Street1 == txtStreet.Text && l.City == txtCity.Text && l.State == ddlState.SelectedValue && l.Zip == txtZip.Text && l.LocationTypeValueId == homeLocationType.Id).ToList(); if (!addressList.Any()) { homeAddress = new Location(); locationService.Add(homeAddress, person.Id); } else { homeAddress = addressList.FirstOrDefault(); } homeAddress.Street1 = txtStreet.Text ?? homeAddress.Street1; homeAddress.City = txtCity.Text ?? homeAddress.City; homeAddress.State = ddlState.SelectedValue ?? homeAddress.State; homeAddress.Zip = txtZip.Text ?? homeAddress.Zip; homeAddress.IsActive = true; homeAddress.IsLocation = true; homeAddress.Country = "US"; homeAddress.LocationTypeValueId = homeLocationType.Id; locationService.Save(homeAddress, person.Id); GroupType familyGroupType = new GroupTypeService().Get(new Guid(Rock.SystemGuid.GroupType.GROUPTYPE_FAMILY)); var familyGroupList = groupMemberService.Queryable().Where(g => g.PersonId == person.Id && g.Group.GroupType.Guid == familyGroupType.Guid).Select(g => g.Group).ToList(); if (!familyGroupList.Any()) { familyGroup = new Group(); familyGroup.IsActive = true; familyGroup.IsSystem = false; familyGroup.IsSecurityRole = false; familyGroup.Name = "The " + txtLastName.Text + " Family"; familyGroup.GroupTypeId = familyGroupType.Id; groupService.Add(familyGroup, person.Id); groupService.Save(familyGroup, person.Id); var familyMember = new GroupMember(); familyMember.IsSystem = false; familyMember.GroupId = familyGroup.Id; familyMember.PersonId = person.Id; familyMember.GroupRoleId = new GroupRoleService().Get(new Guid(Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT)).Id; groupMemberService.Add(familyMember, person.Id); groupMemberService.Save(familyMember, person.Id); } else { familyGroup = familyGroupList.FirstOrDefault(); } var groupLocationList = groupLocationService.Queryable().Where(g => g.GroupLocationTypeValueId == familyGroupType.Id && g.GroupId == familyGroup.Id).ToList(); if (!groupLocationList.Any()) { groupLocation = new GroupLocation(); groupLocation.GroupId = familyGroup.Id; groupLocation.LocationId = homeAddress.Id; groupLocation.IsMailing = true; groupLocation.IsLocation = true; groupLocation.GroupLocationTypeValueId = homeLocationType.Id; groupLocationService.Add(groupLocation, person.Id); groupLocationService.Save(groupLocation, person.Id); } else { groupLocation = groupLocationList.FirstOrDefault(); } groupLocation.LocationId = homeAddress.Id; groupLocationService.Save(groupLocation, person.Id); var homePhoneType = DefinedValueCache.Read(Rock.SystemGuid.DefinedValue.PERSON_PHONE_TYPE_HOME); string phoneNumeric = txtPhone.Text.AsNumeric(); if (!phoneService.Queryable().Where(n => n.PersonId == person.Id && n.NumberTypeValueId == homePhoneType.Id && n.Number == phoneNumeric).Any()) { var homePhone = new PhoneNumber(); homePhone.Number = phoneNumeric; homePhone.PersonId = person.Id; homePhone.IsSystem = false; homePhone.IsMessagingEnabled = false; homePhone.IsUnlisted = false; homePhone.NumberTypeValueId = homePhoneType.Id; phoneService.Add(homePhone, person.Id); phoneService.Save(homePhone, person.Id); } }); } var amountList = (Dictionary <FinancialAccount, Decimal>)Session["CachedAmounts"]; var profileId = (int)Session["CachedProfileId"]; Location giftLocation = new Location(); var configValues = (Dictionary <string, object>)Session["CachedMergeFields"]; configValues.Add("Date", DateTimeOffset.Now.ToString("MM/dd/yyyy hh:mm tt")); var receiptTemplate = GetAttributeValue("ReceiptMessage"); lReceipt.Text = receiptTemplate.ResolveMergeFields(configValues); var summaryTemplate = GetAttributeValue("SummaryMessage"); string summaryMessage = summaryTemplate.ResolveMergeFields(configValues); var creditProcessorId = GetAttributeValue("CreditCardProvider"); var achProcessorId = GetAttributeValue("Checking/ACHProvider"); var gatewayService = new FinancialGatewayService(); FinancialGateway gateway; if (!string.IsNullOrEmpty(txtCreditCard.Text) && !string.IsNullOrWhiteSpace(creditProcessorId)) { int creditId = Convert.ToInt32(creditProcessorId); gateway = new FinancialGatewayService().Get(creditId); } else if (!string.IsNullOrEmpty(txtAccountNumber.Text) && !string.IsNullOrWhiteSpace(achProcessorId)) { int achId = Convert.ToInt32(achProcessorId); gateway = new FinancialGatewayService().Get(achId); } else { gateway = gatewayService.Queryable().FirstOrDefault(); } // #TODO test card through gateway if (btnFrequency.SelectedIndex > -1 && btnFrequency.SelectedValueAsInt() != DefinedValueCache.Read(Rock.SystemGuid.DefinedValue.TRANSACTION_FREQUENCY_TYPE_ONE_TIME).Id) { using (new UnitOfWorkScope()) { RockTransactionScope.WrapTransaction(() => { var scheduledTransactionDetailService = new FinancialScheduledTransactionDetailService(); var scheduledTransactionService = new FinancialScheduledTransactionService(); FinancialScheduledTransaction scheduledTransaction; var detailList = amountList.ToList(); if (profileId > 0) { scheduledTransaction = scheduledTransactionService.Get(profileId); } else { scheduledTransaction = new FinancialScheduledTransaction(); scheduledTransactionService.Add(scheduledTransaction, person.Id); } DateTime startDate = (DateTime)dtpStartDate.SelectedDate; if (startDate != null) { scheduledTransaction.StartDate = startDate; } scheduledTransaction.TransactionFrequencyValueId = (int)btnFrequency.SelectedValueAsInt(); scheduledTransaction.AuthorizedPersonId = person.Id; scheduledTransaction.IsActive = true; if (!string.IsNullOrEmpty(txtCreditCard.Text)) { scheduledTransaction.CardReminderDate = mypExpiration.SelectedDate; } if (chkLimitGifts.Checked && !string.IsNullOrWhiteSpace(txtLimitNumber.Text)) { scheduledTransaction.NumberOfPayments = Convert.ToInt32(txtLimitNumber.Text); } foreach (var detail in amountList.ToList()) { var scheduledTransactionDetail = new FinancialScheduledTransactionDetail(); scheduledTransactionDetail.AccountId = detail.Key.Id; scheduledTransactionDetail.Amount = detail.Value; scheduledTransactionDetail.ScheduledTransactionId = scheduledTransaction.Id; scheduledTransactionDetailService.Add(scheduledTransactionDetail, person.Id); scheduledTransactionDetailService.Save(scheduledTransactionDetail, person.Id); } // implement gateway charge() scheduledTransactionService.Save(scheduledTransaction, person.Id); }); } } else { using (new UnitOfWorkScope()) { RockTransactionScope.WrapTransaction(() => { var transactionService = new FinancialTransactionService(); var tdService = new FinancialTransactionDetailService(); var transaction = new FinancialTransaction(); var detailList = amountList.ToList(); transaction.Summary = summaryMessage; transaction.Amount = detailList.Sum(d => d.Value); transaction.TransactionTypeValueId = DefinedValueCache.Read(Rock.SystemGuid.DefinedValue.TRANSACTION_TYPE_CONTRIBUTION).Id; transaction.TransactionDateTime = DateTimeOffset.Now.DateTime; transaction.AuthorizedPersonId = person.Id; transactionService.Add(transaction, person.Id); foreach (var detail in detailList) { var td = new FinancialTransactionDetail(); td.TransactionId = transaction.Id; td.AccountId = detail.Key.Id; td.Amount = detail.Value; td.TransactionId = transaction.Id; tdService.Add(td, person.Id); tdService.Save(td, person.Id); } // #TODO implement gateway.charge() transactionService.Save(transaction, person.Id); }); } } Session["CachedMergeFields"] = configValues; pnlConfirm.Visible = false; pnlComplete.Visible = true; pnlContribution.Update(); }
/// <summary> /// Sets the account fee coverage amount textbox text. /// </summary> /// <param name="tbAccountFeeCoverageAmount">The tb account fee coverage amount.</param> /// <param name="transactionDetail">The transaction detail.</param> private void SetAccountFeeCoverageAmountTextboxText(CurrencyBox tbAccountFeeCoverageAmount, FinancialScheduledTransactionDetail transactionDetail) { tbAccountFeeCoverageAmount.CurrencyCodeDefinedValueId = ForeignCurrencyDefinedValueId ?? 0; tbAccountFeeCoverageAmount.Value = transactionDetail.FeeCoverageAmount; tbAccountFeeCoverageAmount.Visible = transactionDetail.FeeCoverageAmount.HasValue; }
/// <summary> /// Updates the scheduled payment. /// </summary> /// <param name="usePaymentToken">if set to <c>true</c> [use payment token].</param> /// <param name="paymentToken">The payment token.</param> protected void UpdateScheduledPayment(bool usePaymentToken, string paymentToken = null) { var giftTerm = this.GetAttributeValue(AttributeKey.GiftTerm); if (dtpStartDate.SelectedDate <= RockDateTime.Today) { nbUpdateScheduledPaymentWarning.Visible = true; nbUpdateScheduledPaymentWarning.Text = string.Format("When scheduling a {0}, make sure the starting date is in the future (after today)", giftTerm.ToLower()); return; } var rockContext = new RockContext(); var financialScheduledTransactionService = new FinancialScheduledTransactionService(rockContext); var financialScheduledTransactionDetailService = new FinancialScheduledTransactionDetailService(rockContext); Guid scheduledTransactionGuid = hfScheduledTransactionGuid.Value.AsGuid(); var financialScheduledTransaction = financialScheduledTransactionService.Get(scheduledTransactionGuid); financialScheduledTransaction.StartDate = dtpStartDate.SelectedDate.Value; financialScheduledTransaction.TransactionFrequencyValueId = ddlFrequency.SelectedValue.AsInteger(); ReferencePaymentInfo referencePaymentInfo; var person = financialScheduledTransaction.AuthorizedPersonAlias.Person; string errorMessage; var financialGateway = this.FinancialGateway; var financialGatewayComponent = this.FinancialGatewayComponent; var existingPaymentOrPersonSavedAccountId = rblExistingPaymentOrPersonSavedAccount.SelectedValue.AsInteger(); bool useExistingPaymentMethod = pnlUseExistingPaymentNoSavedAccounts.Visible || existingPaymentOrPersonSavedAccountId == 0; bool useSavedAccount = pnlUseExistingPaymentWithSavedAccounts.Visible && existingPaymentOrPersonSavedAccountId > 0; if (usePaymentToken) { referencePaymentInfo = new ReferencePaymentInfo(); referencePaymentInfo.FirstName = person.FirstName; referencePaymentInfo.LastName = person.LastName; referencePaymentInfo.UpdateAddressFieldsFromAddressControl(acBillingAddress); referencePaymentInfo.ReferenceNumber = paymentToken; var customerToken = financialGatewayComponent.CreateCustomerAccount(this.FinancialGateway, referencePaymentInfo, out errorMessage); if (errorMessage.IsNotNullOrWhiteSpace() || customerToken.IsNullOrWhiteSpace()) { nbMessage.NotificationBoxType = NotificationBoxType.Danger; nbMessage.Text = errorMessage ?? "Unknown Error"; nbMessage.Visible = true; return; } referencePaymentInfo.GatewayPersonIdentifier = customerToken; } else if (useExistingPaymentMethod) { // use save payment method as original transaction referencePaymentInfo = new ReferencePaymentInfo(); referencePaymentInfo.GatewayPersonIdentifier = financialScheduledTransaction.FinancialPaymentDetail.GatewayPersonIdentifier; referencePaymentInfo.FinancialPersonSavedAccountId = financialScheduledTransaction.FinancialPaymentDetail.FinancialPersonSavedAccountId; } else if (useSavedAccount) { var savedAccount = new FinancialPersonSavedAccountService(rockContext).Get(existingPaymentOrPersonSavedAccountId); if (savedAccount != null) { referencePaymentInfo = savedAccount.GetReferencePayment(); } else { // shouldn't happen throw new Exception("Unable to determine Saved Account"); } } else { // shouldn't happen throw new Exception("Unable to determine payment method"); } var selectedAccountAmounts = caapPromptForAccountAmounts.AccountAmounts.Where(a => a.Amount.HasValue && a.Amount.Value != 0).Select(a => new { a.AccountId, Amount = a.Amount.Value }).ToArray(); referencePaymentInfo.Amount = selectedAccountAmounts.Sum(a => a.Amount); var originalGatewayScheduleId = financialScheduledTransaction.GatewayScheduleId; try { financialScheduledTransaction.FinancialPaymentDetail.ClearPaymentInfo(); var successfullyUpdated = financialGatewayComponent.UpdateScheduledPayment(financialScheduledTransaction, referencePaymentInfo, out errorMessage); if (!successfullyUpdated) { nbMessage.NotificationBoxType = NotificationBoxType.Danger; nbMessage.Text = errorMessage ?? "Unknown Error"; nbMessage.Visible = true; return; } financialScheduledTransaction.FinancialPaymentDetail.SetFromPaymentInfo(referencePaymentInfo, financialGatewayComponent as GatewayComponent, rockContext); var selectedAccountIds = selectedAccountAmounts.Select(a => a.AccountId).ToArray(); var deletedTransactionDetails = financialScheduledTransaction.ScheduledTransactionDetails.ToList().Where(a => !selectedAccountIds.Contains(a.AccountId)).ToList(); foreach (var deletedTransactionDetail in deletedTransactionDetails) { financialScheduledTransaction.ScheduledTransactionDetails.Remove(deletedTransactionDetail); financialScheduledTransactionDetailService.Delete(deletedTransactionDetail); } foreach (var selectedAccountAmount in selectedAccountAmounts) { var scheduledTransactionDetail = financialScheduledTransaction.ScheduledTransactionDetails.FirstOrDefault(a => a.AccountId == selectedAccountAmount.AccountId); if (scheduledTransactionDetail == null) { scheduledTransactionDetail = new FinancialScheduledTransactionDetail(); scheduledTransactionDetail.AccountId = selectedAccountAmount.AccountId; financialScheduledTransaction.ScheduledTransactionDetails.Add(scheduledTransactionDetail); } scheduledTransactionDetail.Amount = selectedAccountAmount.Amount; } rockContext.SaveChanges(); Task.Run(() => ScheduledGiftWasModifiedMessage.PublishScheduledTransactionEvent(financialScheduledTransaction.Id, ScheduledGiftEventTypes.ScheduledGiftUpdated)); } catch (Exception) { // if the GatewayScheduleId was updated, but there was an exception, // make sure we save the financialScheduledTransaction record with the updated GatewayScheduleId so we don't orphan it if (financialScheduledTransaction.GatewayScheduleId.IsNotNullOrWhiteSpace() && (originalGatewayScheduleId != financialScheduledTransaction.GatewayScheduleId)) { rockContext.SaveChanges(); } throw; } var mergeFields = LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson, new CommonMergeFieldsOptions { GetLegacyGlobalMergeFields = false }); var finishLavaTemplate = this.GetAttributeValue(AttributeKey.FinishLavaTemplate); // re-fetch financialScheduledTransaction with a new RockContext from database to ensure that lazy loaded fields will be populated using (var rockContextForSummary = new RockContext()) { if (pnlHostedPaymentControl.Visible && hfSaveNewAccount.Value.AsInteger() == 1 && tbSaveAccount.Text.IsNotNullOrWhiteSpace()) { SaveNewFinancialPersonSavedAccount(financialScheduledTransaction); } financialScheduledTransaction = new FinancialScheduledTransactionService(rockContextForSummary).Get(scheduledTransactionGuid); mergeFields.Add("Transaction", financialScheduledTransaction); mergeFields.Add("Person", financialScheduledTransaction.AuthorizedPersonAlias.Person); mergeFields.Add("PaymentDetail", financialScheduledTransaction.FinancialPaymentDetail); mergeFields.Add("BillingLocation", financialScheduledTransaction.FinancialPaymentDetail.BillingLocation); pnlPromptForChanges.Visible = false; pnlTransactionSummary.Visible = true; lTransactionSummaryHTML.Text = finishLavaTemplate.ResolveMergeFields(mergeFields); } }
/// <summary> /// Handles the Click event of the btnSaveAccounts control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void btnSaveAccounts_Click(object sender, EventArgs e) { using (var rockContext = new RockContext()) { var financialScheduledTransaction = GetTransaction(rockContext); decimal totalAmount = TransactionDetailsState.Select(d => d.Amount).ToList().Sum(); if (financialScheduledTransaction.TotalAmount != totalAmount) { nbError.Title = "Incorrect Amount"; nbError.Text = string.Format("<p>When updating account allocations, the total amount needs to remain the same as the original amount ({0}).</p>", financialScheduledTransaction.TotalAmount.FormatAsCurrency()); nbError.Visible = true; return; } var txnDetailService = new FinancialScheduledTransactionDetailService(rockContext); var accountService = new FinancialAccountService(rockContext); // Delete any transaction details that were removed var txnDetailsInDB = txnDetailService.Queryable().Where(a => a.ScheduledTransactionId.Equals(financialScheduledTransaction.Id)).ToList(); var deletedDetails = from txnDetail in txnDetailsInDB where !TransactionDetailsState.Select(d => d.Guid).Contains(txnDetail.Guid) select txnDetail; bool accountChanges = deletedDetails.Any(); deletedDetails.ToList().ForEach(txnDetail => { txnDetailService.Delete(txnDetail); }); // Save Transaction Details foreach (var editorTxnDetail in TransactionDetailsState) { editorTxnDetail.Account = accountService.Get(editorTxnDetail.AccountId); // Add or Update the activity type var financialTransactionDetail = financialScheduledTransaction.ScheduledTransactionDetails.FirstOrDefault(d => d.Guid.Equals(editorTxnDetail.Guid)); if (financialTransactionDetail == null) { accountChanges = true; financialTransactionDetail = new FinancialScheduledTransactionDetail(); financialTransactionDetail.Guid = editorTxnDetail.Guid; financialScheduledTransaction.ScheduledTransactionDetails.Add(financialTransactionDetail); } else { if (financialTransactionDetail.AccountId != editorTxnDetail.AccountId || financialTransactionDetail.Amount != editorTxnDetail.Amount || financialTransactionDetail.Summary != editorTxnDetail.Summary) { accountChanges = true; } } financialTransactionDetail.AccountId = editorTxnDetail.AccountId; financialTransactionDetail.Amount = editorTxnDetail.Amount; financialTransactionDetail.Summary = editorTxnDetail.Summary; } if (accountChanges) { // save changes rockContext.SaveChanges(); } ShowView(financialScheduledTransaction); } }
/// <summary> /// Processes the confirmation. /// </summary> /// <param name="errorMessage">The error message.</param> /// <returns></returns> private bool ProcessConfirmation( out string errorMessage ) { if ( string.IsNullOrWhiteSpace( TransactionCode ) ) { GatewayComponent gateway = hfPaymentTab.Value == "ACH" ? _achGateway : _ccGateway; if ( gateway == null ) { errorMessage = "There was a problem creating the payment gateway information"; return false; } Person person = GetPerson( true ); if ( person == null ) { errorMessage = "There was a problem creating the person information"; return false; } PaymentInfo paymentInfo = GetPaymentInfo(); if ( paymentInfo == null ) { errorMessage = "There was a problem creating the payment information"; return false; } else { paymentInfo.FirstName = person.FirstName; paymentInfo.LastName = person.LastName; } if ( paymentInfo.CreditCardTypeValue != null ) { CreditCardTypeValueId = paymentInfo.CreditCardTypeValue.Id; } PaymentSchedule schedule = GetSchedule(); if ( schedule != null ) { schedule.PersonId = person.Id; var scheduledTransaction = gateway.AddScheduledPayment( schedule, paymentInfo, out errorMessage ); if ( scheduledTransaction != null ) { scheduledTransaction.TransactionFrequencyValueId = schedule.TransactionFrequencyValue.Id; scheduledTransaction.AuthorizedPersonId = person.Id; scheduledTransaction.GatewayEntityTypeId = EntityTypeCache.Read( gateway.TypeGuid ).Id; foreach ( var account in SelectedAccounts.Where( a => a.Amount > 0 ) ) { var transactionDetail = new FinancialScheduledTransactionDetail(); transactionDetail.Amount = account.Amount; transactionDetail.AccountId = account.Id; scheduledTransaction.ScheduledTransactionDetails.Add( transactionDetail ); } var transactionService = new FinancialScheduledTransactionService(); transactionService.Add( scheduledTransaction, CurrentPersonId ); transactionService.Save( scheduledTransaction, CurrentPersonId ); ScheduleId = scheduledTransaction.GatewayScheduleId; TransactionCode = scheduledTransaction.TransactionCode; } else { return false; } } else { var transaction = gateway.Charge( paymentInfo, out errorMessage ); if ( transaction != null ) { transaction.TransactionDateTime = DateTime.Now; transaction.AuthorizedPersonId = person.Id; transaction.GatewayEntityTypeId = gateway.TypeId; transaction.Amount = paymentInfo.Amount; transaction.TransactionTypeValueId = DefinedValueCache.Read(new Guid(Rock.SystemGuid.DefinedValue.TRANSACTION_TYPE_CONTRIBUTION)).Id; transaction.CurrencyTypeValueId = paymentInfo.CurrencyTypeValue.Id; transaction.CreditCardTypeValueId = CreditCardTypeValueId; Guid sourceGuid = Guid.Empty; if (Guid.TryParse(GetAttributeValue("Source"), out sourceGuid)) { transaction.SourceTypeValueId = DefinedValueCache.Read(sourceGuid).Id; } foreach ( var account in SelectedAccounts.Where( a => a.Amount > 0 ) ) { var transactionDetail = new FinancialTransactionDetail(); transactionDetail.Amount = account.Amount; transactionDetail.AccountId = account.Id; transaction.TransactionDetails.Add( transactionDetail ); } // Get the batch name string ccSuffix = string.Empty; if ( paymentInfo.CreditCardTypeValue != null ) { ccSuffix = paymentInfo.CreditCardTypeValue.GetAttributeValue( "BatchNameSuffix" ); } if ( string.IsNullOrWhiteSpace( ccSuffix ) ) { ccSuffix = paymentInfo.CurrencyTypeValue.Name; } string batchName = GetAttributeValue( "BatchNamePrefix" ).Trim() + " " + ccSuffix; using ( new UnitOfWorkScope() ) { var batchService = new FinancialBatchService(); var batch = batchService.Queryable() .Where( b => b.Status == BatchStatus.Open && b.BatchStartDateTime <= transaction.TransactionDateTime && b.BatchEndDateTime > transaction.TransactionDateTime && b.Name == batchName ) .FirstOrDefault(); if ( batch == null ) { batch = new FinancialBatch(); batch.Name = batchName; batch.Status = BatchStatus.Open; batch.BatchStartDateTime = transaction.TransactionDateTime.Value.Date.Add( gateway.BatchTimeOffset ); if ( batch.BatchStartDateTime > transaction.TransactionDateTime ) { batch.BatchStartDateTime.Value.AddDays( -1 ); } batch.BatchEndDateTime = batch.BatchStartDateTime.Value.AddDays( 1 ).AddMilliseconds( -1 ); batch.CreatedByPersonId = person.Id; batchService.Add( batch, CurrentPersonId ); batchService.Save( batch, CurrentPersonId ); batch = batchService.Get( batch.Id ); } batch.ControlAmount += transaction.Amount; batchService.Save( batch, CurrentPersonId ); var transactionService = new FinancialTransactionService(); transaction.BatchId = batch.Id; transactionService.Add( transaction, CurrentPersonId ); transactionService.Save( transaction, CurrentPersonId ); } TransactionCode = transaction.TransactionCode; } else { return false; } } tdTransactionCode.Description = TransactionCode; tdTransactionCode.Visible = !string.IsNullOrWhiteSpace( TransactionCode ); tdScheduleId.Description = ScheduleId; tdScheduleId.Visible = !string.IsNullOrWhiteSpace( ScheduleId ); // If there was a transaction code returned and this was not already created from a previous saved account, // show the option to save the account. if ( !( paymentInfo is ReferencePaymentInfo ) && !string.IsNullOrWhiteSpace( TransactionCode ) ) { cbSaveAccount.Visible = true; pnlSaveAccount.Visible = true; txtSaveAccount.Visible = true; // If current person does not have a login, have them create a username and password phCreateLogin.Visible = !new UserLoginService().GetByPersonId( person.Id ).Any(); } else { pnlSaveAccount.Visible = false; } return true; } else { pnlDupWarning.Visible = true; errorMessage = string.Empty; return false; } }
/// <summary> /// Processes the confirmation. /// </summary> /// <param name="errorMessage">The error message.</param> /// <returns></returns> private bool ProcessConfirmation( out string errorMessage ) { var rockContext = new RockContext(); if ( string.IsNullOrWhiteSpace( TransactionCode ) ) { GatewayComponent gateway = null; var financialGateway = hfPaymentTab.Value == "ACH" ? _achGateway : _ccGateway; if ( financialGateway != null ) { gateway = financialGateway.GetGatewayComponent(); } if ( gateway == null ) { errorMessage = "There was a problem creating the payment gateway information"; return false; } Person person = GetPerson( true ); if ( person == null ) { errorMessage = "There was a problem creating the person information"; return false; } if ( !person.PrimaryAliasId.HasValue ) { errorMessage = "There was a problem creating the person's primary alias"; return false; } PaymentInfo paymentInfo = GetPaymentInfo(); if ( paymentInfo == null ) { errorMessage = "There was a problem creating the payment information"; return false; } else { paymentInfo.FirstName = person.FirstName; paymentInfo.LastName = person.LastName; } if ( paymentInfo.CreditCardTypeValue != null ) { CreditCardTypeValueId = paymentInfo.CreditCardTypeValue.Id; } if ( _showCommmentEntry ) { paymentInfo.Comment1 = !string.IsNullOrWhiteSpace(GetAttributeValue( "PaymentComment" )) ? string.Format("{0}: {1}", GetAttributeValue( "PaymentComment" ), txtCommentEntry.Text ) : txtCommentEntry.Text; } else { paymentInfo.Comment1 = GetAttributeValue( "PaymentComment" ); } PaymentSchedule schedule = GetSchedule(); if ( schedule != null ) { schedule.PersonId = person.Id; var scheduledTransaction = gateway.AddScheduledPayment( financialGateway, schedule, paymentInfo, out errorMessage ); if ( scheduledTransaction != null ) { scheduledTransaction.TransactionFrequencyValueId = schedule.TransactionFrequencyValue.Id; scheduledTransaction.AuthorizedPersonAliasId = person.PrimaryAliasId.Value; scheduledTransaction.FinancialGatewayId = financialGateway.Id; if ( scheduledTransaction.FinancialPaymentDetail == null ) { scheduledTransaction.FinancialPaymentDetail = new FinancialPaymentDetail(); } scheduledTransaction.FinancialPaymentDetail.SetFromPaymentInfo( paymentInfo, gateway, rockContext ); var changeSummary = new StringBuilder(); changeSummary.AppendFormat( "{0} starting {1}", schedule.TransactionFrequencyValue.Value, schedule.StartDate.ToShortDateString() ); changeSummary.AppendLine(); changeSummary.Append( paymentInfo.CurrencyTypeValue.Value ); if ( paymentInfo.CreditCardTypeValue != null ) { changeSummary.AppendFormat( " - {0}", paymentInfo.CreditCardTypeValue.Value ); } changeSummary.AppendFormat( " {0}", paymentInfo.MaskedNumber ); changeSummary.AppendLine(); foreach ( var account in SelectedAccounts.Where( a => a.Amount > 0 ) ) { var transactionDetail = new FinancialScheduledTransactionDetail(); transactionDetail.Amount = account.Amount; transactionDetail.AccountId = account.Id; scheduledTransaction.ScheduledTransactionDetails.Add( transactionDetail ); changeSummary.AppendFormat( "{0}: {1}", account.Name, account.Amount.FormatAsCurrency() ); changeSummary.AppendLine(); } var transactionService = new FinancialScheduledTransactionService( rockContext ); transactionService.Add( scheduledTransaction ); rockContext.SaveChanges(); // Add a note about the change var noteType = NoteTypeCache.Read( Rock.SystemGuid.NoteType.SCHEDULED_TRANSACTION_NOTE.AsGuid() ); if ( noteType != null ) { var noteService = new NoteService( rockContext ); var note = new Note(); note.NoteTypeId = noteType.Id; note.EntityId = scheduledTransaction.Id; note.Caption = "Created Transaction"; note.Text = changeSummary.ToString(); noteService.Add( note ); } rockContext.SaveChanges(); ScheduleId = scheduledTransaction.GatewayScheduleId; TransactionCode = scheduledTransaction.TransactionCode; } else { return false; } } else { var transaction = gateway.Charge( financialGateway, paymentInfo, out errorMessage ); if ( transaction != null ) { var txnChanges = new List<string>(); txnChanges.Add( "Created Transaction" ); History.EvaluateChange( txnChanges, "Transaction Code", string.Empty, transaction.TransactionCode ); transaction.AuthorizedPersonAliasId = person.PrimaryAliasId; History.EvaluateChange( txnChanges, "Person", string.Empty, person.FullName ); transaction.TransactionDateTime = RockDateTime.Now; History.EvaluateChange( txnChanges, "Date/Time", null, transaction.TransactionDateTime ); transaction.FinancialGatewayId = financialGateway.Id; History.EvaluateChange( txnChanges, "Gateway", string.Empty, financialGateway.Name ); var txnType = DefinedValueCache.Read( new Guid( Rock.SystemGuid.DefinedValue.TRANSACTION_TYPE_CONTRIBUTION ) ); transaction.TransactionTypeValueId = txnType.Id; History.EvaluateChange( txnChanges, "Type", string.Empty, txnType.Value ); if ( transaction.FinancialPaymentDetail == null ) { transaction.FinancialPaymentDetail = new FinancialPaymentDetail(); } transaction.FinancialPaymentDetail.SetFromPaymentInfo( paymentInfo, gateway, rockContext, txnChanges ); Guid sourceGuid = Guid.Empty; if ( Guid.TryParse( GetAttributeValue( "Source" ), out sourceGuid ) ) { var source = DefinedValueCache.Read( sourceGuid ); if ( source != null ) { transaction.SourceTypeValueId = source.Id; History.EvaluateChange( txnChanges, "Source", string.Empty, source.Value ); } } foreach ( var account in SelectedAccounts.Where( a => a.Amount > 0 ) ) { var transactionDetail = new FinancialTransactionDetail(); transactionDetail.Amount = account.Amount; transactionDetail.AccountId = account.Id; transaction.TransactionDetails.Add( transactionDetail ); History.EvaluateChange( txnChanges, account.Name, 0.0M.FormatAsCurrency(), transactionDetail.Amount.FormatAsCurrency() ); } var batchService = new FinancialBatchService( rockContext ); // Get the batch var batch = batchService.Get( GetAttributeValue( "BatchNamePrefix" ), paymentInfo.CurrencyTypeValue, paymentInfo.CreditCardTypeValue, transaction.TransactionDateTime.Value, financialGateway.GetBatchTimeOffset() ); var batchChanges = new List<string>(); if ( batch.Id == 0 ) { batchChanges.Add( "Generated the batch" ); History.EvaluateChange( batchChanges, "Batch Name", string.Empty, batch.Name ); History.EvaluateChange( batchChanges, "Status", null, batch.Status ); History.EvaluateChange( batchChanges, "Start Date/Time", null, batch.BatchStartDateTime ); History.EvaluateChange( batchChanges, "End Date/Time", null, batch.BatchEndDateTime ); } decimal newControlAmount = batch.ControlAmount + transaction.TotalAmount; History.EvaluateChange( batchChanges, "Control Amount", batch.ControlAmount.FormatAsCurrency(), newControlAmount.FormatAsCurrency() ); batch.ControlAmount = newControlAmount; transaction.BatchId = batch.Id; batch.Transactions.Add( transaction ); rockContext.SaveChanges(); HistoryService.SaveChanges( rockContext, typeof( FinancialBatch ), Rock.SystemGuid.Category.HISTORY_FINANCIAL_BATCH.AsGuid(), batch.Id, batchChanges ); HistoryService.SaveChanges( rockContext, typeof( FinancialBatch ), Rock.SystemGuid.Category.HISTORY_FINANCIAL_TRANSACTION.AsGuid(), batch.Id, txnChanges, person.FullName, typeof( FinancialTransaction ), transaction.Id ); SendReceipt( transaction.Id ); TransactionCode = transaction.TransactionCode; } else { return false; } } tdTransactionCodeReceipt.Description = TransactionCode; tdTransactionCodeReceipt.Visible = !string.IsNullOrWhiteSpace( TransactionCode ); tdScheduleId.Description = ScheduleId; tdScheduleId.Visible = !string.IsNullOrWhiteSpace( ScheduleId ); tdNameReceipt.Description = paymentInfo.FullName; tdPhoneReceipt.Description = paymentInfo.Phone; tdEmailReceipt.Description = paymentInfo.Email; tdAddressReceipt.Description = string.Format( "{0} {1}, {2} {3}", paymentInfo.Street1, paymentInfo.City, paymentInfo.State, paymentInfo.PostalCode ); rptAccountListReceipt.DataSource = SelectedAccounts.Where( a => a.Amount != 0 ); rptAccountListReceipt.DataBind(); tdTotalReceipt.Description = paymentInfo.Amount.ToString( "C" ); tdPaymentMethodReceipt.Description = paymentInfo.CurrencyTypeValue.Description; tdAccountNumberReceipt.Description = paymentInfo.MaskedNumber; tdWhenReceipt.Description = schedule != null ? schedule.ToString() : "Today"; // If there was a transaction code returned and this was not already created from a previous saved account, // show the option to save the account. if ( !( paymentInfo is ReferencePaymentInfo ) && !string.IsNullOrWhiteSpace( TransactionCode ) && gateway.SupportsSavedAccount( paymentInfo.CurrencyTypeValue ) ) { cbSaveAccount.Visible = true; pnlSaveAccount.Visible = true; txtSaveAccount.Visible = true; // If current person does not have a login, have them create a username and password phCreateLogin.Visible = !new UserLoginService( rockContext ).GetByPersonId( person.Id ).Any(); } else { pnlSaveAccount.Visible = false; } return true; } else { pnlDupWarning.Visible = true; divActions.Visible = false; errorMessage = string.Empty; return false; } }
/// <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; } }
private void SaveScheduledTransaction( FinancialGateway financialGateway, GatewayComponent gateway, Person person, PaymentInfo paymentInfo, PaymentSchedule schedule, FinancialScheduledTransaction scheduledTransaction, RockContext rockContext ) { scheduledTransaction.TransactionFrequencyValueId = schedule.TransactionFrequencyValue.Id; scheduledTransaction.StartDate = schedule.StartDate; scheduledTransaction.AuthorizedPersonAliasId = person.PrimaryAliasId.Value; scheduledTransaction.FinancialGatewayId = financialGateway.Id; if ( scheduledTransaction.FinancialPaymentDetail == null ) { scheduledTransaction.FinancialPaymentDetail = new FinancialPaymentDetail(); } scheduledTransaction.FinancialPaymentDetail.SetFromPaymentInfo( paymentInfo, gateway, rockContext ); Guid sourceGuid = Guid.Empty; if ( Guid.TryParse( GetAttributeValue( "Source" ), out sourceGuid ) ) { var source = DefinedValueCache.Read( sourceGuid ); if ( source != null ) { scheduledTransaction.SourceTypeValueId = source.Id; } } var changeSummary = new StringBuilder(); changeSummary.AppendFormat( "{0} starting {1}", schedule.TransactionFrequencyValue.Value, schedule.StartDate.ToShortDateString() ); changeSummary.AppendLine(); changeSummary.Append( paymentInfo.CurrencyTypeValue.Value ); if ( paymentInfo.CreditCardTypeValue != null ) { changeSummary.AppendFormat( " - {0}", paymentInfo.CreditCardTypeValue.Value ); } changeSummary.AppendFormat( " {0}", paymentInfo.MaskedNumber ); changeSummary.AppendLine(); foreach ( var account in SelectedAccounts.Where( a => a.Amount > 0 ) ) { var transactionDetail = new FinancialScheduledTransactionDetail(); transactionDetail.Amount = account.Amount; transactionDetail.AccountId = account.Id; scheduledTransaction.ScheduledTransactionDetails.Add( transactionDetail ); changeSummary.AppendFormat( "{0}: {1}", account.Name, account.Amount.FormatAsCurrency() ); changeSummary.AppendLine(); } if ( !string.IsNullOrWhiteSpace( paymentInfo.Comment1 ) ) { changeSummary.Append( paymentInfo.Comment1 ); changeSummary.AppendLine(); } var transactionService = new FinancialScheduledTransactionService( rockContext ); transactionService.Add( scheduledTransaction ); rockContext.SaveChanges(); // If this is a transfer, now we can delete the old transaction if ( _scheduledTransactionToBeTransferred != null ) { DeleteOldTransaction( _scheduledTransactionToBeTransferred.Id ); } // Add a note about the change var noteType = NoteTypeCache.Read( Rock.SystemGuid.NoteType.SCHEDULED_TRANSACTION_NOTE.AsGuid() ); if ( noteType != null ) { var noteService = new NoteService( rockContext ); var note = new Note(); note.NoteTypeId = noteType.Id; note.EntityId = scheduledTransaction.Id; note.Caption = "Created Transaction"; note.Text = changeSummary.ToString(); noteService.Add( note ); } rockContext.SaveChanges(); ScheduleId = scheduledTransaction.GatewayScheduleId; TransactionCode = scheduledTransaction.TransactionCode; }
/// <summary> /// Sets the account amount minus fee coverage textbox text. /// </summary> /// <param name="tbAccountAmount">The tb account amount.</param> /// <param name="transactionDetail">The transaction detail.</param> private void SetAccountAmountMinusFeeCoverageTextboxText(CurrencyBox tbAccountAmountMinusFeeCoverageAmount, FinancialScheduledTransactionDetail transactionDetail) { /* 2021-01-28 MDP * * FinancialScheduledTransactionDetail.Amount includes the FeeCoverageAmount. * For example, if a person scheduled to gave $100.00 but elected to pay $1.80 to cover the fee. * FinancialScheduledTransactionDetail.Amount would be stored as $101.80 and * FinancialScheduledTransactionDetail.FeeCoverageAmount would be stored as $1.80. * * However, when the FinancialScheduledTransactionDetail.Amount is used in this EditBox, * don't include the FinanciaFinancialScheduledTransactionDetaillTransactionDetail.FeeCoverageAmount. * So in the above example, the Textbox would say $100.00 * */ var feeCoverageAmount = transactionDetail.FeeCoverageAmount; var accountAmount = transactionDetail.Amount; tbAccountAmountMinusFeeCoverageAmount.CurrencyCodeDefinedValueId = ForeignCurrencyDefinedValueId ?? 0; if (feeCoverageAmount.HasValue) { tbAccountAmountMinusFeeCoverageAmount.Value = (accountAmount - feeCoverageAmount.Value); } else { tbAccountAmountMinusFeeCoverageAmount.Value = accountAmount; } }