Skip to content

Commit

Permalink
Merge pull request #3 from mercadopago/enhancement/error-message
Browse files Browse the repository at this point in the history
feat: show sdk error message on frontend
  • Loading branch information
andreagostinho-meli authored Dec 16, 2021
2 parents b9603d7 + 21e3fa8 commit c6d15b8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
24 changes: 21 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,35 @@ app.post("/process_payment", (req, res) => {
mercadopago.payment.save(paymentData)
.then(function(response) {
const { response: data } = response;
res.status(response.status).json({
status_detail: data.status_detail,

res.status(201).json({
detail: data.status_detail,
status: data.status,
id: data.id
});
})
.catch(function(error) {
res.status(error.status).send(error);
console.log(error);
const { errorMessage, errorStatus } = validateError(error);
res.status(errorStatus).json({ error_message: errorMessage });
});
});

function validateError(error) {
let errorMessage = 'Unknown error cause';
let errorStatus = 400;

if(error.cause) {
const sdkErrorMessage = error.cause[0].description;
errorMessage = sdkErrorMessage || errorMessage;

const sdkErrorStatus = error.status;
errorStatus = sdkErrorStatus || errorStatus;
}

return { errorMessage, errorStatus };
}

app.listen(8080, () => {
console.log("The server is now running on port 8080");
open("http://localhost:8080");
Expand Down
6 changes: 5 additions & 1 deletion static/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,8 @@ footer p a:hover {
/* Payment Result Section - Start */
.container__result {
display: none;
}
}

#fail-response, #success-response {
display: none;
}
Binary file added static/img/fail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 10 additions & 3 deletions static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,16 @@ function loadCardForm() {
return response.json();
})
.then(result => {
document.getElementById("payment-id").innerText = result.id;
document.getElementById("payment-status").innerText = result.status;
document.getElementById("payment-detail").innerText = result.status_detail;
if(!result.hasOwnProperty("error_message")) {
document.getElementById("success-response").style.display = "block";
document.getElementById("payment-id").innerText = result.id;
document.getElementById("payment-status").innerText = result.status;
document.getElementById("payment-detail").innerText = result.detail;
} else {
document.getElementById("error-message").textContent = result.error_message;
document.getElementById("fail-response").style.display = "block";
}

$('.container__payment').fadeOut(500);
setTimeout(() => { $('.container__result').show(500).fadeIn(); }, 500);
})
Expand Down
20 changes: 15 additions & 5 deletions views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,21 @@ <h2>Payment Result</h2>
<div class="row justify-content-md-center">
<div class="col-md-4 product-detail">
<div class="product-info">
<br>
<p><b>ID: </b><span id="payment-id"></span></p>
<p><b>Status: </b><span id="payment-status"></span></p>
<p><b>Detail: </b><span id="payment-detail"></span></p>
<br>
<div id="fail-response">
<br/>
<img src="img/fail.png" width="350px">
<p class="text-center font-weight-bold">Something went wrong</p>
<p id="error-message" class="text-center"></p>
<br/>
</div>

<div id="success-response">
<br/>
<p><b>ID: </b><span id="payment-id"></span></p>
<p><b>Status: </b><span id="payment-status"></span></p>
<p><b>Detail: </b><span id="payment-detail"></span></p>
<br/>
</div>
</div>
</div>
</div>
Expand Down

0 comments on commit c6d15b8

Please sign in to comment.