Map<String, Object> params = new LinkedHashMap<>();
params.put("openid", vo.getOpenId());
params.put("amount", vo.getPayPrice());
params.put("payitem", vo.getProductId());
params.put("order_id", vo.getPayNo());
params.put("env", weixinpayProperties.getEnv());
String sigDataStr = JsonUtils.objToString(params);
String paySig =
hmacSha256Hex(weixinpayProperties.getAppKey(), "requestVirtualPayment&" + sigDataStr);
String paySig = calcPaySig("/xpay/currency_pay", sigDataStr, weixinpayProperties.getAppKey());
private String calcPaySig(String uri, String postBody, String appKey) {
return hmacSha256Hex(appKey, uri + "&" + postBody);
}
private String hmacSha256Hex(String key, String data) {
try {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
byte[] bytes = mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder(bytes.length * 2);
for (byte b : bytes) {
sb.append(String.format("%02x", b & 0xff));
}
return sb.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
currency_pay 是服务端 API,这里参与pay_sig的uri是 /xpay/currency_pay
但是同时也需要signature
https://developers.weixin.qq.com/miniprogram/dev/server/API/VirtualPayment/api_currency_pay.html
扣减代币是第二种吧