/// <summary> /// Handles the Click event of the btnSavePaymentInfo 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 btnSavePaymentInfo_Click(object sender, EventArgs e) { var accountService = new FinancialPersonSavedAccountService(); var configValues = (Dictionary <string, object>)Session["CachedMergeFields"]; string accountNickname = txtPaymentNick.Text; Person person = FindPerson(); var account = accountService.Queryable().Where(a => a.Name == accountNickname && a.PersonId == person.Id).FirstOrDefault(); if (account == null) { account = new FinancialPersonSavedAccount(); accountService.Add(account, person.Id); } account.Name = accountNickname; // #TODO WITH GATEWAY CALL account.TransactionCode = "Unknown"; account.PersonId = person.Id; account.MaskedAccountNumber = configValues["PaymentLastFour"].ToString(); if (!string.IsNullOrEmpty(txtCreditCard.Text)) { account.PaymentMethod = PaymentMethod.CreditCard; } else if (!string.IsNullOrEmpty(txtAccountNumber.Text)) { account.PaymentMethod = PaymentMethod.ACH; } accountService.Save(account, person.Id); divPaymentNick.Visible = false; }
/// <summary> /// Get the name of the saved account /// </summary> /// <param name="savedAccount"></param> /// <returns></returns> private string GetSavedAccountName(FinancialPersonSavedAccount savedAccount) { const string unnamed = "<Unnamed>"; if (savedAccount == null) { return(unnamed); } var name = savedAccount.Name.IsNullOrWhiteSpace() ? unnamed : savedAccount.Name.Trim(); if (savedAccount.FinancialPaymentDetail != null) { var expirationMonth = savedAccount.FinancialPaymentDetail.ExpirationMonth; var expirationYear = savedAccount.FinancialPaymentDetail.ExpirationYear; if (expirationMonth.HasValue || expirationYear.HasValue) { var monthString = expirationMonth.HasValue ? (expirationMonth.Value < 10 ? ("0" + expirationMonth.Value.ToString()) : expirationMonth.Value.ToString()) : "??"; var yearString = expirationYear.HasValue ? (expirationYear.Value % 100).ToString() : "??"; name += string.Format(" ({0}/{1})", monthString, yearString); } } return(name); }
public void RockCleanup_Execute_ShouldUpdatePeopleWithFinancialPersonSavedAccountToAccountProtectionProfileHigh() { var personGuid = Guid.NewGuid(); var personWithFinancialPersonBankAccount = new Person { FirstName = "Test", LastName = personGuid.ToString(), Email = $"{personGuid}@test.com", Guid = personGuid }; using (var rockContext = new RockContext()) { var personService = new PersonService(rockContext); personService.Add(personWithFinancialPersonBankAccount); rockContext.SaveChanges(); personWithFinancialPersonBankAccount = personService.Get(personWithFinancialPersonBankAccount.Id); var financialGateway = new FinancialGatewayService(rockContext).Get("6432D2D2-32FF-443D-B5B3-FB6C8414C3AD".AsGuid()); var creditCardTypeValue = DefinedTypeCache.Get(SystemGuid.DefinedType.FINANCIAL_CREDIT_CARD_TYPE.AsGuid()).DefinedValues.OrderBy(a => Guid.NewGuid()).First().Id; var currencyTypeValue = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.CURRENCY_TYPE_CREDIT_CARD.AsGuid()).Id; var definedValueService = new DefinedValueService(rockContext); var financialPersonSavedAccount = new FinancialPersonSavedAccount { Name = "Test Saved Account", PersonAliasId = personWithFinancialPersonBankAccount.PrimaryAliasId.Value, FinancialGateway = financialGateway, FinancialPaymentDetail = new FinancialPaymentDetail { AccountNumberMasked = "1111", CreditCardTypeValue = definedValueService.Get(creditCardTypeValue), CurrencyTypeValue = definedValueService.Get(currencyTypeValue), NameOnCard = "Test User" } }; var service = new FinancialPersonSavedAccountService(rockContext); service.Add(financialPersonSavedAccount); rockContext.SaveChanges(); } ExecuteRockCleanupJob(); using (var rockContext = new RockContext()) { var actualPerson = new PersonService(rockContext).Get(personGuid); Assert.That.AreEqual(AccountProtectionProfile.High, actualPerson.AccountProtectionProfile); } }
/// <summary> /// Creates the saved account. /// </summary> /// <param name="parameters">The parameters.</param> /// <param name="paymentDetail">The payment detail.</param> /// <param name="financialGateway">The financial gateway.</param> /// <param name="person">The person.</param> /// <param name="rockContext">The rock context.</param> /// <returns></returns> private FinancialPersonSavedAccount CreateSavedAccount(PaymentParameters parameters, FinancialPaymentDetail paymentDetail, FinancialGateway financialGateway, Person person, RockContext rockContext) { var lastFour = paymentDetail.AccountNumberMasked.Substring(paymentDetail.AccountNumberMasked.Length - 4); var name = string.Empty; if (parameters.AccountType.ToLower() != "credit") { if (string.IsNullOrWhiteSpace(parameters.RoutingNumber)) { GenerateResponse(HttpStatusCode.BadRequest, "RoutingNumber is required for ACH transactions"); return(null); } if (string.IsNullOrWhiteSpace(parameters.AccountNumber)) { GenerateResponse(HttpStatusCode.BadRequest, "AccountNumber is required"); return(null); } name = "Bank card ***" + lastFour; var bankAccountService = new FinancialPersonBankAccountService(rockContext); var accountNumberSecured = FinancialPersonBankAccount.EncodeAccountNumber(parameters.RoutingNumber, parameters.AccountNumber); var bankAccount = bankAccountService.Queryable().Where(a => a.AccountNumberSecured == accountNumberSecured && a.PersonAliasId == person.PrimaryAliasId.Value).FirstOrDefault(); if (bankAccount == null) { bankAccount = new FinancialPersonBankAccount(); bankAccount.PersonAliasId = person.PrimaryAliasId.Value; bankAccount.AccountNumberMasked = paymentDetail.AccountNumberMasked; bankAccount.AccountNumberSecured = accountNumberSecured; bankAccountService.Add(bankAccount); } } else { name = "Credit card ***" + lastFour; } var savedAccount = new FinancialPersonSavedAccount { PersonAliasId = person.PrimaryAliasId, FinancialGatewayId = financialGateway.Id, Name = name, FinancialPaymentDetailId = paymentDetail.Id }; new FinancialPersonSavedAccountService(rockContext).Add(savedAccount); rockContext.SaveChanges(); return(savedAccount); }
/// <summary> /// Deletes the specified item. /// </summary> /// <param name="item">The item.</param> /// <returns></returns> public override bool Delete( FinancialPersonSavedAccount item ) { if ( item.FinancialPaymentDetailId.HasValue ) { var paymentDetailsService = new FinancialPaymentDetailService( (Rock.Data.RockContext)this.Context ); var paymentDetail = paymentDetailsService.Get( item.FinancialPaymentDetailId.Value ); if ( paymentDetail != null ) { paymentDetailsService.Delete( paymentDetail ); } } return base.Delete( item ); }
/// <summary> /// Handles the Click event of the btnSaveAccount 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 SaveNewFinancialPersonSavedAccount(FinancialScheduledTransaction financialScheduledTransaction) { var rockContext = new RockContext(); var scheduledTransaction = this.GetFinancialScheduledTransaction(rockContext); var targetPerson = scheduledTransaction.AuthorizedPersonAlias.Person; var financialGatewayComponent = this.FinancialGatewayComponent; var financialGateway = this.FinancialGateway; var savedAccount = new FinancialPersonSavedAccount(); var paymentDetail = financialScheduledTransaction.FinancialPaymentDetail; savedAccount.PersonAliasId = targetPerson.PrimaryAliasId; savedAccount.ReferenceNumber = paymentDetail.GatewayPersonIdentifier; savedAccount.Name = tbSaveAccount.Text; savedAccount.TransactionCode = financialScheduledTransaction.TransactionCode; savedAccount.GatewayPersonIdentifier = paymentDetail.GatewayPersonIdentifier; savedAccount.FinancialGatewayId = financialGateway.Id; savedAccount.FinancialPaymentDetail = new FinancialPaymentDetail(); savedAccount.FinancialPaymentDetail.AccountNumberMasked = paymentDetail.AccountNumberMasked; savedAccount.FinancialPaymentDetail.CurrencyTypeValueId = paymentDetail.CurrencyTypeValueId; savedAccount.FinancialPaymentDetail.CreditCardTypeValueId = paymentDetail.CreditCardTypeValueId; savedAccount.FinancialPaymentDetail.NameOnCard = paymentDetail.NameOnCard; savedAccount.FinancialPaymentDetail.ExpirationMonth = paymentDetail.ExpirationMonth; savedAccount.FinancialPaymentDetail.ExpirationYear = paymentDetail.ExpirationYear; savedAccount.FinancialPaymentDetail.BillingLocationId = paymentDetail.BillingLocationId; var savedAccountService = new FinancialPersonSavedAccountService(rockContext); savedAccountService.Add(savedAccount); rockContext.SaveChanges(); savedAccount.FinancialPaymentDetail.FinancialPersonSavedAccountId = savedAccount.Id; }
/// <summary> /// Handles the Click event of the lbSaveAccount 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 lbSaveAccount_Click( object sender, EventArgs e ) { if ( string.IsNullOrWhiteSpace( TransactionCode ) ) { nbSaveAccount.Text = "Sorry, the account information cannot be saved as there's not a valid transaction code to reference"; nbSaveAccount.Visible = true; return; } using ( var rockContext = new RockContext() ) { if ( phCreateLogin.Visible ) { if ( string.IsNullOrWhiteSpace( txtUserName.Text ) || string.IsNullOrWhiteSpace( txtPassword.Text ) ) { nbSaveAccount.Title = "Missing Informaton"; nbSaveAccount.Text = "A username and password are required when saving an account"; nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; return; } if ( new UserLoginService( rockContext ).GetByUserName( txtUserName.Text ) != null ) { nbSaveAccount.Title = "Invalid Username"; nbSaveAccount.Text = "The selected Username is already being used. Please select a different Username"; nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; return; } if ( !UserLoginService.IsPasswordValid( txtPassword.Text ) ) { nbSaveAccount.Title = string.Empty; nbSaveAccount.Text = UserLoginService.FriendlyPasswordRules(); nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; return; } if ( txtPasswordConfirm.Text != txtPassword.Text ) { nbSaveAccount.Title = "Invalid Password"; nbSaveAccount.Text = "The password and password confirmation do not match"; nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; return; } } if ( !string.IsNullOrWhiteSpace( txtSaveAccount.Text ) ) { bool isACHTxn = hfPaymentTab.Value == "ACH"; var financialGateway = isACHTxn ? _achGateway : _ccGateway; var gateway = isACHTxn ? _achGatewayComponent : _ccGatewayComponent; if ( gateway != null ) { var ccCurrencyType = DefinedValueCache.Read( new Guid( Rock.SystemGuid.DefinedValue.CURRENCY_TYPE_CREDIT_CARD ) ); var achCurrencyType = DefinedValueCache.Read( new Guid( Rock.SystemGuid.DefinedValue.CURRENCY_TYPE_ACH ) ); string errorMessage = string.Empty; var person = GetPerson( false ); string referenceNumber = string.Empty; FinancialPaymentDetail paymentDetail = null; int? currencyTypeValueId = isACHTxn ? achCurrencyType.Id : ccCurrencyType.Id; if ( string.IsNullOrWhiteSpace( ScheduleId ) ) { var transaction = new FinancialTransactionService( rockContext ).GetByTransactionCode( TransactionCode ); if ( transaction != null && transaction.AuthorizedPersonAlias != null ) { if ( transaction.FinancialGateway != null ) { transaction.FinancialGateway.LoadAttributes( rockContext ); } referenceNumber = gateway.GetReferenceNumber( transaction, out errorMessage ); paymentDetail = transaction.FinancialPaymentDetail; } } else { var scheduledTransaction = new FinancialScheduledTransactionService( rockContext ).GetByScheduleId( ScheduleId ); if ( scheduledTransaction != null ) { if ( scheduledTransaction.FinancialGateway != null ) { scheduledTransaction.FinancialGateway.LoadAttributes( rockContext ); } referenceNumber = gateway.GetReferenceNumber( scheduledTransaction, out errorMessage ); paymentDetail = scheduledTransaction.FinancialPaymentDetail; } } if ( person != null && paymentDetail != null ) { if ( phCreateLogin.Visible ) { var user = UserLoginService.Create( rockContext, person, Rock.Model.AuthenticationServiceType.Internal, EntityTypeCache.Read( Rock.SystemGuid.EntityType.AUTHENTICATION_DATABASE.AsGuid() ).Id, txtUserName.Text, txtPassword.Text, false ); var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields( this.RockPage, this.CurrentPerson ); mergeFields.Add( "ConfirmAccountUrl", RootPath + "ConfirmAccount" ); var personDictionary = person.ToLiquid() as Dictionary<string, object>; mergeFields.Add( "Person", personDictionary ); mergeFields.Add( "User", user ); var recipients = new List<Rock.Communication.RecipientData>(); recipients.Add( new Rock.Communication.RecipientData( person.Email, mergeFields ) ); Rock.Communication.Email.Send( GetAttributeValue( "ConfirmAccountTemplate" ).AsGuid(), recipients, ResolveRockUrl( "~/" ), ResolveRockUrl( "~~/" ), false ); } if ( errorMessage.Any() ) { nbSaveAccount.Title = "Invalid Transaction"; nbSaveAccount.Text = "Sorry, the account information cannot be saved. " + errorMessage; nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; } else { var savedAccount = new FinancialPersonSavedAccount(); savedAccount.PersonAliasId = person.PrimaryAliasId; savedAccount.ReferenceNumber = referenceNumber; savedAccount.Name = txtSaveAccount.Text; savedAccount.TransactionCode = TransactionCode; savedAccount.FinancialGatewayId = financialGateway.Id; savedAccount.FinancialPaymentDetail = new FinancialPaymentDetail(); savedAccount.FinancialPaymentDetail.AccountNumberMasked = paymentDetail.AccountNumberMasked; savedAccount.FinancialPaymentDetail.CurrencyTypeValueId = paymentDetail.CurrencyTypeValueId; savedAccount.FinancialPaymentDetail.CreditCardTypeValueId = paymentDetail.CreditCardTypeValueId; savedAccount.FinancialPaymentDetail.NameOnCardEncrypted = paymentDetail.NameOnCardEncrypted; savedAccount.FinancialPaymentDetail.ExpirationMonthEncrypted = paymentDetail.ExpirationMonthEncrypted; savedAccount.FinancialPaymentDetail.ExpirationYearEncrypted = paymentDetail.ExpirationYearEncrypted; savedAccount.FinancialPaymentDetail.BillingLocationId = paymentDetail.BillingLocationId; var savedAccountService = new FinancialPersonSavedAccountService( rockContext ); savedAccountService.Add( savedAccount ); rockContext.SaveChanges(); cbSaveAccount.Visible = false; txtSaveAccount.Visible = false; phCreateLogin.Visible = false; divSaveActions.Visible = false; nbSaveAccount.Title = "Success"; nbSaveAccount.Text = "The account has been saved for future use"; nbSaveAccount.NotificationBoxType = NotificationBoxType.Success; nbSaveAccount.Visible = true; } } else { nbSaveAccount.Title = "Invalid Transaction"; nbSaveAccount.Text = "Sorry, the account information cannot be saved as there's not a valid transaction code to reference."; nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; } } else { nbSaveAccount.Title = "Invalid Gateway"; nbSaveAccount.Text = "Sorry, the financial gateway information for this type of transaction is not valid."; nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; } } else { nbSaveAccount.Title = "Missing Account Name"; nbSaveAccount.Text = "Please enter a name to use for this account."; nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; } } }
/// <summary> /// Safely load entities that have not yet been assigned a non-null value based on the arguments. /// </summary> private void LoadEntities() { if (_automatedPaymentArgs.ScheduledTransactionId.HasValue && _financialScheduledTransaction == null) { _financialScheduledTransaction = _financialScheduledTransactionService.Queryable() .AsNoTracking() .Include(s => s.TransactionFrequencyValue) .FirstOrDefault(s => s.Id == _automatedPaymentArgs.ScheduledTransactionId.Value); } if (_authorizedPerson == null) { _authorizedPerson = _personAliasService.GetPersonNoTracking(_automatedPaymentArgs.AuthorizedPersonAliasId); } if (_financialGateway == null) { _financialGateway = _financialGatewayService.GetNoTracking(_automatedPaymentArgs.AutomatedGatewayId); } if (_financialGateway != null && _automatedGatewayComponent == null) { _automatedGatewayComponent = _financialGateway.GetGatewayComponent(); } if (_financialAccounts == null) { var accountIds = _automatedPaymentArgs.AutomatedPaymentDetails.Select(d => d.AccountId).ToList(); _financialAccounts = _financialAccountService.GetByIds(accountIds).AsNoTracking().ToDictionary(fa => fa.Id, fa => fa); } if (_authorizedPerson != null && _financialPersonSavedAccount == null && _financialGateway != null) { // Pick the correct saved account based on args or default for the user var financialGatewayId = _financialGateway.Id; var savedAccounts = _financialPersonSavedAccountService .GetByPersonId(_authorizedPerson.Id) .AsNoTracking() .Where(sa => sa.FinancialGatewayId == financialGatewayId) .Include(sa => sa.FinancialPaymentDetail) .OrderByDescending(sa => sa.CreatedDateTime ?? DateTime.MinValue) .ToList(); if (_automatedPaymentArgs.FinancialPersonSavedAccountId.HasValue) { // If there is an indicated saved account to use, don't assume any other saved account even with a schedule var savedAccountId = _automatedPaymentArgs.FinancialPersonSavedAccountId.Value; _financialPersonSavedAccount = savedAccounts.FirstOrDefault(sa => sa.Id == savedAccountId); } else { // If there is a schedule and no indicated saved account to use, try to use payment info associated with the schedule if (_financialScheduledTransaction != null) { _financialPersonSavedAccount = // sa.ReferenceNumber == fst.TransactionCode savedAccounts.FirstOrDefault(sa => !string.IsNullOrEmpty(sa.ReferenceNumber) && sa.ReferenceNumber == _financialScheduledTransaction.TransactionCode) ?? // sa.GatewayPersonIdentifier == fst.TransactionCode savedAccounts.FirstOrDefault(sa => !string.IsNullOrEmpty(sa.GatewayPersonIdentifier) && sa.GatewayPersonIdentifier == _financialScheduledTransaction.TransactionCode) ?? // sa.FinancialPaymentDetailId == fst.FinancialPaymentDetailId savedAccounts.FirstOrDefault(sa => sa.FinancialPaymentDetailId.HasValue && sa.FinancialPaymentDetailId == _financialScheduledTransaction.FinancialPaymentDetailId) ?? // sa.TransactionCode == fst.TransactionCode savedAccounts.FirstOrDefault(sa => !string.IsNullOrEmpty(sa.TransactionCode) && sa.TransactionCode == _financialScheduledTransaction.TransactionCode); } if (_financialPersonSavedAccount == null) { // Use the default or first if no default _financialPersonSavedAccount = savedAccounts.FirstOrDefault(sa => sa.IsDefault) ?? savedAccounts.FirstOrDefault(); } } } if (_financialPersonSavedAccount != null && _referencePaymentInfo == null) { _referencePaymentInfo = _financialPersonSavedAccount.GetReferencePayment(); } if (_transactionType == null) { _transactionType = DefinedValueCache.Get(_automatedPaymentArgs.TransactionTypeGuid ?? SystemGuid.DefinedValue.TRANSACTION_TYPE_CONTRIBUTION.AsGuid()); } if (_financialSource == null) { _financialSource = DefinedValueCache.Get(_automatedPaymentArgs.FinancialSourceGuid ?? SystemGuid.DefinedValue.FINANCIAL_SOURCE_TYPE_WEBSITE.AsGuid()); } }
/// <summary> /// Handles the Click event of the lbSaveAccount 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 lbSaveAccount_Click( object sender, EventArgs e ) { var rockContext = new RockContext(); if ( string.IsNullOrWhiteSpace( TransactionCode ) ) { nbSaveAccount.Text = "Sorry, the account information cannot be saved as there's not a valid transaction code to reference"; nbSaveAccount.Visible = true; return; } if ( phCreateLogin.Visible ) { if ( string.IsNullOrWhiteSpace( txtUserName.Text ) || string.IsNullOrWhiteSpace( txtPassword.Text ) ) { nbSaveAccount.Title = "Missing Informaton"; nbSaveAccount.Text = "A username and password are required when saving an account"; nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; return; } if ( new UserLoginService( rockContext ).GetByUserName( txtUserName.Text ) != null ) { nbSaveAccount.Title = "Invalid Username"; nbSaveAccount.Text = "The selected Username is already being used. Please select a different Username"; nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; return; } if ( txtPasswordConfirm.Text != txtPassword.Text ) { nbSaveAccount.Title = "Invalid Password"; nbSaveAccount.Text = "The password and password confirmation do not match"; nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; return; } } if ( !string.IsNullOrWhiteSpace( txtSaveAccount.Text ) ) { GatewayComponent gateway = hfPaymentTab.Value == "ACH" ? _achGateway : _ccGateway; var ccCurrencyType = DefinedValueCache.Read( new Guid( Rock.SystemGuid.DefinedValue.CURRENCY_TYPE_CREDIT_CARD ) ); var achCurrencyType = DefinedValueCache.Read( new Guid( Rock.SystemGuid.DefinedValue.CURRENCY_TYPE_ACH ) ); string errorMessage = string.Empty; PersonAlias authorizedPersonAlias = null; string referenceNumber = string.Empty; int? currencyTypeValueId = hfPaymentTab.Value == "ACH" ? achCurrencyType.Id : ccCurrencyType.Id; if ( string.IsNullOrWhiteSpace( ScheduleId ) ) { var transaction = new FinancialTransactionService( rockContext ).GetByTransactionCode( TransactionCode ); if ( transaction != null && transaction.AuthorizedPersonAlias != null ) { authorizedPersonAlias = transaction.AuthorizedPersonAlias; referenceNumber = gateway.GetReferenceNumber( transaction, out errorMessage ); } } else { var scheduledTransaction = new FinancialScheduledTransactionService( rockContext ).GetByScheduleId( ScheduleId ); if ( scheduledTransaction != null ) { authorizedPersonAlias = scheduledTransaction.AuthorizedPersonAlias; referenceNumber = gateway.GetReferenceNumber( scheduledTransaction, out errorMessage ); } } if ( authorizedPersonAlias != null && authorizedPersonAlias.Person != null ) { if ( phCreateLogin.Visible ) { var user = UserLoginService.Create( rockContext, authorizedPersonAlias.Person, Rock.Model.AuthenticationServiceType.Internal, EntityTypeCache.Read( Rock.SystemGuid.EntityType.AUTHENTICATION_DATABASE.AsGuid() ).Id, txtUserName.Text, txtPassword.Text, false ); var mergeObjects = GlobalAttributesCache.GetMergeFields( null ); mergeObjects.Add( "ConfirmAccountUrl", RootPath + "ConfirmAccount" ); var personDictionary = authorizedPersonAlias.Person.ToLiquid() as Dictionary<string, object>; mergeObjects.Add( "Person", personDictionary ); mergeObjects.Add( "User", user ); var recipients = new List<Rock.Communication.RecipientData>(); recipients.Add( new Rock.Communication.RecipientData( authorizedPersonAlias.Person.Email, mergeObjects ) ); Rock.Communication.Email.Send( GetAttributeValue( "ConfirmAccountTemplate" ).AsGuid(), recipients, ResolveRockUrl( "~/" ), ResolveRockUrl( "~~/" ) ); } var paymentInfo = GetPaymentInfo(); if ( errorMessage.Any() ) { nbSaveAccount.Title = "Invalid Transaction"; nbSaveAccount.Text = "Sorry, the account information cannot be saved. " + errorMessage; nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; } else { if ( authorizedPersonAlias != null ) { var savedAccount = new FinancialPersonSavedAccount(); savedAccount.PersonAliasId = authorizedPersonAlias.Id; savedAccount.ReferenceNumber = referenceNumber; savedAccount.Name = txtSaveAccount.Text; savedAccount.MaskedAccountNumber = paymentInfo.MaskedNumber; savedAccount.TransactionCode = TransactionCode; savedAccount.GatewayEntityTypeId = gateway.TypeId; savedAccount.CurrencyTypeValueId = currencyTypeValueId; savedAccount.CreditCardTypeValueId = CreditCardTypeValueId; var savedAccountService = new FinancialPersonSavedAccountService( rockContext ); savedAccountService.Add( savedAccount ); rockContext.SaveChanges(); cbSaveAccount.Visible = false; txtSaveAccount.Visible = false; phCreateLogin.Visible = false; divSaveActions.Visible = false; nbSaveAccount.Title = "Success"; nbSaveAccount.Text = "The account has been saved for future use"; nbSaveAccount.NotificationBoxType = NotificationBoxType.Success; nbSaveAccount.Visible = true; } } } else { nbSaveAccount.Title = "Invalid Transaction"; nbSaveAccount.Text = "Sorry, the account information cannot be saved as there's not a valid transaction code to reference"; nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; } } else { nbSaveAccount.Title = "Missing Account Name"; nbSaveAccount.Text = "Please enter a name to use for this account"; nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; } }
/// <summary> /// Creates the saved account. /// </summary> /// <param name="parameters">The parameters.</param> /// <param name="paymentDetail">The payment detail.</param> /// <param name="financialGateway">The financial gateway.</param> /// <param name="person">The person.</param> /// <param name="rockContext">The rock context.</param> /// <returns></returns> private FinancialPersonSavedAccount CreateSavedAccount( PaymentParameters parameters, FinancialPaymentDetail paymentDetail, FinancialGateway financialGateway, Person person, RockContext rockContext) { var lastFour = paymentDetail.AccountNumberMasked.Substring(paymentDetail.AccountNumberMasked.Length - 4); var name = string.Empty; if ( parameters.AccountType.ToLower() != "credit" ) { if ( string.IsNullOrWhiteSpace( parameters.RoutingNumber ) ) { GenerateResponse( HttpStatusCode.BadRequest, "RoutingNumber is required for ACH transactions" ); return null; } if ( string.IsNullOrWhiteSpace( parameters.AccountNumber ) ) { GenerateResponse( HttpStatusCode.BadRequest, "AccountNumber is required" ); return null; } name = "Bank card ***" + lastFour; var bankAccountService = new FinancialPersonBankAccountService( rockContext ); var accountNumberSecured = FinancialPersonBankAccount.EncodeAccountNumber( parameters.RoutingNumber, parameters.AccountNumber ); var bankAccount = bankAccountService.Queryable().Where( a => a.AccountNumberSecured == accountNumberSecured && a.PersonAliasId == person.PrimaryAliasId.Value ).FirstOrDefault(); if ( bankAccount == null ) { bankAccount = new FinancialPersonBankAccount(); bankAccount.PersonAliasId = person.PrimaryAliasId.Value; bankAccount.AccountNumberMasked = paymentDetail.AccountNumberMasked; bankAccount.AccountNumberSecured = accountNumberSecured; bankAccountService.Add( bankAccount ); } } else { name = "Credit card ***" + lastFour; } var savedAccount = new FinancialPersonSavedAccount { PersonAliasId = person.PrimaryAliasId, FinancialGatewayId = financialGateway.Id, Name = name, FinancialPaymentDetailId = paymentDetail.Id }; new FinancialPersonSavedAccountService(rockContext).Add( savedAccount ); rockContext.SaveChanges(); return savedAccount; }
public SaveFinancialAccountFormResult SaveFinancialAccount(Guid gatewayGuid, [FromBody] SaveFinancialAccountFormArgs args) { // Validate the args if (args?.TransactionCode.IsNullOrWhiteSpace() != false) { return(new SaveFinancialAccountFormResult { Title = "Sorry", Detail = "The account information cannot be saved as there's not a valid transaction code to reference", IsSuccess = false }); } if (args.SavedAccountName.IsNullOrWhiteSpace()) { return(new SaveFinancialAccountFormResult { Title = "Missing Account Name", Detail = "Please enter a name to use for this account", IsSuccess = false }); } var currentPerson = GetPerson(); var isAnonymous = currentPerson == null; using (var rockContext = new RockContext()) { if (isAnonymous) { if (args.Username.IsNullOrWhiteSpace() || args.Password.IsNullOrWhiteSpace()) { return(new SaveFinancialAccountFormResult { Title = "Missing Information", Detail = "A username and password are required when saving an account", IsSuccess = false }); } var userLoginService = new UserLoginService(rockContext); if (userLoginService.GetByUserName(args.Username) != null) { return(new SaveFinancialAccountFormResult { Title = "Invalid Username", Detail = "The selected Username is already being used. Please select a different Username", IsSuccess = false }); } if (!UserLoginService.IsPasswordValid(args.Password)) { return(new SaveFinancialAccountFormResult { Title = "Invalid Password", Detail = UserLoginService.FriendlyPasswordRules(), IsSuccess = false }); } } // Load the gateway from the database var financialGatewayService = new FinancialGatewayService(rockContext); var financialGateway = financialGatewayService.Get(gatewayGuid); var gateway = financialGateway?.GetGatewayComponent(); if (gateway is null) { return(new SaveFinancialAccountFormResult { Title = "Invalid Gateway", Detail = "Sorry, the financial gateway information is not valid.", IsSuccess = false }); } // Load the transaction from the database var financialTransactionService = new FinancialTransactionService(rockContext); var transaction = financialTransactionService.GetByTransactionCode(financialGateway.Id, args.TransactionCode); var transactionPersonAlias = transaction?.AuthorizedPersonAlias; var transactionPerson = transactionPersonAlias?.Person; var paymentDetail = transaction?.FinancialPaymentDetail; if (transactionPerson is null || paymentDetail is null) { return(new SaveFinancialAccountFormResult { Title = "Invalid Transaction", Detail = "Sorry, the account information cannot be saved as there's not a valid transaction to reference", IsSuccess = false }); } // Create the login if needed if (isAnonymous) { var user = UserLoginService.Create( rockContext, transactionPerson, AuthenticationServiceType.Internal, EntityTypeCache.Get(SystemGuid.EntityType.AUTHENTICATION_DATABASE.AsGuid()).Id, args.Username, args.Password, false); var mergeFields = Lava.LavaHelper.GetCommonMergeFields(null, currentPerson); // TODO mergeFields.Add( "ConfirmAccountUrl", RootPath + "ConfirmAccount" ); mergeFields.Add("Person", transactionPerson); mergeFields.Add("User", user); var emailMessage = new RockEmailMessage(SystemGuid.SystemCommunication.SECURITY_CONFIRM_ACCOUNT.AsGuid()); emailMessage.AddRecipient(new RockEmailMessageRecipient(transactionPerson, mergeFields)); // TODO emailMessage.AppRoot = ResolveRockUrl( "~/" ); // TODO emailMessage.ThemeRoot = ResolveRockUrl( "~~/" ); emailMessage.CreateCommunicationRecord = false; emailMessage.Send(); } var savedAccount = new FinancialPersonSavedAccount { PersonAliasId = transactionPersonAlias.Id, ReferenceNumber = args.TransactionCode, GatewayPersonIdentifier = args.GatewayPersonIdentifier, Name = args.SavedAccountName, TransactionCode = args.TransactionCode, FinancialGatewayId = financialGateway.Id, FinancialPaymentDetail = new FinancialPaymentDetail { AccountNumberMasked = paymentDetail.AccountNumberMasked, CurrencyTypeValueId = paymentDetail.CurrencyTypeValueId, CreditCardTypeValueId = paymentDetail.CreditCardTypeValueId, NameOnCard = paymentDetail.NameOnCard, ExpirationMonth = paymentDetail.ExpirationMonth, ExpirationYear = paymentDetail.ExpirationYear, BillingLocationId = paymentDetail.BillingLocationId } }; var financialPersonSavedAccountService = new FinancialPersonSavedAccountService(rockContext); financialPersonSavedAccountService.Add(savedAccount); rockContext.SaveChanges(); return(new SaveFinancialAccountFormResult { Title = "Success", Detail = "The account has been saved for future use", IsSuccess = true }); } }
/// <summary> /// Handles the Click event of the lbSaveAccount 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 lbSaveAccount_Click( object sender, EventArgs e ) { if ( string.IsNullOrWhiteSpace( TransactionCode ) ) { nbSaveAccount.Text = "Sorry, the account information cannot be saved as there's not a valid transaction code to reference"; nbSaveAccount.Visible = true; return; } using ( var rockContext = new RockContext() ) { if ( phCreateLogin.Visible ) { if ( string.IsNullOrWhiteSpace( txtUserName.Text ) || string.IsNullOrWhiteSpace( txtPassword.Text ) ) { nbSaveAccount.Title = "Missing Informaton"; nbSaveAccount.Text = "A username and password are required when saving an account"; nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; return; } if ( new UserLoginService( rockContext ).GetByUserName( txtUserName.Text ) != null ) { nbSaveAccount.Title = "Invalid Username"; nbSaveAccount.Text = "The selected Username is already being used. Please select a different Username"; nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; return; } if ( !UserLoginService.IsPasswordValid( txtPassword.Text ) ) { nbSaveAccount.Title = string.Empty; nbSaveAccount.Text = UserLoginService.FriendlyPasswordRules(); nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; return; } if ( txtPasswordConfirm.Text != txtPassword.Text ) { nbSaveAccount.Title = "Invalid Password"; nbSaveAccount.Text = "The password and password confirmation do not match"; nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; return; } } if ( !string.IsNullOrWhiteSpace( txtSaveAccount.Text ) ) { GatewayComponent gateway = null; if ( RegistrationTemplate != null && RegistrationTemplate.FinancialGateway != null ) { gateway = RegistrationTemplate.FinancialGateway.GetGatewayComponent(); } if ( gateway != null ) { var ccCurrencyType = DefinedValueCache.Read( new Guid( Rock.SystemGuid.DefinedValue.CURRENCY_TYPE_CREDIT_CARD ) ); string errorMessage = string.Empty; PersonAlias authorizedPersonAlias = null; string referenceNumber = string.Empty; int? currencyTypeValueId = ccCurrencyType.Id; var transaction = new FinancialTransactionService( rockContext ).GetByTransactionCode( TransactionCode ); if ( transaction != null && transaction.AuthorizedPersonAlias != null ) { authorizedPersonAlias = transaction.AuthorizedPersonAlias; if ( transaction.FinancialGateway != null ) { transaction.FinancialGateway.LoadAttributes( rockContext ); } referenceNumber = gateway.GetReferenceNumber( transaction, out errorMessage ); } if ( authorizedPersonAlias != null && authorizedPersonAlias.Person != null ) { if ( phCreateLogin.Visible ) { var user = UserLoginService.Create( rockContext, authorizedPersonAlias.Person, Rock.Model.AuthenticationServiceType.Internal, EntityTypeCache.Read( Rock.SystemGuid.EntityType.AUTHENTICATION_DATABASE.AsGuid() ).Id, txtUserName.Text, txtPassword.Text, false ); var mergeObjects = Rock.Lava.LavaHelper.GetCommonMergeFields(RockPage); mergeObjects.Add( "ConfirmAccountUrl", RootPath + "ConfirmAccount" ); var personDictionary = authorizedPersonAlias.Person.ToLiquid() as Dictionary<string, object>; mergeObjects.Add( "Person", personDictionary ); mergeObjects.Add( "User", user ); var recipients = new List<Rock.Communication.RecipientData>(); recipients.Add( new Rock.Communication.RecipientData( authorizedPersonAlias.Person.Email, mergeObjects ) ); try { Rock.Communication.Email.Send( GetAttributeValue( "ConfirmAccountTemplate" ).AsGuid(), recipients, ResolveRockUrl( "~/" ), ResolveRockUrl( "~~/" ), false ); } catch (Exception ex) { ExceptionLogService.LogException( ex, Context, this.RockPage.PageId, this.RockPage.Site.Id, CurrentPersonAlias ); } } var paymentInfo = GetCCPaymentInfo( gateway ); if ( errorMessage.Any() ) { nbSaveAccount.Title = "Invalid Transaction"; nbSaveAccount.Text = "Sorry, the account information cannot be saved. " + errorMessage; nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; } else { if ( authorizedPersonAlias != null ) { var savedAccount = new FinancialPersonSavedAccount(); savedAccount.PersonAliasId = authorizedPersonAlias.Id; savedAccount.ReferenceNumber = referenceNumber; savedAccount.Name = txtSaveAccount.Text; savedAccount.TransactionCode = TransactionCode; savedAccount.FinancialGatewayId = RegistrationTemplate.FinancialGateway.Id; savedAccount.FinancialPaymentDetail = new FinancialPaymentDetail(); savedAccount.FinancialPaymentDetail.SetFromPaymentInfo( paymentInfo, gateway, rockContext ); var savedAccountService = new FinancialPersonSavedAccountService( rockContext ); savedAccountService.Add( savedAccount ); rockContext.SaveChanges(); cbSaveAccount.Visible = false; txtSaveAccount.Visible = false; phCreateLogin.Visible = false; divSaveActions.Visible = false; nbSaveAccount.Title = "Success"; nbSaveAccount.Text = "The account has been saved for future use"; nbSaveAccount.NotificationBoxType = NotificationBoxType.Success; nbSaveAccount.Visible = true; } } } else { nbSaveAccount.Title = "Invalid Transaction"; nbSaveAccount.Text = "Sorry, the account information cannot be saved as there's not a valid transaction code to reference."; nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; } } else { nbSaveAccount.Title = "Invalid Gateway"; nbSaveAccount.Text = "Sorry, the financial gateway information for this type of transaction is not valid."; nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; } } else { nbSaveAccount.Title = "Missing Account Name"; nbSaveAccount.Text = "Please enter a name to use for this account."; nbSaveAccount.NotificationBoxType = NotificationBoxType.Danger; nbSaveAccount.Visible = true; } } }
public HttpResponseMessage Give([FromBody] GiveParameters giveParameters) { var rockContext = new RockContext(); try { rockContext.WrapTransaction(() => { int?locationId = null; FinancialPaymentDetail paymentDetail = null; PaymentInfo paymentInfo = null; FinancialPersonSavedAccount savedAccount = null; bool newSavedAccount = true; var gatewayComponent = GatewayContainer.GetComponent(gatewayName); if (gatewayComponent == null) { GenerateResponse(HttpStatusCode.InternalServerError, "There was a problem creating the gateway component"); } var financialGateway = new FinancialGatewayService(rockContext).Queryable().FirstOrDefault(g => g.EntityTypeId == gatewayComponent.EntityType.Id); if (financialGateway == null) { GenerateResponse(HttpStatusCode.InternalServerError, "There was a problem creating the financial gateway"); } var totalAmount = CalculateTotalAmount(giveParameters, rockContext); var person = GetExistingPerson(giveParameters.PersonId, rockContext); if (person == null) { // New person locationId = CreateLocation(giveParameters, rockContext); person = CreatePerson(giveParameters, locationId.Value, rockContext); } else { // Existing person savedAccount = GetExistingSavedAccount(giveParameters, person, rockContext); if (savedAccount != null) { locationId = savedAccount.FinancialPaymentDetail.BillingLocationId; newSavedAccount = false; } } if (!locationId.HasValue) { locationId = CreateLocation(giveParameters, rockContext); } if (savedAccount == null) { paymentDetail = CreatePaymentDetail(giveParameters, person, locationId.Value, rockContext); savedAccount = CreateSavedAccount(giveParameters, paymentDetail, financialGateway, person, rockContext); newSavedAccount = true; paymentInfo = GetPaymentInfo(giveParameters, person, rockContext, totalAmount.Value, paymentDetail); } else { paymentDetail = savedAccount.FinancialPaymentDetail; locationId = paymentDetail.BillingLocationId; paymentInfo = savedAccount.GetReferencePayment(); UpdatePaymentInfoForSavedAccount(giveParameters, paymentInfo, person, rockContext, locationId.Value, totalAmount.Value); } SaveLocationToFamilyIfNone(person, locationId.Value, rockContext); string errorMessage; var transaction = gatewayComponent.Charge(financialGateway, paymentInfo, out errorMessage); if (transaction == null || !string.IsNullOrWhiteSpace(errorMessage)) { GenerateResponse(HttpStatusCode.InternalServerError, errorMessage ?? "The gateway had a problem and/or did not create a transaction as expected"); } transaction.FinancialPaymentDetail = null; transaction.SourceTypeValueId = DefinedValueCache.Read(new Guid(Rock.SystemGuid.DefinedValue.FINANCIAL_SOURCE_TYPE_WEBSITE)).Id; transaction.TransactionDateTime = RockDateTime.Now; transaction.AuthorizedPersonAliasId = person.PrimaryAliasId; transaction.AuthorizedPersonAlias = person.PrimaryAlias; transaction.FinancialGatewayId = financialGateway.Id; transaction.TransactionTypeValueId = DefinedValueCache.Read(new Guid(Rock.SystemGuid.DefinedValue.TRANSACTION_TYPE_CONTRIBUTION)).Id; transaction.FinancialPaymentDetailId = paymentDetail.Id; savedAccount.TransactionCode = transaction.TransactionCode; foreach (var accountAmount in giveParameters.AmountDetails) { transaction.TransactionDetails.Add(new FinancialTransactionDetail() { Amount = accountAmount.Amount, AccountId = accountAmount.TargetAccountId }); } new FinancialTransactionService(rockContext).Add(transaction); rockContext.SaveChanges(); if (newSavedAccount) { var newReferenceNumber = gatewayComponent.GetReferenceNumber(transaction, out errorMessage); savedAccount.ReferenceNumber = newReferenceNumber; } rockContext.SaveChanges(); }); } catch (HttpResponseException exception) { return(exception.Response); } catch (Exception exception) { var response = new HttpResponseMessage(HttpStatusCode.InternalServerError); response.Content = new StringContent(exception.Message); return(response); } return(new HttpResponseMessage(HttpStatusCode.NoContent)); }