fix payments and add new functions
This commit is contained in:
@@ -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,91 @@ 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);
|
||||||
|
Long clientId = tools.getClientIdFromRequest(request);
|
||||||
|
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(response);
|
||||||
System.out.println(request);
|
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(); // lub UNAUTHORIZED
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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