Skip to content

Commit

Permalink
#425 Reverted backend to original, changed only frontend.
Browse files Browse the repository at this point in the history
  • Loading branch information
criske committed Apr 24, 2021
1 parent 7505f73 commit fbc4140
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 40 deletions.
13 changes: 6 additions & 7 deletions src/main/java/com/selfxdsd/selfweb/api/ContractsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -581,25 +581,27 @@ 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 if (payment.status()
.equals(Payment.Status.SUCCESSFUL)) {
final Invoice active = contract.invoices().active();
} else {
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 @@ -613,9 +615,6 @@ public ResponseEntity<String> payInvoice(
.build()
.toString()
);
} else {
resp = ResponseEntity.badRequest()
.body("Something went wrong, please try again.");
}
}
}
Expand Down
23 changes: 13 additions & 10 deletions src/main/java/com/selfxdsd/selfweb/api/output/JsonInvoice.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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 @@ -54,9 +55,10 @@ public JsonInvoice(final Invoice invoice) {
*/
public JsonInvoice(final Invoice invoice, final boolean full) {
super(() -> {
final JsonObjectBuilder builder = Json.createObjectBuilder();
final JsonObject json;
JsonObjectBuilder builder;
if(!full) {
builder
builder = Json.createObjectBuilder()
.add("id", invoice.invoiceId())
.add("createdAt", String.valueOf(invoice.createdAt()))
.add("isPaid", invoice.isPaid())
Expand All @@ -78,7 +80,7 @@ public JsonInvoice(final Invoice invoice, final boolean full) {
)
);
} else {
builder
builder = Json.createObjectBuilder()
.add("id", invoice.invoiceId())
.add("createdAt", String.valueOf(invoice.createdAt()))
.add("isPaid", invoice.isPaid())
Expand All @@ -103,21 +105,22 @@ public JsonInvoice(final Invoice invoice, final boolean full) {
);
}
final Payment latest = invoice.latest();
// 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(
if(latest == null) {
json = builder.build();
} else {
json = 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 builder.build();
return json;
});
}
}
12 changes: 12 additions & 0 deletions src/main/resources/public/js/getContributor.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,18 @@ function invoiceAsTableRow(contract) {
var latestPayment = invoice.latestPayment;
if(latestPayment === undefined) {
status = "Active";
} else {
var timestamp = latestPayment.timestamp;
var failMessage = "Something went wrong, please try again.";
if(timestamp.length > 0) {
timestamp = timestamp.split('T')[0];
failMessage = timestamp + ": " + failMessage;
}

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 + "\">"
+"</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
63 changes: 52 additions & 11 deletions src/main/resources/public/js/wallets.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,17 +307,55 @@ function payInvoice(invoice, contract, payButton) {
success: function (json) {
$("#invoicesBody").hide();
$("#loadingInvoices").show();
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");
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();
);
$("#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 = "Something went wrong, please try again.";
if(timestamp.length > 0) {
timestamp = timestamp.split('T')[0];
failMessage = timestamp + ": " + failMessage;
}
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 + "\">"
+"</i>";

$(row).find("td:eq(3)").html(status);
$('[data-toggle="tooltip"]').tooltip();
}
}
);
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))
);
}
);
}
$("#loadingInvoices").hide();
$("#invoicesBody").show();
},
Expand All @@ -340,8 +378,11 @@ function payInvoice(invoice, contract, payButton) {
);
}
);
var message = (jqXHR.status == 412)
? JSON.parse(jqXHR.responseText).message
: "Something went wrong (" + jqXHR.status + "). Please try again.";
confirmDialog
.create(jqXHR.responseText, "Error", "Yes")
.create(message, "Error", "Yes")
.then(()=> {
payInvoice(invoice, contract, payButton);
});
Expand Down
58 changes: 46 additions & 12 deletions src/test/java/com/selfxdsd/selfweb/api/ContractsApiTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,11 @@ public void invoicePaymentIsOk(){
final Invoice invoice = this.mockInvoice(1, false,
null);
final Invoice active = this.mockInvoice(2, false,
this.mockPayment(Payment.Status.SUCCESSFUL));
this.mockPayment(Payment.Status.SUCCESSFUL, ""));
final Invoices invoices = Mockito.mock(Invoices.class);
final Wallet wallet = Mockito.mock(Wallet.class);
final Wallets wallets = Mockito.mock(Wallets.class);
final Payment payment = this.mockPayment(Payment.Status.SUCCESSFUL);
final Payment payment = this.mockPayment(Payment.Status.SUCCESSFUL, "");

Mockito.when(user.username()).thenReturn("mihai");
Mockito.when(provider.name()).thenReturn("github");
Expand Down Expand Up @@ -447,6 +447,11 @@ public void invoicePaymentIsOk(){
.getString("transactionId"),
Matchers.equalTo("tx_123")
);
MatcherAssert.assertThat(
json.getJsonObject("payment")
.getString("status"),
Matchers.equalTo(Payment.Status.SUCCESSFUL)
);
MatcherAssert.assertThat(
json.getJsonObject("active")
.getInt("id"),
Expand All @@ -455,11 +460,10 @@ public void invoicePaymentIsOk(){
}

/**
* ContractsApi.payInvoice(...) fails and the fail reason is not
* delivered to client. A generic message is sent instead.
* ContractsApi.payInvoice(...) fails with either FAILED or ERROR status.
*/
@Test
public void invoicePaymentFailsAndReasonIsNotSent(){
public void invoicePaymentFails(){
final User user = Mockito.mock(User.class);
final Provider provider = Mockito.mock(Provider.class);
final Projects projects = Mockito.mock(Projects.class);
Expand All @@ -480,10 +484,13 @@ public void invoicePaymentFailsAndReasonIsNotSent(){
);
final Invoice invoice = this.mockInvoice(1, false,
null);
final Invoice active = this.mockInvoice(2, false,
this.mockPayment(Payment.Status.SUCCESSFUL, ""));
final Invoices invoices = Mockito.mock(Invoices.class);
final Wallet wallet = Mockito.mock(Wallet.class);
final Wallets wallets = Mockito.mock(Wallets.class);
final Payment payment = this.mockPayment(Payment.Status.FAILED);
final Payment payment = this.mockPayment(Payment.Status.FAILED,
"failed");

Mockito.when(user.username()).thenReturn("mihai");
Mockito.when(provider.name()).thenReturn("github");
Expand All @@ -496,6 +503,7 @@ public void invoicePaymentFailsAndReasonIsNotSent(){

Mockito.when(contract.invoices()).thenReturn(invoices);
Mockito.when(invoices.getById(1)).thenReturn(invoice);
Mockito.when(invoices.active()).thenReturn(active);
Mockito.when(project.wallets()).thenReturn(wallets);
Mockito.when(wallets.active()).thenReturn(wallet);
Mockito.when(wallet.pay(invoice)).thenReturn(payment);
Expand All @@ -504,10 +512,35 @@ public void invoicePaymentFailsAndReasonIsNotSent(){

final ResponseEntity<String> resp =
api.payInvoice("mihai", "test", "john", 1, "DEV");
final JsonObject json = Json.createReader(
new StringReader(Objects.requireNonNull(resp.getBody()))
).readObject();
MatcherAssert.assertThat(resp.getStatusCode(),
Matchers.equalTo(HttpStatus.BAD_REQUEST));
MatcherAssert.assertThat(resp.getBody(),
Matchers.equalTo("Something went wrong, please try again."));
Matchers.equalTo(HttpStatus.OK));
MatcherAssert.assertThat(
json.getInt("paid"),
Matchers.equalTo(1)
);
MatcherAssert.assertThat(
json.getJsonObject("payment")
.getString("status"),
Matchers.equalTo(Payment.Status.FAILED)
);
MatcherAssert.assertThat(
json.getJsonObject("payment")
.getString("failReason"),
Matchers.equalTo("failed")
);
MatcherAssert.assertThat(
json.getJsonObject("payment")
.getString("transactionId"),
Matchers.equalTo("tx_123")
);
MatcherAssert.assertThat(
json.getJsonObject("active")
.getInt("id"),
Matchers.equalTo(2)
);
}

/**
Expand Down Expand Up @@ -536,7 +569,7 @@ public void invoiceIsAlreadyPaid(){
final Invoice invoice = this.mockInvoice(1, true,
null);
final Invoice active = this.mockInvoice(2, false,
this.mockPayment(Payment.Status.SUCCESSFUL));
this.mockPayment(Payment.Status.SUCCESSFUL, ""));
final Invoices invoices = Mockito.mock(Invoices.class);

Mockito.when(user.username()).thenReturn("mihai");
Expand Down Expand Up @@ -676,12 +709,13 @@ private Invoice mockInvoice(
/**
* Mocks Payment.
* @param status One of: {@link Payment.Status}.
* @param failReason Fail reason.
* @return Payment.
*/
private Payment mockPayment(final String status){
private Payment mockPayment(final String status, final String failReason){
final Payment payment = Mockito.mock(Payment.class);
Mockito.when(payment.status()).thenReturn(status);
Mockito.when(payment.failReason()).thenReturn("");
Mockito.when(payment.failReason()).thenReturn(failReason);
Mockito.when(payment.transactionId()).thenReturn("tx_123");
Mockito.when(payment.paymentTime()).thenReturn(LocalDateTime.now());
return payment;
Expand Down

0 comments on commit fbc4140

Please sign in to comment.