1.添加配置文件
wxpay.appID=此处填写APPID
wxpay.mchID=此处填写MCHID
wxpay.key=此处填写KEY
wxpay.spbillCreateIp=127.0.0.1
wxpay.notifyUrl=http://外网地址/api/pay/wxPay/notify
wxpay.successUrl=http://外网地址/api/pay/wxPay/success
wxpay.failUrl=http://外网地址/api/pay/wxPay/fail
wxpay.tradeType=NATIVE
2.下载微信SDK
链接: <a href="https://pan.baidu.com/s/1GafO4vJUCF7AkGiTWuyfnQ "></a>
提取码:5n3m
3.将SDK里面的所有java文件拷贝到sdk包中
![https://www.yaconit.com/md/java/springboot/resources/9.jpg](https://i-blog.csdnimg.cn/blog_migrate/4d5d0827b0f99c7113aca63b7102680f.png)
4.实现SDK中的IWXPayDomain接口
package com.example.wxpay.config;
import com.example.wxpay.sdk.IWXPayDomain;
import com.example.wxpay.sdk.WXPayConfig;
import com.example.wxpay.sdk.WXPayConstants;
import org.springframework.stereotype.Component;
@Component
public class WXPayDomain implements IWXPayDomain {
@Override
public void report(String domain, long elapsedTimeMillis, Exception ex) { }
@Override
public DomainInfo getDomain(WXPayConfig config) {
return new DomainInfo(WXPayConstants.DOMAIN_API, true);
}
}
5.创建配置对象类
package com.example.wxpay.config;
import com.example.wxpay.sdk.IWXPayDomain;
import com.example.wxpay.sdk.WXPayConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.InputStream;
@Component
@ConfigurationProperties(prefix = "wxpay")
@PropertySource("classpath:wechat.properties")
public class WechatConfig extends WXPayConfig {
private String appID;
private String mchID;
private String key;
private String spbillCreateIp;
private String notifyUrl;
private String successUrl;
private String failUrl;
private String tradeType;
@Resource
private WXPayDomain wxPayDomain;
@Override
protected InputStream getCertStream() { return null; }
@Override
protected IWXPayDomain getWXPayDomain() { return wxPayDomain; }
@Override
public String getAppID() { return appID; }
@Override
public String getMchID() { return mchID; }
@Override
public String getKey() { return key; }
}
6.创建测试服务器
package com.example.wxpay.controller;
import com.example.wxpay.config.WechatConfig;
import com.example.wxpay.sdk.WXPay;
import com.example.wxpay.sdk.WXPayConstants;
import com.example.wxpay.sdk.WXPayUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/api/pay")
public class TestController {
@Resource
private WechatConfig wechatConfig;
@GetMapping("/wxpay/{orderNo}")
public Map<String, Object> wxPay(@PathVariable String orderNo) {
Map<String, Object> dto = new HashMap<>();
Map<String, String> reqData = new HashMap<>();
reqData.put("body", "Java精品课");
reqData.put("out_trade_no", orderNo);
reqData.put("total_fee", "1");
reqData.put("spbill_create_ip", wechatConfig.getSpbillCreateIp());
reqData.put("notify_url", wechatConfig.getNotifyUrl());
reqData.put("trade_type", wechatConfig.getTradeType());
try {
WXPay wxPay = new WXPay(wechatConfig, wechatConfig.getNotifyUrl(), false, false);
Map<String, String> stringMap = wxPay.unifiedOrder(reqData);
if (!WXPayUtil.isSignatureValid(stringMap, wechatConfig.getKey(), WXPayConstants.SignType.HMACSHA256)) {
dto.put("code", 0);
dto.put("msg", "签名校验失败");
} else if (!stringMap.get("return_code").equals(WXPayConstants.SUCCESS) || !stringMap.get("result_code").equals(WXPayConstants.SUCCESS)) {
dto.put("code", 0);
dto.put("msg", "获取付款二维码失败:" + stringMap.get("err_code_des").split(" ")[1]);
} else {
dto.put("code", 1);
dto.put("code_url", stringMap.get("code_url"));
}
} catch (Exception e) {
dto.put("code", 0);
dto.put("msg", e.getMessage());
}
return dto;
}
@PostMapping("/wxPay/notify")
public Map<String, Object> wxPayNotify(HttpServletRequest request) {
Map<String, Object> dto = new HashMap<>();
InputStream inputStream = null;
BufferedReader reader = null;
try {
StringBuilder builder = new StringBuilder();
inputStream = request.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String line = "";
while ((line = reader.readLine()) != null) {
builder.append(line);
}
String wxParamXml = builder.toString();
if (!WXPayUtil.isSignatureValid(wxParamXml, wechatConfig.getKey())) {
dto.put("code", 0);
dto.put("msg", "签名校验失败");
} else {
Map wxPayResult = WXPayUtil.xmlToMap(wxParamXml);
dto.put("code", 1);
dto.put("data", wxPayResult);
}
} catch (Exception e) {
dto.put("code", 0);
dto.put("msg", e.getMessage());
} finally {
try {
reader.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return dto;
}
}