Ver Fonte

公众号支付接口开发

zsf há 4 semanas atrás
pai
commit
346c24713c

+ 10 - 0
src/main/java/com/rf/psychological/PsychologicalApplication.java

@@ -8,12 +8,14 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.web.client.RestTemplateBuilder;
 import org.springframework.context.annotation.Bean;
 import org.springframework.core.env.Environment;
 import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.transaction.annotation.EnableTransactionManagement;
+import org.springframework.web.client.RestTemplate;
 
 import javax.persistence.EntityManager;
 
@@ -36,6 +38,14 @@ public class PsychologicalApplication {
     @Value("${spring.profiles.active}")
     static String profile;
 
+    @Autowired
+    private RestTemplateBuilder builder;
+
+    @Bean
+    public RestTemplate restTemplate(){
+        return builder.build();
+    }
+
 
     public static void main(String[] args) throws Exception {
         //检查环境

+ 1 - 1
src/main/java/com/rf/psychological/filter/JWTInterceptorConfig.java

@@ -30,7 +30,7 @@ public class JWTInterceptorConfig implements WebMvcConfigurer {
         String[] institutionLogin = new String[]{"/user/login", "/institution/loginV2", "/admin/login", "/superAdmin/login"};
         String[] webLogin = new String[]{"/user/authCode", "/user/saveUser", "/user/webLogin", "/user/webUpdatePassword", "/user/*/checkUser"};
         String[] automaticImport = new String[]{"/category/loadCategory", "/subjectInfo", "/subjectInfoList", "/importTest", "/subjectInfoUpload/importTest", "/subjectInfo/update", "/subjectInfoUpload/update"};
-        String[] wxPay = new String[]{"/api/wx-pay/native/notify","/api/wx-pay/refunds/notify","/api/wx-pay/code2openid"};
+        String[] wxPay = new String[]{"/api/wx-pay/native/notify","/api/wx-pay/refunds/notify","/api/wx-pay/code2openid","/mp/api/**","/system/temporaryUser"};
         String[] systemApi = new String[]{"/system/login","/system/forgotPassword","/system/registerUser","/system/update/*","/v1/getToken","/v1/registerUser","/api/douyin/code2Session"};
         String[] temporarily = new String []{"/category/**","/api/wx-pay/**"};
         registry.addInterceptor(jwtInterceptor)

+ 1 - 0
src/main/java/com/rf/psychological/user/rest/SystemController.java

@@ -132,6 +132,7 @@ public class SystemController extends BaseController {
            log.info("响应消息:"+resultJson.toJSONString());
            return success(resultJson);
        }catch (Exception e){
+           e.printStackTrace();
            log.error(e.getMessage());
            return fail();
        }

+ 2 - 0
src/main/java/com/rf/psychological/wxpay/config/WxPayConfig.java

@@ -67,6 +67,8 @@ public class WxPayConfig {
     //ASPI APPID
     private String aspi_appid;
 
+    private String oa_appid;
+
     /**
      * 获取商户的私钥文件
      * @param fileName

+ 24 - 5
src/main/java/com/rf/psychological/wxpay/service/impl/WxPayServiceImpl.java

@@ -304,6 +304,7 @@ public class WxPayServiceImpl implements WxPayService {
      * @param paramData 参数
      * @return
      */
+    @Transactional(rollbackFor = Exception.class)
     @Override
     public Map<String,Object> jsapiPay(JSONObject paramData) throws Exception {
         log.info("jsapiPay===="+paramData.toString());
@@ -324,12 +325,12 @@ public class WxPayServiceImpl implements WxPayService {
             return map;
         }
         //调用统一下单API
-        HttpPost httpPost = new HttpPost(wxPayConfig.getDomain().concat(WxTradeType.JSAPI.getType()));
+        HttpPost httpPost = new HttpPost(wxPayConfig.getDomain().concat(WxApiType.ASPI_PAY.getType()));
         //构建必要参数
         Gson gson = new Gson();
         Map<String,Object> paramsMap = new HashMap<>();
-        paramsMap.put("sp_appid",wxPayConfig.getAppId());
-        paramsMap.put("sp_mchid",wxPayConfig.getMchId());
+        paramsMap.put("appid",wxPayConfig.getOa_appid());
+        paramsMap.put("mchid",wxPayConfig.getMchId());
         paramsMap.put("description",description);
         paramsMap.put("out_trade_no",orderInfo.getOrderNo());
         paramsMap.put("notify_url",wxPayConfig.getNotifyDomain().concat(WxNotifyType.NATIVE_NOTIFY.getType()));
@@ -340,7 +341,7 @@ public class WxPayServiceImpl implements WxPayService {
         log.info("paramsMap:"+paramsMap);
 
         Map<String,String> payerMap = new HashMap<>();
-        payerMap.put("sp_openid",paramData.getString("openId"));
+        payerMap.put("openid",paramData.getString("openId"));
         paramsMap.put("payer",payerMap);
 
         //将body转化为json字符串
@@ -366,7 +367,25 @@ public class WxPayServiceImpl implements WxPayService {
             }
             //响应结果
             Map<String,String> resultMap = gson.fromJson(bodyAsString,HashMap.class);
-            codeUrl = resultMap.get("prepay_id");
+            //codeUrl = resultMap.get("prepay_id");
+            //构建拉起微信支付参数
+            JSONObject payParam = new JSONObject();
+            payParam.put("prepay_id",resultMap.get("prepay_id"));
+            payParam.put("timeStamp",System.currentTimeMillis()/1000+"");
+            payParam.put("package","prepay_id="+resultMap.get("prepay_id"));
+            payParam.put("signType","RSA");
+            payParam.put("appId",wxPayConfig.getAspi_appid());
+            payParam.put("nonceStr",RandomUtil.getRandomString(32));
+            Signature signature = Signature.getInstance("SHA256withRSA");
+            signature.initSign(wxPayConfig.getPrivateKey(wxPayConfig.getPrivateKeyPath()));
+            StringBuffer buffer = new StringBuffer();
+            buffer.append(payParam.getString("appId")).append("\n").
+                    append(payParam.getLong("timeStamp")).append("\n").
+                    append(payParam.getString("nonceStr")).append("\n").
+                    append(payParam.getString("package")).append("\n");
+            signature.update(buffer.toString().getBytes(StandardCharsets.UTF_8));
+            payParam.put("paySign",Base64Utils.encodeToString(signature.sign()));
+            codeUrl = payParam.toJSONString();
             orderInfo.setCodeUrl(codeUrl);
             orderInfoService.saveCodeUrl(orderInfo);
             log.info("orderInfo:" + orderInfo);

+ 13 - 0
src/main/resources/config/application-public.yml

@@ -75,6 +75,8 @@ wx.pay:
   #小程序appid
   ASPI_APPID: wx2f422a2a1cb24c3c
 
+  #公众号appId
+  oa-app-id : wx43ca40dd2be85dd6
 #抖音支付
 douyin:
   #hongduo
@@ -94,3 +96,14 @@ douyin:
   pushOrderUrl: https://developer.toutiao.com/api/apps/order/v2/push
   isDouYin: true
 
+
+#微信公众号
+weixin:
+  mptoken: D061104CAB980AC122153E6B95D63A6E
+  mpAppid: wx43ca40dd2be85dd6
+  mpSecret: efbc0f26d511fd44bfbe2a54154fee57
+  domain: https://mp.weixin.qq.com
+  accessToken: /cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s
+  mpJsapiTicket: /cgi-bin/ticket/getticket?access_token=%s&type=jsapi
+  mpCode2AccessToken: /sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code
+

+ 32 - 11
src/main/resources/config/application-test.yml

@@ -1,6 +1,6 @@
 # 机构版44测试服务器
 server:
-  port: 8090
+  port: 8447
   address: 0.0.0.0
 spring:
   datasource:
@@ -8,7 +8,7 @@ spring:
     druid:
       # 使用druid连接池
       driver-class-name: com.mysql.cj.jdbc.Driver
-      url: jdbc:mysql://49.232.26.44:3306/psychological_112?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
+      url: jdbc:mysql://49.232.26.44:3306/psychological_web_test?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
       username: root
       #112/44
       password: Mysql@.2020
@@ -72,31 +72,52 @@ spring:
         min-idle: 0
 #微信支付
 wx.pay:
+  #HD
   # 商户号
-  mch-id: 1627594233
+  mch-id: 1655519363
   #商户api证书序列号
-  mch-serial-no: 25BB4D9C58F3D9FFCBC0F882B57F7E842CBE5A23
+  mch-serial-no: 692670931729B82A48747B47EFE3142BCA73D978
   # 商户私钥文件
-  private-key-path: apiclient_key.pem
+  private-key-path: apiclient_key_hd.pem
   # API秘钥
-  api-v3-key: MentalHealth20191025JuemingTest1
+  api-v3-key: MentalHealth20191025HongduoTest1
+  # 接受结果回调地址
+  notify-domain: https://hnhong-duo.com/
+
+  #GY
+  #  mch-id: 1680226340
+  #  mch-serial-no: 2D81528EEFB70503C10339D9655408E0A583C963
+  #  api-v3-key: MentalHealth20240624HuYaTest0628
+  #  private-key-path: apiclient_key_gy.pem
+  #  notify-domain: https://guya-tech.com
+
+
   # APPID
-  app-id: wxfee6e2d1588e3f7f
+  app-id: wx43ca40dd2be85dd6
   # 微信服务器地址
   domain: https://api.mch.weixin.qq.com
-  # 接受结果回调地址
-  #notify-domain: http://49.232.26.44:8090
+  is_lan: false
   # 小程序秘钥
   ASPI_SECRET: b4edc4638f9182a1684dffdf7a1fa4c0
   #小程序code2session
   ASPI_URL: https://api.weixin.qq.com/sns/jscode2session
   #小程序appid
   ASPI_APPID: wx2f422a2a1cb24c3c
-  is_lan: false
+
+  #公众号appId
+  oa-app-id : wx43ca40dd2be85dd6
 #swagger 显示隐藏配置
 swagger:
   show: true
 
-
+#微信公众号
+weixin:
+  mptoken: D061104CAB980AC122153E6B95D63A6E
+  mpAppid: wx43ca40dd2be85dd6
+  mpSecret: efbc0f26d511fd44bfbe2a54154fee57
+  domain: https://api.weixin.qq.com
+  accessToken: /cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s
+  mpJsapiTicket: /cgi-bin/ticket/getticket?access_token=%s&type=jsapi
+  mpCode2AccessToken: /sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code