修改人:邹江涛

修改内容:尝试调用付款处理接口1.0
修改时间:2024/11/14
This commit is contained in:
zoujiangtao 2024-11-14 14:42:38 +08:00
parent a1956b40d3
commit 8448331791
3 changed files with 263 additions and 0 deletions

View File

@ -0,0 +1,105 @@
package shkd.sys.sys.common;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.util.ArrayList;
import java.util.Map;
/**
* @Description
* @Author Tao
* @Date 2024/11/14
*/
public class ApiEntity {
private String URL;//链接
private String method;//请求方式
private Map<String, Object> queryParams;//请求参数
private Map<String, Object> headers;//请求头参数
private JSONObject requestBody;//请求体
public String getURL() {
return URL;
}
public void setURL(String URL) {
this.URL = URL;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public Map<String, Object> getQueryParams() {
return queryParams;
}
public void setQueryParams(Map<String, Object> queryParams) {
this.queryParams = queryParams;
}
public Map<String, Object> getHeaders() {
return headers;
}
public void setHeaders(Map<String, Object> headers) {
this.headers = headers;
}
public JSONObject getRequestBody() {
return requestBody;
}
public void setRequestBody(JSONObject requestBody) {
this.requestBody = requestBody;
}
public ApiEntity(String URL, String method, Map<String, Object> queryParams, Map<String, Object> headers, JSONObject requestBody) {
this.URL = URL;
this.method = method;
this.queryParams = queryParams;
this.headers = headers;
this.requestBody = requestBody;
}
public ApiEntity() {
}
public static JSONObject getResponseBody(ApiEntity apiEntity) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
Map<String, Object> heads = apiEntity.getHeaders();
if (heads != null) {
for (String key : heads.keySet()) {
httpHeaders.add(key, heads.get(key).toString());
}
}
HttpEntity<Object> objectHttpEntity = new HttpEntity<>(httpHeaders);
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(apiEntity.getURL());
Map<String, Object> params = apiEntity.getQueryParams();
if (params != null) {
for (String key : params.keySet()) {
uriComponentsBuilder.queryParam(key, params.get(key));
}
}
HttpMethod method;
if (apiEntity.getMethod().equals("POST")) {
method = HttpMethod.POST;
} else {
method = HttpMethod.GET;
}
ResponseEntity<String> exchange = restTemplate.exchange(uriComponentsBuilder.build().toString(), method, objectHttpEntity, String.class);
return JSONObject.parseObject(exchange.getBody());
}
}

View File

@ -0,0 +1,156 @@
package shkd.sys.sys.mservice;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import shkd.sys.sys.common.ApiEntity;
import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
/**
* @Description
* @Author Tao
* @Date 2024/11/11
*/
public class BIPService {
private static final Log logger = LogFactory.getLog(BIPService.class);
public static String getBIPToken() {
String access_token = null;
long currentTimeMillis = System.currentTimeMillis();
try {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
HttpEntity<Object> objectHttpEntity = new HttpEntity<>(httpHeaders);
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("https://biptest.ctny.com.cn/iuap-api-auth/open-auth/selfAppAuth/getAccessToken")
.queryParam("appKey", "22564a240d3140d0b15582aca71a748c")
.queryParam("timestamp", currentTimeMillis)
.queryParam("signature", generateSignature("appKey22564a240d3140d0b15582aca71a748ctimestamp" + currentTimeMillis));
ResponseEntity<String> exchange = restTemplate.exchange(builder.build().toString(), HttpMethod.GET, objectHttpEntity, String.class);
JSONObject jsonObject = JSON.parseObject(exchange.getBody());
if ("00000".equals(jsonObject.getString("code"))){
JSONObject data = jsonObject.getJSONObject("data");
access_token = data.getString("access_token");
logger.info("getBIPToken → access_token{}", access_token);
return access_token;
}
} catch (Exception e) {
e.printStackTrace();
}
return access_token;
}
private static RestTemplate createRestTemplateIgnoringSsl() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(null, (chain, authType) -> true)
.build();
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(socketFactory)
.build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
return new RestTemplate(factory);
}
private static String generateSignature(String toSign) throws Exception {
// 计算 HmacSHA256 签名
byte[] hmacData = computeHmacSha256(toSign);
// Base64 编码
String base64Encoded = Base64.getEncoder().encodeToString(hmacData);
// URL 编码
return URLEncoder.encode(base64Encoded, StandardCharsets.UTF_8.toString());
}
private static byte[] computeHmacSha256(String data) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec("79f0b7daa428c1abd9272565dbc65486ab565c80".getBytes(StandardCharsets.UTF_8), "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(secretKey);
return mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
}
/**
* 组装推送BIP付款单JSON
*
* @return
*/
public static JSONObject paymentSlipsJson() {
ApiEntity apiEntity = new ApiEntity();
JSONObject root = new JSONObject();
// 创建"data"节点
JSONObject data = new JSONObject();
// 设置"data"节点的属性
data.put("accentity_code", "SIG1040100");
data.put("vouchdate", "2022-04-20");
data.put("tradetype_code", "cmp_fund_payment_other");
data.put("exchangeRateType_code", "01");
data.put("exchRate", 1);
data.put("currency_code", "CNY");
data.put("_status", "Insert");
// 创建"FundPayment_b"列表
JSONArray fundPaymentList = new JSONArray();
// 创建"FundPayment_b"列表中的第一个对象
JSONObject fundPayment = new JSONObject();
fundPayment.put("quickType_code", "6");
fundPayment.put("settlestatus", "1");
fundPayment.put("oriSum", 5000);
fundPayment.put("caobject", 2);
fundPayment.put("oppositeobjectname", "11510107MB1526717D");
fundPayment.put("_status", "Insert");
// "FundPayment_b"对象添加到列表中
fundPaymentList.add(fundPayment);
// "FundPayment_b"列表添加到"data"节点中
data.put("FundPayment_b", fundPaymentList);
// "data"节点添加到根JSON对象中
root.put("data", data);
apiEntity.setURL("");
apiEntity.setRequestBody(root);
apiEntity.setMethod("POST");
Map<String, Object> queryParams = new HashMap<>();
queryParams.put("access_token", getBIPToken());
apiEntity.setQueryParams(queryParams);
Map<String, Object> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
apiEntity.setHeaders(headers);
return ApiEntity.getResponseBody(apiEntity);
}
}

View File

@ -65,7 +65,9 @@ public class PayBillApiSavePlugin implements ApiSavePlugin, ApiSerializerPlugin
Object payeebankObj = map.get("payeebank"); Object payeebankObj = map.get("payeebank");
logger.info("payeebankObj{}", payeebankObj);
Map<String, Object> payeebankMap = (Map<String, Object>) payeebankObj; Map<String, Object> payeebankMap = (Map<String, Object>) payeebankObj;
logger.info("payeebankMap{}", payeebankMap);
bankNumber = (String) payeebankMap.get("number"); bankNumber = (String) payeebankMap.get("number");