Exemplo n.º 1
0
        /// <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;
            }

            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().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 ) )
            {
                using ( new UnitOfWorkScope() )
                {
                    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;

                    Person authorizedPerson = null;
                    string referenceNumber = string.Empty;
                    int? currencyTypeValueId = hfPaymentTab.Value == "ACH" ? achCurrencyType.Id : ccCurrencyType.Id;

                    if ( string.IsNullOrWhiteSpace( ScheduleId ) )
                    {
                        var transaction = new FinancialTransactionService().GetByTransactionCode( TransactionCode );
                        if ( transaction != null )
                        {
                            authorizedPerson = transaction.AuthorizedPerson;
                            referenceNumber = gateway.GetReferenceNumber( transaction, out errorMessage );
                        }
                    }
                    else
                    {
                        var scheduledTransaction = new FinancialScheduledTransactionService().GetByScheduleId( ScheduleId );
                        if ( scheduledTransaction != null )
                        {
                            authorizedPerson = scheduledTransaction.AuthorizedPerson;
                            referenceNumber = gateway.GetReferenceNumber( scheduledTransaction, out errorMessage );
                        }
                    }

                    if ( authorizedPerson != null )
                    {
                        if ( phCreateLogin.Visible )
                        {
                            var userLoginService = new Rock.Model.UserLoginService();
                            var user = userLoginService.Create( authorizedPerson, Rock.Model.AuthenticationServiceType.Internal, 
                                EntityTypeCache.Read(Rock.SystemGuid.EntityType.AUTHENTICATION_DATABASE.AsGuid()).Id,
                                txtUserName.Text, txtPassword.Text, false, CurrentPersonId );

                            var mergeObjects = new Dictionary<string, object>();
                            mergeObjects.Add( "ConfirmAccountUrl", RootPath + "ConfirmAccount" );

                            var personDictionary = authorizedPerson.ToDictionary();
                            mergeObjects.Add( "Person", personDictionary );

                            mergeObjects.Add( "User", user.ToDictionary() );

                            var recipients = new Dictionary<string, Dictionary<string, object>>();
                            recipients.Add( authorizedPerson.Email, mergeObjects );

                            var email = new Rock.Communication.Email( GetAttributeValue( "ConfirmAccountTemplate" ) );
                            email.Send( recipients );
                        }

                        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
                        {
                            var savedAccount = new FinancialPersonSavedAccount();
                            savedAccount.PersonId = authorizedPerson.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();
                            savedAccountService.Add( savedAccount, CurrentPersonId );
                            savedAccountService.Save( savedAccount, CurrentPersonId );

                            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;
            }
        }