diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/out/mercadopago/MercadoPagoAdapter.java b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/out/mercadopago/MercadoPagoAdapter.java deleted file mode 100644 index 3b40bc6..0000000 --- a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/out/mercadopago/MercadoPagoAdapter.java +++ /dev/null @@ -1,94 +0,0 @@ -package br.com.fiap.grupo30.fastfood.adapters.out.mercadopago; - -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.mercadopago.MercadoPagoPaymentDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.mercadopago.MercadoPagoQrCodeDTO; -import com.fasterxml.jackson.databind.ObjectMapper; -import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; - -@Component -public class MercadoPagoAdapter { - - @Value("${integrations.mercadopago.base-url}") - private String baseUrl; - - @Value("${integrations.mercadopago.public-key}") - private String publicKey; - - @Value("${integrations.mercadopago.access-token}") - private String privateAccessToken; - - @Value("${integrations.mercadopago.app-user-id}") - private Long appUserId; - - @Value("${integrations.mercadopago.point-of-sale-id}") - private String pointOfSaleId; - - @Value("${integrations.mercadopago.notifications-url}") - private String notificationsUrl; - - @Value("${integrations.mercadopago.payment-collection.user-id}") - private String userIdForPaymentCollection; - - private final MercadoPagoRequestBuilder requestBuilder; - - public MercadoPagoAdapter(MercadoPagoRequestBuilder requestBuilder) { - this.requestBuilder = requestBuilder; - } - - public MercadoPagoQrCodeDTO createQrCodeForPaymentCollection(OrderDTO order) throws Exception { - String resourceUrlTemplate = - "https://api.mercadopago.com/instore/orders/qr/seller/collectors/%s/pos/%s/qrs"; - String resourceUrl = String.format(resourceUrlTemplate, appUserId, pointOfSaleId); - - var requestBody = - this.requestBuilder.buildQrCodePaymentCollectionRequest(order, notificationsUrl); - - ObjectMapper mapper = new ObjectMapper(); - String serializedRequestBody = mapper.writeValueAsString(requestBody); - - try (HttpClient client = HttpClient.newHttpClient()) { - HttpRequest request = - HttpRequest.newBuilder() - .uri(URI.create(resourceUrl)) - .header("Authorization", String.format("Bearer %s", privateAccessToken)) - .header("Content-type", "application/json") - .PUT(HttpRequest.BodyPublishers.ofString(serializedRequestBody)) - .build(); - - HttpResponse response = - client.send(request, HttpResponse.BodyHandlers.ofString()); - MercadoPagoQrCodeDTO result = - mapper.readValue(response.body(), MercadoPagoQrCodeDTO.class); - - return result; - } - } - - public MercadoPagoPaymentDTO getPayment(String paymentId) throws Exception { - String resourceUrlTemplate = "https://api.mercadopago.com/v1/payments/%s"; - String resourceUrl = String.format(resourceUrlTemplate, paymentId); - - try (HttpClient client = HttpClient.newHttpClient()) { - HttpRequest request = - HttpRequest.newBuilder() - .uri(URI.create(resourceUrl)) - .header("Authorization", String.format("Bearer %s", privateAccessToken)) - .header("Content-type", "application/json") - .GET() - .build(); - - HttpResponse response = - client.send(request, HttpResponse.BodyHandlers.ofString()); - MercadoPagoPaymentDTO result = - new ObjectMapper().readValue(response.body(), MercadoPagoPaymentDTO.class); - - return result; - } - } -} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/out/mercadopago/MercadoPagoRequestBuilder.java b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/out/mercadopago/MercadoPagoRequestBuilder.java deleted file mode 100644 index 88d8e6e..0000000 --- a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/out/mercadopago/MercadoPagoRequestBuilder.java +++ /dev/null @@ -1,52 +0,0 @@ -package br.com.fiap.grupo30.fastfood.adapters.out.mercadopago; - -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.mercadopago.MercadoPagoCashOutDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.mercadopago.MercadoPagoCreateQrCodeForPaymentCollectionRequestDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.mercadopago.MercadoPagoOrderItemDTO; -import java.time.Instant; -import java.util.Date; -import java.util.stream.Stream; -import org.springframework.stereotype.Component; - -@Component -public class MercadoPagoRequestBuilder { - public MercadoPagoCreateQrCodeForPaymentCollectionRequestDTO - buildQrCodePaymentCollectionRequest(OrderDTO order, String notificationsUrl) { - String orderId = order.getOrderId().toString(); - MercadoPagoCreateQrCodeForPaymentCollectionRequestDTO requestBody = - new MercadoPagoCreateQrCodeForPaymentCollectionRequestDTO(); - requestBody.setTitle(String.format("Pedido %s", orderId)); - requestBody.setDescription(String.format("Pedido da lanchonete fastfood")); - requestBody.setExpirationDate(Date.from(Instant.now().plusSeconds(3600))); - requestBody.setExternalReference(orderId); - requestBody.setTotalAmount(order.getTotalPrice()); - requestBody.setNotificationUrl(notificationsUrl); - - MercadoPagoCashOutDTO cashOut = new MercadoPagoCashOutDTO(); - cashOut.setAmount(order.getTotalPrice()); - - MercadoPagoOrderItemDTO[] items = - Stream.of(order.getItems()) - .map( - (ourOrderItem) -> { - MercadoPagoOrderItemDTO theirOrderItem = - new MercadoPagoOrderItemDTO(); - theirOrderItem.setTitle(ourOrderItem.getProduct().getName()); - theirOrderItem.setDescription( - ourOrderItem.getProduct().getDescription()); - theirOrderItem.setCategory( - ourOrderItem.getProduct().getCategory()); - theirOrderItem.setQuantity(ourOrderItem.getQuantity()); - theirOrderItem.setUnitPrice( - ourOrderItem.getProduct().getPrice()); - theirOrderItem.setUnitMeasure("unit"); - theirOrderItem.setTotalAmount(ourOrderItem.getTotalPrice()); - return theirOrderItem; - }) - .toArray(MercadoPagoOrderItemDTO[]::new); - requestBody.setItems(items); - - return requestBody; - } -} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/CollectOrderPaymentViaCashUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/CollectOrderPaymentViaCashUseCase.java index b1454c3..1d485a2 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/CollectOrderPaymentViaCashUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/CollectOrderPaymentViaCashUseCase.java @@ -2,7 +2,6 @@ import br.com.fiap.grupo30.fastfood.domain.entities.Order; import br.com.fiap.grupo30.fastfood.infrastructure.gateways.OrderGateway; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CollectPaymentViaCashRequest; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -16,10 +15,10 @@ public CollectOrderPaymentViaCashUseCase(OrderGateway orderGateway) { this.orderGateway = orderGateway; } - public OrderDTO execute(Long orderId, CollectPaymentViaCashRequest payment) { + public OrderDTO execute(Long orderId, Double paidAmount) { Order order = this.orderGateway.findById(orderId); - order.setPaymentCollected(payment.getAmount()); + order.setPaymentCollected(paidAmount); return orderGateway.save(order).toDTO(); } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/CollectOrderPaymentViaMercadoPagoUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/CollectOrderPaymentViaMercadoPagoUseCase.java index d17dfd3..7b6639f 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/CollectOrderPaymentViaMercadoPagoUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/CollectOrderPaymentViaMercadoPagoUseCase.java @@ -1,7 +1,7 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.payment; -import br.com.fiap.grupo30.fastfood.adapters.out.mercadopago.MercadoPagoAdapter; import br.com.fiap.grupo30.fastfood.domain.entities.Order; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.MercadoPagoGateway; import br.com.fiap.grupo30.fastfood.infrastructure.gateways.OrderGateway; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.mercadopago.MercadoPagoPaymentDTO; @@ -14,19 +14,20 @@ @Component public class CollectOrderPaymentViaMercadoPagoUseCase { private final OrderGateway orderGateway; - private final MercadoPagoAdapter mercadoPagoAdapter; + private final MercadoPagoGateway mercadoPagoGateway; @Autowired public CollectOrderPaymentViaMercadoPagoUseCase( - OrderGateway orderGateway, MercadoPagoAdapter mercadoPagoAdapter) { + OrderGateway orderGateway, MercadoPagoGateway mercadoPagoGateway) { this.orderGateway = orderGateway; - this.mercadoPagoAdapter = mercadoPagoAdapter; + this.mercadoPagoGateway = mercadoPagoGateway; } public OrderDTO execute(MercadoPagoActionEventDTO mercadoPagoPaymentEvent) { try { MercadoPagoPaymentDTO payment = - this.mercadoPagoAdapter.getPayment(mercadoPagoPaymentEvent.getData().getId()); + this.mercadoPagoGateway.getPaymentState( + mercadoPagoPaymentEvent.getData().getId()); Order order = this.orderGateway.findById(Long.parseLong(payment.getExternalReference())); diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/GeneratePaymentQrCodeUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/GeneratePaymentQrCodeUseCase.java index 0a84a15..8a00985 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/GeneratePaymentQrCodeUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/payment/GeneratePaymentQrCodeUseCase.java @@ -1,29 +1,24 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.payment; -import br.com.fiap.grupo30.fastfood.adapters.out.mercadopago.MercadoPagoAdapter; import br.com.fiap.grupo30.fastfood.domain.entities.Order; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.MercadoPagoGateway; import br.com.fiap.grupo30.fastfood.infrastructure.gateways.OrderGateway; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.PaymentQrCodeDTO; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.mercadopago.MercadoPagoQrCodeDTO; import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.PaymentProcessingFailedException; -import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.PaymentQrCodeDTOMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class GeneratePaymentQrCodeUseCase { private final OrderGateway orderGateway; - private final MercadoPagoAdapter mercadoPagoAdapter; - private final PaymentQrCodeDTOMapper qrCodeMapper; + private final MercadoPagoGateway mercadoPagoGateway; @Autowired public GeneratePaymentQrCodeUseCase( - OrderGateway orderGateway, - MercadoPagoAdapter mercadoPagoAdapter, - PaymentQrCodeDTOMapper qrCodeMapper) { + OrderGateway orderGateway, MercadoPagoGateway mercadoPagoGateway) { this.orderGateway = orderGateway; - this.mercadoPagoAdapter = mercadoPagoAdapter; - this.qrCodeMapper = qrCodeMapper; + this.mercadoPagoGateway = mercadoPagoGateway; } public PaymentQrCodeDTO execute(Long orderId) { @@ -32,7 +27,7 @@ public PaymentQrCodeDTO execute(Long orderId) { MercadoPagoQrCodeDTO qrCodeResponse; try { qrCodeResponse = - this.mercadoPagoAdapter.createQrCodeForPaymentCollection(order.toDTO()); + this.mercadoPagoGateway.createQrCodeForOrderPaymentCollection(order.toDTO()); } catch (Exception e) { throw new PaymentProcessingFailedException("Could not start payment processing", e); } @@ -40,6 +35,6 @@ public PaymentQrCodeDTO execute(Long orderId) { order.setPaymentProcessing(); this.orderGateway.save(order); - return qrCodeMapper.map(qrCodeResponse); + return new PaymentQrCodeDTO(qrCodeResponse.getQrData()); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/MercadoPagoGateway.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/MercadoPagoGateway.java new file mode 100644 index 0000000..4178751 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/MercadoPagoGateway.java @@ -0,0 +1,96 @@ +package br.com.fiap.grupo30.fastfood.infrastructure.gateways; + +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.mercadopago.MercadoPagoOrderDTO; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.mercadopago.MercadoPagoPaymentDTO; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.mercadopago.MercadoPagoQrCodeDTO; +import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.MercadoPagoOrderMapper; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpRequest.BodyPublisher; +import java.net.http.HttpRequest.BodyPublishers; +import java.net.http.HttpResponse; +import java.util.HashMap; +import java.util.Map; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class MercadoPagoGateway { + @Value("${integrations.mercadopago.base-url}") + private String baseUrl; + + @Value("${integrations.mercadopago.public-key}") + private String publicKey; + + @Value("${integrations.mercadopago.access-token}") + private String privateAccessToken; + + @Value("${integrations.mercadopago.app-user-id}") + private Long appUserId; + + @Value("${integrations.mercadopago.point-of-sale-id}") + private String pointOfSaleId; + + @Value("${integrations.mercadopago.notifications-url}") + private String notificationsUrl; + + private ObjectMapper jsonMapper; + private MercadoPagoOrderMapper orderMapper; + + @Autowired + public MercadoPagoGateway(MercadoPagoOrderMapper orderMapper) { + this.jsonMapper = new ObjectMapper(); + this.orderMapper = orderMapper; + } + + private Map getHeaders() { + Map headers = new HashMap(); + headers.put("Authorization", String.format("Bearer %s", privateAccessToken)); + headers.put("Content-type", "application/json"); + return headers; + } + + private HttpResponse makeRequest(String httpMethod, URI resourceUri) throws Exception { + return makeRequest(httpMethod, resourceUri, BodyPublishers.noBody()); + } + + private HttpResponse makeRequest(String httpMethod, URI resourceUri, BodyPublisher body) + throws Exception { + try (HttpClient client = HttpClient.newHttpClient()) { + HttpRequest.Builder builder = + HttpRequest.newBuilder().uri(resourceUri).method(httpMethod, body); + getHeaders().forEach(builder::header); + + HttpRequest request = builder.build(); + + return client.send(request, HttpResponse.BodyHandlers.ofString()); + } + } + + public MercadoPagoQrCodeDTO createQrCodeForOrderPaymentCollection(OrderDTO order) + throws Exception { + String resourceUriTemplate = + "https://api.mercadopago.com/instore/orders/qr/seller/collectors/%s/pos/%s/qrs"; + URI resourceUri = URI.create(String.format(resourceUriTemplate, appUserId, pointOfSaleId)); + + MercadoPagoOrderDTO mercadoPagoOrder = orderMapper.map(order, notificationsUrl); + BodyPublisher requestBody = + HttpRequest.BodyPublishers.ofString( + jsonMapper.writeValueAsString(mercadoPagoOrder)); + + var response = makeRequest("PUT", resourceUri, requestBody); + return jsonMapper.readValue(response.body(), MercadoPagoQrCodeDTO.class); + } + + public MercadoPagoPaymentDTO getPaymentState(String paymentId) throws Exception { + String resourceUriTemplate = "https://api.mercadopago.com/v1/payments/%s"; + URI resourceUri = URI.create(String.format(resourceUriTemplate, paymentId)); + + var response = makeRequest("GET", resourceUri); + return jsonMapper.readValue(response.body(), MercadoPagoPaymentDTO.class); + } +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/PaymentResource.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/PaymentResource.java index 456ac26..9415fd5 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/PaymentResource.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/PaymentResource.java @@ -43,7 +43,8 @@ public ResponseEntity generateQrCodeForPaymentCollection( @Operation(summary = "Collect payment by cash") public ResponseEntity collectPaymentByBash( @PathVariable Long orderId, @RequestBody CollectPaymentViaCashRequest request) { - OrderDTO order = this.collectOrderPaymentViaCashUseCase.execute(orderId, request); + OrderDTO order = + this.collectOrderPaymentViaCashUseCase.execute(orderId, request.getAmount()); return ResponseEntity.ok().body(order); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/CollectPaymentViaCashRequest.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/CollectPaymentViaCashRequest.java index 098955b..cfa4972 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/CollectPaymentViaCashRequest.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/CollectPaymentViaCashRequest.java @@ -1,12 +1,10 @@ package br.com.fiap.grupo30.fastfood.presentation.presenters.dto; import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.Getter; -@Data +@Getter @AllArgsConstructor -@NoArgsConstructor public class CollectPaymentViaCashRequest { private Double amount; } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/MercadoPagoNotificationDTO.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/MercadoPagoNotificationDTO.java index b484e12..8d01308 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/MercadoPagoNotificationDTO.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/MercadoPagoNotificationDTO.java @@ -3,11 +3,9 @@ import com.fasterxml.jackson.annotation.JsonProperty; import java.util.Date; import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.Getter; -@Data -@NoArgsConstructor +@Getter @AllArgsConstructor public class MercadoPagoNotificationDTO { private String action; diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/PaymentQrCodeDTO.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/PaymentQrCodeDTO.java index 217f0fb..1050e2f 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/PaymentQrCodeDTO.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/PaymentQrCodeDTO.java @@ -1,9 +1,9 @@ package br.com.fiap.grupo30.fastfood.presentation.presenters.dto; import lombok.AllArgsConstructor; -import lombok.Data; +import lombok.Getter; -@Data +@Getter @AllArgsConstructor public class PaymentQrCodeDTO { public String qrCodeData; diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/mercadopago/MercadoPagoCreateQrCodeForPaymentCollectionRequestDTO.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/mercadopago/MercadoPagoOrderDTO.java similarity index 91% rename from src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/mercadopago/MercadoPagoCreateQrCodeForPaymentCollectionRequestDTO.java rename to src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/mercadopago/MercadoPagoOrderDTO.java index e146dbd..1029428 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/mercadopago/MercadoPagoCreateQrCodeForPaymentCollectionRequestDTO.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/mercadopago/MercadoPagoOrderDTO.java @@ -4,15 +4,17 @@ import com.fasterxml.jackson.annotation.JsonProperty; import java.util.Date; import lombok.AllArgsConstructor; -import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; -@Data +@Getter +@Setter @NoArgsConstructor @AllArgsConstructor @EqualsAndHashCode -public class MercadoPagoCreateQrCodeForPaymentCollectionRequestDTO { +public class MercadoPagoOrderDTO { private String title; private String description; diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/mercadopago/MercadoPagoQrCodeDTO.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/mercadopago/MercadoPagoQrCodeDTO.java index 317712b..6ccd311 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/mercadopago/MercadoPagoQrCodeDTO.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/mercadopago/MercadoPagoQrCodeDTO.java @@ -2,12 +2,10 @@ import com.fasterxml.jackson.annotation.JsonProperty; import lombok.AllArgsConstructor; -import lombok.Data; import lombok.EqualsAndHashCode; -import lombok.NoArgsConstructor; +import lombok.Getter; -@Data -@NoArgsConstructor +@Getter @AllArgsConstructor @EqualsAndHashCode public class MercadoPagoQrCodeDTO { diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/MercadoPagoOrderMapper.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/MercadoPagoOrderMapper.java new file mode 100644 index 0000000..58e726e --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/MercadoPagoOrderMapper.java @@ -0,0 +1,48 @@ +package br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl; + +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderItemDTO; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.mercadopago.MercadoPagoCashOutDTO; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.mercadopago.MercadoPagoOrderDTO; +import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.mercadopago.MercadoPagoOrderItemDTO; +import java.time.Instant; +import java.util.Date; +import java.util.stream.Stream; +import org.springframework.stereotype.Component; + +@Component +public class MercadoPagoOrderMapper { + public MercadoPagoOrderDTO map(OrderDTO order, String notificationsUrl) { + String orderId = order.getOrderId().toString(); + MercadoPagoOrderDTO mercadoPagoOrder = new MercadoPagoOrderDTO(); + mercadoPagoOrder.setTitle(String.format("Pedido %s", orderId)); + mercadoPagoOrder.setDescription(String.format("Pedido da lanchonete fastfood")); + mercadoPagoOrder.setExpirationDate(Date.from(Instant.now().plusSeconds(3600))); + mercadoPagoOrder.setExternalReference(orderId); + mercadoPagoOrder.setTotalAmount(order.getTotalPrice()); + mercadoPagoOrder.setNotificationUrl(notificationsUrl); + + MercadoPagoCashOutDTO cashOut = new MercadoPagoCashOutDTO(); + cashOut.setAmount(order.getTotalPrice()); + + MercadoPagoOrderItemDTO[] items = + Stream.of(order.getItems()) + .map(this::mapOrderItem) + .toArray(MercadoPagoOrderItemDTO[]::new); + mercadoPagoOrder.setItems(items); + + return mercadoPagoOrder; + } + + private MercadoPagoOrderItemDTO mapOrderItem(OrderItemDTO orderItem) { + MercadoPagoOrderItemDTO mercadoPagoOrderItem = new MercadoPagoOrderItemDTO(); + mercadoPagoOrderItem.setTitle(orderItem.getProduct().getName()); + mercadoPagoOrderItem.setDescription(orderItem.getProduct().getDescription()); + mercadoPagoOrderItem.setCategory(orderItem.getProduct().getCategory()); + mercadoPagoOrderItem.setQuantity(orderItem.getQuantity()); + mercadoPagoOrderItem.setUnitPrice(orderItem.getProduct().getPrice()); + mercadoPagoOrderItem.setUnitMeasure("unit"); + mercadoPagoOrderItem.setTotalAmount(orderItem.getTotalPrice()); + return mercadoPagoOrderItem; + } +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/PaymentQrCodeDTOMapper.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/PaymentQrCodeDTOMapper.java deleted file mode 100644 index 6cb1016..0000000 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/PaymentQrCodeDTOMapper.java +++ /dev/null @@ -1,16 +0,0 @@ -package br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl; - -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.PaymentQrCodeDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.mercadopago.MercadoPagoQrCodeDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.UniDirectionalMapper; -import org.springframework.stereotype.Component; - -@Component -public class PaymentQrCodeDTOMapper - implements UniDirectionalMapper { - @Override - public PaymentQrCodeDTO map(MercadoPagoQrCodeDTO mercadoPagoData) { - PaymentQrCodeDTO dto = new PaymentQrCodeDTO(mercadoPagoData.getQrData()); - return dto; - } -} diff --git a/src/main/resources/application-dev-docker.properties b/src/main/resources/application-dev-docker.properties index 3b16849..7ef60b1 100644 --- a/src/main/resources/application-dev-docker.properties +++ b/src/main/resources/application-dev-docker.properties @@ -20,4 +20,3 @@ integrations.mercadopago.access-token=APP_USR-5669469328830836-072119-fbf214281e integrations.mercadopago.app-user-id=1910105219 integrations.mercadopago.point-of-sale-id=STORE00001POS001 integrations.mercadopago.notifications-url=https://e916-189-29-149-200.ngrok-free.app/webhooks/mercadopago -integrations.mercadopago.payment-collection.user-id=1910105219 \ No newline at end of file diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index 262ef2b..52e59a8 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -20,4 +20,3 @@ integrations.mercadopago.access-token=APP_USR-5669469328830836-072119-fbf214281e integrations.mercadopago.app-user-id=1910105219 integrations.mercadopago.point-of-sale-id=STORE00001POS001 integrations.mercadopago.notifications-url=https://e916-189-29-149-200.ngrok-free.app/webhooks/mercadopago -integrations.mercadopago.payment-collection.user-id=1910105219 \ No newline at end of file diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties index 44c3413..f23afff 100644 --- a/src/main/resources/application-prod.properties +++ b/src/main/resources/application-prod.properties @@ -15,4 +15,3 @@ integrations.mercadopago.access-token=${MERCADOPAGO_PRIVATE_ACCESS_TOKEN} integrations.mercadopago.app-user-id=${MERCADOPAGO_APP_USER_ID} integrations.mercadopago.point-of-sale-id=${MERCADOPAGO_POINT_OF_SALE_ID} integrations.mercadopago.notifications-url=${MERCADOPAGO_NOTIFICATIONS_URL} -integrations.mercadopago.payment-collection.user-id=${MERCADOPAGO_PAYMENT_COLLECTION_USER_ID} \ No newline at end of file diff --git a/src/main/resources/application-test.properties b/src/main/resources/application-test.properties index afdbb8c..017e3a3 100644 --- a/src/main/resources/application-test.properties +++ b/src/main/resources/application-test.properties @@ -21,4 +21,3 @@ integrations.mercadopago.access-token=APP_USR-5669469328830836-072119-fbf214281e integrations.mercadopago.app-user-id=1910105219 integrations.mercadopago.point-of-sale-id=STORE00001POS001 integrations.mercadopago.notifications-url=https://e916-189-29-149-200.ngrok-free.app/webhooks/mercadopago -integrations.mercadopago.payment-collection.user-id=1910105219 \ No newline at end of file