Althars123 há 4 anos atrás
pai
commit
e5c7fbb88f

+ 35 - 6
zzjs-admin/src/main/java/com/miaxis/app/controller/film/FilmController.java

@@ -13,6 +13,7 @@ import com.miaxis.common.core.page.ResponsePageInfo;
 import com.miaxis.common.sms.MD5Utils;
 import com.miaxis.common.utils.SecurityUtils;
 import com.miaxis.common.utils.StringUtils;
+import com.miaxis.common.utils.uuid.CommonUtils;
 import com.miaxis.customer.dto.AppletCustomerInfoDto;
 import com.miaxis.customer.service.ICustomerInfoService;
 import com.miaxis.customer.vo.ExhibitionCustomerInfoVo;
@@ -35,9 +36,14 @@ import org.apache.http.impl.client.HttpClientBuilder;
 import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.util.EntityUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.core.io.ClassPathResource;
 import org.springframework.web.bind.annotation.*;
 
 import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.security.PrivateKey;
+import java.security.Signature;
 import java.util.*;
 
 /**
@@ -58,16 +64,21 @@ public class FilmController extends BaseController {
     @Autowired
     private WxpayConfig wxpayConfig;
 
+    @Value("${app.appid}")
+    private String appid;
+
     private static String appKey = "10000000000";
 
     private static String appSecret = "25f9e794323b453885f5181f1b624d0b";
 
+
+
     /**
      * 微信支付获取预订单id
      */
     @GetMapping(value = "/prepareOrder")
     @ApiOperation("微信支付获取预订单id")
-    public Response getPrepareOrderId() throws Exception{
+    public Response<JSONObject> getPrepareOrderId() throws Exception{
         HttpPost httpPost = new HttpPost(wxpayConfig.getV3url());
         httpPost.addHeader("Accept", "application/json");
         httpPost.addHeader("Content-type","application/json; charset=utf-8");
@@ -89,14 +100,32 @@ public class FilmController extends BaseController {
         httpPost.setEntity(new StringEntity(bos.toString("UTF-8")));
         HttpResponse response = httpClient.execute(httpPost);
         String bodyAsString = EntityUtils.toString(response.getEntity());
+        String  packageStr = "prepay_id="+JSONObject.parseObject(bodyAsString).get("prepay_id");
         JSONObject jsonObject = new JSONObject();
-        jsonObject.put("package",bodyAsString.replaceAll("\"",""));
+        jsonObject.put("package",packageStr);
         String nonce_str = RandomStringUtils.randomAlphanumeric(32);
         jsonObject.put("nonceStr",nonce_str);
-
-
-
-        return Response.success();
+        long timestamp =  System.currentTimeMillis()/1000;
+        jsonObject.put("timeStamp",timestamp);
+        jsonObject.put("signType","RSA");
+        StringBuffer sb = new StringBuffer();
+        sb.append(appid + "\\n");
+        sb.append(timestamp + "\\n");
+        sb.append(nonce_str + "\\n");
+        sb.append(packageStr+ "\\n");
+        System.out.println(sb);
+
+        File file = new ClassPathResource("wechatpay/apiclient_key.pem").getFile();
+        String realPath =file.getAbsolutePath();
+        PrivateKey privateKey = CommonUtils.getPrivateKey(realPath);
+        // 进行签名服务
+        Signature signature = Signature.getInstance("SHA256withRSA");
+        signature.initSign(privateKey);
+        signature.update(sb.toString().getBytes("UTF-8"));
+        byte[] signedData = signature.sign();
+        String base64Str =  Base64.getEncoder().encodeToString(signedData);
+        jsonObject.put("paySign",base64Str);
+        return Response.success(jsonObject);
 
     }
 

+ 2 - 22
zzjs-common/src/main/java/com/miaxis/common/config/BeanConfig.java

@@ -1,5 +1,6 @@
 package com.miaxis.common.config;
 
+import com.miaxis.common.utils.uuid.CommonUtils;
 import com.qcloud.cos.COSClient;
 import com.qcloud.cos.ClientConfig;
 import com.qcloud.cos.auth.BasicCOSCredentials;
@@ -72,7 +73,7 @@ public class BeanConfig {
     public HttpClient wxpayConfigBean() throws Exception{
         File file = new ClassPathResource("wechatpay/apiclient_key.pem").getFile();
         String realPath =file.getAbsolutePath();
-        PrivateKey privateKey = getPrivateKey(realPath);
+        PrivateKey privateKey = CommonUtils.getPrivateKey(realPath);
         // 加载平台证书(mchId:商户号,mchSerialNo:商户证书序列号,apiV3Key:V3密钥)
         AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
                 new WechatPay2Credentials(wxpayConfig.getMerchantId(),
@@ -86,26 +87,5 @@ public class BeanConfig {
         return httpClient;
 
     }
-    /**
-     * 获取私钥。
-     *
-     * @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("无效的密钥格式");
-        }
-    }
 }

+ 36 - 0
zzjs-common/src/main/java/com/miaxis/common/utils/uuid/CommonUtils.java

@@ -0,0 +1,36 @@
+package com.miaxis.common.utils.uuid;
+
+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;
+
+public class CommonUtils {
+    /**
+     * 获取私钥。
+     *
+     * @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("无效的密钥格式");
+        }
+    }
+}