准备工作
准备好公众号及支付商户号相关的:appid,商户号id,appsecret,apikey(支付密钥v2),下载好api支付证书路径,公众号中配置好业务域名(前端与域名)。
[common]
server_addr = #fprs程序所在服务器地址
server_port = 7000
[web]
type = http
local_port = 5100
custom_domains =#域名一
[web2]
type = http
local_port = 5101
custom_domains = #域名二
[web3]
type = http
local_port = 5102
custom_domains =#域名三
[web4]
type = http
local_port = 80
custom_domains =#域名四
将frps.exe 和frps.ini 复制到域名解析到的服务器,运行
本地运行 frpc -c frpc.ini,域名就能解析到本地了
修改WXPay
修改WXPay构造函数,使加密方式一直是md5,前端使用chooseWXPay调起微信支付时,修改wxpay后就不再出现:支付签名验证错误了
public WXPay(final WXPayConfig config, final String notifyUrl, final boolean autoReport, final boolean useSandbox) throws Exception {
this.config = config;
this.notifyUrl = notifyUrl;
this.autoReport = autoReport;
this.useSandbox = useSandbox;
// if (useSandbox) {
// this.signType = WXPayConstants.SignType.MD5; // 沙箱环境
// }
// else {
// this.signType = WXPayConstants.SignType.HMACSHA256;
// }
this.signType = WXPayConstants.SignType.MD5;
this.wxPayRequest = new WXPayRequest(config);
}
新建PayConfig
public class PayConfig extends WXPayConfig {
private byte[] certData;
private String appId;
private String appsecret;
private String mchId;
private String certpath;
private String key;
public PayConfig(String appId,String appsecret,String mchId,String certpath,String apiKey) throws Exception {
this.appId = appId;
this.appsecret = appsecret;
this.mchId = mchId;
this.certpath = certpath;
this.key = apiKey;
String filePath = certpath +"apiclient_cert.p12";
File file = new File(filePath);
InputStream certStream = new FileInputStream(file);
this.certData = new byte[(int) file.length()];
certStream.read(this.certData);
certStream.close();
}
public String getAppID() {
return appId;
}
public String getMchID() {
return mchId;
}
public String getKey() {
return key;
}
public InputStream getCertStream() {
ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
return certBis;
}
public int getHttpConnectTimeoutMs() {
return 8000;
}
public int getHttpReadTimeoutMs() {
return 10000;
}
@Override
protected IWXPayDomain getWXPayDomain() {
return new IWXPayDomain() {
@Override
public void report(String domain, long elapsedTimeMillis, Exception ex) {
}
@Override
public DomainInfo getDomain(WXPayConfig config) {
return new IWXPayDomain.DomainInfo(WXPayConstants.DOMAIN_API,true);
}
};
}
}
统一下单 java端
@Override
public PayResultVo pay(String openId,String body,String orderId,String totalFee,String ip) throws Exception {
PayConfig config = new PayConfig(appId,appsecret,mchId,certpath,apikey);
WXPay wxpay = new WXPay(config);
Map<String, String> data = new HashMap<String, String>();
data.put("body", body);
data.put("out_trade_no", orderId);
data.put("device_info", "");
data.put("fee_type", "CNY");
data.put("total_fee", totalFee);
data.put("spbill_create_ip", ip);
data.put("notify_url", myUrl