直接上代码,在order模块添加依赖
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
在配置类添加申请的商家号信息
#关联的公众号appid
weixin.pay.appid=wxXXXXXXX
#商户号
weixin.pay.partner=XXXXXXXXX
#商户key
weixin.pay.partnerkey=XXXXXXXXXX
添加微信生成二维码service
@Service
public class WeiXinServiceImpl implements WeiXinService {
@Autowired
private PaymentInfoService paymentInfoService;
@Autowired
private OrderInfoService orderInfoService;
@Autowired
private WeiXinService weiXinService;
//生成支付的二维码
@Override
public Map createNative(Long orderId) {
//支付记录表添加数据
//根据单号查询订单相关信息
OrderInfo orderInfo = orderInfoService.getById(orderId);
if (orderInfo == null){
throw new OrderException(20001,"订单不存在");
}
//添加订单状态
paymentInfoService.savePaymentInfo(orderInfo,PaymentTypeEnum.WEIXIN.getStatus());
//调用微信接口返回二维码
try {
//2 调用微信接口,得到二维码地址等信息
//封装传递微信地址参数
Map paramMap = new HashMap();
paramMap.put("appid", ConstantPropertiesUtils.APPID); //公众号id
paramMap.put("mch_id", ConstantPropertiesUtils.PARTNER); //商户号
paramMap.put("nonce_str", WXPayUtil.generateNonceStr()); //随机字符串,调用工具类
Date reserveDate = orderInfo.getReserveDate();
String reserveDateString = new DateTime(reserveDate).toString("yyyy/MM/dd");
String body = reserveDateString + "就诊"+ orderInfo.getDepname();
paramMap.put("body", body);//扫码后手机显示内容
paramMap.put("out_trade_no", orderInfo.getOutTradeNo()); //订单流水号
//paramMap.put("total_fee", order.getAmount().multiply(new BigDecimal("100")).longValue()+"");
paramMap.put("total_fee", "1");//TODO 为了测试 支付金额
paramMap.put("spbi