-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
EuVatChecker.java
116 lines (98 loc) · 5.44 KB
/
EuVatChecker.java
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* This file is part of alf.io.
*
* alf.io is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* alf.io is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with alf.io. If not, see <http://www.gnu.org/licenses/>.
*/
package alfio.manager;
import alfio.manager.system.ConfigurationManager;
import alfio.model.VatDetail;
import alfio.model.system.Configuration;
import alfio.model.system.ConfigurationKeys;
import alfio.util.Json;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.log4j.Log4j2;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Supplier;
import static alfio.model.system.Configuration.getSystemConfiguration;
import static alfio.model.system.ConfigurationKeys.APPLY_VAT_FOREIGN_BUSINESS;
@Component
@Log4j2
public class EuVatChecker {
private final ConfigurationManager configurationManager;
private final OkHttpClient client = new OkHttpClient();
public EuVatChecker(ConfigurationManager configurationManager) {
this.configurationManager = configurationManager;
}
public boolean isVatCheckingEnabledFor(int organizationId) {
return checkingEnabled(configurationManager, organizationId) && StringUtils.isNotEmpty(apiAddress(configurationManager)) && StringUtils.isNotEmpty(organizerCountry(configurationManager, organizationId));
}
public Optional<VatDetail> checkVat(String vatNr, String countryCode, int organizationId) {
return performCheck(vatNr, countryCode, organizationId).apply(configurationManager, client);
}
static BiFunction<ConfigurationManager, OkHttpClient, Optional<VatDetail>> performCheck(String vatNr, String countryCode, int organizationId) {
return (configurationManager, client) -> {
boolean vatNrNotEmpty = StringUtils.isNotEmpty(vatNr);
boolean validCountryCode = StringUtils.length(StringUtils.trimToNull(countryCode)) == 2;
if(!vatNrNotEmpty || !validCountryCode) {
return Optional.empty();
}
boolean euCountryCode = configurationManager.getRequiredValue(getSystemConfiguration(ConfigurationKeys.EU_COUNTRIES_LIST)).contains(countryCode);
if(euCountryCode && checkingEnabled(configurationManager, organizationId)) {
Request request = new Request.Builder()
.url(apiAddress(configurationManager) + "?country="+countryCode.toUpperCase()+"&number="+vatNr)
.get()
.build();
try (Response resp = client.newCall(request).execute()) {
if(resp.isSuccessful()) {
return Optional.of(getVatDetail(resp, vatNr, countryCode, organizerCountry(configurationManager, organizationId)));
} else {
return Optional.empty();
}
} catch (IOException e) {
log.warn("Error while calling VAT NR check.", e);
return Optional.empty();
}
} else {
String organizerCountry = organizerCountry(configurationManager, organizationId);
Supplier<Boolean> applyVatToForeignBusiness = () -> configurationManager.getBooleanConfigValue(Configuration.from(organizationId, APPLY_VAT_FOREIGN_BUSINESS), true);
return Optional.of(new VatDetail(vatNr, countryCode, true, "", "", !organizerCountry.equals(countryCode) && !applyVatToForeignBusiness.get()));
}
};
}
private static VatDetail getVatDetail(Response resp, String vatNr, String countryCode, String organizerCountryCode) throws IOException {
ResponseBody body = resp.body();
String jsonString = body != null ? body.string() : "{}";
Map<String, String> json = Json.fromJson(jsonString, new TypeReference<Map<String, String>>() {});
boolean isValid = Boolean.parseBoolean(json.get("isValid"));
return new VatDetail(vatNr, countryCode, isValid, json.get("name"), json.get("address"), isValid && !organizerCountryCode.equals(countryCode));
}
private static String apiAddress(ConfigurationManager configurationManager) {
return configurationManager.getStringConfigValue(getSystemConfiguration(ConfigurationKeys.EU_VAT_API_ADDRESS), null);
}
private static String organizerCountry(ConfigurationManager configurationManager, int organizationId) {
return configurationManager.getStringConfigValue(Configuration.from(organizationId, ConfigurationKeys.COUNTRY_OF_BUSINESS), null);
}
private static boolean checkingEnabled(ConfigurationManager configurationManager, int organizationId) {
return configurationManager.getBooleanConfigValue(Configuration.from(organizationId, ConfigurationKeys.ENABLE_EU_VAT_DIRECTIVE), false);
}
}