修正项目
This commit is contained in:
5
xlcs-parent/service/service-payment/Dockerfile
Normal file
5
xlcs-parent/service/service-payment/Dockerfile
Normal file
@@ -0,0 +1,5 @@
|
||||
FROM openjdk:8-jdk-alpine
|
||||
LABEL authors="yovinchen"
|
||||
VOLUME /tmp
|
||||
ADD ./target/service-payment.jar service-order.jar
|
||||
ENTRYPOINT ["java","-jar","/service-payment.jar", "&"]
|
39
xlcs-parent/service/service-payment/pom.xml
Normal file
39
xlcs-parent/service/service-payment/pom.xml
Normal file
@@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.yovinchen</groupId>
|
||||
<artifactId>service</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>service-payment</artifactId>
|
||||
<dependencies>
|
||||
<!--导入微信支付sdk-->
|
||||
<dependency>
|
||||
<groupId>com.github.wxpay</groupId>
|
||||
<artifactId>wxpay-sdk</artifactId>
|
||||
<version>0.0.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.yovinchen</groupId>
|
||||
<artifactId>service-order-client</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.yovinchen</groupId>
|
||||
<artifactId>rabbit_util</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
</project>
|
@@ -0,0 +1,22 @@
|
||||
package com.yovinchen.xlcs;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* ClassName: ServicePaymentApplication
|
||||
* Package: vom.yovinchen.xlcs
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/10/13 10:29
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
@EnableFeignClients
|
||||
public class ServicePaymentApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ServicePaymentApplication.class, args);
|
||||
}
|
||||
}
|
@@ -0,0 +1,66 @@
|
||||
package com.yovinchen.xlcs.payment.controller;
|
||||
|
||||
import com.yovinchen.xlcs.common.result.Result;
|
||||
import com.yovinchen.xlcs.common.result.ResultCodeEnum;
|
||||
import com.yovinchen.xlcs.payment.service.PaymentInfoService;
|
||||
import com.yovinchen.xlcs.payment.service.WeixinService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ClassName: WeiXinController
|
||||
* Package: com.yovinchen.xlcs.payment.payment.controller
|
||||
* 微信支付API
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/10/13 10:32
|
||||
*/
|
||||
@Api(tags = "微信支付接口")
|
||||
@RestController
|
||||
@RequestMapping("/api/payment/weixin")
|
||||
@Slf4j
|
||||
public class WeiXinController {
|
||||
|
||||
@Autowired
|
||||
private WeixinService weixinService;
|
||||
|
||||
@Autowired
|
||||
private PaymentInfoService paymentInfoService;
|
||||
|
||||
@ApiOperation(value = "调用微信支付系统生成预付单")
|
||||
@GetMapping("/createJsapi/{orderNo}")
|
||||
public Result createJsapi(@PathVariable("orderNo") String orderNo) {
|
||||
Map<String, String> map = weixinService.createJsapi(orderNo);
|
||||
return Result.ok(map);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "查询订单支付状态")
|
||||
@GetMapping("/queryPayStatus/{orderNo}")
|
||||
public Result queryPayStatus(@PathVariable("orderNo") String orderNo) {
|
||||
//1 调用微信支付系统接口查询订单支付状态
|
||||
Map<String, String> resultMap = weixinService.queryPayStatus(orderNo);
|
||||
|
||||
//2 微信支付系统返回值为null,支付失败
|
||||
if (resultMap == null) {
|
||||
return Result.build(null, ResultCodeEnum.PAYMENT_FAIL);
|
||||
}
|
||||
|
||||
//3 如果微信支付系统返回值,判断支付成功
|
||||
if ("SUCCESS".equals(resultMap.get("trade_state"))) {
|
||||
String out_trade_no = resultMap.get("out_trade_no");
|
||||
paymentInfoService.paySuccess(out_trade_no, resultMap);
|
||||
return Result.ok(null);
|
||||
}
|
||||
|
||||
//4 支付中,等待
|
||||
return Result.build(null, ResultCodeEnum.PAYMENT_WAITING);
|
||||
}
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
package com.yovinchen.xlcs.payment.mapper;
|
||||
|
||||
import com.yovinchen.xlcs.model.order.PaymentInfo;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 支付信息表 Mapper 接口
|
||||
* </p>
|
||||
*
|
||||
* @author yovinchen
|
||||
* @since 2023-10-12
|
||||
*/
|
||||
public interface PaymentInfoMapper extends BaseMapper<PaymentInfo> {
|
||||
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.yovinchen.xlcs.order.mapper.PaymentInfoMapper">
|
||||
|
||||
</mapper>
|
@@ -0,0 +1,40 @@
|
||||
package com.yovinchen.xlcs.payment.service;
|
||||
|
||||
import com.yovinchen.xlcs.model.order.PaymentInfo;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ClassName: PaymentInfoService
|
||||
* Package: com.yovinchen.xlcs.payment.payment.service
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/10/13 10:33
|
||||
*/
|
||||
|
||||
public interface PaymentInfoService extends IService<PaymentInfo> {
|
||||
/**
|
||||
* 根据orderNo查询支付订单
|
||||
*
|
||||
* @param orderNo
|
||||
* @return
|
||||
*/
|
||||
PaymentInfo getPaymentInfoByOrderNo(String orderNo);
|
||||
|
||||
/**
|
||||
* 保存支付订单
|
||||
*
|
||||
* @param orderNo
|
||||
* @return
|
||||
*/
|
||||
PaymentInfo savePaymentInfo(String orderNo);
|
||||
|
||||
/**
|
||||
* 支付成功
|
||||
*
|
||||
* @param orderNo
|
||||
* @param resultMap
|
||||
*/
|
||||
void paySuccess(String orderNo, Map<String, String> resultMap);
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
package com.yovinchen.xlcs.payment.service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ClassName: WeixinService
|
||||
* Package: com.yovinchen.xlcs.payment.payment.controller.payment.service
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/10/13 10:32
|
||||
*/
|
||||
public interface WeixinService {
|
||||
/**
|
||||
* 调用微信支付系统生成预付单
|
||||
*
|
||||
* @param orderNo
|
||||
* @return
|
||||
*/
|
||||
Map<String, String> createJsapi(String orderNo);
|
||||
|
||||
/**
|
||||
* 调用微信支付系统接口查询订单支付状态
|
||||
*
|
||||
* @param orderNo
|
||||
* @return
|
||||
*/
|
||||
Map<String, String> queryPayStatus(String orderNo);
|
||||
}
|
@@ -0,0 +1,111 @@
|
||||
package com.yovinchen.xlcs.payment.service.impl;
|
||||
|
||||
import com.yovinchen.xlcs.client.order.OrderFeignClient;
|
||||
import com.yovinchen.xlcs.common.exception.xlcsException;
|
||||
import com.yovinchen.xlcs.common.result.ResultCodeEnum;
|
||||
import com.yovinchen.xlcs.enums.PaymentStatus;
|
||||
import com.yovinchen.xlcs.enums.PaymentType;
|
||||
import com.yovinchen.xlcs.model.order.OrderInfo;
|
||||
import com.yovinchen.xlcs.model.order.PaymentInfo;
|
||||
import com.yovinchen.xlcs.mq.constant.MqConst;
|
||||
import com.yovinchen.xlcs.mq.service.RabbitService;
|
||||
import com.yovinchen.xlcs.payment.mapper.PaymentInfoMapper;
|
||||
import com.yovinchen.xlcs.payment.service.PaymentInfoService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 支付信息表 服务实现类
|
||||
* </p>
|
||||
*
|
||||
* @author yovinchen
|
||||
* @since 2023-10-12
|
||||
*/
|
||||
@Service
|
||||
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {
|
||||
|
||||
|
||||
@Autowired
|
||||
private OrderFeignClient orderFeignClient;
|
||||
|
||||
@Autowired
|
||||
private RabbitService rabbitService;
|
||||
|
||||
/**
|
||||
* 根据orderNo查询支付订单
|
||||
*
|
||||
* @param orderNo
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public PaymentInfo getPaymentInfoByOrderNo(String orderNo) {
|
||||
return baseMapper.selectOne(new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOrderNo, orderNo));
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存支付订单
|
||||
*
|
||||
* @param orderNo
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public PaymentInfo savePaymentInfo(String orderNo) {
|
||||
//远程调用调用,根据orderNo查询订单信息
|
||||
OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderNo);
|
||||
if (orderInfo == null) {
|
||||
throw new xlcsException(ResultCodeEnum.DATA_ERROR);
|
||||
}
|
||||
//封装到PaymentInfo对象
|
||||
PaymentInfo paymentInfo = new PaymentInfo();
|
||||
paymentInfo.setCreateTime(new Date());
|
||||
paymentInfo.setOrderId(orderInfo.getId());
|
||||
paymentInfo.setPaymentType(PaymentType.WEIXIN);
|
||||
paymentInfo.setUserId(orderInfo.getUserId());
|
||||
paymentInfo.setOrderNo(orderInfo.getOrderNo());
|
||||
paymentInfo.setPaymentStatus(PaymentStatus.UNPAID);
|
||||
String subject = "userID:" + orderInfo.getUserId() + "下订单";
|
||||
paymentInfo.setSubject(subject);
|
||||
//paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
|
||||
//TODO 为了测试
|
||||
paymentInfo.setTotalAmount(new BigDecimal("0.01"));
|
||||
|
||||
//调用方法实现添加
|
||||
baseMapper.insert(paymentInfo);
|
||||
return paymentInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付成功
|
||||
*
|
||||
* @param orderNo
|
||||
* @param resultMap
|
||||
*/
|
||||
@Override
|
||||
public void paySuccess(String orderNo, Map<String, String> resultMap) {
|
||||
//1 查询当前订单支付记录表状态是否是已经支付
|
||||
PaymentInfo paymentInfo = baseMapper.selectOne(
|
||||
new LambdaQueryWrapper<PaymentInfo>()
|
||||
.eq(PaymentInfo::getOrderNo, orderNo)
|
||||
);
|
||||
if (paymentInfo.getPaymentStatus() != PaymentStatus.UNPAID) {
|
||||
return;
|
||||
}
|
||||
|
||||
//2 如果支付记录表支付状态没有支付,更新
|
||||
paymentInfo.setPaymentStatus(PaymentStatus.PAID);
|
||||
paymentInfo.setTradeNo(resultMap.get("transaction_id"));
|
||||
paymentInfo.setCallbackContent(resultMap.toString());
|
||||
baseMapper.updateById(paymentInfo);
|
||||
|
||||
//3 整合RabbitMQ实现 修改订单记录已经支付,库存扣减
|
||||
rabbitService.sendMessage(MqConst.EXCHANGE_PAY_DIRECT,
|
||||
MqConst.ROUTING_PAY_SUCCESS, orderNo);
|
||||
}
|
||||
}
|
@@ -0,0 +1,140 @@
|
||||
package com.yovinchen.xlcs.payment.service.impl;
|
||||
|
||||
import com.yovinchen.xlcs.common.constant.RedisConst;
|
||||
import com.yovinchen.xlcs.model.order.PaymentInfo;
|
||||
import com.yovinchen.xlcs.payment.service.PaymentInfoService;
|
||||
import com.yovinchen.xlcs.payment.service.WeixinService;
|
||||
import com.yovinchen.xlcs.payment.utils.ConstantPropertiesUtils;
|
||||
import com.yovinchen.xlcs.payment.utils.HttpClient;
|
||||
import com.yovinchen.xlcs.vo.user.UserLoginVo;
|
||||
import com.github.wxpay.sdk.WXPayUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ClassName: WeXinServiceImpl
|
||||
* Package: com.yovinchen.xlcs.payment.payment.service.impl
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/10/13 10:33
|
||||
*/
|
||||
@Service
|
||||
public class WeXinServiceImpl implements WeixinService {
|
||||
|
||||
|
||||
@Autowired
|
||||
private PaymentInfoService paymentInfoService;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
/**
|
||||
* 调用微信支付系统生成预付单
|
||||
*
|
||||
* @param orderNo
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Map<String, String> createJsapi(String orderNo) {
|
||||
//1 向payment_info支付记录表添加记录,目前支付状态:正在支付中
|
||||
PaymentInfo paymentInfo = paymentInfoService.getPaymentInfoByOrderNo(orderNo);
|
||||
if (paymentInfo == null) {
|
||||
paymentInfo = paymentInfoService.savePaymentInfo(orderNo);
|
||||
}
|
||||
|
||||
//2 封装微信支付系统接口需要参数
|
||||
Map<String, String> paramMap = new HashMap<>();
|
||||
paramMap.put("appid", ConstantPropertiesUtils.APPID);
|
||||
paramMap.put("mch_id", ConstantPropertiesUtils.PARTNER);
|
||||
paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
|
||||
paramMap.put("body", paymentInfo.getSubject());
|
||||
paramMap.put("out_trade_no", paymentInfo.getOrderNo());
|
||||
int totalFee = paymentInfo.getTotalAmount().multiply(new BigDecimal(100)).intValue();
|
||||
paramMap.put("total_fee", String.valueOf(totalFee));
|
||||
paramMap.put("spbill_create_ip", "127.0.0.1");
|
||||
paramMap.put("notify_url", ConstantPropertiesUtils.NOTIFYURL);
|
||||
paramMap.put("trade_type", "JSAPI");
|
||||
|
||||
//openid
|
||||
UserLoginVo userLoginVo = (UserLoginVo) redisTemplate.opsForValue().get(RedisConst.USER_LOGIN_KEY_PREFIX + paymentInfo.getUserId());
|
||||
if (null != userLoginVo && !StringUtils.isEmpty(userLoginVo.getOpenId())) {
|
||||
paramMap.put("openid", userLoginVo.getOpenId());
|
||||
} else {
|
||||
paramMap.put("openid", "odo3j4q2KskkbbW-krfE-cAxUnzU1");
|
||||
}
|
||||
//3 使用HttpClient调用微信支付系统接口
|
||||
HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
|
||||
//设置参数,xml格式
|
||||
try {
|
||||
client.setXmlParam(WXPayUtil.generateSignedXml(paramMap, ConstantPropertiesUtils.PARTNERKEY));
|
||||
client.setHttps(true);
|
||||
client.post();
|
||||
|
||||
//4 调用微信支付系统接口之后,返回结果 prepay_id
|
||||
String xml = client.getContent();
|
||||
Map<String, String> resultMap = WXPayUtil.xmlToMap(xml);
|
||||
|
||||
//5 封装需要数据-包含预付单标识 prepay_id
|
||||
Map<String, String> parameterMap = new HashMap<>();
|
||||
String prepayId = String.valueOf(resultMap.get("prepay_id"));
|
||||
String packages = "prepay_id=" + prepayId;
|
||||
parameterMap.put("appId", ConstantPropertiesUtils.APPID);
|
||||
parameterMap.put("nonceStr", resultMap.get("nonce_str"));
|
||||
parameterMap.put("package", packages);
|
||||
parameterMap.put("signType", "MD5");
|
||||
parameterMap.put("timeStamp", String.valueOf(new Date().getTime()));
|
||||
String sign = WXPayUtil.generateSignature(parameterMap, ConstantPropertiesUtils.PARTNERKEY);
|
||||
|
||||
//返回结果
|
||||
Map<String, String> result = new HashMap();
|
||||
result.put("timeStamp", parameterMap.get("timeStamp"));
|
||||
result.put("nonceStr", parameterMap.get("nonceStr"));
|
||||
result.put("signType", "MD5");
|
||||
result.put("paySign", sign);
|
||||
result.put("package", packages);
|
||||
|
||||
//6 返回结果
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用微信支付系统接口查询订单支付状态
|
||||
*
|
||||
* @param orderNo
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Map<String, String> queryPayStatus(String orderNo) {
|
||||
//封装数据、
|
||||
Map paramMap = new HashMap();
|
||||
paramMap.put("appid", ConstantPropertiesUtils.APPID);
|
||||
paramMap.put("mch_id", ConstantPropertiesUtils.PARTNER);
|
||||
paramMap.put("out_trade_no", orderNo);
|
||||
paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
|
||||
|
||||
//2、设置请求
|
||||
HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
|
||||
try {
|
||||
client.setXmlParam(WXPayUtil.generateSignedXml(paramMap, ConstantPropertiesUtils.PARTNERKEY));
|
||||
client.setHttps(true);
|
||||
client.post();
|
||||
|
||||
//3 得到返回结果
|
||||
String xml = client.getContent();
|
||||
Map<String, String> stringMap = WXPayUtil.xmlToMap(xml);
|
||||
return stringMap;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
package com.yovinchen.xlcs.payment.utils;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* ClassName: ConstantPropertiesUtils
|
||||
* Package: com.yovinchen.xlcs.payment.payment.utils
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/10/13 10:48
|
||||
*/
|
||||
@Component
|
||||
public class ConstantPropertiesUtils implements InitializingBean {
|
||||
|
||||
public static String APPID;
|
||||
public static String PARTNER;
|
||||
public static String PARTNERKEY;
|
||||
public static String NOTIFYURL;
|
||||
public static String CERT;
|
||||
@Value("${weixin.appid}")
|
||||
private String appid;
|
||||
@Value("${weixin.partner}")
|
||||
private String partner;
|
||||
@Value("${weixin.partnerkey}")
|
||||
private String partnerkey;
|
||||
@Value("${weixin.notifyurl}")
|
||||
private String notifyurl;
|
||||
@Value("${weixin.cert}")
|
||||
private String cert;
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
APPID = appid;
|
||||
PARTNER = partner;
|
||||
PARTNERKEY = partnerkey;
|
||||
NOTIFYURL = notifyurl;
|
||||
CERT = cert;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,206 @@
|
||||
package com.yovinchen.xlcs.payment.utils;
|
||||
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.*;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.conn.ssl.SSLContextBuilder;
|
||||
import org.apache.http.conn.ssl.TrustStrategy;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.ssl.SSLContexts;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.security.KeyStore;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.text.ParseException;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ClassName: HttpClient
|
||||
* Package: com.yovinchen.xlcs.payment.payment.utils
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2023/10/13 10:47
|
||||
*/
|
||||
public class HttpClient {
|
||||
|
||||
private String url;
|
||||
private Map<String, String> param;
|
||||
private int statusCode;
|
||||
private String content;
|
||||
private String xmlParam;
|
||||
private boolean isHttps;
|
||||
private boolean isCert = false;
|
||||
//证书密码 微信商户号(mch_id)
|
||||
private String certPassword;
|
||||
|
||||
public HttpClient(String url, Map<String, String> param) {
|
||||
this.url = url;
|
||||
this.param = param;
|
||||
}
|
||||
|
||||
public HttpClient(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public boolean isHttps() {
|
||||
return isHttps;
|
||||
}
|
||||
|
||||
public void setHttps(boolean isHttps) {
|
||||
this.isHttps = isHttps;
|
||||
}
|
||||
|
||||
public boolean isCert() {
|
||||
return isCert;
|
||||
}
|
||||
|
||||
public void setCert(boolean cert) {
|
||||
isCert = cert;
|
||||
}
|
||||
|
||||
public String getXmlParam() {
|
||||
return xmlParam;
|
||||
}
|
||||
|
||||
public void setXmlParam(String xmlParam) {
|
||||
this.xmlParam = xmlParam;
|
||||
}
|
||||
|
||||
public String getCertPassword() {
|
||||
return certPassword;
|
||||
}
|
||||
|
||||
public void setCertPassword(String certPassword) {
|
||||
this.certPassword = certPassword;
|
||||
}
|
||||
|
||||
public void setParameter(Map<String, String> map) {
|
||||
param = map;
|
||||
}
|
||||
|
||||
public void addParameter(String key, String value) {
|
||||
if (param == null)
|
||||
param = new HashMap<String, String>();
|
||||
param.put(key, value);
|
||||
}
|
||||
|
||||
public void post() throws IOException {
|
||||
HttpPost http = new HttpPost(url);
|
||||
setEntity(http);
|
||||
execute(http);
|
||||
}
|
||||
|
||||
public void put() throws IOException {
|
||||
HttpPut http = new HttpPut(url);
|
||||
setEntity(http);
|
||||
execute(http);
|
||||
}
|
||||
|
||||
public void get() throws IOException {
|
||||
if (param != null) {
|
||||
StringBuilder url = new StringBuilder(this.url);
|
||||
boolean isFirst = true;
|
||||
for (String key : param.keySet()) {
|
||||
if (isFirst)
|
||||
url.append("?");
|
||||
else
|
||||
url.append("&");
|
||||
url.append(key).append("=").append(param.get(key));
|
||||
}
|
||||
this.url = url.toString();
|
||||
}
|
||||
HttpGet http = new HttpGet(url);
|
||||
execute(http);
|
||||
}
|
||||
|
||||
/**
|
||||
* set http post,put param
|
||||
*/
|
||||
private void setEntity(HttpEntityEnclosingRequestBase http) {
|
||||
if (param != null) {
|
||||
List<NameValuePair> nvps = new LinkedList<NameValuePair>();
|
||||
for (String key : param.keySet())
|
||||
nvps.add(new BasicNameValuePair(key, param.get(key))); // 参数
|
||||
http.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); // 设置参数
|
||||
}
|
||||
if (xmlParam != null) {
|
||||
http.setEntity(new StringEntity(xmlParam, Consts.UTF_8));
|
||||
}
|
||||
}
|
||||
|
||||
private void execute(HttpUriRequest http) throws
|
||||
IOException {
|
||||
CloseableHttpClient httpClient = null;
|
||||
try {
|
||||
if (isHttps) {
|
||||
if (isCert) {
|
||||
FileInputStream inputStream = new FileInputStream(new File(ConstantPropertiesUtils.CERT));
|
||||
KeyStore keystore = KeyStore.getInstance("PKCS12");
|
||||
char[] partnerId2charArray = certPassword.toCharArray();
|
||||
keystore.load(inputStream, partnerId2charArray);
|
||||
SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keystore, partnerId2charArray).build();
|
||||
SSLConnectionSocketFactory sslsf =
|
||||
new SSLConnectionSocketFactory(sslContext,
|
||||
new String[]{"TLSv1"},
|
||||
null,
|
||||
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
|
||||
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
|
||||
} else {
|
||||
SSLContext sslContext = new SSLContextBuilder()
|
||||
.loadTrustMaterial(null, new TrustStrategy() {
|
||||
// 信任所有
|
||||
public boolean isTrusted(X509Certificate[] chain,
|
||||
String authType)
|
||||
throws CertificateException {
|
||||
return true;
|
||||
}
|
||||
}).build();
|
||||
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
|
||||
sslContext);
|
||||
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf)
|
||||
.build();
|
||||
}
|
||||
} else {
|
||||
httpClient = HttpClients.createDefault();
|
||||
}
|
||||
CloseableHttpResponse response = httpClient.execute(http);
|
||||
try {
|
||||
if (response != null) {
|
||||
if (response.getStatusLine() != null)
|
||||
statusCode = response.getStatusLine().getStatusCode();
|
||||
HttpEntity entity = response.getEntity();
|
||||
// 响应内容
|
||||
content = EntityUtils.toString(entity, Consts.UTF_8);
|
||||
}
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
httpClient.close();
|
||||
}
|
||||
}
|
||||
|
||||
public int getStatusCode() {
|
||||
return statusCode;
|
||||
}
|
||||
|
||||
public String getContent() throws ParseException, IOException {
|
||||
return content;
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
port: 8210
|
||||
spring:
|
||||
application:
|
||||
name: service-payment
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 82.157.68.223:8848
|
||||
username: nacos
|
||||
password: nacos
|
@@ -0,0 +1,24 @@
|
||||
spring:
|
||||
cloud:
|
||||
nacos:
|
||||
config:
|
||||
namespace: dd5265c5-8290-45bc-9d07-395c14c977d3
|
||||
server-addr: 82.157.68.223:8848
|
||||
group: service
|
||||
username: nacos
|
||||
password: nacos
|
||||
enabled: true
|
||||
file-extension: yml
|
||||
extension-configs:
|
||||
- data-id: common.yml
|
||||
group: common
|
||||
refresh: true
|
||||
- data-id: service-redis.yml
|
||||
group: common
|
||||
refresh: true
|
||||
- data-id: service-rabbitmq.yml
|
||||
group: common
|
||||
refresh: true
|
||||
- data-id: service-openfeign.yml
|
||||
group: common
|
||||
refresh: true
|
Binary file not shown.
Reference in New Issue
Block a user