Althars123 4 anni fa
parent
commit
1d8c4484cb

+ 93 - 0
zzjs-admin/src/main/java/com/miaxis/app/controller/film/FilmController.java

@@ -0,0 +1,93 @@
+package com.miaxis.app.controller.film;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.miaxis.common.config.WxpayConfig;
+import com.miaxis.common.constant.Constants;
+import com.miaxis.common.core.controller.BaseController;
+import com.miaxis.common.core.domain.Response;
+import com.miaxis.common.core.domain.entity.UserInfo;
+import com.miaxis.common.core.page.ResponsePageInfo;
+import com.miaxis.common.utils.SecurityUtils;
+import com.miaxis.customer.dto.AppletCustomerInfoDto;
+import com.miaxis.customer.service.ICustomerInfoService;
+import com.miaxis.customer.vo.ExhibitionCustomerInfoVo;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiImplicitParams;
+import io.swagger.annotations.ApiOperation;
+import lombok.Data;
+import lombok.RequiredArgsConstructor;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.util.EntityUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.ModelAttribute;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.io.ByteArrayOutputStream;
+import java.util.List;
+
+/**
+ * 【小程序-客户信息】Controller
+ *
+ * @author miaxis
+ * @date 2021-03-10
+ */
+@RestController
+@RequiredArgsConstructor
+@RequestMapping(Constants.STUDENT_PREFIX+"/film/wxpay")
+@Api(tags = {"【小程序-电影】"})
+public class FilmController extends BaseController {
+
+    @Autowired
+    private HttpClient httpClient;
+
+    @Autowired
+    private WxpayConfig wxpayConfig;
+
+    /**
+     * 微信支付获取预订单id
+     */
+    @GetMapping(value = "/prepareOrder")
+    @ApiOperation("微信支付获取预订单id")
+    public Response<PrepareOrderEntity> getPrepareOrderId() throws Exception{
+        HttpPost httpPost = new HttpPost(wxpayConfig.getV3url());
+        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());
+        PrepareOrderEntity prepareOrderEntity = JSON.parseObject(bodyAsString,PrepareOrderEntity.class);
+        return Response.success(prepareOrderEntity);
+
+    }
+    @Data
+    public static class  PrepareOrderEntity{
+        String prepay_id;
+    }
+
+
+
+}

+ 11 - 0
zzjs-admin/src/main/resources/application.yml

@@ -225,4 +225,15 @@ cos:
   preffix: t1
 
 
+# 微信支付
+wxpay:
+  v3url: https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi
+  merchantId: 1608699504
+  serialNumber: 487159E168001CDBA0EFE2C988249A84023AD6CC
+  v3key: qqwweerrttyyuuiioopp123456789000
+
+
+
+
+
 

+ 65 - 0
zzjs-common/src/main/java/com/miaxis/common/config/BeanConfig.java

@@ -6,9 +6,28 @@ import com.qcloud.cos.auth.BasicCOSCredentials;
 import com.qcloud.cos.auth.COSCredentials;
 import com.qcloud.cos.http.HttpProtocol;
 import com.qcloud.cos.region.Region;
+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 org.apache.http.client.HttpClient;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.core.io.ClassPathResource;
+
+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;
 
 
 @Configuration
@@ -27,6 +46,9 @@ public class BeanConfig {
     @Value("${cos.preffix}")
     private String preffix;
 
+    @Autowired
+    private  WxpayConfig wxpayConfig;
+
 
     @Bean
     public COSClient  configBean() {
@@ -44,4 +66,47 @@ public class BeanConfig {
         COSClient cosClient = new COSClient(cred, clientConfig);
         return cosClient;
     }
+
+
+    @Bean
+    public HttpClient wxpayConfigBean() throws Exception{
+        ClassPathResource classPathResource = new ClassPathResource("");
+        File file = new ClassPathResource("wechatpay/apiclient_key.pem").getFile();
+        String realPath =file.getAbsolutePath();
+        PrivateKey privateKey = getPrivateKey(realPath);
+        // 加载平台证书(mchId:商户号,mchSerialNo:商户证书序列号,apiV3Key:V3密钥)
+        AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
+                new WechatPay2Credentials(wxpayConfig.getMerchantId(),
+                        new PrivateKeySigner(wxpayConfig.getSerialNumber(), privateKey)),
+                wxpayConfig.getV3key().getBytes("utf-8"));
+
+        // 初始化httpClient
+        HttpClient httpClient = WechatPayHttpClientBuilder.create()
+                .withMerchant(wxpayConfig.getMerchantId(), wxpayConfig.getSerialNumber(), privateKey)
+                .withValidator(new WechatPay2Validator(verifier)).build();
+        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("无效的密钥格式");
+        }
+    }
 }

+ 16 - 0
zzjs-common/src/main/java/com/miaxis/common/config/WxpayConfig.java

@@ -0,0 +1,16 @@
+package com.miaxis.common.config;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+@Data
+@Component
+@ConfigurationProperties(prefix = "wxpay")
+public class WxpayConfig {
+
+    private String v3url;
+    private String merchantId;
+    private String serialNumber;
+    private String v3key;
+}

+ 8 - 0
zzjs-service/src/main/java/com/miaxis/feign/service/IWxService.java

@@ -17,6 +17,14 @@ import org.springframework.web.bind.annotation.RequestParam;
         url = "https://api.weixin.qq.com",configuration = FeignConfig.class)
 @Component
 public interface IWxService {
+    /**
+     *  获取openid
+     * @param appid
+     * @param secret
+     * @param jsCode
+     * @param grantType
+     * @return
+     */
     @GetMapping(value = "/sns/jscode2session")
     String getWxInfo(
             @RequestParam("appid") String appid,