Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #41 from microservices-demo/improve-payment-errors
Browse files Browse the repository at this point in the history
Improved payment errors
  • Loading branch information
pidster authored Jan 20, 2017
2 parents 85d49de + 8162b2f commit e2abb76
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@ CustomerOrder newOrder(@RequestBody NewOrderResource item) {
cardFuture.get(timeout, TimeUnit.SECONDS).getContent(),
customerFuture.get(timeout, TimeUnit.SECONDS).getContent(),
amount);
LOG.debug("Sending payment request: " + paymentRequest);
LOG.info("Sending payment request: " + paymentRequest);
Future<PaymentResponse> paymentFuture = asyncGetService.postResource(
config.getPaymentUri(),
paymentRequest,
new ParameterizedTypeReference<PaymentResponse>() {
});
PaymentResponse paymentResponse = paymentFuture.get(timeout, TimeUnit.SECONDS);
LOG.debug("Received payment response: " + paymentResponse);
LOG.info("Received payment response: " + paymentResponse);
if (paymentResponse == null) {
throw new PaymentDeclinedException("Unable to parse authorisation packet");
}
if (!paymentResponse.isAuthorised()) {
throw new PaymentDeclinedException("Payment declined");
throw new PaymentDeclinedException(paymentResponse.getMessage());
}

// Ship
Expand Down Expand Up @@ -159,14 +159,14 @@ private float calculateTotal(List<Item> items) {
return amount;
}

@ResponseStatus(value = HttpStatus.NOT_ACCEPTABLE, reason = "Payment declined")
@ResponseStatus(value = HttpStatus.NOT_ACCEPTABLE)
public class PaymentDeclinedException extends IllegalStateException {
public PaymentDeclinedException(String s) {
super(s);
}
}

@ResponseStatus(value = HttpStatus.NOT_ACCEPTABLE, reason = "Invalid order request. Order requires customer, address, card and items.")
@ResponseStatus(value = HttpStatus.NOT_ACCEPTABLE)
public class InvalidOrderException extends IllegalStateException {
public InvalidOrderException(String s) {
super(s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

public class PaymentResponse {
private boolean authorised = false;
private String message;

// For jackson
public PaymentResponse() {
}

public PaymentResponse(boolean authorised) {
public PaymentResponse(boolean authorised, String message) {
this.authorised = authorised;
this.message = message;
}

@Override
public String toString() {
return "PaymentResponse{" +
"authorised=" + authorised +
", message=" + message +
'}';
}

Expand All @@ -25,4 +28,12 @@ public boolean isAuthorised() {
public void setAuthorised(boolean authorised) {
this.authorised = authorised;
}

public void setMessage(String message) {
this.message = message;
}

public String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

public class PaymentResponse {
private boolean authorised = false;
private String message;

// For jackson
public PaymentResponse() {
}

public PaymentResponse(boolean authorised) {
public PaymentResponse(boolean authorised, String message) {
this.authorised = authorised;
this.message = message;
}

@Override
public String toString() {
return "PaymentResponse{" +
"authorised=" + authorised +
", message=" + message +
'}';
}

Expand All @@ -25,4 +28,12 @@ public boolean isAuthorised() {
public void setAuthorised(boolean authorised) {
this.authorised = authorised;
}

public void setMessage(String message) {
this.message = message;
}

public String getMessage() {
return message;
}
}

0 comments on commit e2abb76

Please sign in to comment.