|
|
@ -11,6 +11,10 @@ import com.google.zxing.common.BitMatrix; |
|
|
|
import okhttp3.HttpUrl; |
|
|
|
import org.apache.commons.lang3.RandomStringUtils; |
|
|
|
|
|
|
|
import javax.crypto.Cipher; |
|
|
|
import javax.crypto.NoSuchPaddingException; |
|
|
|
import javax.crypto.spec.GCMParameterSpec; |
|
|
|
import javax.crypto.spec.SecretKeySpec; |
|
|
|
import java.io.*; |
|
|
|
import java.net.HttpURLConnection; |
|
|
|
import java.net.URL; |
|
|
@ -33,81 +37,11 @@ import java.util.UUID; |
|
|
|
public class NativeUtils { |
|
|
|
|
|
|
|
static String schema = "WECHATPAY2-SHA256-RSA2048"; |
|
|
|
|
|
|
|
// /**
|
|
|
|
// * 填写你的商户号
|
|
|
|
// */
|
|
|
|
// static String MCH_ID = "1624859575";
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * 填写证书序列号
|
|
|
|
// */
|
|
|
|
// static String SERIAL_NO = "71433832548290D95806FAF490878BB9B2677D4E";
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * 填写应用APP ID
|
|
|
|
// */
|
|
|
|
// static String APP_ID = "wxcb60fcfeaddeb3e3";
|
|
|
|
//
|
|
|
|
// /**
|
|
|
|
// * apiclient_key.pem文件地址 如C:\Users\Administrator\Desktop\wechat\apiclient_key.pem
|
|
|
|
// */
|
|
|
|
// static String PATH_PEM = "C:\\Users\\dou\\Desktop\\wxpay\\apiclient_key.pem";
|
|
|
|
|
|
|
|
/** |
|
|
|
* 请求地址 |
|
|
|
*/ |
|
|
|
static String NATIVE_URL = "https://api.mch.weixin.qq.com/v3/pay/transactions/native"; |
|
|
|
//
|
|
|
|
// /**
|
|
|
|
// * 填写你的返回地址 需要HTTPS返回地址
|
|
|
|
// */
|
|
|
|
// static String RETURN_ADDRESS = "https://test.tall.wiki/gateway/defaultwbs/debug";
|
|
|
|
|
|
|
|
// public static void main(String[] args) {
|
|
|
|
// try {
|
|
|
|
// int money = 100;
|
|
|
|
// String description = "测试支付";
|
|
|
|
// HttpUrl httpurl = HttpUrl.parse(NATIVE_URL);
|
|
|
|
// //签名表头信息 Authorization
|
|
|
|
// String orderId = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
|
|
|
|
// String body = OrderData(orderId, money, description);
|
|
|
|
// String authorization = schema + " " + getToken("POST", httpurl, body, orderId);
|
|
|
|
// //下单调用的接口,JSON格式
|
|
|
|
// String codeUrl = nativePostBody(NATIVE_URL, body, authorization);
|
|
|
|
// System.out.println(codeUrl);
|
|
|
|
//
|
|
|
|
// JSONObject jsonObject = JSONObject.parseObject(codeUrl);
|
|
|
|
// String code_url = jsonObject.getString("code_url");
|
|
|
|
// if(StrUtil.isNotBlank(code_url)){
|
|
|
|
// String s = generateQRCode(code_url);
|
|
|
|
// System.out.println(s);
|
|
|
|
// }
|
|
|
|
// } catch (Exception e) {
|
|
|
|
// e.printStackTrace();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// static String OrderData(String orderId, int money, String description) {
|
|
|
|
// // 应用ID appid
|
|
|
|
// // 直连商户号 mchid
|
|
|
|
// // 商品描述 description
|
|
|
|
// //商户订单号 out_trade_no
|
|
|
|
// //通知地址 notify_url
|
|
|
|
// //订单金额 amount对象: 总金额 total 货币类型 currency
|
|
|
|
// JSONObject jsonObject = new JSONObject();
|
|
|
|
// jsonObject.put("mchid", MCH_ID);
|
|
|
|
// jsonObject.put("out_trade_no", orderId);
|
|
|
|
// jsonObject.put("appid", APP_ID);
|
|
|
|
// jsonObject.put("description", description);
|
|
|
|
// jsonObject.put("notify_url", RETURN_ADDRESS);
|
|
|
|
// Map<String, Object> map = new HashMap<>();
|
|
|
|
// map.put("total", money);
|
|
|
|
// map.put("currency", "CNY");
|
|
|
|
// jsonObject.put("amount", map);
|
|
|
|
// return String.valueOf(jsonObject);
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
|
|
|
|
/** |
|
|
|
* 获取签名信息所有值 |
|
|
@ -240,26 +174,51 @@ public class NativeUtils { |
|
|
|
return results; |
|
|
|
} |
|
|
|
|
|
|
|
public static String generateQRCode(String code_url) throws WriterException, IOException { |
|
|
|
//生成二维码
|
|
|
|
//设置二维码的尺寸
|
|
|
|
int width = 200; |
|
|
|
int hight = 200; |
|
|
|
//创建map
|
|
|
|
Map<EncodeHintType, Object> hints = new HashMap<>(); |
|
|
|
hints.put(EncodeHintType.CHARACTER_SET,"UTF-8"); |
|
|
|
// public static void main(String[] args) throws Exception {
|
|
|
|
// String apiV3 = "Bb507Ek1mr95aAvvv9OMp2i9eWxZSv1G";
|
|
|
|
// String nonce = "bexg7pfYD50D";
|
|
|
|
// String ciphertext="i01zjaHjODHIkNkIXal6lKrfxrYtp2IZFe2NeUL/tU7si3p/G52XXqxTRQLuM03D1MTImmzLXocxVOgjNmAT3wb5/2RB6ty0HT2RJzGDnAkR0OxMQseUyiOUNRW3skJtj2/somJ/O1yHI2uktJLd1hitfu2TqoFe18ueBZdFYFHqnSKoayc3qTCKFsNLQ/gArvM0ODG3qCa2x9PypmcpDEx2U0KKdNmn/kLqDjIE6RdW9RpAhG+5wqOw3717sD5uvIg66PvUp7MdGMDiDi1SdcUqRm8x7Fl43uSIq9bAcsJxW4IT877tzkDHNjSY37iOFr9vuFpjMwLnhkZfClCfivh2R9ShrmFVlR1sXikr7rYQQGn0coSQZJ09UVd0UuwSxd5SNiLISkB/MzuLfvFSe2YTFvM/MR4iJy+JLQIEBZc7ZNhv4qrcRUOnjQlWO7G9+iUpFrviItzC7eN1KTkyOBs6QPI28uUDOt/OV548i+0Pf6xmD+rOS+6vvl1WR9F4LFUvCWr3w5uMmejyCRJmWoQY12boiwT+FxsdF0NJnoSheiY+bBZfcDJ/udpo2PBHuryJ";
|
|
|
|
// String associated_data = "transaction";
|
|
|
|
//
|
|
|
|
// byte[] key = apiV3.getBytes("UTF-8");
|
|
|
|
// WxAPIV3AesUtil aesUtil = new WxAPIV3AesUtil(key);
|
|
|
|
// String decryptToString = aesUtil.decryptToString(associated_data.getBytes("UTF-8"),nonce.getBytes("UTF-8"),ciphertext);
|
|
|
|
// System.out.println(decryptToString);
|
|
|
|
// }
|
|
|
|
|
|
|
|
/** |
|
|
|
* 解密微信ApiV3消息 |
|
|
|
*/ |
|
|
|
public static class WxAPIV3AesUtil { |
|
|
|
|
|
|
|
static final int KEY_LENGTH_BYTE = 32; |
|
|
|
static final int TAG_LENGTH_BIT = 128; |
|
|
|
private final byte[] aesKey; |
|
|
|
|
|
|
|
//创建矩阵对象 调用谷歌提供的
|
|
|
|
BitMatrix bitMatrix = new MultiFormatWriter().encode(code_url, BarcodeFormat.QR_CODE, width, hight, hints); |
|
|
|
public WxAPIV3AesUtil(byte[] key) { |
|
|
|
if (key.length != KEY_LENGTH_BYTE) { |
|
|
|
throw new IllegalArgumentException("无效的ApiV3Key,长度必须为32个字节"); |
|
|
|
} |
|
|
|
this.aesKey = key; |
|
|
|
} |
|
|
|
|
|
|
|
//创建二维码生成路径
|
|
|
|
String filePath = "C:\\Users\\dou\\Desktop\\wxpay\\"; |
|
|
|
String fileName = RandomStringUtils.randomAlphanumeric(10)+ ".jpg"; |
|
|
|
public String decryptToString(byte[] associatedData, byte[] nonce, String ciphertext) |
|
|
|
throws GeneralSecurityException, IOException { |
|
|
|
try { |
|
|
|
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); |
|
|
|
|
|
|
|
Path path = FileSystems.getDefault().getPath(filePath,fileName); |
|
|
|
SecretKeySpec key = new SecretKeySpec(aesKey, "AES"); |
|
|
|
GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH_BIT, nonce); |
|
|
|
|
|
|
|
//将创建的矩阵转换成图片
|
|
|
|
MatrixToImageWriter.writeToPath(bitMatrix,"jpg",path); |
|
|
|
return filePath + fileName; |
|
|
|
cipher.init(Cipher.DECRYPT_MODE, key, spec); |
|
|
|
cipher.updateAAD(associatedData); |
|
|
|
|
|
|
|
return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), "utf-8"); |
|
|
|
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) { |
|
|
|
throw new IllegalStateException(e); |
|
|
|
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) { |
|
|
|
throw new IllegalArgumentException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|