Пример #1
0
        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
                });
            }
        }