Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
refactor: clean arch approach for mercado pago payments
Browse files Browse the repository at this point in the history
  • Loading branch information
MuriloKakazu committed Jul 27, 2024
1 parent dc8f017 commit 93d035d
Show file tree
Hide file tree
Showing 18 changed files with 174 additions and 202 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -32,14 +27,14 @@ 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);
}

order.setPaymentProcessing();
this.orderGateway.save(order);

return qrCodeMapper.map(qrCodeResponse);
return new PaymentQrCodeDTO(qrCodeResponse.getQrData());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
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<String, String> getHeaders() {
return new HashMap<String, String>() {
{
put("Authorization", String.format("Bearer %s", privateAccessToken));
put("Content-type", "application/json");
}
};
}

private HttpResponse<String> makeRequest(String httpMethod, URI resourceUri) throws Exception {
return makeRequest(httpMethod, resourceUri, BodyPublishers.noBody());
}

private HttpResponse<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ResponseEntity<PaymentQrCodeDTO> generateQrCodeForPaymentCollection(
@Operation(summary = "Collect payment by cash")
public ResponseEntity<OrderDTO> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading

0 comments on commit 93d035d

Please sign in to comment.