|
|
@@ -4,6 +4,8 @@ import com.fasterxml.jackson.core.JsonParser;
|
|
|
import com.fasterxml.jackson.core.JsonToken;
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.huifu.adapay.APIUrlEnum;
|
|
|
+import com.huifu.adapay.core.AdapayCore;
|
|
|
import com.ym.mec.biz.dal.dto.ReceiptEntryDto;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
@@ -21,7 +23,7 @@ final class ProviderBills {
|
|
|
NotReady(String message) { super(message); }
|
|
|
}
|
|
|
private static final ObjectMapper JSON = new ObjectMapper().enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
|
|
|
- private static final String WX = "api.mch.weixin.qq.com", ADA = "https://api.adapay.tech/v1/";
|
|
|
+ private static final String WX = "api.mch.weixin.qq.com";
|
|
|
private final String channel;
|
|
|
private final BillCredentials keys;
|
|
|
private final BillHttp http;
|
|
|
@@ -59,9 +61,8 @@ final class ProviderBills {
|
|
|
}
|
|
|
String owner;
|
|
|
if ("HUIFU".equals(channel)) {
|
|
|
- JsonNode ticket = adaBill(Collections.singletonMap("bill_date", day.format(DateTimeFormatter.BASIC_ISO_DATE)));
|
|
|
- download = BillHttp.download(text(ticket, "bill_download_url"), "jarvisfile.oss-cn-shanghai.aliyuncs.com");
|
|
|
- owner = keys.value("appId");
|
|
|
+ byte[] file = huifuFile(day);
|
|
|
+ return BillFiles.parse(channel, keys.value("appId"), day, file);
|
|
|
} else {
|
|
|
if (day.isBefore(now.toLocalDate().minusYears(6))) throw new BillHttp.Failure("超出支付宝近六年账单窗口");
|
|
|
Map<String, String> biz = new TreeMap<>();
|
|
|
@@ -97,19 +98,75 @@ final class ProviderBills {
|
|
|
throw new BillHttp.Failure("HUIFU".equals(channel) ? "汇付可用余额未证实等于当前可提现金额" : "支付宝余额账户未提供已确认的可提现金额");
|
|
|
}
|
|
|
|
|
|
+ private byte[] huifuFile(LocalDate day) throws Exception {
|
|
|
+ URI download;
|
|
|
+ try {
|
|
|
+ JsonNode ticket = adaBill(Collections.singletonMap("bill_date", day.format(DateTimeFormatter.BASIC_ISO_DATE)));
|
|
|
+ download = BillHttp.download(text(ticket, "bill_download_url"), "jarvisfile.oss-cn-shanghai.aliyuncs.com");
|
|
|
+ } catch (NotReady pending) {
|
|
|
+ throw pending;
|
|
|
+ } catch (Exception failure) {
|
|
|
+ throw new BillHttp.Failure("汇付申请账单地址失败:" + BillHttp.reason(failure));
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return huifuBody(http.send(download, Collections.emptyMap(), null, BillHttp.FILE_LIMIT));
|
|
|
+ } catch (Exception failure) {
|
|
|
+ throw new BillHttp.Failure("汇付下载账单文件失败:" + BillHttp.reason(failure));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static byte[] huifuBody(BillHttp.Reply reply) throws BillHttp.Failure {
|
|
|
+ if (reply.status == 200) return reply.data;
|
|
|
+ String code = "";
|
|
|
+ String body = new String(reply.data, StandardCharsets.UTF_8).trim();
|
|
|
+ String message = body;
|
|
|
+ if (body.startsWith("{")) {
|
|
|
+ try {
|
|
|
+ JsonNode error = JSON.readTree(body);
|
|
|
+ JsonNode data = error.path("data");
|
|
|
+ if (data.isTextual()) data = JSON.readTree(data.textValue());
|
|
|
+ if (!error.hasNonNull("error_code") && data != null && data.isObject()) error = data;
|
|
|
+ code = error.path("error_code").asText(error.path("code").asText());
|
|
|
+ message = error.path("error_msg").asText(error.path("message").asText());
|
|
|
+ } catch (java.io.IOException ignored) {
|
|
|
+ // Keep the HTTP status when an intermediary returns an invalid response.
|
|
|
+ }
|
|
|
+ } else if (body.startsWith("<")) {
|
|
|
+ java.util.regex.Matcher match = java.util.regex.Pattern.compile("<Code>([A-Za-z]{1,48})</Code>").matcher(body);
|
|
|
+ if (match.find()) code = match.group(1);
|
|
|
+ }
|
|
|
+ String detail;
|
|
|
+ switch (code) {
|
|
|
+ case "security_exception": case "SignatureDoesNotMatch": detail = "签名校验失败"; break;
|
|
|
+ case "invalid_api_key": case "InvalidAccessKeyId": detail = "访问密钥无效"; break;
|
|
|
+ case "ExpiredToken": case "RequestExpired": case "token_invalided": detail = "访问凭据已过期"; break;
|
|
|
+ case "AccessDenied": detail = body.contains("<Message>Request has expired</Message>") ? "下载地址已过期" : "访问被拒绝"; break;
|
|
|
+ case "RequestTimeTooSkewed": detail = "请求时间偏差过大"; break;
|
|
|
+ case "config_exception": detail = "渠道配置错误"; break;
|
|
|
+ case "mer_not_register": detail = "商户未入驻"; break;
|
|
|
+ case "request_parameter_error": case "request_param_error": detail = "账单请求参数错误"; break;
|
|
|
+ default:
|
|
|
+ if ("Invalid API key".equalsIgnoreCase(message)) detail = "API Key 无效";
|
|
|
+ else if ("Invalid signature".equalsIgnoreCase(message)) detail = "签名校验失败";
|
|
|
+ else detail = reply.status == 401 ? "认证未通过,渠道未提供可识别的具体原因" : "渠道未提供可识别的具体原因";
|
|
|
+ code = "";
|
|
|
+ }
|
|
|
+ throw new BillHttp.Failure("HTTP " + reply.status + "," + detail + (code.isEmpty() ? "" : "(" + code + ")"));
|
|
|
+ }
|
|
|
+
|
|
|
private JsonNode adaBill(Map<String, String> params) throws Exception {
|
|
|
- String path = "bill/download";
|
|
|
+ String path = APIUrlEnum.BILL_DOWNLOAD_V1.getCode();
|
|
|
String body = JSON.writeValueAsString(params);
|
|
|
- String url = ADA + path;
|
|
|
+ String url = AdapayCore.apiBase + path;
|
|
|
Map<String, String> headers = new HashMap<>();
|
|
|
headers.put("Authorization", keys.value("apiKey"));
|
|
|
headers.put("Signature", keys.sign("SHA1withRSA", url + body));
|
|
|
headers.put("Content-Type", "application/json;charset=UTF-8");
|
|
|
- headers.put("sdk_version", "Java_v1.0.0");
|
|
|
+ headers.put("sdk_version", AdapayCore.sdkVersion);
|
|
|
headers.put("api_name", path);
|
|
|
headers.put("seq_id", UUID.randomUUID().toString());
|
|
|
BillHttp.Reply reply = http.send(URI.create(url), headers, body, BillHttp.API_LIMIT);
|
|
|
- JsonNode envelope = JSON.readTree(reply.ok());
|
|
|
+ JsonNode envelope = JSON.readTree(huifuBody(reply));
|
|
|
String data = text(envelope, "data");
|
|
|
keys.verify(null, "SHA1withRSA", data.getBytes(StandardCharsets.UTF_8), text(envelope, "signature"));
|
|
|
JsonNode result = JSON.readTree(data);
|