|
@@ -1,18 +1,127 @@
|
|
|
package com.miaxis.test;
|
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
import com.miaxis.ZzjsApplication;
|
|
|
+import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
|
|
|
+import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier;
|
|
|
+import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
|
|
|
+import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
|
|
|
+import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
|
|
|
+import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
+import org.apache.http.client.HttpClient;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.junit.After;
|
|
|
+import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
import org.junit.runner.RunWith;
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.core.io.ClassPathResource;
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.security.KeyFactory;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.security.PrivateKey;
|
|
|
+import java.security.spec.InvalidKeySpecException;
|
|
|
+import java.security.spec.PKCS8EncodedKeySpec;
|
|
|
+import java.util.Base64;
|
|
|
+
|
|
|
+
|
|
|
@SpringBootTest(classes = ZzjsApplication.class)
|
|
|
@RunWith(SpringRunner.class)
|
|
|
public class NormalTest {
|
|
|
|
|
|
- @Test
|
|
|
+ private HttpClient httpClient = null;
|
|
|
+ @Before
|
|
|
public void test1() throws Exception {
|
|
|
+ ClassPathResource classPathResource = new ClassPathResource("");
|
|
|
+ File file = new ClassPathResource("wechatpay/apiclient_key.pem").getFile();
|
|
|
+ String realPath =file.getAbsolutePath();
|
|
|
+ PrivateKey privateKey = getPrivateKey(realPath);
|
|
|
+// // 加载商户私钥(privateKey:私钥字符串)
|
|
|
+// PrivateKey merchantPrivateKey = PemUtil
|
|
|
+// .loadPrivateKey(new ByteArrayInputStream(privateKey.getBytes("utf-8")));
|
|
|
+
|
|
|
+ // 加载平台证书(mchId:商户号,mchSerialNo:商户证书序列号,apiV3Key:V3密钥)
|
|
|
+ AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
|
|
|
+ new WechatPay2Credentials("1608699504",
|
|
|
+ new PrivateKeySigner("487159E168001CDBA0EFE2C988249A84023AD6CC", privateKey)),
|
|
|
+ "qqwweerrttyyuuiioopp123456789000".getBytes("utf-8"));
|
|
|
+
|
|
|
+ // 初始化httpClient
|
|
|
+ httpClient = WechatPayHttpClientBuilder.create()
|
|
|
+ .withMerchant("1608699504", "487159E168001CDBA0EFE2C988249A84023AD6CC", privateKey)
|
|
|
+ .withValidator(new WechatPay2Validator(verifier)).build();
|
|
|
+
|
|
|
+ //创建订单
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ @Test
|
|
|
+ public void CreateOrder() throws Exception{
|
|
|
+ HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi");
|
|
|
+ httpPost.addHeader("Accept", "application/json");
|
|
|
+ httpPost.addHeader("Content-type","application/json; charset=utf-8");
|
|
|
+
|
|
|
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+
|
|
|
+ ObjectNode rootNode = objectMapper.createObjectNode();
|
|
|
+ rootNode.put("mchid","1608699504")
|
|
|
+ .put("appid", "wx8f43db501343feab")
|
|
|
+ .put("description", "Image形象店-深圳腾大-QQ公仔")
|
|
|
+ .put("notify_url", "https://www.weixin.qq.com/wxpay/pay.php")
|
|
|
+ .put("out_trade_no", "1217752501201407033233368044");
|
|
|
+ rootNode.putObject("amount")
|
|
|
+ .put("total", 1);
|
|
|
+ rootNode.putObject("payer")
|
|
|
+ .put("openid", "oO7PJ5GVKa0c_rfU34yIO0RgfTTg");
|
|
|
+
|
|
|
+ objectMapper.writeValue(bos, rootNode);
|
|
|
+
|
|
|
+ httpPost.setEntity(new StringEntity(bos.toString("UTF-8")));
|
|
|
+ HttpResponse response = httpClient.execute(httpPost);
|
|
|
+
|
|
|
+ String bodyAsString = EntityUtils.toString(response.getEntity());
|
|
|
+ System.out.println(bodyAsString);
|
|
|
+ System.out.println(bodyAsString);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取私钥。
|
|
|
+ *
|
|
|
+ * @param filename 私钥文件路径 (required)
|
|
|
+ * @return 私钥对象
|
|
|
+ */
|
|
|
+ public static PrivateKey getPrivateKey(String filename) throws IOException {
|
|
|
+ String content = new String(Files.readAllBytes(Paths.get(filename)), "utf-8");
|
|
|
+ try {
|
|
|
+ String privateKey = content.replace("-----BEGIN PRIVATE KEY-----", "")
|
|
|
+ .replace("-----END PRIVATE KEY-----", "")
|
|
|
+ .replaceAll("\\s+", "");
|
|
|
|
|
|
+ KeyFactory kf = KeyFactory.getInstance("RSA");
|
|
|
+ return kf.generatePrivate(
|
|
|
+ new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey)));
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+ throw new RuntimeException("当前Java环境不支持RSA", e);
|
|
|
+ } catch (InvalidKeySpecException e) {
|
|
|
+ throw new RuntimeException("无效的密钥格式");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|