From 4e13ea546627607d68e5450d087cd7a0f1507470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iwo=20Stanis=C5=82awski?= Date: Mon, 20 May 2024 21:01:14 +0200 Subject: [PATCH] HotFix --- Back/micro-services/.idea/dataSources.xml | 26 +++++++++++++++++++ .../.idea/sonarlint/issuestore/index.pb | 4 ++- .../sonarlint/securityhotspotstore/index.pb | 4 ++- .../resources/configurations/transaction.yml | 12 ++++----- .../paypal/controller/PaypalController.java | 21 ++++++++++----- .../example/paypal/dto/AddTransactionDto.java | 1 + .../example/paypal/service/PaypalService.java | 11 +++++--- .../transaction/dto/AddTransactionDto.java | 2 +- .../transaction/model/Transaction.java | 5 ++-- .../transaction/model/TypeOfTransaction.java | 1 - .../service/TransactionService.java | 12 +++++---- 11 files changed, 73 insertions(+), 26 deletions(-) diff --git a/Back/micro-services/.idea/dataSources.xml b/Back/micro-services/.idea/dataSources.xml index 517ca2b9..d71b80c6 100644 --- a/Back/micro-services/.idea/dataSources.xml +++ b/Back/micro-services/.idea/dataSources.xml @@ -66,5 +66,31 @@ $ProjectFileDir$ + + postgresql + true + true + org.postgresql.Driver + jdbc:postgresql://localhost:5436/currency_accounts + + + + + + $ProjectFileDir$ + + + postgresql + true + true + org.postgresql.Driver + jdbc:postgresql://localhost:5434/transaction + + + + + + $ProjectFileDir$ + \ No newline at end of file diff --git a/Back/micro-services/.idea/sonarlint/issuestore/index.pb b/Back/micro-services/.idea/sonarlint/issuestore/index.pb index 9d3c77de..4840454f 100644 --- a/Back/micro-services/.idea/sonarlint/issuestore/index.pb +++ b/Back/micro-services/.idea/sonarlint/issuestore/index.pb @@ -206,4 +206,6 @@ Ntransaction/src/main/java/org/example/transaction/model/TypeOfTransaction.java } Mtransaction/src/main/java/org/example/transaction/TransactionApplication.java,3\3\339e7dbfe19ecf78203a9fe162e21778238d18ce o -?paypal/src/main/java/org/example/paypal/service/ApiService.java,3\f\3fcf963ec75898e42aa98a16c67b8f84bbdb2d66 \ No newline at end of file +?paypal/src/main/java/org/example/paypal/service/ApiService.java,3\f\3fcf963ec75898e42aa98a16c67b8f84bbdb2d66 +| +Ltransaction/src/main/java/org/example/transaction/dto/GetTransactionDto.java,f\6\f62de79d4a0bdf68e29b117df4e85541886dcd7b \ No newline at end of file diff --git a/Back/micro-services/.idea/sonarlint/securityhotspotstore/index.pb b/Back/micro-services/.idea/sonarlint/securityhotspotstore/index.pb index 9d3c77de..4840454f 100644 --- a/Back/micro-services/.idea/sonarlint/securityhotspotstore/index.pb +++ b/Back/micro-services/.idea/sonarlint/securityhotspotstore/index.pb @@ -206,4 +206,6 @@ Ntransaction/src/main/java/org/example/transaction/model/TypeOfTransaction.java } Mtransaction/src/main/java/org/example/transaction/TransactionApplication.java,3\3\339e7dbfe19ecf78203a9fe162e21778238d18ce o -?paypal/src/main/java/org/example/paypal/service/ApiService.java,3\f\3fcf963ec75898e42aa98a16c67b8f84bbdb2d66 \ No newline at end of file +?paypal/src/main/java/org/example/paypal/service/ApiService.java,3\f\3fcf963ec75898e42aa98a16c67b8f84bbdb2d66 +| +Ltransaction/src/main/java/org/example/transaction/dto/GetTransactionDto.java,f\6\f62de79d4a0bdf68e29b117df4e85541886dcd7b \ No newline at end of file diff --git a/Back/micro-services/config-server/src/main/resources/configurations/transaction.yml b/Back/micro-services/config-server/src/main/resources/configurations/transaction.yml index 5cfef3ff..02d339ec 100644 --- a/Back/micro-services/config-server/src/main/resources/configurations/transaction.yml +++ b/Back/micro-services/config-server/src/main/resources/configurations/transaction.yml @@ -13,13 +13,13 @@ spring: datasource: driver-class-name: org.postgresql.Driver # Lokalnie przez dockera - # url: jdbc:postgresql://localhost:5434/transaction - # username: postgres - # password: admin - # Przez Azure - url: jdbc:postgresql://postgres-transaction.postgres.database.azure.com:5432/postgres + url: jdbc:postgresql://localhost:5434/transaction username: postgres - password: admin123@ + password: admin + # Przez Azure +# url: jdbc:postgresql://postgres-transaction.postgres.database.azure.com:5432/postgres +# username: postgres +# password: admin123@ eureka: instance: hostname: localhost diff --git a/Back/micro-services/paypal/src/main/java/org/example/paypal/controller/PaypalController.java b/Back/micro-services/paypal/src/main/java/org/example/paypal/controller/PaypalController.java index 0701caec..03f2f4be 100644 --- a/Back/micro-services/paypal/src/main/java/org/example/paypal/controller/PaypalController.java +++ b/Back/micro-services/paypal/src/main/java/org/example/paypal/controller/PaypalController.java @@ -66,13 +66,15 @@ public ResponseEntity createPayment(@RequestBody PaymentPaypalDto HttpHeaders headers = new HttpHeaders(); headers.add("Authorization", "Bearer " + userDto.getToken()); String successUrl = paymentPaypalDto.successUrl() + "?userId=" + userDto.getId() + "&JWTtoken=" + userDto.getToken() + "¤cyId=" + currency.getId() + "&total=" + paymentPaypalDto.total(); - logger.info("Currency code: " + currency.getCode()); ApiService apiService = new ApiService(); String json = apiService.callExternalApi(currency.getCode()); JSONObject jsonObject = new JSONObject(json); JSONObject ratesObject = jsonObject.getJSONArray("rates").getJSONObject(0); double mid = ratesObject.getDouble("mid"); BigDecimal targetAmount = BigDecimal.valueOf(mid).multiply(BigDecimal.valueOf(paymentPaypalDto.total())); + logger.error("PLN: " + targetAmount); + logger.error("obca waluta: w pln" + mid); + logger.error("obca waluta: " + paymentPaypalDto.total()); Payment payment = paypalService.createPayment( targetAmount.doubleValue(), "PLN", @@ -117,17 +119,23 @@ public ResponseEntity createPayout(@RequestBody PayoutRequestPaypal } CurrencyDto currencyDto = currency.get(); + ApiService apiService = new ApiService(); + String json = apiService.callExternalApi(currencyDto.getCode()); + JSONObject jsonObject = new JSONObject(json); + JSONObject ratesObject = jsonObject.getJSONArray("rates").getJSONObject(0); + double mid = ratesObject.getDouble("mid"); + BigDecimal targetAmount = BigDecimal.valueOf(mid).multiply(BigDecimal.valueOf(payoutRequestPaypalDto.total())); PayoutBatch payoutBatch = paypalService.createPayout( payoutRequestPaypalDto.receiverEmail(), - payoutRequestPaypalDto.total(), + targetAmount.doubleValue(), currencyDto.getCode() ); AddTransactionDto addTransactionDto = AddTransactionDto.builder() .typeOfTransaction("PAYOUT") .amountOfForeginCurrency(String.valueOf(payoutRequestPaypalDto.total())) - .ForeginCurrencyId(63L) + .ForeginCurrencyId(Long.valueOf(payoutRequestPaypalDto.currencyId())) .targetCurrencyId(63L) .targetCurrency(String.valueOf(payoutRequestPaypalDto.total())) .appUserId(String.valueOf(userDto.getId())) @@ -145,7 +153,7 @@ public RedirectView successPayment(@RequestParam("paymentId") String paymentId, Payment payment = paypalService.executePayment(paymentId, payerId); if (payment.getState().equals("approved")) { - paypalService.addAmountToKantorAccount(payment, userId,currencyId); + logger.info("Payment approved asdasdasd"); Optional currency = currencyClient.getCurrencyById(Long.valueOf(currencyId)); @@ -154,7 +162,7 @@ public RedirectView successPayment(@RequestParam("paymentId") String paymentId, } CurrencyDto currencyDto = currency.get(); - + paypalService.addAmountToKantorAccount(payment, userId,currencyId, currencyDto.getCode()); ApiService apiService = new ApiService(); String json = apiService.callExternalApi(currencyDto.getCode()); @@ -167,11 +175,12 @@ public RedirectView successPayment(@RequestParam("paymentId") String paymentId, BigDecimal targetAmount = BigDecimal.valueOf(mid).multiply(amount); AddTransactionDto addTransactionDto = AddTransactionDto.builder() .typeOfTransaction("BUY") - .amountOfForeginCurrency(payment.getTransactions().get(0).getAmount().getTotal()) + .amountOfForeginCurrency(String.valueOf(Double.valueOf(payment.getTransactions().get(0).getAmount().getTotal())/mid)) .ForeginCurrencyId(63L) .targetCurrencyId(Long.valueOf(currencyId)) .targetCurrency(targetAmount.toString()) .appUserId(userId) + .exchangeRate(String.valueOf(mid)) .build(); tranactionClient.addTranactionHistory(addTransactionDto); return new RedirectView("http://localhost:4200"); diff --git a/Back/micro-services/paypal/src/main/java/org/example/paypal/dto/AddTransactionDto.java b/Back/micro-services/paypal/src/main/java/org/example/paypal/dto/AddTransactionDto.java index c9f2a7b3..547d59a3 100644 --- a/Back/micro-services/paypal/src/main/java/org/example/paypal/dto/AddTransactionDto.java +++ b/Back/micro-services/paypal/src/main/java/org/example/paypal/dto/AddTransactionDto.java @@ -14,5 +14,6 @@ public class AddTransactionDto { private String amountOfForeginCurrency; private String targetCurrency; private String typeOfTransaction; + private String exchangeRate; } diff --git a/Back/micro-services/paypal/src/main/java/org/example/paypal/service/PaypalService.java b/Back/micro-services/paypal/src/main/java/org/example/paypal/service/PaypalService.java index d6d3faa4..7d3aa1fc 100644 --- a/Back/micro-services/paypal/src/main/java/org/example/paypal/service/PaypalService.java +++ b/Back/micro-services/paypal/src/main/java/org/example/paypal/service/PaypalService.java @@ -8,10 +8,10 @@ import com.paypal.base.rest.PayPalRESTException; import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; -import org.example.paypal.controller.PaypalController; import org.example.paypal.dto.ForeignCurrencyAccountDto; import org.example.paypal.exeption.AppExeption; import org.example.paypal.feign.CurrencyAccountFeigin; +import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; @@ -194,11 +194,16 @@ public Payment executePayment( return payment.execute(apiContext, paymentExecute); } - public void addAmountToKantorAccount(Payment payment, String userId, String currencyId) { + public void addAmountToKantorAccount(Payment payment, String userId, String currencyId, String code) { Optional account = currencyAccountFeigin.findByCurrencyCodeAndUserId(currencyId, Long.parseLong(userId)); if (account.isPresent()) { + ApiService apiService = new ApiService(); + String json = apiService.callExternalApi(code); + JSONObject jsonObject = new JSONObject(json); + JSONObject ratesObject = jsonObject.getJSONArray("rates").getJSONObject(0); + double mid = ratesObject.getDouble("mid"); ForeignCurrencyAccountDto foreignCurrencyAccount = account.get(); - foreignCurrencyAccount.setBalance(foreignCurrencyAccount.getBalance().add(new BigDecimal(payment.getTransactions().get(0).getAmount().getTotal()))); + foreignCurrencyAccount.setBalance(foreignCurrencyAccount.getBalance().add(new BigDecimal(payment.getTransactions().get(0).getAmount().getTotal()).divide(BigDecimal.valueOf(mid)))); currencyAccountFeigin.save(foreignCurrencyAccount); } else { throw new AppExeption("Account does not exist", HttpStatus.NOT_FOUND); diff --git a/Back/micro-services/transaction/src/main/java/org/example/transaction/dto/AddTransactionDto.java b/Back/micro-services/transaction/src/main/java/org/example/transaction/dto/AddTransactionDto.java index 259679ca..00846bf7 100644 --- a/Back/micro-services/transaction/src/main/java/org/example/transaction/dto/AddTransactionDto.java +++ b/Back/micro-services/transaction/src/main/java/org/example/transaction/dto/AddTransactionDto.java @@ -1,4 +1,4 @@ package org.example.transaction.dto; -public record AddTransactionDto(String appUserId,Long ForeginCurrencyId,Long targetCurrencyId, String amountOfForeginCurrency, String targetCurrency, String typeOfTransaction) { +public record AddTransactionDto(String appUserId,Long ForeginCurrencyId,Long targetCurrencyId, String amountOfForeginCurrency, String targetCurrency, String typeOfTransaction, String exchangeRate) { } diff --git a/Back/micro-services/transaction/src/main/java/org/example/transaction/model/Transaction.java b/Back/micro-services/transaction/src/main/java/org/example/transaction/model/Transaction.java index 4ae818a4..35332afe 100644 --- a/Back/micro-services/transaction/src/main/java/org/example/transaction/model/Transaction.java +++ b/Back/micro-services/transaction/src/main/java/org/example/transaction/model/Transaction.java @@ -19,10 +19,11 @@ public class Transaction { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private Long appUserId; - private Long foreginCurrencyId; + private Long foreignCurrencyId; private Long targetCurrencyId; - private BigDecimal amountOfForeginCurrency; + private BigDecimal amountOfForeignCurrency; private BigDecimal targetCurrency; + private Float exchangeRate; @Enumerated(EnumType.STRING) private TypeOfTransaction typeOfTransaction; private LocalDateTime transactionDate; diff --git a/Back/micro-services/transaction/src/main/java/org/example/transaction/model/TypeOfTransaction.java b/Back/micro-services/transaction/src/main/java/org/example/transaction/model/TypeOfTransaction.java index 4c00f6a2..6e07f63b 100644 --- a/Back/micro-services/transaction/src/main/java/org/example/transaction/model/TypeOfTransaction.java +++ b/Back/micro-services/transaction/src/main/java/org/example/transaction/model/TypeOfTransaction.java @@ -2,6 +2,5 @@ public enum TypeOfTransaction { BUY, - SELL, PAYOUT, } diff --git a/Back/micro-services/transaction/src/main/java/org/example/transaction/service/TransactionService.java b/Back/micro-services/transaction/src/main/java/org/example/transaction/service/TransactionService.java index 6fa88614..36aabafe 100644 --- a/Back/micro-services/transaction/src/main/java/org/example/transaction/service/TransactionService.java +++ b/Back/micro-services/transaction/src/main/java/org/example/transaction/service/TransactionService.java @@ -26,16 +26,18 @@ public class TransactionService { public void addTransaction(AddTransactionDto addTransactionDto) { try { logger.info("Add transaction"); + logger.info("asdasdasdasd"+addTransactionDto.exchangeRate()); // TODO: Ustawić odpowiednie typy Dla dto Transaction transaction = new Transaction(); transaction.setTypeOfTransaction(TypeOfTransaction.valueOf(addTransactionDto.typeOfTransaction())); transaction.setAppUserId(Long.parseLong(addTransactionDto.appUserId())); - transaction.setAmountOfForeginCurrency(new BigDecimal(addTransactionDto.amountOfForeginCurrency())); + transaction.setAmountOfForeignCurrency(new BigDecimal(addTransactionDto.amountOfForeginCurrency())); transaction.setTargetCurrency(new BigDecimal(addTransactionDto.targetCurrency())); transaction.setTransactionDate(LocalDateTime.now()); - transaction.setAmountOfForeginCurrency( new BigDecimal(addTransactionDto.amountOfForeginCurrency())); - - transaction.setForeginCurrencyId(63L); + transaction.setAmountOfForeignCurrency( new BigDecimal(addTransactionDto.amountOfForeginCurrency())); + transaction.setExchangeRate(Float.valueOf(String.valueOf(new BigDecimal(addTransactionDto.exchangeRate())))); + logger.info("asdasdasdasd"+transaction.getExchangeRate()); + transaction.setForeignCurrencyId(63L); transaction.setTargetCurrencyId(addTransactionDto.targetCurrencyId()); transactionRepo.save(transaction); } catch (Exception e) { @@ -51,7 +53,7 @@ public List getTransactions(String id) { transactionDtos.add(new TransactionDto( transaction.getId().toString(), transaction.getAppUserId().toString(), - transaction.getAmountOfForeginCurrency().toString(), + transaction.getAmountOfForeignCurrency().toString(), transaction.getTargetCurrency().toString(), transaction.getTypeOfTransaction().name(), transaction.getTransactionDate().toString()