Browse Source

feat 0 支付工程

devlop
yang_shj 4 years ago
parent
commit
a8a7f8a851
  1. 94
      payment-base-app/src/main/java/com/base/springcloud/service/imp/MerchantServiceImp.java
  2. 4
      payment-base-app/src/main/java/com/base/springcloud/wxsdk/WxpayUtils.java
  3. 2
      payment-base-app/src/main/resources/application.yml

94
payment-base-app/src/main/java/com/base/springcloud/service/imp/MerchantServiceImp.java

@ -7,11 +7,14 @@ import com.base.springcloud.bo.OrderBO;
import com.base.springcloud.constant.ErrorConstant; import com.base.springcloud.constant.ErrorConstant;
import com.base.springcloud.dao.MerchantConfigMapper; import com.base.springcloud.dao.MerchantConfigMapper;
import com.base.springcloud.entity.MerchantConfig; import com.base.springcloud.entity.MerchantConfig;
import com.base.springcloud.handle.RBSQueueHandler;
import com.base.springcloud.service.DepartService; import com.base.springcloud.service.DepartService;
import com.base.springcloud.service.MerchantService; import com.base.springcloud.service.MerchantService;
import com.base.springcloud.util.FtpUtil; import com.base.springcloud.util.FtpUtil;
import com.base.springcloud.util.OssBootUtil; import com.base.springcloud.util.OssBootUtil;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -31,18 +34,14 @@ import java.util.stream.Collectors;
@Service @Service
public class MerchantServiceImp implements MerchantService { public class MerchantServiceImp implements MerchantService {
Logger logger = LoggerFactory.getLogger(MerchantServiceImp.class);
@Autowired @Autowired
private MerchantConfigMapper merchantConfigMapper; private MerchantConfigMapper merchantConfigMapper;
@Autowired @Autowired
private DepartService departService; private DepartService departService;
@Autowired
private FtpUtil ftpUtil;
@Value("${test_ftp.file_path}")
public String FTP_BASEPATH;
/** /**
* 支付信息配置 * 支付信息配置
* @param merchantConfigBO * @param merchantConfigBO
@ -70,6 +69,11 @@ public class MerchantServiceImp implements MerchantService {
departService.savePayDepart(departBO); departService.savePayDepart(departBO);
} }
/**
* 证书上传
* @param certFile
* @return
*/
private String uploadCertificate(MultipartFile certFile) { private String uploadCertificate(MultipartFile certFile) {
return OssBootUtil.upload(certFile, "merchant/paymentConfigure"); return OssBootUtil.upload(certFile, "merchant/paymentConfigure");
} }
@ -97,10 +101,10 @@ public class MerchantServiceImp implements MerchantService {
@Override @Override
public void updateMerchantConfig(MerchantConfigBO merchantConfigBO) { public void updateMerchantConfig(MerchantConfigBO merchantConfigBO) {
// 证书上传服务器 // 证书上传服务器
String certPath = uplodWecWindow(merchantConfigBO.getMchId(),merchantConfigBO.getCertFile()); //String certPath = uplodWecWindow(merchantConfigBO.getMchId(),merchantConfigBO.getCertFile());
// 商户支付信息保存 // 商户支付信息保存
MerchantConfig merchantConfig = new MerchantConfig(); MerchantConfig merchantConfig = new MerchantConfig();
merchantConfig.setCertPath(certPath); //merchantConfig.setCertPath(certPath);
BeanUtils.copyProperties(merchantConfigBO,merchantConfig); BeanUtils.copyProperties(merchantConfigBO,merchantConfig);
merchantConfig.setUpdateTime(new Date()); merchantConfig.setUpdateTime(new Date());
int count = merchantConfigMapper.updateMerchantConfig(merchantConfig); int count = merchantConfigMapper.updateMerchantConfig(merchantConfig);
@ -140,45 +144,38 @@ public class MerchantServiceImp implements MerchantService {
} }
orderBO.setAppId(merchantConfig.getAppId()); orderBO.setAppId(merchantConfig.getAppId());
orderBO.setMchPrivateKey(merchantConfig.getMchPrivateKey()); orderBO.setMchPrivateKey(merchantConfig.getMchPrivateKey());
//String certificate = readCert(merchantConfig.getCertPath()); String certificate = readCertificate(merchantConfig.getCertPath());
orderBO.setCertificate(merchantConfig.getCertPath()); orderBO.setCertificate(certificate);
orderBO.setSerialNo(merchantConfig.getSerialNo()); orderBO.setSerialNo(merchantConfig.getSerialNo());
} }
/** /**
* 测试文件上传 * 证书文件内容读取
* @param certPath
* @return
*/ */
private String uplodWecWindow(String mchId, MultipartFile certfile){ private String readCertificate(String certPath) {
if (certfile.isEmpty()) { InputStream is = OssBootUtil.getOssFile(certPath);
// TODO 抛出业务异常 StringBuilder sb = new StringBuilder();
return "文件上传失败!";
}
String fileName = certfile.getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf("."));
File file = new File(FTP_BASEPATH + mchId + suffixName);
try{ try{
certfile.transferTo(file); byte[] buf = new byte[1024];
return file.getPath(); int length;
} catch (IOException e) { //循环读取文件内容,输入流中将最多buf.length个字节的数据读入一个buf数组中,返回类型是读取到的字节数。
// TODO 抛出业务异常 while((length = is.read(buf)) != -1){
sb.append(new String(buf,0,length));
} }
return "文件上传失败!";
}
/**
* 文件读取
* @throws IOException
*/
public String readCert(String certPath) {
byte[] bytes;
try{
bytes = Files.readAllBytes(Paths.get(certPath));
}catch (IOException e){ }catch (IOException e){
logger.info("read_certificate_exception : {}",e.getMessage());
throw new BusinessException(ErrorConstant.MERCHANT_PAY_CERT_READ_FAIL); throw new BusinessException(ErrorConstant.MERCHANT_PAY_CERT_READ_FAIL);
}finally {
try{
is.close();
}catch (IOException e){
} }
return new String(bytes, StandardCharsets.UTF_8); }
return sb.toString();
} }
/** /**
@ -197,29 +194,4 @@ public class MerchantServiceImp implements MerchantService {
} }
return true; return true;
} }
/**
* 证书上传服务器
* @param mchId
* @param certfile
* @return
*/
private String uplodWecPayCert(String mchId,File certfile){
//获取文件名
String fileName = certfile.getName();
//获取文件的后缀名
String suffixName = fileName.substring(fileName.lastIndexOf("."));
fileName = mchId + suffixName;
//上传的文件名也需要加上后缀,不然虚拟机不知道文件格式
try{
InputStream inputStream = new FileInputStream(certfile);
Boolean flag = ftpUtil.uploadFile(fileName, inputStream);
if (flag == true) {
return ftpUtil.FTP_BASEPATH + fileName;
}
}catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
} }

4
payment-base-app/src/main/java/com/base/springcloud/wxsdk/WxpayUtils.java

@ -216,8 +216,8 @@ public class WxpayUtils {
* @throws UnsupportedEncodingException * @throws UnsupportedEncodingException
*/ */
public static CloseableHttpClient getClient(String mchid, String serialNo, PrivateKey privateKey, String certificate) throws CertificateException, IOException { public static CloseableHttpClient getClient(String mchid, String serialNo, PrivateKey privateKey, String certificate) throws CertificateException, IOException {
//byte[] bytes = certificate.getBytes(StandardCharsets.UTF_8); byte[] bytes = certificate.getBytes(StandardCharsets.UTF_8);
ByteArrayInputStream base = new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(certificate)); ByteArrayInputStream base = new ByteArrayInputStream(bytes);
List<X509Certificate> certs = new ArrayList<>(); List<X509Certificate> certs = new ArrayList<>();
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
X509Certificate cert = null; X509Certificate cert = null;

2
payment-base-app/src/main/resources/application.yml

@ -1,3 +1,3 @@
spring: spring:
profiles: profiles:
active: prod active: dev
Loading…
Cancel
Save