springboot引入依赖
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-pay</artifactId>
<version>4.6.0</version>
</dependency>
配置
wx:
pay:
appId: *********
mchId: ********
apiV3Key: ********
certSerialNo: ********
privateKeyPath: classpath:apiclient_key.pem #apiclient_key.pem证书文件的绝对路径或者以classpath:开头的类路径
privateCertPath: classpath:apiclient_cert.pem #apiclient_cert.pem证书文件的绝对路径或者以classpath:开头的类路径
引入证书
微信支付自动配置
package com.ruoyi.system.pay;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* <pre>
* 微信支付自动配置
* Created by BinaryWang on 2019/4/17.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@Configuration
@EnableConfigurationProperties(WxPayProperties.class)
@ConditionalOnClass(WxPayService.class)
@ConditionalOnProperty(prefix = "wx.pay", value = "enabled", matchIfMissing = true)
public class WxPayAutoConfiguration {
private WxPayProperties properties;
@Autowired
public WxPayAutoConfiguration(WxPayProperties properties) {
this.properties = properties;
}
/**
* 构造微信支付服务对象.
*
* @return 微信支付service
*/
@Bean
@ConditionalOnMissingBean(WxPayService.class)
public WxPayService wxPayService() {
final WxPayServiceImpl wxPayService = new WxPayServiceImpl();
WxPayConfig payConfig = new WxPayConfig();
payConfig.setAppId(StringUtils.trimToNull(this.properties.getAppId()));
payConfig.setMchId(StringUtils.trimToNull(this.properties.getMchId()));
payConfig.setMchKey(StringUtils.trimToNull(this.properties.getMchKey()));
payConfig.setSubAppId(StringUtils.trimToNull(this.properties.getSubAppId()));
payConfig.setSubMchId(StringUtils.trimToNull(this.properties.getSubMchId()));
payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getKeyPath()));
payConfig.setUseSandboxEnv(this.properties.isUseSandboxEnv());
//以下是apiv3以及支付分相关
payConfig.setServiceId(StringUtils.trimToNull(this.properties.getServiceId()));
payConfig.setPayScoreNotifyUrl(StringUtils.trimToNull(this.properties.getPayScoreNotifyUrl()));
payConfig.setPrivateKeyPath(StringUtils.trimToNull(this.properties.getPrivateKeyPath()));
payConfig.setPrivateCertPath(StringUtils.trimToNull(this.properties.getPrivateCertPath()));
payConfig.setCertSerialNo(StringUtils.trimToNull(this.properties.getCertSerialNo()));
payConfig.setApiV3Key(StringUtils.trimToNull(this.properties.getApiv3Key()));
wxPayService.setConfig(payConfig);
return wxPayService;
}
}
package com.ruoyi.system.pay;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* <pre>
* 微信支付属性配置类
* Created by Binary Wang on 2019/4/17.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@ConfigurationProperties(prefix = "wx.pay")
public class WxPayProperties {
/**
* 设置微信公众号或者小程序等的appid.
*/
private String appId;
/**
* 微信支付商户号.
*/
private String mchId;
/**
* 微信支付商户密钥.
*/
private String mchKey;
/**
* 服务商模式下的子商户公众账号ID,普通模式请不要配置,请在配置文件中将对应项删除.
*/
private String subAppId;
/**
* 服务商模式下的子商户号,普通模式请不要配置,最好是请在配置文件中将对应项删除.
*/
private String subMchId;
/**
* apiclient_cert.p12文件的绝对路径,或者如果放在项目中,请以classpath:开头指定.
*/
private String keyPath;
/**
* 微信支付分serviceId
*/
private String serviceId;
/**
* 证书序列号
*/
private String certSerialNo;
/**
* apiV3秘钥
*/
private String apiv3Key;
/**
* 微信支付分回调地址
*/
private String payScoreNotifyUrl;
/**
* apiv3 商户apiclient_key.pem
*/
private String privateKeyPath;
/**
* apiv3 商户apiclient_cert.pem
*/
private String privateCertPath;
/**
* 微信支付是否使用仿真测试环境.
* 默认不使用
*/
private boolean useSandboxEnv;
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getMchId() {
return mchId;
}
public void setMchId(String mchId) {
this.mchId = mchId;
}
public String getMchKey() {
return mchKey;
}
public void setMchKey(String mchKey) {
this.mchKey = mchKey;
}
public String getSubAppId() {
return subAppId;
}
public void setSubAppId(String subAppId) {
this.subAppId = subAppId;
}
public String getSubMchId() {
return subMchId;
}
public void setSubMchId(String subMchId) {
this.subMchId = subMchId;
}
public String getKeyPath() {
return keyPath;
}
public void setKeyPath(String keyPath) {
this.keyPath = keyPath;
}
public String getServiceId() {
return serviceId;
}
public void setServiceId(String serviceId) {
this.serviceId = serviceId;
}
public String getCertSerialNo() {
return certSerialNo;
}
public void setCertSerialNo(String certSerialNo) {
this.certSerialNo = certSerialNo;
}
public String getApiv3Key() {
return apiv3Key;
}
public void setApiv3Key(String apiv3Key) {
this.apiv3Key = apiv3Key;
}
public String getPayScoreNotifyUrl() {
return payScoreNotifyUrl;
}
public void setPayScoreNotifyUrl(String payScoreNotifyUrl) {
this.payScoreNotifyUrl = payScoreNotifyUrl;
}
public String getPrivateKeyPath() {
return privateKeyPath;
}
public void setPrivateKeyPath(String privateKeyPath) {
this.privateKeyPath = privateKeyPath;
}
public String getPrivateCertPath() {
return privateCertPath;
}
public void setPrivateCertPath(String privateCertPath) {
this.privateCertPath = privateCertPath;
}
public boolean isUseSandboxEnv() {
return useSandboxEnv;
}
public void setUseSandboxEnv(boolean useSandboxEnv) {
this.useSandboxEnv = useSandboxEnv;
}
}
支付demo
package com.ruoyi.system.pay;
import com.alibaba.fastjson.JSON;
import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderV3Request;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.util.ResourcesUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import me.chanjar.weixin.common.util.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
/**
* 参数配置 信息操作处理
*
* @author ruoyi
*/
@RestController
@RequestMapping("/pay")
public class SysPayController extends BaseController
{
@Autowired
private WxPayService wxPayService;
/**
* 微信小程序支付
*/
@GetMapping("/demo")
public AjaxResult demo() throws WxPayException {
String outTradeNo = RandomUtils.getRandomStr();
String notifyUrl = "https://api.qq.com/";
System.out.println("outTradeNo = " + outTradeNo);
WxPayUnifiedOrderV3Request request = new WxPayUnifiedOrderV3Request();
request.setOutTradeNo(outTradeNo);
request.setNotifyUrl(notifyUrl);
request.setDescription("test");
WxPayUnifiedOrderV3Request.Payer payer = new WxPayUnifiedOrderV3Request.Payer();
payer.setOpenid("oQfWg**********p60Kcw5s");
request.setPayer(payer);
//构建金额信息
WxPayUnifiedOrderV3Request.Amount amount = new WxPayUnifiedOrderV3Request.Amount();
//设置币种信息
amount.setCurrency(WxPayConstants.CurrencyType.CNY);
//设置金额
amount.setTotal(BaseWxPayRequest.yuan2Fen(BigDecimal.ONE));
request.setAmount(amount);
WxPayUnifiedOrderV3Result.JsapiResult result = this.wxPayService.createOrderV3(TradeTypeEnum.JSAPI, request);
System.out.println(JSON.toJSONString(result));
return success();
}
/**
* 关闭订单
* @return
* @throws WxPayException
*/
@GetMapping("/close")
public AjaxResult close() throws WxPayException {
this.wxPayService.closeOrderV3("tradeNo000001");
return success();
}
}