From 1f7aa8aebdafdf051ca74572e0b780ca35445000 Mon Sep 17 00:00:00 2001 From: Peyman <46522754+Marchosiax@users.noreply.github.com> Date: Sun, 19 Dec 2021 16:37:15 +0330 Subject: [PATCH 1/2] close #165: Payment gateway implemented (#166) --- Deployment/nginx.conf | 13 ++++ .../opex/wallet/app/config/SecurityConfig.kt | 1 + .../controller/PaymentGatewayController.kt | 72 +++++++++++++++++++ .../opex/wallet/app/dto/PaymentCurrency.kt | 5 ++ .../wallet/app/dto/PaymentDepositRequest.kt | 9 +++ .../wallet/app/dto/PaymentDepositResponse.kt | 3 + .../src/main/resources/schema.sql | 2 +- 7 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 wallet/wallet-app/src/main/kotlin/co/nilin/opex/wallet/app/controller/PaymentGatewayController.kt create mode 100644 wallet/wallet-app/src/main/kotlin/co/nilin/opex/wallet/app/dto/PaymentCurrency.kt create mode 100644 wallet/wallet-app/src/main/kotlin/co/nilin/opex/wallet/app/dto/PaymentDepositRequest.kt create mode 100644 wallet/wallet-app/src/main/kotlin/co/nilin/opex/wallet/app/dto/PaymentDepositResponse.kt diff --git a/Deployment/nginx.conf b/Deployment/nginx.conf index 00e39ffc4..dd8491f4a 100644 --- a/Deployment/nginx.conf +++ b/Deployment/nginx.conf @@ -31,6 +31,10 @@ http { server websocket:8097; } + upstream docker-payment { + server payment:9995; + } + proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; @@ -59,6 +63,10 @@ http { return 403; } + location /wallet/payment/internal { + return 403; + } + location /wallet { proxy_pass http://docker-wallet; rewrite ^/wallet(.*)$ $1 break; @@ -74,6 +82,11 @@ http { rewrite ^/storage/(.*)$ /$1 break; } + location /payment { + proxy_pass http://docker-payment; + rewrite ^/payment(.*)$ $1 break; + } + location /stream { proxy_pass http://docker-websocket; # WS config diff --git a/wallet/wallet-app/src/main/kotlin/co/nilin/opex/wallet/app/config/SecurityConfig.kt b/wallet/wallet-app/src/main/kotlin/co/nilin/opex/wallet/app/config/SecurityConfig.kt index 0273e6734..a92bc17ab 100644 --- a/wallet/wallet-app/src/main/kotlin/co/nilin/opex/wallet/app/config/SecurityConfig.kt +++ b/wallet/wallet-app/src/main/kotlin/co/nilin/opex/wallet/app/config/SecurityConfig.kt @@ -37,6 +37,7 @@ class SecurityConfig(private val webClient: WebClient) { AuthorizationDecision(granted) } } + .pathMatchers("/payment/internal/**").permitAll() .pathMatchers("/**").permitAll() .anyExchange().authenticated() .and() diff --git a/wallet/wallet-app/src/main/kotlin/co/nilin/opex/wallet/app/controller/PaymentGatewayController.kt b/wallet/wallet-app/src/main/kotlin/co/nilin/opex/wallet/app/controller/PaymentGatewayController.kt new file mode 100644 index 000000000..8676c15c6 --- /dev/null +++ b/wallet/wallet-app/src/main/kotlin/co/nilin/opex/wallet/app/controller/PaymentGatewayController.kt @@ -0,0 +1,72 @@ +package co.nilin.opex.wallet.app.controller + +import co.nilin.opex.utility.error.data.OpexError +import co.nilin.opex.utility.error.data.OpexException +import co.nilin.opex.wallet.app.dto.PaymentCurrency +import co.nilin.opex.wallet.app.dto.PaymentDepositRequest +import co.nilin.opex.wallet.app.dto.PaymentDepositResponse +import co.nilin.opex.wallet.core.inout.TransferCommand +import co.nilin.opex.wallet.core.model.Amount +import co.nilin.opex.wallet.core.service.TransferService +import co.nilin.opex.wallet.core.spi.CurrencyService +import co.nilin.opex.wallet.core.spi.WalletManager +import co.nilin.opex.wallet.core.spi.WalletOwnerManager +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController +import java.math.BigDecimal + +@RestController +@RequestMapping("/payment") +class PaymentGatewayController( + val transferService: TransferService, + val currencyService: CurrencyService, + val walletManager: WalletManager, + val walletOwnerManager: WalletOwnerManager +) { + + @PostMapping("/internal/deposit") + suspend fun paymentDeposit(@RequestBody request: PaymentDepositRequest): PaymentDepositResponse { + val systemUuid = "1" + val receiverWalletType = "main" + val convertedAmount = when (request.currency) { + PaymentCurrency.RIALS -> (request.amount / 10).toLong() + PaymentCurrency.TOMAN -> request.amount.toLong() + } + + val currency = currencyService.getCurrency("IRT") ?: throw OpexException(OpexError.CurrencyNotFound) + val sourceOwner = walletOwnerManager.findWalletOwner(systemUuid) + ?: throw OpexException(OpexError.WalletOwnerNotFound) + val sourceWallet = walletManager.findWalletByOwnerAndCurrencyAndType(sourceOwner, "main", currency) + ?: walletManager.createWallet(sourceOwner, Amount(currency, BigDecimal.ZERO), currency, "main") + + val receiverOwner = walletOwnerManager.findWalletOwner(request.userId) + ?: walletOwnerManager.createWalletOwner(request.userId, "not set", "") + + val receiverWallet = walletManager.findWalletByOwnerAndCurrencyAndType( + receiverOwner, + receiverWalletType, + currency + ) ?: walletManager.createWallet( + receiverOwner, + Amount(currency, BigDecimal.ZERO), + currency, + receiverWalletType + ) + + val command = transferService.transfer( + TransferCommand( + sourceWallet, + receiverWallet, + Amount(sourceWallet.currency(), convertedAmount.toBigDecimal()), + request.description, + request.reference, + emptyMap() + ) + ) + + return PaymentDepositResponse(true) + } + +} \ No newline at end of file diff --git a/wallet/wallet-app/src/main/kotlin/co/nilin/opex/wallet/app/dto/PaymentCurrency.kt b/wallet/wallet-app/src/main/kotlin/co/nilin/opex/wallet/app/dto/PaymentCurrency.kt new file mode 100644 index 000000000..91f76f21c --- /dev/null +++ b/wallet/wallet-app/src/main/kotlin/co/nilin/opex/wallet/app/dto/PaymentCurrency.kt @@ -0,0 +1,5 @@ +package co.nilin.opex.wallet.app.dto + +enum class PaymentCurrency { + RIALS, TOMAN +} \ No newline at end of file diff --git a/wallet/wallet-app/src/main/kotlin/co/nilin/opex/wallet/app/dto/PaymentDepositRequest.kt b/wallet/wallet-app/src/main/kotlin/co/nilin/opex/wallet/app/dto/PaymentDepositRequest.kt new file mode 100644 index 000000000..0b9b871a0 --- /dev/null +++ b/wallet/wallet-app/src/main/kotlin/co/nilin/opex/wallet/app/dto/PaymentDepositRequest.kt @@ -0,0 +1,9 @@ +package co.nilin.opex.wallet.app.dto + +data class PaymentDepositRequest( + val userId: String, // user uuid + val amount: Double, + val currency: PaymentCurrency, + val reference: String, + val description: String? +) \ No newline at end of file diff --git a/wallet/wallet-app/src/main/kotlin/co/nilin/opex/wallet/app/dto/PaymentDepositResponse.kt b/wallet/wallet-app/src/main/kotlin/co/nilin/opex/wallet/app/dto/PaymentDepositResponse.kt new file mode 100644 index 000000000..61438d55a --- /dev/null +++ b/wallet/wallet-app/src/main/kotlin/co/nilin/opex/wallet/app/dto/PaymentDepositResponse.kt @@ -0,0 +1,3 @@ +package co.nilin.opex.wallet.app.dto + +data class PaymentDepositResponse(val success: Boolean) \ No newline at end of file diff --git a/wallet/wallet-ports/wallet-persister-postgres/src/main/resources/schema.sql b/wallet/wallet-ports/wallet-persister-postgres/src/main/resources/schema.sql index 019fef299..a2750ca74 100644 --- a/wallet/wallet-ports/wallet-persister-postgres/src/main/resources/schema.sql +++ b/wallet/wallet-ports/wallet-persister-postgres/src/main/resources/schema.sql @@ -37,7 +37,7 @@ CREATE TABLE IF NOT EXISTS transaction ( source_amount DECIMAL NOT NULL, dest_amount DECIMAL NOT NULL, description TEXT, - transfer_ref TEXT, + transfer_ref TEXT UNIQUE, transaction_date TIMESTAMP NOT NULL DEFAULT CURRENT_DATE ); From 7441d43322655996b204f4fca6904354ae9db7aa Mon Sep 17 00:00:00 2001 From: Peyman Date: Sun, 19 Dec 2021 17:52:47 +0330 Subject: [PATCH 2/2] remove payment from enginx config --- Deployment/nginx.conf | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Deployment/nginx.conf b/Deployment/nginx.conf index dd8491f4a..ef1e81ebe 100644 --- a/Deployment/nginx.conf +++ b/Deployment/nginx.conf @@ -31,10 +31,6 @@ http { server websocket:8097; } - upstream docker-payment { - server payment:9995; - } - proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; @@ -82,11 +78,6 @@ http { rewrite ^/storage/(.*)$ /$1 break; } - location /payment { - proxy_pass http://docker-payment; - rewrite ^/payment(.*)$ $1 break; - } - location /stream { proxy_pass http://docker-websocket; # WS config