-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckApiClient.java
More file actions
82 lines (65 loc) · 3.17 KB
/
Copy pathCheckApiClient.java
File metadata and controls
82 lines (65 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.checkapi;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.UUID;
public class CheckApiClient implements AutoCloseable {
private final String apiKey;
private final String baseUrl;
private final HttpClient http;
public CheckApiClient(String apiKey) {
this(apiKey, "https://check.sarmkadan.com");
}
public CheckApiClient(String apiKey, String baseUrl) {
if (apiKey == null || apiKey.isBlank()) throw new IllegalArgumentException("apiKey required");
this.apiKey = apiKey;
this.baseUrl = baseUrl.replaceAll("/+$", "");
this.http = HttpClient.newHttpClient();
}
public CheckApiResult verify(Path imagePath, double expectedAmount, String expectedCardLast4)
throws IOException, InterruptedException {
if (!Files.exists(imagePath))
throw new IOException("File not found: " + imagePath);
String boundary = UUID.randomUUID().toString();
byte[] imageBytes = Files.readAllBytes(imagePath);
String fileName = imagePath.getFileName().toString();
String body = "--" + boundary + "\r\n"
+ "Content-Disposition: form-data; name=\"image\"; filename=\"" + fileName + "\"\r\n"
+ "Content-Type: " + mimeType(fileName) + "\r\n\r\n";
String fields = "\r\n--" + boundary + "\r\n"
+ "Content-Disposition: form-data; name=\"expectedAmount\"\r\n\r\n"
+ String.format("%.2f", expectedAmount)
+ "\r\n--" + boundary + "\r\n"
+ "Content-Disposition: form-data; name=\"expectedCardLast4\"\r\n\r\n"
+ expectedCardLast4
+ "\r\n--" + boundary + "--\r\n";
byte[] prefix = body.getBytes();
byte[] suffix = fields.getBytes();
byte[] payload = new byte[prefix.length + imageBytes.length + suffix.length];
System.arraycopy(prefix, 0, payload, 0, prefix.length);
System.arraycopy(imageBytes, 0, payload, prefix.length, imageBytes.length);
System.arraycopy(suffix, 0, payload, prefix.length + imageBytes.length, suffix.length);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl + "/api/v1/verify"))
.header("X-API-Key", apiKey)
.header("Content-Type", "multipart/form-data; boundary=" + boundary)
.POST(HttpRequest.BodyPublishers.ofByteArray(payload))
.build();
HttpResponse<String> response = http.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() < 200 || response.statusCode() >= 300)
throw new CheckApiException(response.statusCode(), response.body());
return CheckApiResult.fromJson(response.body());
}
private static String mimeType(String name) {
String lower = name.toLowerCase();
if (lower.endsWith(".png")) return "image/png";
if (lower.endsWith(".webp")) return "image/webp";
return "image/jpeg";
}
@Override
public void close() {}
}