Beispiel #1
0
        public async Task <IActionResult> Process()
        {
            bool cookieExists = this.Request.Cookies.TryGetValue(PaymentDataCookie, out var data);

            if (!cookieExists)
            {
                return(this.RedirectToHome());
            }

            try
            {
                dynamic paymentRequest =
                    DirectPaymentsHelper.ParsePaymentRequest(data, this.bankConfiguration.CentralApiPublicKey);
                if (paymentRequest == null)
                {
                    return(this.BadRequest());
                }

                dynamic paymentInfo = DirectPaymentsHelper.GetPaymentInfo(paymentRequest);

                var userId = await this.userService.GetUserIdByUsernameAsync(this.User.Identity.Name);

                var model = new PaymentConfirmBindingModel
                {
                    Amount                         = paymentInfo.Amount,
                    Description                    = paymentInfo.Description,
                    DestinationBankName            = paymentInfo.DestinationBankName,
                    DestinationBankCountry         = paymentInfo.DestinationBankCountry,
                    DestinationBankAccountUniqueId = paymentInfo.DestinationBankAccountUniqueId,
                    RecipientName                  = paymentInfo.RecipientName,
                    OwnAccounts                    = await this.GetAllAccountsAsync(userId),
                    DataHash                       = DirectPaymentsHelper.Sha256Hash(data)
                };

                return(this.View(model));
            }
            catch
            {
                return(this.BadRequest());
            }
        }
Beispiel #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));
            }
        }