Skip to content

Commit

Permalink
#425 Don't Display Whole FailReason If Payment Error.
Browse files Browse the repository at this point in the history
  • Loading branch information
criske committed Apr 24, 2021
1 parent 00a4f27 commit 7505f73
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 91 deletions.
13 changes: 7 additions & 6 deletions src/main/java/com/selfxdsd/selfweb/api/ContractsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -581,27 +581,25 @@ public ResponseEntity<String> payInvoice(
final Wallet wallet = project.wallets().active();
payment = wallet.pay(found);
}
final Invoice active = contract.invoices().active();
if(payment == null) {
final Invoice active = contract.invoices().active();
resp = ResponseEntity.ok(
Json.createObjectBuilder()
.add("paid", found.invoiceId())
.add("active", new JsonInvoice(active))
.build()
.toString()
);
} else {
} else if (payment.status()
.equals(Payment.Status.SUCCESSFUL)) {
final Invoice active = contract.invoices().active();
resp = ResponseEntity.ok(
Json.createObjectBuilder()
.add("paid", found.invoiceId())
.add(
"payment",
Json.createObjectBuilder()
.add("status", payment.status())
.add(
"failReason",
payment.failReason()
).add(
"transactionId",
payment.transactionId()
).add(
Expand All @@ -615,6 +613,9 @@ public ResponseEntity<String> payInvoice(
.build()
.toString()
);
} else {
resp = ResponseEntity.badRequest()
.body("Something went wrong, please try again.");
}
}
}
Expand Down
23 changes: 10 additions & 13 deletions src/main/java/com/selfxdsd/selfweb/api/output/JsonInvoice.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.selfxdsd.api.Payment;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import java.math.BigDecimal;
import java.text.NumberFormat;
Expand Down Expand Up @@ -55,10 +54,9 @@ public JsonInvoice(final Invoice invoice) {
*/
public JsonInvoice(final Invoice invoice, final boolean full) {
super(() -> {
final JsonObject json;
JsonObjectBuilder builder;
final JsonObjectBuilder builder = Json.createObjectBuilder();
if(!full) {
builder = Json.createObjectBuilder()
builder
.add("id", invoice.invoiceId())
.add("createdAt", String.valueOf(invoice.createdAt()))
.add("isPaid", invoice.isPaid())
Expand All @@ -80,7 +78,7 @@ public JsonInvoice(final Invoice invoice, final boolean full) {
)
);
} else {
builder = Json.createObjectBuilder()
builder
.add("id", invoice.invoiceId())
.add("createdAt", String.valueOf(invoice.createdAt()))
.add("isPaid", invoice.isPaid())
Expand All @@ -105,22 +103,21 @@ public JsonInvoice(final Invoice invoice, final boolean full) {
);
}
final Payment latest = invoice.latest();
if(latest == null) {
json = builder.build();
} else {
json = builder.add(
// attach only if the latest payment is successfully paid.
// this way the fail reason will not leak to client;
if (latest != null && latest.status()
.equals(Payment.Status.SUCCESSFUL)) {
builder.add(
"latestPayment",
Json.createObjectBuilder()
.add("status", latest.status())
.add("failReason", latest.failReason())
.add("transactionId", latest.transactionId())
.add(
"timestamp",
String.valueOf(latest.paymentTime())
).build()
).build();
);
}
return json;
return builder.build();
});
}
}
14 changes: 0 additions & 14 deletions src/main/resources/public/js/getContributor.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,20 +241,6 @@ function invoiceAsTableRow(contract) {
var latestPayment = invoice.latestPayment;
if(latestPayment === undefined) {
status = "Active";
} else {
var timestamp = latestPayment.timestamp;
var failMessage;
if(timestamp.length > 0) {
timestamp = timestamp.split('T')[0];
failMessage = timestamp + ": " + latestPayment.failReason;
} else {
failMessage = latestPayment.failReason;
}

status = "Payment failed " + "<i class='fa fa-exclamation-triangle fa-lg' style='color:red;' aria-hidden='true' "
+ "data-toggle='tooltip' data-placement='top' "
+ "data-original-title=\"" + failMessage.replaceAll('"', '\'') + "\">"
+"</i>";
}
downloadIcons="<a href='" + invoicePdfHref + "' target='_blank' class='downloadInvoice'>" + "<i title='Your Invoice To The Project' class='fa fa-file-pdf-o fa-lg'></i></a>"
} else {
Expand Down
73 changes: 15 additions & 58 deletions src/main/resources/public/js/wallets.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,57 +307,17 @@ function payInvoice(invoice, contract, payButton) {
success: function (json) {
$("#invoicesBody").hide();
$("#loadingInvoices").show();
if(json.payment.status == 'SUCCESSFUL') {
payButton.hide();
$('#invoicesTable > tbody > tr').each(
function (index, row) {
if ($(row).find("td:eq(0)").text() == json.paid) {
$(row).find("td:eq(3)").text("Paid");
}
}
);
$("#invoicesTable").DataTable().row.add(
$(newInvoiceAsTableRow(json.active))[0]
).draw();
} else {
$('#invoicesTable > tbody > tr').each(
function (index, row) {
if ($(row).find("td:eq(0)").text() == json.paid) {
var timestamp = json.payment.timestamp;
var failMessage;
if(timestamp.length > 0) {
timestamp = timestamp.split('T')[0];
failMessage = timestamp + ": " + json.payment.failReason;
} else {
failMessage = json.payment.failReason;
}
var status = "Payment failed " + "<i class='fa fa-exclamation-triangle fa-lg' style='color:red;' aria-hidden='true' "
+ "data-toggle='tooltip' data-placement='top' "
+ "data-original-title=\"" + failMessage.replaceAll('"', '\'') + "\">"
+"</i>";

$(row).find("td:eq(3)").html(status);
$('[data-toggle="tooltip"]').tooltip();
}
payButton.hide();
$('#invoicesTable > tbody > tr').each(
function (index, row) {
if ($(row).find("td:eq(0)").text() == json.paid) {
$(row).find("td:eq(3)").text("Paid");
}
);
payButton.html('<i class="fa fa-credit-card fa-lg"></i>');
payButton.on(
"click",
function(event) {
event.preventDefault();
var message = "Are you sure you want to make this payment?"
if(activeWallet.type == 'FAKE') {
message += ' You are using a fake wallet, the payment will be fictive.'
}
confirmDialog
.create(message, "Warning", "Yes")
.then(
() => payInvoice(invoice, contract, $(this))
);
}
);
}
}
);
$("#invoicesTable").DataTable().row.add(
$(newInvoiceAsTableRow(json.active))[0]
).draw();
$("#loadingInvoices").hide();
$("#invoicesBody").show();
},
Expand All @@ -380,14 +340,11 @@ function payInvoice(invoice, contract, payButton) {
);
}
);
if(jqXHR.status == 412) {
alert(JSON.parse(jqXHR.responseText).message);
} else {
alert(
"Something went wrong (" + jqXHR.status + ")." +
"Please, refresh the page and try again."
);
}
confirmDialog
.create(jqXHR.responseText, "Error", "Yes")
.then(()=> {
payInvoice(invoice, contract, payButton);
});
}
}
);
Expand Down
Loading

0 comments on commit 7505f73

Please sign in to comment.