Merge branch 'fix-payments'
This commit is contained in:
@@ -26,7 +26,7 @@ public class SecurityConfig {
|
|||||||
.cors(cors -> cors.configure(http))
|
.cors(cors -> cors.configure(http))
|
||||||
.csrf(AbstractHttpConfigurer::disable)
|
.csrf(AbstractHttpConfigurer::disable)
|
||||||
.authorizeHttpRequests(auth -> auth
|
.authorizeHttpRequests(auth -> auth
|
||||||
.requestMatchers("/api/v1/auth/**").permitAll()
|
.requestMatchers("/api/v1/auth/**", "/api/v1/payments/notification").permitAll()
|
||||||
.anyRequest().authenticated())
|
.anyRequest().authenticated())
|
||||||
.sessionManagement(session -> session
|
.sessionManagement(session -> session
|
||||||
.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
|
.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package _11.asktpk.artisanconnectbackend.controller;
|
package _11.asktpk.artisanconnectbackend.controller;
|
||||||
|
|
||||||
import _11.asktpk.artisanconnectbackend.dto.*;
|
import _11.asktpk.artisanconnectbackend.dto.*;
|
||||||
|
import _11.asktpk.artisanconnectbackend.entities.Client;
|
||||||
import _11.asktpk.artisanconnectbackend.entities.Order;
|
import _11.asktpk.artisanconnectbackend.entities.Order;
|
||||||
|
import _11.asktpk.artisanconnectbackend.entities.Payment;
|
||||||
import _11.asktpk.artisanconnectbackend.service.OrderService;
|
import _11.asktpk.artisanconnectbackend.service.OrderService;
|
||||||
import _11.asktpk.artisanconnectbackend.service.PaymentService;
|
import _11.asktpk.artisanconnectbackend.service.PaymentService;
|
||||||
import _11.asktpk.artisanconnectbackend.utils.Enums;
|
import _11.asktpk.artisanconnectbackend.utils.Enums;
|
||||||
@@ -11,6 +13,8 @@ import org.springframework.http.HttpStatus;
|
|||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/v1/orders")
|
@RequestMapping("/api/v1/orders")
|
||||||
@@ -38,21 +42,88 @@ public class OrderController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/token")
|
@PostMapping("/token")
|
||||||
public ResponseEntity<?> fetchToken() {
|
public ResponseEntity<?> fetchToken(HttpServletRequest request,@RequestParam Long orderId) {
|
||||||
Order order = orderService.getOrderById(1L);
|
Order order = orderService.getOrderById(orderId);
|
||||||
|
Client client = order.getClient();
|
||||||
OAuthPaymentResponseDTO authPaymentDTO = paymentService.getOAuthToken();
|
OAuthPaymentResponseDTO authPaymentDTO = paymentService.getOAuthToken();
|
||||||
TransactionPaymentRequestDTO.Payer payer = new TransactionPaymentRequestDTO.Payer(
|
TransactionPaymentRequestDTO.Payer payer = new TransactionPaymentRequestDTO.Payer(
|
||||||
"patryk@test.pl", "Patryk Test");
|
client.getEmail(), client.getFirstName()+' '+client.getLastName());
|
||||||
|
|
||||||
String paymentDescription = order.getOrderType() == Enums.OrderType.ACTIVATION ? "Aktywacja ogłoszenia" : "Podbicie ogłoszenia";
|
String paymentDescription = order.getOrderType() == Enums.OrderType.ACTIVATION ? "Aktywacja ogłoszenia" : "Podbicie ogłoszenia";
|
||||||
paymentDescription += order.getNotice().getTitle();
|
paymentDescription += order.getNotice().getTitle();
|
||||||
TransactionPaymentRequestDTO request = new TransactionPaymentRequestDTO(
|
TransactionPaymentRequestDTO paymentRequest = new TransactionPaymentRequestDTO(
|
||||||
order.getAmount(), paymentDescription, payer);
|
order.getAmount(), paymentDescription, payer);
|
||||||
|
|
||||||
String response = paymentService.createTransaction(order, authPaymentDTO.getAccess_token(), request);
|
String response = paymentService.createTransaction(order, authPaymentDTO.getAccess_token(), paymentRequest);
|
||||||
System.out.println(response);
|
|
||||||
System.out.println(request);
|
|
||||||
|
|
||||||
return ResponseEntity.status(HttpStatus.OK).body(response);
|
return ResponseEntity.status(HttpStatus.OK).body(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/get/all")
|
||||||
|
public ResponseEntity<List<OrderWithPaymentsDTO>> getOrders(HttpServletRequest request) {
|
||||||
|
Long clientId = tools.getClientIdFromRequest(request);
|
||||||
|
List<Order> orders = orderService.getOrdersByClientId(clientId);
|
||||||
|
|
||||||
|
List<OrderWithPaymentsDTO> dtoList = orders.stream().map(order -> {
|
||||||
|
OrderWithPaymentsDTO dto = new OrderWithPaymentsDTO();
|
||||||
|
dto.setOrderId(order.getId());
|
||||||
|
dto.setOrderType(order.getOrderType().name());
|
||||||
|
dto.setStatus(order.getStatus().name());
|
||||||
|
dto.setAmount(order.getAmount());
|
||||||
|
dto.setCreatedAt(order.getCreatedAt());
|
||||||
|
|
||||||
|
List<Payment> payments = paymentService.getPaymentsByOrderId(order.getId());
|
||||||
|
|
||||||
|
List<PaymentDTO> paymentDTOs = payments.stream().map(payment -> {
|
||||||
|
PaymentDTO pDto = new PaymentDTO();
|
||||||
|
pDto.setPaymentId(payment.getIdPayment());
|
||||||
|
pDto.setAmount(payment.getAmount());
|
||||||
|
pDto.setStatus(payment.getStatus().name());
|
||||||
|
pDto.setTransactionPaymentUrl(payment.getTransactionPaymentUrl());
|
||||||
|
pDto.setTransactionId(payment.getTransactionId());
|
||||||
|
return pDto;
|
||||||
|
}).toList();
|
||||||
|
|
||||||
|
dto.setPayments(paymentDTOs);
|
||||||
|
return dto;
|
||||||
|
}).toList();
|
||||||
|
|
||||||
|
return ResponseEntity.ok(dtoList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/get/{orderId}")
|
||||||
|
public ResponseEntity<OrderWithPaymentsDTO> getOrderById(HttpServletRequest request,
|
||||||
|
@PathVariable Long orderId) {
|
||||||
|
Long clientId = tools.getClientIdFromRequest(request);
|
||||||
|
|
||||||
|
Order order = orderService.getOrderById(orderId);
|
||||||
|
|
||||||
|
if (!order.getClient().getId().equals(clientId)) {
|
||||||
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
OrderWithPaymentsDTO dto = new OrderWithPaymentsDTO();
|
||||||
|
dto.setOrderId(order.getId());
|
||||||
|
dto.setOrderType(order.getOrderType().name());
|
||||||
|
dto.setStatus(order.getStatus().name());
|
||||||
|
dto.setAmount(order.getAmount());
|
||||||
|
dto.setCreatedAt(order.getCreatedAt());
|
||||||
|
|
||||||
|
List<Payment> payments = paymentService.getPaymentsByOrderId(order.getId());
|
||||||
|
List<PaymentDTO> paymentDTOs = payments.stream().map(payment -> {
|
||||||
|
PaymentDTO pDto = new PaymentDTO();
|
||||||
|
pDto.setPaymentId(payment.getIdPayment());
|
||||||
|
pDto.setAmount(payment.getAmount());
|
||||||
|
pDto.setStatus(payment.getStatus().name());
|
||||||
|
pDto.setTransactionPaymentUrl(payment.getTransactionPaymentUrl());
|
||||||
|
pDto.setTransactionId(payment.getTransactionId());
|
||||||
|
return pDto;
|
||||||
|
}).toList();
|
||||||
|
|
||||||
|
dto.setPayments(paymentDTOs);
|
||||||
|
|
||||||
|
return ResponseEntity.ok(dto);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,9 +39,6 @@ public class PaymentController {
|
|||||||
|
|
||||||
@PostMapping(value = "/notification", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
|
@PostMapping(value = "/notification", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
|
||||||
public ResponseEntity<String> handleTpayNotification(@RequestParam Map<String, String> params) {
|
public ResponseEntity<String> handleTpayNotification(@RequestParam Map<String, String> params) {
|
||||||
log.info("=== ODEBRANO NOTYFIKACJĘ Tpay ===");
|
|
||||||
log.info("Parametry:\n{}", paramsToLogString(params));
|
|
||||||
|
|
||||||
String id = params.get("id");
|
String id = params.get("id");
|
||||||
String trId = params.get("tr_id");
|
String trId = params.get("tr_id");
|
||||||
String trAmount = params.get("tr_amount");
|
String trAmount = params.get("tr_amount");
|
||||||
@@ -54,7 +51,6 @@ public class PaymentController {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (!expectedMd5.equals(md5sum)) {
|
if (!expectedMd5.equals(md5sum)) {
|
||||||
log.warn("❌ Błędna suma kontrolna! Otrzymano: {}, Oczekiwano: {}", md5sum, expectedMd5);
|
|
||||||
return ResponseEntity.status(400).body("INVALID CHECKSUM");
|
return ResponseEntity.status(400).body("INVALID CHECKSUM");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +59,6 @@ public class PaymentController {
|
|||||||
Payment payment = optionalPayment.get();
|
Payment payment = optionalPayment.get();
|
||||||
|
|
||||||
if ("true".equalsIgnoreCase(trStatus) || "PAID".equalsIgnoreCase(trStatus)) {
|
if ("true".equalsIgnoreCase(trStatus) || "PAID".equalsIgnoreCase(trStatus)) {
|
||||||
log.info("✅ Transakcja opłacona: tr_id={}, kwota={}", trId, params.get("tr_paid"));
|
|
||||||
payment.setStatus(Enums.PaymentStatus.CORRECT);
|
payment.setStatus(Enums.PaymentStatus.CORRECT);
|
||||||
|
|
||||||
if (payment.getOrder() != null) {
|
if (payment.getOrder() != null) {
|
||||||
@@ -78,7 +73,6 @@ public class PaymentController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else if ("false".equalsIgnoreCase(trStatus)) {
|
} else if ("false".equalsIgnoreCase(trStatus)) {
|
||||||
log.warn("❌ Transakcja nieudana: {}", trId);
|
|
||||||
payment.setStatus(Enums.PaymentStatus.INCORRECT);
|
payment.setStatus(Enums.PaymentStatus.INCORRECT);
|
||||||
|
|
||||||
if (payment.getOrder() != null) {
|
if (payment.getOrder() != null) {
|
||||||
@@ -87,10 +81,7 @@ public class PaymentController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
paymentRepository.save(payment);
|
paymentRepository.save(payment);
|
||||||
} else {
|
|
||||||
log.warn("⚠️ Brak płatności o tr_id={}", trId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ResponseEntity.ok("TRUE");
|
return ResponseEntity.ok("TRUE");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package _11.asktpk.artisanconnectbackend.dto;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class OrderWithPaymentsDTO {
|
||||||
|
private Long orderId;
|
||||||
|
private String orderType;
|
||||||
|
private String status;
|
||||||
|
private Double amount;
|
||||||
|
private LocalDateTime createdAt;
|
||||||
|
private List<PaymentDTO> payments;
|
||||||
|
|
||||||
|
// Gettery i settery
|
||||||
|
public Long getOrderId() {
|
||||||
|
return orderId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrderId(Long orderId) {
|
||||||
|
this.orderId = orderId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getOrderType() {
|
||||||
|
return orderType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrderType(String orderType) {
|
||||||
|
this.orderType = orderType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStatus() {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStatus(String status) {
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getAmount() {
|
||||||
|
return amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAmount(Double amount) {
|
||||||
|
this.amount = amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getCreatedAt() {
|
||||||
|
return createdAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreatedAt(LocalDateTime createdAt) {
|
||||||
|
this.createdAt = createdAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<PaymentDTO> getPayments() {
|
||||||
|
return payments;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPayments(List<PaymentDTO> payments) {
|
||||||
|
this.payments = payments;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package _11.asktpk.artisanconnectbackend.dto;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class PaymentDTO {
|
||||||
|
private Long paymentId;
|
||||||
|
private Double amount;
|
||||||
|
private String status;
|
||||||
|
private String transactionPaymentUrl;
|
||||||
|
private String transactionId;
|
||||||
|
|
||||||
|
public void setPaymentId(Long paymentId) {
|
||||||
|
this.paymentId = paymentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAmount(Double amount) {
|
||||||
|
this.amount = amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStatus(String status) {
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTransactionPaymentUrl(String transactionPaymentUrl) {
|
||||||
|
this.transactionPaymentUrl = transactionPaymentUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTransactionId(String transactionId) {
|
||||||
|
this.transactionId = transactionId;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,8 +4,10 @@ import _11.asktpk.artisanconnectbackend.entities.Order;
|
|||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface OrderRepository extends JpaRepository<Order, Long> {
|
public interface OrderRepository extends JpaRepository<Order, Long> {
|
||||||
|
List<Order> findByClientId(Long clientId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,12 @@ import _11.asktpk.artisanconnectbackend.entities.Payment;
|
|||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface PaymentRepository extends JpaRepository<Payment, Long> {
|
public interface PaymentRepository extends JpaRepository<Payment, Long> {
|
||||||
Optional<Payment> findByTransactionId(String transactionId);
|
Optional<Payment> findByTransactionId(String transactionId);
|
||||||
|
|
||||||
|
List<Payment> findAllByOrderId(Long id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import _11.asktpk.artisanconnectbackend.entities.Order;
|
import _11.asktpk.artisanconnectbackend.entities.Order;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class OrderService {
|
public class OrderService {
|
||||||
@@ -75,4 +76,8 @@ public class OrderService {
|
|||||||
return orderRepository.findById(id)
|
return orderRepository.findById(id)
|
||||||
.orElseThrow(() -> new RuntimeException("Nie znaleziono zamówienia o ID: " + id));
|
.orElseThrow(() -> new RuntimeException("Nie znaleziono zamówienia o ID: " + id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Order> getOrdersByClientId(Long clientId) {
|
||||||
|
return orderRepository.findByClientId(clientId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ import org.springframework.web.reactive.function.BodyInserters;
|
|||||||
import org.springframework.web.reactive.function.client.WebClient;
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class PaymentService {
|
public class PaymentService {
|
||||||
private final WebClient webClient;
|
private final WebClient webClient;
|
||||||
@@ -80,4 +82,10 @@ public class PaymentService {
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Payment> getPaymentsByOrderId(Long id) {
|
||||||
|
return paymentRepository.findAllByOrderId(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user