|
@@ -2,6 +2,7 @@ package fr.sayasoft.fake.zinc;
|
2
|
2
|
|
3
|
3
|
import com.google.gson.Gson;
|
4
|
4
|
import fr.sayasoft.zinc.sdk.domain.OrderRequest;
|
|
5
|
+import fr.sayasoft.zinc.sdk.domain.OrderResponse;
|
5
|
6
|
import fr.sayasoft.zinc.sdk.domain.ZincConstants;
|
6
|
7
|
import fr.sayasoft.zinc.sdk.domain.ZincError;
|
7
|
8
|
import fr.sayasoft.zinc.sdk.enums.ZincErrorCode;
|
|
@@ -14,6 +15,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
14
|
15
|
import org.springframework.web.bind.annotation.RequestMethod;
|
15
|
16
|
import org.springframework.web.bind.annotation.RequestParam;
|
16
|
17
|
import org.springframework.web.bind.annotation.RestController;
|
|
18
|
+import org.springframework.web.client.RestTemplate;
|
17
|
19
|
|
18
|
20
|
import java.util.concurrent.atomic.AtomicLong;
|
19
|
21
|
|
|
@@ -93,35 +95,51 @@ public class FakeZincController {
|
93
|
95
|
final String idempotencyKey = orderRequest.getIdempotencyKey();
|
94
|
96
|
|
95
|
97
|
try {
|
96
|
|
- final ZincErrorCode zincErrorCode = ZincErrorCode.valueOf(orderRequest.getClientNotes().toString());
|
97
|
|
- final ZincError zincError = ZincError.builder()
|
98
|
|
- .type(ZincConstants.error)
|
99
|
|
- .code(zincErrorCode)
|
100
|
|
- .data("{'fakeField': '" + idempotencyKey + "'}") // TODO replace with a fake Map<>
|
101
|
|
- .message(zincErrorCode.getMeaning())
|
102
|
|
- .orderRequest(orderRequest)
|
103
|
|
- .build()
|
104
|
|
- ;
|
105
|
|
- log.info("Received request to generate error code, returning: " + zincError);
|
|
98
|
+ if (null != orderRequest.getClientNotes()) {
|
|
99
|
+ final ZincErrorCode zincErrorCode = ZincErrorCode.valueOf(orderRequest.getClientNotes().toString());
|
|
100
|
+ final ZincError zincError = ZincError.builder()
|
|
101
|
+ .type(ZincConstants.error)
|
|
102
|
+ .code(zincErrorCode)
|
|
103
|
+ .data("{'fakeField': '" + idempotencyKey + "'}") // TODO replace with a fake Map<>
|
|
104
|
+ .message(zincErrorCode.getMeaning())
|
|
105
|
+ .orderRequest(orderRequest)
|
|
106
|
+ .build();
|
|
107
|
+ log.info("Received request to generate error code, returning: " + zincError);
|
106
|
108
|
/*
|
107
|
109
|
Precision obtained from Zinc support: although an error message is returned, the HTTP header is a 200
|
108
|
110
|
(and not 4XX as may be assumed)
|
109
|
111
|
*/
|
110
|
|
- return new ResponseEntity<>(
|
111
|
|
- zincError,
|
112
|
|
- HttpStatus.OK);
|
|
112
|
+ return new ResponseEntity<>(
|
|
113
|
+ zincError,
|
|
114
|
+ HttpStatus.OK);
|
|
115
|
+ }
|
113
|
116
|
} catch (IllegalArgumentException e) {
|
114
|
117
|
// nothing to do
|
115
|
|
- if (log.isInfoEnabled()){
|
|
118
|
+ if (log.isInfoEnabled()) {
|
116
|
119
|
log.info("received clientNotes: " + orderRequest.getClientNotes() + " ; will go on");
|
117
|
120
|
}
|
118
|
121
|
} catch (Exception e) {
|
119
|
122
|
log.error("An error occured", e);
|
120
|
123
|
}
|
121
|
124
|
|
|
125
|
+ if (!orderRequest.getWebhooks().isEmpty()) {
|
|
126
|
+ callbackWebHooks(orderRequest);
|
|
127
|
+ }
|
|
128
|
+
|
122
|
129
|
return new ResponseEntity<>(
|
123
|
130
|
POST_ORDER_RESPONSE.replace(POST_ORDER_RESPONSE_TO_BE_REPLACED, idempotencyKey),
|
124
|
131
|
HttpStatus.CREATED);
|
125
|
132
|
// TODO call the webhooks if present
|
126
|
133
|
}
|
|
134
|
+
|
|
135
|
+ protected void callbackWebHooks(OrderRequest orderRequest) {
|
|
136
|
+ final RestTemplate restTemplate = new RestTemplate();
|
|
137
|
+ final OrderResponse orderResponse = OrderResponse.builder().orderRequest(orderRequest).build();
|
|
138
|
+ orderRequest.getWebhooks()
|
|
139
|
+ .forEach((zincWebhookType, url) -> {
|
|
140
|
+ log.info("Calling webhook for zincWebhookType: " + zincWebhookType + " and URL: " + url);
|
|
141
|
+ restTemplate.postForObject(url, orderResponse, String.class);
|
|
142
|
+ }
|
|
143
|
+ );
|
|
144
|
+ }
|
127
|
145
|
}
|