Skip to content

Commit

Permalink
Merge pull request #4 from rhames07/main
Browse files Browse the repository at this point in the history
Update SDK version to 2.0.0
  • Loading branch information
andreagostinho-meli authored Mar 21, 2022
2 parents cd32755 + b35a6ea commit 55fec8c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 70 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<dependency>
<groupId>com.mercadopago</groupId>
<artifactId>sdk-java</artifactId>
<version>1.11.0</version>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.mercadopago.sample.controller;

import com.mercadopago.sample.dto.PaymentResponseDTO;
import com.mercadopago.sample.dto.CardPaymentDTO;
import com.mercadopago.sample.dto.PaymentResponseDTO;
import com.mercadopago.sample.service.CardPaymentService;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -11,12 +12,10 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/process_payment")
public class CardPaymentController {
private CardPaymentService cardPaymentService;
private final CardPaymentService cardPaymentService;

@Autowired
public CardPaymentController(CardPaymentService cardPaymentService) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/mercadopago/sample/dto/CardPaymentDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;

import java.math.BigDecimal;
import javax.validation.constraints.NotNull;

public class CardPaymentDTO {
Expand All @@ -14,7 +15,7 @@ public class CardPaymentDTO {
private String paymentMethodId;

@NotNull
private Float transactionAmount;
private BigDecimal transactionAmount;

@NotNull
private Integer installments;
Expand Down Expand Up @@ -53,11 +54,11 @@ public void setPaymentMethodId(String paymentMethodId) {
this.paymentMethodId = paymentMethodId;
}

public Float getTransactionAmount() {
public BigDecimal getTransactionAmount() {
return transactionAmount;
}

public void setTransactionAmount(Float transactionAmount) {
public void setTransactionAmount(BigDecimal transactionAmount) {
this.transactionAmount = transactionAmount;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.mercadopago.sample.dto;

public class PaymentResponseDTO {
private String id;
private Long id;
private String status;
private String detail;

public PaymentResponseDTO(String id, String status, String detail) {
public PaymentResponseDTO(Long id, String status, String detail) {
this.id = id;
this.status = status;
this.detail = detail;
}

public String getId() {
public Long getId() {
return id;
}

public void setId(String id) {
public void setId(Long id) {
this.id = id;
}

Expand Down
107 changes: 49 additions & 58 deletions src/main/java/com/mercadopago/sample/service/CardPaymentService.java
Original file line number Diff line number Diff line change
@@ -1,69 +1,60 @@
package com.mercadopago.sample.service;

import com.mercadopago.MercadoPagoConfig;
import com.mercadopago.client.common.IdentificationRequest;
import com.mercadopago.client.payment.PaymentClient;
import com.mercadopago.client.payment.PaymentCreateRequest;
import com.mercadopago.client.payment.PaymentPayerRequest;
import com.mercadopago.exceptions.MPApiException;
import com.mercadopago.exceptions.MPException;
import com.mercadopago.resources.payment.Payment;
import com.mercadopago.sample.dto.CardPaymentDTO;
import com.mercadopago.sample.dto.PaymentResponseDTO;
import com.mercadopago.sample.exception.MercadoPagoException;
import com.mercadopago.sample.dto.CardPaymentDTO;
import com.mercadopago.MercadoPago;
import com.mercadopago.exceptions.MPException;
import com.mercadopago.resources.Payment;
import com.mercadopago.resources.datastructures.payment.Identification;
import com.mercadopago.resources.datastructures.payment.Payer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class CardPaymentService {
@Value("${mercado_pago_sample_access_token}")
private String mercadoPagoAccessToken;

public PaymentResponseDTO processPayment(CardPaymentDTO cardPaymentDTO) {
try {
MercadoPago.SDK.setAccessToken(mercadoPagoAccessToken);

Payment payment = new Payment();
payment.setTransactionAmount(cardPaymentDTO.getTransactionAmount())
.setToken(cardPaymentDTO.getToken())
.setDescription(cardPaymentDTO.getProductDescription())
.setInstallments(cardPaymentDTO.getInstallments())
.setPaymentMethodId(cardPaymentDTO.getPaymentMethodId());

Identification identification = new Identification();
identification.setType(cardPaymentDTO.getPayer().getIdentification().getType())
.setNumber(cardPaymentDTO.getPayer().getIdentification().getNumber());

Payer payer = new Payer();
payer.setEmail(cardPaymentDTO.getPayer().getEmail());
payer.setIdentification(identification);

payment.setPayer(payer);

Payment createdPayment = payment.save();

this.validatePaymentResult(createdPayment);

PaymentResponseDTO paymentResponseDTO = new PaymentResponseDTO(
createdPayment.getId(),
String.valueOf(createdPayment.getStatus()),
createdPayment.getStatusDetail()
);

return paymentResponseDTO;
} catch (MPException exception) {
System.out.println(exception.getMessage());
throw new MercadoPagoException(exception.getMessage());
}
}

private void validatePaymentResult(Payment createdPayment) throws MPException {
if(createdPayment.getId() == null) {
String errorMessage = "Unknown error cause";

if(createdPayment.getLastApiResponse() != null) {
String sdkErrorMessage = createdPayment.getLastApiResponse().getJsonElementResponse().getAsJsonObject().get("message").getAsString();
errorMessage = sdkErrorMessage != null ? sdkErrorMessage : errorMessage;
}

throw new MPException(errorMessage);
}
@Value("${mercado_pago_sample_access_token}")
private String mercadoPagoAccessToken;

public PaymentResponseDTO processPayment(CardPaymentDTO cardPaymentDTO) {
try {
MercadoPagoConfig.setAccessToken(mercadoPagoAccessToken);

PaymentClient paymentClient = new PaymentClient();

PaymentCreateRequest paymentCreateRequest =
PaymentCreateRequest.builder()
.transactionAmount(cardPaymentDTO.getTransactionAmount())
.token(cardPaymentDTO.getToken())
.description(cardPaymentDTO.getProductDescription())
.installments(cardPaymentDTO.getInstallments())
.paymentMethodId(cardPaymentDTO.getPaymentMethodId())
.payer(
PaymentPayerRequest.builder()
.email(cardPaymentDTO.getPayer().getEmail())
.identification(
IdentificationRequest.builder()
.type(cardPaymentDTO.getPayer().getIdentification().getType())
.number(cardPaymentDTO.getPayer().getIdentification().getNumber())
.build())
.build())
.build();

Payment createdPayment = paymentClient.create(paymentCreateRequest);

return new PaymentResponseDTO(
createdPayment.getId(),
String.valueOf(createdPayment.getStatus()),
createdPayment.getStatusDetail());
} catch (MPApiException apiException) {
System.out.println(apiException.getApiResponse().getContent());
throw new MercadoPagoException(apiException.getApiResponse().getContent());
} catch (MPException exception) {
System.out.println(exception.getMessage());
throw new MercadoPagoException(exception.getMessage());
}
}
}

0 comments on commit 55fec8c

Please sign in to comment.