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

Commit e2abb76

Browse files
authored
Merge pull request #41 from microservices-demo/improve-payment-errors
Improved payment errors
2 parents 85d49de + 8162b2f commit e2abb76

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

src/main/java/works/weave/socks/orders/controllers/OrdersController.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,19 @@ CustomerOrder newOrder(@RequestBody NewOrderResource item) {
8181
cardFuture.get(timeout, TimeUnit.SECONDS).getContent(),
8282
customerFuture.get(timeout, TimeUnit.SECONDS).getContent(),
8383
amount);
84-
LOG.debug("Sending payment request: " + paymentRequest);
84+
LOG.info("Sending payment request: " + paymentRequest);
8585
Future<PaymentResponse> paymentFuture = asyncGetService.postResource(
8686
config.getPaymentUri(),
8787
paymentRequest,
8888
new ParameterizedTypeReference<PaymentResponse>() {
8989
});
9090
PaymentResponse paymentResponse = paymentFuture.get(timeout, TimeUnit.SECONDS);
91-
LOG.debug("Received payment response: " + paymentResponse);
91+
LOG.info("Received payment response: " + paymentResponse);
9292
if (paymentResponse == null) {
9393
throw new PaymentDeclinedException("Unable to parse authorisation packet");
9494
}
9595
if (!paymentResponse.isAuthorised()) {
96-
throw new PaymentDeclinedException("Payment declined");
96+
throw new PaymentDeclinedException(paymentResponse.getMessage());
9797
}
9898

9999
// Ship
@@ -159,14 +159,14 @@ private float calculateTotal(List<Item> items) {
159159
return amount;
160160
}
161161

162-
@ResponseStatus(value = HttpStatus.NOT_ACCEPTABLE, reason = "Payment declined")
162+
@ResponseStatus(value = HttpStatus.NOT_ACCEPTABLE)
163163
public class PaymentDeclinedException extends IllegalStateException {
164164
public PaymentDeclinedException(String s) {
165165
super(s);
166166
}
167167
}
168168

169-
@ResponseStatus(value = HttpStatus.NOT_ACCEPTABLE, reason = "Invalid order request. Order requires customer, address, card and items.")
169+
@ResponseStatus(value = HttpStatus.NOT_ACCEPTABLE)
170170
public class InvalidOrderException extends IllegalStateException {
171171
public InvalidOrderException(String s) {
172172
super(s);

src/main/java/works/weave/socks/orders/resources/PaymentResponse.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22

33
public class PaymentResponse {
44
private boolean authorised = false;
5+
private String message;
56

67
// For jackson
78
public PaymentResponse() {
89
}
910

10-
public PaymentResponse(boolean authorised) {
11+
public PaymentResponse(boolean authorised, String message) {
1112
this.authorised = authorised;
13+
this.message = message;
1214
}
1315

1416
@Override
1517
public String toString() {
1618
return "PaymentResponse{" +
1719
"authorised=" + authorised +
20+
", message=" + message +
1821
'}';
1922
}
2023

@@ -25,4 +28,12 @@ public boolean isAuthorised() {
2528
public void setAuthorised(boolean authorised) {
2629
this.authorised = authorised;
2730
}
31+
32+
public void setMessage(String message) {
33+
this.message = message;
34+
}
35+
36+
public String getMessage() {
37+
return message;
38+
}
2839
}

src/main/java/works/weave/socks/orders/values/PaymentResponse.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22

33
public class PaymentResponse {
44
private boolean authorised = false;
5+
private String message;
56

67
// For jackson
78
public PaymentResponse() {
89
}
910

10-
public PaymentResponse(boolean authorised) {
11+
public PaymentResponse(boolean authorised, String message) {
1112
this.authorised = authorised;
13+
this.message = message;
1214
}
1315

1416
@Override
1517
public String toString() {
1618
return "PaymentResponse{" +
1719
"authorised=" + authorised +
20+
", message=" + message +
1821
'}';
1922
}
2023

@@ -25,4 +28,12 @@ public boolean isAuthorised() {
2528
public void setAuthorised(boolean authorised) {
2629
this.authorised = authorised;
2730
}
31+
32+
public void setMessage(String message) {
33+
this.message = message;
34+
}
35+
36+
public String getMessage() {
37+
return message;
38+
}
2839
}

0 commit comments

Comments
 (0)