This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: clean arch approach for mercado pago payments
- Loading branch information
1 parent
dc8f017
commit 93d035d
Showing
18 changed files
with
174 additions
and
202 deletions.
There are no files selected for viewing
94 changes: 0 additions & 94 deletions
94
src/main/java/br/com/fiap/grupo30/fastfood/adapters/out/mercadopago/MercadoPagoAdapter.java
This file was deleted.
Oops, something went wrong.
52 changes: 0 additions & 52 deletions
52
...java/br/com/fiap/grupo30/fastfood/adapters/out/mercadopago/MercadoPagoRequestBuilder.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/MercadoPagoGateway.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 2 additions & 4 deletions
6
...r/com/fiap/grupo30/fastfood/presentation/presenters/dto/CollectPaymentViaCashRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/dto/PaymentQrCodeDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.