|
| 1 | +package ru.chrshnv.enotiosdk.client.impl; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.GsonBuilder; |
| 5 | +import com.google.gson.reflect.TypeToken; |
| 6 | +import ru.chrshnv.enotiosdk.client.EnotIoSDKClient; |
| 7 | +import ru.chrshnv.enotiosdk.invoice.InvoiceInfoResponse; |
| 8 | +import ru.chrshnv.enotiosdk.invoice.InvoiceRequest; |
| 9 | +import ru.chrshnv.enotiosdk.invoice.InvoiceResponse; |
| 10 | +import ru.chrshnv.enotiosdk.other.Currency; |
| 11 | +import ru.chrshnv.enotiosdk.other.Response; |
| 12 | +import ru.chrshnv.enotiosdk.tariffs.Tariffs; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.lang.reflect.Type; |
| 16 | +import java.net.URI; |
| 17 | +import java.net.URISyntaxException; |
| 18 | +import java.net.http.HttpClient; |
| 19 | +import java.net.http.HttpRequest; |
| 20 | +import java.net.http.HttpResponse; |
| 21 | +import java.util.List; |
| 22 | +import java.util.UUID; |
| 23 | + |
| 24 | +public class GsonEnotIoSDKClient implements EnotIoSDKClient { |
| 25 | + private final String apiKey; |
| 26 | + private final UUID shopId; |
| 27 | + private final String enotUrl = "https://api.enot.io/"; |
| 28 | + private final HttpClient httpClient = HttpClient.newHttpClient(); |
| 29 | + private final Gson gson = new GsonBuilder().create(); |
| 30 | + |
| 31 | + public GsonEnotIoSDKClient(String apiKey, UUID shopId) { |
| 32 | + this.apiKey = apiKey; |
| 33 | + this.shopId = shopId; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public Response<Tariffs> getTariffs() throws URISyntaxException, IOException, InterruptedException { |
| 38 | + URI url = new URI(enotUrl + "/shops/" + shopId + "/payment-tariffs"); |
| 39 | + HttpRequest req = HttpRequest |
| 40 | + .newBuilder() |
| 41 | + .uri(url) |
| 42 | + .header("Content-Type", "application/json") |
| 43 | + .header("x-api-key", apiKey) |
| 44 | + .GET() |
| 45 | + .build(); |
| 46 | + |
| 47 | + HttpResponse<String> stringHttpResponse = httpClient.send(req, HttpResponse.BodyHandlers.ofString()); |
| 48 | + Type responseType = TypeToken.getParameterized(Response.class, Tariffs.class).getType(); |
| 49 | + |
| 50 | + return gson.fromJson(stringHttpResponse.body(), responseType); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public Response<InvoiceResponse> createInvoice(Double amount, String orderId) throws URISyntaxException, IOException, InterruptedException { |
| 55 | + InvoiceRequest build = InvoiceRequest.builder() |
| 56 | + .setAmount(amount) |
| 57 | + .setOrderId(orderId) |
| 58 | + .setShopId(shopId) |
| 59 | + .build(); |
| 60 | + |
| 61 | + return getInvoiceResponseResponse(build); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Response<InvoiceResponse> createInvoice(Double amount, String orderId, Currency currency, String hookUrl, String customFields, String comment, URI failUrl, URI successUrl, Integer expire, List<String> includeService, List<String> excludeService) throws URISyntaxException, IOException, InterruptedException { |
| 66 | + InvoiceRequest request = new InvoiceRequest(amount, orderId, currency, shopId, hookUrl, customFields, comment, failUrl, successUrl, expire, includeService, excludeService); |
| 67 | + |
| 68 | + return getInvoiceResponseResponse(request); |
| 69 | + } |
| 70 | + |
| 71 | + private Response<InvoiceResponse> getInvoiceResponseResponse(InvoiceRequest request) throws URISyntaxException, IOException, InterruptedException { |
| 72 | + String body = gson.toJson(request); |
| 73 | + |
| 74 | + URI url = new URI(enotUrl + "/invoice/create"); |
| 75 | + HttpRequest req = HttpRequest |
| 76 | + .newBuilder() |
| 77 | + .uri(url) |
| 78 | + .header("Content-Type", "application/json") |
| 79 | + .header("x-api-key", apiKey) |
| 80 | + .POST(HttpRequest.BodyPublishers.ofString(body)) |
| 81 | + .build(); |
| 82 | + |
| 83 | + HttpResponse<String> stringHttpResponse = httpClient.send(req, HttpResponse.BodyHandlers.ofString()); |
| 84 | + Type responseType = TypeToken.getParameterized(Response.class, InvoiceResponse.class).getType(); |
| 85 | + |
| 86 | + return gson.fromJson(stringHttpResponse.body(), responseType); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public Response<InvoiceInfoResponse> invoiceInfo(UUID shopId, UUID invoiceId, String orderId) throws URISyntaxException, IOException, InterruptedException { |
| 91 | + URI url = new URI(enotUrl + "/invoice/info?invoice_id=" + invoiceId + "&shop_id=" + shopId + "&order_id=" + orderId); |
| 92 | + |
| 93 | + HttpRequest req = HttpRequest |
| 94 | + .newBuilder() |
| 95 | + .uri(url) |
| 96 | + .header("Content-Type", "application/json") |
| 97 | + .header("x-api-key", apiKey) |
| 98 | + .GET() |
| 99 | + .build(); |
| 100 | + |
| 101 | + HttpResponse<String> stringHttpResponse = httpClient.send(req, HttpResponse.BodyHandlers.ofString()); |
| 102 | + Type responseType = TypeToken.getParameterized(Response.class, InvoiceInfoResponse.class).getType(); |
| 103 | + |
| 104 | + return gson.fromJson(stringHttpResponse.body(), responseType); |
| 105 | + } |
| 106 | +} |
0 commit comments