优化小程序获取证书

This commit is contained in:
yuyang 2022-02-08 14:24:27 +08:00
parent 6055228c6b
commit 913fc3ef62
12 changed files with 186 additions and 90 deletions

View File

@ -0,0 +1,21 @@
package com.xhpc.common.api;
import com.xhpc.common.api.factory.CardHistoryOrderFactory;
import com.xhpc.common.api.factory.SettingConfigFactory;
import com.xhpc.common.core.constant.ServiceNameConstants;
import com.xhpc.common.core.domain.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author yuyang
* @date 2022/2/8 13:55
*/
@FeignClient(contextId ="settingConfigFactory",value = ServiceNameConstants.XHPC_PAYMENT, fallbackFactory = SettingConfigFactory.class)
public interface SettingConfigService {
//获取支付配置
@GetMapping("/commonPayment/settingConfig")
R settingConfig(@RequestParam(value = "status") Integer status, @RequestParam(value = "tenantId") String tenantId);
}

View File

@ -0,0 +1,25 @@
package com.xhpc.common.api.factory;
import com.xhpc.common.api.SettingConfigService;
import com.xhpc.common.core.constant.ServiceNameConstants;
import com.xhpc.common.core.domain.R;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
/**
* @author yuyang
* @date 2022/2/8 13:56
*/
@Component
public class SettingConfigFactory implements FallbackFactory<SettingConfigService> {
@Override
public SettingConfigService create(Throwable cause) {
return new SettingConfigService() {
@Override
public R settingConfig(Integer status, String tenantId) {
return R.fail("获取支付配置信息失败:" + cause.getMessage());
}
};
}
}

View File

@ -1,4 +1,4 @@
package com.xhpc.payment.domain; package com.xhpc.common.domain;
import com.xhpc.common.core.web.domain.BaseEntity; import com.xhpc.common.core.web.domain.BaseEntity;
import lombok.Data; import lombok.Data;

View File

@ -20,9 +20,14 @@ import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.core.web.domain.AjaxResult; import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.data.down.StartChargingData; import com.xhpc.common.data.down.StartChargingData;
import com.xhpc.common.domain.XhpcRechargeOrder; import com.xhpc.common.domain.XhpcRechargeOrder;
import com.xhpc.common.log.annotation.Log;
import com.xhpc.common.log.enums.BusinessType;
import com.xhpc.common.security.service.TokenService; import com.xhpc.common.security.service.TokenService;
import com.xhpc.common.util.UserTypeUtil; import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.payment.domain.*; import com.xhpc.payment.domain.AlipayNotifyParam;
import com.xhpc.payment.domain.XhpcAppUser;
import com.xhpc.common.domain.XhpcSettingConfig;
import com.xhpc.payment.domain.XhpcUserAccountStatement;
import com.xhpc.payment.mapper.XhpcUserAccountStatementMapper; import com.xhpc.payment.mapper.XhpcUserAccountStatementMapper;
import com.xhpc.payment.service.IXhpcCommonPayment; import com.xhpc.payment.service.IXhpcCommonPayment;
import com.xhpc.payment.service.IXhpcRechargeOrderService; import com.xhpc.payment.service.IXhpcRechargeOrderService;
@ -33,16 +38,19 @@ import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.RequestBody;
import com.xhpc.common.log.annotation.Log; import org.springframework.web.bind.annotation.RequestMapping;
import com.xhpc.common.log.enums.BusinessType; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.*; import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@RestController @RestController
@ -50,8 +58,6 @@ import java.util.*;
@Api(value = "支付宝支付接口", tags = "支付宝支付接口") @Api(value = "支付宝支付接口", tags = "支付宝支付接口")
public class AlipayPaymentController { public class AlipayPaymentController {
@Autowired
private Environment environment;
@Autowired @Autowired
private IXhpcRechargeOrderService iXhpcRechargeOrderService; private IXhpcRechargeOrderService iXhpcRechargeOrderService;
@Autowired @Autowired

View File

@ -16,7 +16,7 @@ import com.xhpc.common.security.service.TokenService;
import com.xhpc.common.util.UserTypeUtil; import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.payment.domain.XhpcAppUser; import com.xhpc.payment.domain.XhpcAppUser;
import com.xhpc.common.domain.XhpcRechargeOrder; import com.xhpc.common.domain.XhpcRechargeOrder;
import com.xhpc.payment.domain.XhpcSettingConfig; import com.xhpc.common.domain.XhpcSettingConfig;
import com.xhpc.payment.domain.XhpcUserAccountStatement; import com.xhpc.payment.domain.XhpcUserAccountStatement;
import com.xhpc.payment.mapper.XhpcUserAccountStatementMapper; import com.xhpc.payment.mapper.XhpcUserAccountStatementMapper;
import com.xhpc.payment.service.IXhpcCommonPayment; import com.xhpc.payment.service.IXhpcCommonPayment;
@ -34,7 +34,6 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import javax.servlet.ServletInputStream; import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;

View File

@ -0,0 +1,37 @@
package com.xhpc.payment.controller;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.payment.service.IXhpcCommonPayment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author yuyang
* @date 2022/2/8 14:00
*/
@EnableScheduling
@RestController
@RequestMapping("/commonPayment")
public class XhpcCommonPaymentController extends BaseController {
@Autowired
private IXhpcCommonPayment xhpcCommonPayment;
/**
* 获取支付配置
* @param status 1微信 2支付宝
* @param tenantId
* @return
*/
@GetMapping("/settingConfig")
public R settingConfig(Integer status, String tenantId) {
return R.ok(xhpcCommonPayment.getXhpcSettingConfigTenantId(status, tenantId));
}
}

View File

@ -11,7 +11,6 @@ import com.xhpc.common.api.SmsService;
import com.xhpc.common.api.UserTypeService; import com.xhpc.common.api.UserTypeService;
import com.xhpc.common.core.constant.HttpStatus; import com.xhpc.common.core.constant.HttpStatus;
import com.xhpc.common.core.constant.StatusConstants; import com.xhpc.common.core.constant.StatusConstants;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.utils.DateUtils; import com.xhpc.common.core.utils.DateUtils;
import com.xhpc.common.core.utils.StringUtils; import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.core.utils.WXPayUtil; import com.xhpc.common.core.utils.WXPayUtil;
@ -25,7 +24,7 @@ import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.payment.domain.XhpcAppUser; import com.xhpc.payment.domain.XhpcAppUser;
import com.xhpc.payment.domain.XhpcRefundAudit; import com.xhpc.payment.domain.XhpcRefundAudit;
import com.xhpc.common.domain.XhpcRefundOrder; import com.xhpc.common.domain.XhpcRefundOrder;
import com.xhpc.payment.domain.XhpcSettingConfig; import com.xhpc.common.domain.XhpcSettingConfig;
import com.xhpc.payment.mapper.XhpcUserAccountStatementMapper; import com.xhpc.payment.mapper.XhpcUserAccountStatementMapper;
import com.xhpc.payment.service.IXhpcCommonPayment; import com.xhpc.payment.service.IXhpcCommonPayment;
import com.xhpc.payment.service.IXhpcRefundAuditService; import com.xhpc.payment.service.IXhpcRefundAuditService;

View File

@ -1,6 +1,6 @@
package com.xhpc.payment.mapper; package com.xhpc.payment.mapper;
import com.xhpc.payment.domain.XhpcSettingConfig; import com.xhpc.common.domain.XhpcSettingConfig;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
/** /**

View File

@ -1,6 +1,6 @@
package com.xhpc.payment.service; package com.xhpc.payment.service;
import com.xhpc.payment.domain.XhpcSettingConfig; import com.xhpc.common.domain.XhpcSettingConfig;
/** /**
* @author yuyang * @author yuyang

View File

@ -1,6 +1,6 @@
package com.xhpc.payment.service.impl; package com.xhpc.payment.service.impl;
import com.xhpc.payment.domain.XhpcSettingConfig; import com.xhpc.common.domain.XhpcSettingConfig;
import com.xhpc.payment.mapper.XhpcCommonPaymentMapper; import com.xhpc.payment.mapper.XhpcCommonPaymentMapper;
import com.xhpc.payment.service.IXhpcCommonPayment; import com.xhpc.payment.service.IXhpcCommonPayment;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;

View File

@ -4,7 +4,7 @@
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xhpc.payment.mapper.XhpcCommonPaymentMapper"> <mapper namespace="com.xhpc.payment.mapper.XhpcCommonPaymentMapper">
<resultMap type="com.xhpc.payment.domain.XhpcSettingConfig" id="XhpcSettingConfigResult"> <resultMap type="com.xhpc.common.domain.XhpcSettingConfig" id="XhpcSettingConfigResult">
<result column="setting_config_id" property="settingConfigId"/> <result column="setting_config_id" property="settingConfigId"/>
<result column="wx_app_id" property="wxAppId"/> <result column="wx_app_id" property="wxAppId"/>

View File

@ -2,12 +2,12 @@ package com.xhpc.user.controller;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient; import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest; import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient; import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest; import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse; import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.xhpc.common.api.SettingConfigService;
import com.xhpc.common.core.constant.HttpStatus; import com.xhpc.common.core.constant.HttpStatus;
import com.xhpc.common.core.domain.R; import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.utils.HttpUtils; import com.xhpc.common.core.utils.HttpUtils;
@ -16,20 +16,14 @@ import com.xhpc.common.core.utils.sign.Base64;
import com.xhpc.common.core.web.controller.BaseController; import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.domain.AjaxResult; import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.page.TableDataInfo; import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.domain.XhpcSettingConfig;
import com.xhpc.common.log.annotation.Log; import com.xhpc.common.log.annotation.Log;
import com.xhpc.common.log.enums.BusinessType; import com.xhpc.common.log.enums.BusinessType;
import com.xhpc.user.service.IXhpcAppUserUserService; import com.xhpc.user.service.IXhpcAppUserUserService;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.IvParameterSpec;
@ -55,8 +49,7 @@ public class XhpcAppUserController extends BaseController {
private IXhpcAppUserUserService iXhpcAppUserUserService; private IXhpcAppUserUserService iXhpcAppUserUserService;
@Autowired @Autowired
private Environment environment; private SettingConfigService settingConfigService;
/** /**
* C端用户详情 * C端用户详情
*/ */
@ -162,38 +155,47 @@ public class XhpcAppUserController extends BaseController {
*/ */
@PostMapping("/jscode2session") @PostMapping("/jscode2session")
@ApiOperation(value = "获取微信权限标识", notes = "jsCode") @ApiOperation(value = "获取微信权限标识", notes = "jsCode")
public R<?> jsCode(String jsCode, String encryptedData, String iv) { public R<?> jsCode(String jsCode, String encryptedData, String iv,String tenantId) {
if (StringUtils.isEmpty(jsCode)) { if (StringUtils.isEmpty(jsCode)) {
return R.fail(HttpStatus.NOT_NULL, "信息不完整"); return R.fail(HttpStatus.NOT_NULL, "信息不完整");
} }
encryptedData =encryptedData.replace(' ','+'); encryptedData =encryptedData.replace(' ','+');
String url = environment.getProperty("WXGETJSCODE") + jsCode + "&grant_type=authorization_code"; if("".equals(tenantId) || null==tenantId){
String result = HttpUtils.get(url); tenantId="000000";
JSONObject json = JSON.parseObject(result);
if (null != json) {
String openid = json.getString("openid");
String sessionKey = json.getString("session_key");
System.out.println("sessionKey:"+sessionKey);
if (StringUtils.isEmpty(openid)) {
return R.fail(HttpStatus.ERROR_STATUS, "openid获取失败");
}
if (StringUtils.isEmpty(sessionKey)) {
return R.fail(HttpStatus.ERROR_STATUS, "openid获取失败");
}
Map<String, Object> map = new HashMap<>(16);
map.put("openid", openid);
JSONObject jsonObject = new JSONObject();
if ((!"".equals(encryptedData) && encryptedData != null) && (!"".equals(iv) && iv != null)) {
jsonObject = getPhoneNumber(encryptedData, sessionKey, iv);
if (jsonObject !=null) {
map.put("name", jsonObject.get("nickName"));
map.put("sex", jsonObject.get("gender"));
map.put("avatar", jsonObject.get("avatarUrl"));
map.put("phone", jsonObject.get("purePhoneNumber"));
}
}
return R.ok(map);
} }
R r = settingConfigService.settingConfig(1, tenantId);
if(r !=null && r.getCode()==200){
XhpcSettingConfig xhpcSettingConfig = (XhpcSettingConfig)r.getData();
String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+xhpcSettingConfig.getWxAppId()+"&secret="+xhpcSettingConfig.getWxAppSecret()+"&js_code="+ jsCode + "&grant_type=authorization_code";
String result = HttpUtils.get(url);
JSONObject json = JSON.parseObject(result);
if (null != json) {
String openid = json.getString("openid");
String sessionKey = json.getString("session_key");
System.out.println("sessionKey:"+sessionKey);
if (StringUtils.isEmpty(openid)) {
return R.fail(HttpStatus.ERROR_STATUS, "openid获取失败");
}
if (StringUtils.isEmpty(sessionKey)) {
return R.fail(HttpStatus.ERROR_STATUS, "openid获取失败");
}
Map<String, Object> map = new HashMap<>(16);
map.put("openid", openid);
JSONObject jsonObject = new JSONObject();
if ((!"".equals(encryptedData) && encryptedData != null) && (!"".equals(iv) && iv != null)) {
jsonObject = getPhoneNumber(encryptedData, sessionKey, iv);
if (jsonObject !=null) {
map.put("name", jsonObject.get("nickName"));
map.put("sex", jsonObject.get("gender"));
map.put("avatar", jsonObject.get("avatarUrl"));
map.put("phone", jsonObject.get("purePhoneNumber"));
}
}
return R.ok(map);
}
}
return R.fail(HttpStatus.ERROR_STATUS, "openid获取失败"); return R.fail(HttpStatus.ERROR_STATUS, "openid获取失败");
} }
@ -241,43 +243,50 @@ public class XhpcAppUserController extends BaseController {
*/ */
@ApiOperation("支付宝授权") @ApiOperation("支付宝授权")
@PostMapping("/alipayEmpower") @PostMapping("/alipayEmpower")
public AjaxResult alipayEmpower(@RequestParam String code) throws Exception { public AjaxResult alipayEmpower(@RequestParam String code,String tenantId) throws Exception {
if("".equals(tenantId) || null==tenantId){
/** 初始化 **/ tenantId="000000";
CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
/** 支付宝网关 **/
certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
/** 应用id如何获取请参考https://opensupport.alipay.com/support/helpcenter/190/201602493024 **/
certAlipayRequest.setAppId(environment.getProperty("ALIPAYAPPID"));
/** 应用私钥, 如何获取请参考https://opensupport.alipay.com/support/helpcenter/207/201602471154?ant_source=antsupport **/
certAlipayRequest.setPrivateKey(environment.getProperty("ALIPAYPRIVATEKEY"));
/** 应用公钥证书路径,下载后保存位置的绝对路径 **/
certAlipayRequest.setCertPath(environment.getProperty("CERTPATH"));
/** 支付宝公钥证书路径,下载后保存位置的绝对路径 **/
certAlipayRequest.setAlipayPublicCertPath(environment.getProperty("PUBLICCERTPATH"));
/** 支付宝根证书路径,下载后保存位置的绝对路径 **/
certAlipayRequest.setRootCertPath(environment.getProperty("ROOTCRETPATH"));
/** 设置签名类型 **/
certAlipayRequest.setSignType("RSA2");
/** 设置请求格式固定值json **/
certAlipayRequest.setFormat("json");
/** 设置编码格式 **/
certAlipayRequest.setCharset("UTF-8");
AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
request.setGrantType("authorization_code");
// auth_code 前端授权后得到的授权码
request.setCode(code);
AlipaySystemOauthTokenResponse response = alipayClient.certificateExecute(request);
//服务端请求返回RSA2密钥和公钥证书请求方式不同如当面付
// RSA2密钥请求response = alipayClient.execute(request);
// 公钥证书请求请求response = alipayClient.certificateExecute(request);
if (response.isSuccess()) {
System.out.println("调用成功");
} else {
System.out.println("调用失败");
} }
return AjaxResult.success(response); R r = settingConfigService.settingConfig(1, tenantId);
if(r !=null && r.getCode()==200){
XhpcSettingConfig xhpcSettingConfig = (XhpcSettingConfig)r.getData();
/** 初始化 **/
CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
/** 支付宝网关 **/
certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
/** 应用id如何获取请参考https://opensupport.alipay.com/support/helpcenter/190/201602493024 **/
certAlipayRequest.setAppId(xhpcSettingConfig.getZfbAppId());
/** 应用私钥, 如何获取请参考https://opensupport.alipay.com/support/helpcenter/207/201602471154?ant_source=antsupport **/
certAlipayRequest.setPrivateKey(xhpcSettingConfig.getZfbPrivateKey());
/** 应用公钥证书路径,下载后保存位置的绝对路径 **/
certAlipayRequest.setCertPath(xhpcSettingConfig.getZfbAppCertPublicKey());
/** 支付宝公钥证书路径,下载后保存位置的绝对路径 **/
certAlipayRequest.setAlipayPublicCertPath(xhpcSettingConfig.getZfbAppCertPublicKey());
/** 支付宝根证书路径,下载后保存位置的绝对路径 **/
certAlipayRequest.setRootCertPath(xhpcSettingConfig.getZfbAlipayRootCert());
/** 设置签名类型 **/
certAlipayRequest.setSignType("RSA2");
/** 设置请求格式固定值json **/
certAlipayRequest.setFormat("json");
/** 设置编码格式 **/
certAlipayRequest.setCharset("UTF-8");
AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
request.setGrantType("authorization_code");
// auth_code 前端授权后得到的授权码
request.setCode(code);
AlipaySystemOauthTokenResponse response = alipayClient.certificateExecute(request);
//服务端请求返回RSA2密钥和公钥证书请求方式不同如当面付
// RSA2密钥请求response = alipayClient.execute(request);
// 公钥证书请求请求response = alipayClient.certificateExecute(request);
if (response.isSuccess()) {
System.out.println("调用成功");
} else {
return AjaxResult.error("获取失败");
}
return AjaxResult.success(response);
}
return AjaxResult.error("获取失败");
} }
/** /**