Java-Spring Boot 微信扫码支付(Native模式)
官方文档:
SDK和DEMO下载:
- 集成SpringBoot,使用Java代码发起支付请求
一、准备工作
二、集成SpringBoot,使用Java代码发起支付请求
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("out_trade_no", "2016090910595900000012");
try {
Map<String, String> resp = wxpay.orderQuery(data);
System.out.println(resp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
其他API的使用和上面类似
有什么问题大家多互相交流^_^
