Skip to content

Commit

Permalink
HotFix
Browse files Browse the repository at this point in the history
  • Loading branch information
iwokonl committed May 20, 2024
1 parent d25230b commit 4e13ea5
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 26 deletions.
26 changes: 26 additions & 0 deletions Back/micro-services/.idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Back/micro-services/.idea/sonarlint/issuestore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ public ResponseEntity<RedirectView> createPayment(@RequestBody PaymentPaypalDto
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Bearer " + userDto.getToken());
String successUrl = paymentPaypalDto.successUrl() + "?userId=" + userDto.getId() + "&JWTtoken=" + userDto.getToken() + "&currencyId=" + 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",
Expand Down Expand Up @@ -117,17 +119,23 @@ public ResponseEntity<PayoutBatch> 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()))
Expand All @@ -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<CurrencyDto> currency = currencyClient.getCurrencyById(Long.valueOf(currencyId));

Expand All @@ -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());
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public class AddTransactionDto {
private String amountOfForeginCurrency;
private String targetCurrency;
private String typeOfTransaction;
private String exchangeRate;
}

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ForeignCurrencyAccountDto> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@

public enum TypeOfTransaction {
BUY,
SELL,
PAYOUT,
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -51,7 +53,7 @@ public List<TransactionDto> 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()
Expand Down

0 comments on commit 4e13ea5

Please sign in to comment.