Example #1
0
        public async Task <GlobalTransferResult> TransferMoneyAsync(GlobalTransferDto model)
        {
            if (!ValidationUtil.IsObjectValid(model))
            {
                return(GlobalTransferResult.GeneralFailure);
            }

            var account = await this.bankAccountService
                          .GetByIdAsync <BankAccountConciseServiceModel>(model.SourceAccountId);

            // check if account exists and recipient name is accurate
            if (account == null)
            {
                return(GlobalTransferResult.GeneralFailure);
            }

            // verify there is enough money in the account
            if (account.Balance < model.Amount)
            {
                return(GlobalTransferResult.InsufficientFunds);
            }

            // contact the CentralApi to execute the transfer
            var submitDto = this.mapper.Map <CentralApiSubmitTransferDto>(model);

            submitDto.SenderName            = account.UserFullName;
            submitDto.SenderAccountUniqueId = account.UniqueId;

            bool remoteSuccess = await this.ContactCentralApiAsync(submitDto);

            if (!remoteSuccess)
            {
                return(GlobalTransferResult.GeneralFailure);
            }

            // remove money from source account
            var serviceModel = new MoneyTransferCreateServiceModel
            {
                Amount      = -model.Amount,
                Source      = account.UniqueId,
                Description = model.Description,
                AccountId   = account.Id,
                DestinationBankAccountUniqueId = model.DestinationBankAccountUniqueId,
                SenderName      = account.UserFullName,
                RecipientName   = model.RecipientName,
                ReferenceNumber = submitDto.ReferenceNumber
            };

            bool success = await this.moneyTransferService.CreateMoneyTransferAsync(serviceModel);

            return(!success ? GlobalTransferResult.GeneralFailure : GlobalTransferResult.Succeeded);
        }
Example #2
0
        public async Task <IActionResult> PayAsync(PaymentConfirmBindingModel model)
        {
            bool cookieExists = this.Request.Cookies.TryGetValue(PaymentDataCookie, out var data);

            if (!this.ModelState.IsValid ||
                !cookieExists ||
                model.DataHash != DirectPaymentsHelper.Sha256Hash(data))
            {
                return(this.PaymentFailed(NotificationMessages.PaymentStateInvalid));
            }

            var account =
                await this.bankAccountService.GetByIdAsync <BankAccountDetailsServiceModel>(model.AccountId);

            if (account == null || account.UserUserName != this.User.Identity.Name)
            {
                return(this.Forbid());
            }

            try
            {
                // read and validate payment data
                dynamic paymentRequest =
                    DirectPaymentsHelper.ParsePaymentRequest(data, this.bankConfiguration.CentralApiPublicKey);

                if (paymentRequest == null)
                {
                    return(this.PaymentFailed(NotificationMessages.PaymentStateInvalid));
                }

                dynamic paymentInfo = DirectPaymentsHelper.GetPaymentInfo(paymentRequest);

                string returnUrl = paymentRequest.ReturnUrl;

                // transfer money to destination account
                var serviceModel = new GlobalTransferDto
                {
                    Amount                         = paymentInfo.Amount,
                    Description                    = paymentInfo.Description,
                    DestinationBankName            = paymentInfo.DestinationBankName,
                    DestinationBankCountry         = paymentInfo.DestinationBankCountry,
                    DestinationBankSwiftCode       = paymentInfo.DestinationBankSwiftCode,
                    DestinationBankAccountUniqueId = paymentInfo.DestinationBankAccountUniqueId,
                    RecipientName                  = paymentInfo.RecipientName,
                    SourceAccountId                = model.AccountId
                };

                var result = await this.globalTransferHelper.TransferMoneyAsync(serviceModel);

                if (result != GlobalTransferResult.Succeeded)
                {
                    return(this.PaymentFailed(result == GlobalTransferResult.InsufficientFunds
                        ? NotificationMessages.InsufficientFunds
                        : NotificationMessages.TryAgainLaterError));
                }

                // delete cookie to prevent accidental duplicate payments
                this.Response.Cookies.Delete(PaymentDataCookie);

                // return signed success response
                var response = DirectPaymentsHelper.GenerateSuccessResponse(paymentRequest,
                                                                            this.bankConfiguration.Key);

                return(this.Ok(new
                {
                    success = true,
                    returnUrl = HttpUtility.HtmlEncode(returnUrl),
                    data = response
                }));
            }
            catch
            {
                return(this.PaymentFailed(NotificationMessages.PaymentStateInvalid));
            }
        }