微信支付SDK准备
- 下载sdk 点击进入sdk下载页面
- 解压可看到
- 文件说明
文件名 | 说明 | 备注 |
---|---|---|
IWXPayDomain | 微信定义的接口,需要我们自己实现,主要的作用就是多域名容灾自动切换 | WXPayConfig中定义了此接口 |
WXPayConfig | 微信定义的抽象类,是微信支付必要基础参数的配置类,我们根据自己的业务实现即可 | IWXPayDomain在此需要实现 |
WXPayConstants | 微信提供的常量类,里面有各节点域名,和各类接口,分为正式和沙箱 | - |
WXPayXmlUtil | XML文档解析类 | - |
WXPayUtil | 微信支付xml转换,签名,时间戳,随机字符串等工具类 | - |
WXPayReport | 微信支付账单等下载支撑类,感兴趣可自行扩展 | - |
WXPayRequest | 微信支付请求支撑类,包含了ssl请求的封装 | 即证书和非证书请求封装 |
WXPay | 微信支付各种场景的封装,下单,删单,退款等,里面还封装了检测方法 | 我们使用的最终类,上面的一切都是为他准备的 |
扩展使用
- 就是很简单的扩展
- WXPayException 定义的一个异常
public class WXPayException extends RuntimeException{
/**
*
*/
private static final long serialVersionUID = 1L;
public WXPayException(String message) {
super(message);
}
public WXPayException(String message, Throwable cause) {
super(message, cause);
}
public static WXPayException throwable(String message) {
return new WXPayException(message);
}
public static WXPayException throwable(String message, Throwable cause) {
return new WXPayException(message, cause);
}
}
WXPayConfigDetails扩展的第一步,继承WXPayConfig实现内部抽象方法
public class WXPayConfigDetails extends WXPayConfig {
private byte[] certData;
private String appID;
private String mchID;
private String key;
private static final String IO_MSG = "证书文件读取失败,请联系管理员";
public WXPayConfigDetails(String appID, String MchID, String key, String certPath) {
this.appID = appID;
this.mchID = MchID;
this.key = key;
this.certData = this.getCertFilea2Byte(certPath);
}
public WXPayConfigDetails(String appID, String MchID, String key) {
this.appID = appID;
this.mchID = MchID;
this.key = key;
}
@Override
String getAppID() {
// TODO Auto-generated method stub
return appID;
}
@Override
String getMchID() {
// TODO Auto-generated method stub
return mchID;
}
@Override
String getKey() {
// TODO Auto-generated method stub
return key;
}
@Override
InputStream getCertStream() {
// TODO Auto-generated method stub
return new ByteArrayInputStream(certData);
}
@Override
IWXPayDomain getWXPayDomain() {
// TODO Auto-generated method stub
return new IWXPayDomain() {
Test WeChat server is delayed or not ...
public void report(String domain, long elapsedTimeMillis, Exception ex) {
// TODO Auto-generated method stub
//Test WeChat server is delayed or not ...
}
//常量类中的域名 选取一个设为主域名
public DomainInfo getDomain(WXPayConfig config) {
// TODO Auto-generated method stub
return new DomainInfo(WXPayConstants.DOMAIN_API, true);
}
};
}
//将证书转为byte[]
private byte[] getCertFilea2Byte(String certPath) {
if(StringUtils.isBlank(certPath))
return new byte[] {};
File file = new File(certPath);
byte[] certData = new byte[(int) file.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
fis.read(certData);
} catch (Exception e) {
// TODO: handle exception
throw WXPayException.throwable(IO_MSG);
} finally {
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
throw WXPayException.throwable(IO_MSG);
}
}
}
return certData;
}
@Override
public String toString() {
return "WXPayConfigDetails [certData=" + Arrays.toString(certData) + ", appID=" + appID + ", mchID=" + mchID
+ ", key=" + key + "]";
}
}
- WXPays方便使用又加了一点扩展
public class WXPays {
private WXPayConfigDetails wxpayConfigDetails;
private static class WXPaysHoder {
private static WXPays wxpay = new WXPays();
}
private WXPays() {}
public static WXPays getInstance() {
return WXPaysHoder.wxpay;
}
public WXPays build(String appId, String mchId, String key, String certPath) {
wxpayConfigDetails = new WXPayConfigDetails(appId, mchId, key, certPath);
return WXPays.getInstance();
}
public WXPays build(String appId, String mchId, String key) {
return WXPays.getInstance().build(appId, mchId, key, null);
}
public WXPay wxpay() throws Exception {
return new WXPay(wxpayConfigDetails);
}
}
调用
public static void main(String[] args) throws Exception {
Map<?, ?> result = WXPays.getInstance()
.build("wxAppID", "wxMchID", "APIKey")
.wxpay()
.unifiedOrder(null)//统一下单;
System.err.println(result);
}
只是初步研究,之前没好好看SDK,自己转换XML,发送HTTP(S)请求,现在好好看了一下,微信提供好了,还是直接用更好的。
如有问题,欢迎指出,谢谢