-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from rhames07/main
Update SDK version to 2.0.0
- Loading branch information
Showing
5 changed files
with
61 additions
and
70 deletions.
There are no files selected for viewing
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
8 changes: 4 additions & 4 deletions
8
src/main/java/com/mercadopago/sample/dto/PaymentResponseDTO.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
107 changes: 49 additions & 58 deletions
107
src/main/java/com/mercadopago/sample/service/CardPaymentService.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,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()); | ||
} | ||
} | ||
} |