前言:最近开发微信支付功能,总结一下做个分享
官方文档:https://pay.weixin.qq.com/wiki/doc/apiv3/index.shtml
微信商户平台:https://pay.weixin.qq.com/
SDK和DEMO下载:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=11_1
注意事项:微信扫码支付,必须要商户开通NATIVE支付产品,请注意开通后不能长时间无交易,否在功能会被冻结,具体时长请以微信官方公告为准
微信NATIVE支付流程:
- 准备工作:获取微信支付数据(APPID,商户号,API密钥,微信支付证书)
- 集成SpringBoot,使用Java代码发起支付请求
- 微信收到支付请求后,返回URL,把URL生成二维码,用户使用微信扫码,进行支付
- 扫码支付成功后,微信发送异步通知请求,异步通知路径可在配置文件中进行配置
- 收到异步通知结果后,进行验签,验签通过,返回成功信息通知微信不在进行异步通知
- 此时微信支付流程完成,调用微信查询接口,确认支付成功
一、准备工作
1.申请服务号,服务号申请通过后在微信公众平台开通微信支付
2.微信支付开通后,绑定的邮箱会收到对应的微信支付所需要的信息(APPID,商户号)
3.登录微信商户平台,设置API密钥,下载支付证书,设置API密钥的时候保存一份
二、集成SpringBoot,使用Java代码发起支付请求
1、在pom.xml文件添加微信支付依赖
<!-- 微信支付 -->
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
2、在项目中新建一个微信工具类
public class WxPayConfig implements WXPayConfig {
private byte[] certData;
//wx.cert-path:配置文件中配置的证书路径
public WxPayConfig(@Value("${wx.cert-path}") String certPath){
InputStream certStream = null;
try{
File file = new File(certPath);
certStream = new FileInputStream(file);
this.certData = new byte[(int) file.length()];
certStream.read(this.certData);
}catch(Exception e){
log.error(GlobalExceptionHandler.getExceptionMessage("",e));
}finally {
if(certStream != null){
try {
certStream.close();
} catch (IOException e) {
log.error(GlobalExceptionHandler.getExceptionMessage("",e));
}
}
}
}
@Override
public String getAppID() {
// 你的APPID
return "";
}
@Override
public String getMchID() {
// 你的商户号
return "";
}
@Override
public String getKey() {
// 你的API密钥
return "";
}
@Override
public InputStream getCertStream() {
// TODO Auto-generated method stub
ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
return certBis;
}
@Override
public int getHttpConnectTimeoutMs() {
// TODO Auto-generated method stub
return 8000;
}
@Override
public int getHttpReadTimeoutMs() {
// TODO Auto-generated method stub
return 10000;
}
}
3、下载SDK,地址上述
从下载的SDK中解压以下几个文件,导入项目
4、发送支付请求
import com.github.wxpay.sdk.WXPay;
import java.util.HashMap;
import java.util.Map;
public class WXPayExample {
public static void main(String[] args) throws Exception {
MyConfig config = new MyConfig();
WXPay wxpay = new WXPay(config);
Map<String, String> data = new HashMap<String, String>();
data.put("body", "腾讯充值中心-QQ会员充值");
data.put("out_trade_no", "2016090910595900000012");
data.put("device_info", "");
data.put("fee_type", "CNY");
data.put("total_fee", "1");
data.put("spbill_create_ip", "123.12.12.123");
data.put("notify_url", "http://www.example.com/wxpay/notify");
data.put("trade_type", "NATIVE"); // 此处指定为扫码支付
data.put("product_id", "12");
try {
Map<String, String> resp = wxpay.unifiedOrder(data);
System.out.println(resp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
5、微信回调处理
@RequestMapping("/callBack")
public void wxnotify(HttpServletRequest request, HttpServletResponse response) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
String resXml = "";
InputStream inStream;
try {
inStream = request.getInputStream();
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outSteam.write(buffer, 0, len);
}
// 获取微信调用我们notify_url的返回信息
String result = new String(outSteam.toByteArray(), "utf-8");
// 关闭流
outSteam.close();
inStream.close();
// xml转换为map
Map<String, String> resultMap = WXPayUtil.xmlToMap(result);
// 判断状态 验证签名是否正确
if (WXPayConstants.SUCCESS.equalsIgnoreCase(resultMap.get(WXPayConstants.RESULT_CODE))) {
if (WXPayUtil.isSignatureValid(resultMap, WXPayConstants.API_KEY)) {
// 验签成功,修改订单信息
}
} else {
resXml = resFailXml;
}
resXml = resFailXml;
} catch (Exception e) {
WXPayUtil.getLogger().error("wxnotify:支付回调发布异常:", e);
} finally {
try {
// 处理业务完毕
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
out.write(resXml.getBytes());
out.flush();
out.close();
} catch (IOException e) {
WXPayUtil.getLogger().error("wxnotify:支付回调发布异常:out:", e);
}
}
}
6、微信订单查询
import com.github.wxpay.sdk.WXPay;
import java.util.HashMap;
import java.util.Map;
public class WXPayExample {
public static void main(String[] args) throws Exception {
MyConfig config = new MyConfig();
WXPay wxpay = new WXPay(config);
Map<String, String> data = new HashMap<String, String>();
data.put("out_trade_no", "2016090910595900000012");
try {
Map<String, String> resp = wxpay.orderQuery(data);
System.out.println(resp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
其他API的使用和上面类似
到此Spring Boot 整合微信扫码支付(NATIVE模式),就完成了
有什么问题大家多互相交流^_^