2020年1月9日17:22:41
github:
https://github.com/zh7314/wxpay-sdk
官方文档和sdk https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1 官方也是也有一个0.0.3版本在maven上,我下载的官方版本3.0.09版本的pom.xml里面还遗留这maven的oss配置信息,说明是有计划上maven的 但是为什么不在更新了,也不清楚原因 两种方式,第一种自己打包成jar包 需要修改地方有一处,就是配置文件的抽象类 原因: WXPayConfig 默认的类方法名,是default,在同一包内可见,跨包需要改成public 所以在不同包的情况下,官方的sdk是不能直接拿来使用的 0.0.3版本里面这个WXPayConfig 是接口,新版这里改成抽象类了 这里就可以区别,其他参考博客的使用官方的sdk要么是用 很老的版本要么就是他自己随手写的文章,没有实际使用过这个sdk
package com.github.wxpay.sdk; import java.io.InputStream; public abstract class WXPayConfig { /** * 获取 App ID * * @return App ID */ public abstract String getAppID(); /** * 获取 Mch ID * * @return Mch ID */ public abstract String getMchID(); /** * 获取 API 密钥 * * @return API密钥 */ public abstract String getKey(); /** * 获取商户证书内容 * * @return 商户证书内容 */ public abstract InputStream getCertStream(); /** * HTTP(S) 连接超时时间,单位毫秒 * * @return */ public int getHttpConnectTimeoutMs() { return 6 * 1000; } /** * HTTP(S) 读数据超时时间,单位毫秒 * * @return */ public int getHttpReadTimeoutMs() { return 8 * 1000; } /** * 获取WXPayDomain, 用于多域名容灾自动切换 * * @return */ public abstract IWXPayDomain getWXPayDomain(); /** * 是否自动上报。 若要关闭自动上报,子类中实现该函数返回 false 即可。 * * @return */ public boolean shouldAutoReport() { return true; } /** * 进行健康上报的线程的数量 * * @return */ public int getReportWorkerNum() { return 6; } /** * 健康上报缓存消息的最大数量。会有线程去独立上报 粗略计算:加入一条消息200B,10000消息占用空间 2000 KB,约为2MB,可以接受 * * @return */ public int getReportQueueMaxSize() { return 10000; } /** * 批量上报,一次最多上报多个数据 * * @return */ public int getReportBatchSize() { return 10; } }
打包的jar只修改这一个地方, 下载地址: 1:下方的qq群 2:百度网盘, 链接: https://pan.baidu.com/s/1ZHE9sK0IM_75meNeaFa-oA 提取码: s1rg 如果不放心,请自行打包,毕竟是支付重要的文件 readme我也会找时间重写 第二种就是自己吧这个上传到maven上,这个目前在处理中,第一次说构建不成功,也不清楚是什么原因 等有时间,我会把包上传到中央仓库 maven打包额外的jar包 方法1: 1.安装本地jar包到本地仓库 mvn install:install-file -Dfile=alipay-sdk-java-3.0.0.jar -DgroupId=com.aliyun -DartifactId=alipay-sdk-java-3.0.0 -Dversion=3.0.0 -Dpackaging=jar 2.普通方式导入jar包 <dependency> <groupId>com.aliyun</groupId> <artifactId>alipay-sdk-java-3.0.0</artifactId> <version>3.0.0</version> </dependency> 方法2: 打包时添加外部jar同maven中的jar包一起添加到编译后的文件当中 1.在项目根目录创建libs文件夹将使用的jar包放入其中 2.jar包以scope为system的方式导入pom文件 <dependency> <groupId>com.wxpay-sdk</groupId> <artifactId>wxpay-sdk</artifactId> <version>3.0.9</version> <scope>system</scope> <systemPath>${project.basedir}/libs/wxpay-sdk-3.0.9.jar</systemPath> </dependency> 3.添加maven-war-plugin插件 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <webResources> <resource> <directory>${project.basedir}/libs</directory> <targetPath>WEB-INF/lib</targetPath> <includes> <include>**/*.jar</include> </includes> </resource> </webResources> </configuration> </plugin>
对账接口demo数据
这个接口可以本地请求,并且不需要秘钥直接请求,本地直接请求即可测试,并不用上线上
这里需要注意的一点是,对账接口并不是实时同步的,基本是隔天同步,猜测和观察,微信内部热数据同步应该是在当天的凌晨执行的,
所以每次请求数据的时候,连续请求近3天或者5天的数据,这样才能保证数据同步跟得上,不然你请求当天永远是
{return_msg=No Bill Exist, error_code=20002, return_code=FAIL}
导致对账数据同步不上的情况出现
需要注意的一点是,解析对账单的时候是27个数据,请参考本文的数据库实体和分解方法,不然你可能对不上这个27个数据
package com.dlc.modules.business.entity; import com.baomidou.mybatisplus.annotations.TableField; import com.baomidou.mybatisplus.annotations.TableId; import com.baomidou.mybatisplus.annotations.TableName; import com.baomidou.mybatisplus.enums.FieldFill; import com.fasterxml.jackson.annotation.JsonFormat; import java.math.BigDecimal; import java.io.Serializable; import java.util.Date; @TableName("wx_reconciliation") public class WxReconciliationEntity implements Serializable { private static final long serialVersionUID = 1L; /** * 主键id */ @TableId private Long id; /** * 微信交易时间 */ private Date wxTransactionTime; /** * 公众账号id */ private String publicAccountId; /** * 商户号 */ private String merchantNumber; /** * 特约商户号 */ private String specialMerchantNo; /** * 创建时间 */ @TableField(fill = FieldFill.INSERT) private Date createTime; /** * 更新时间 */ @TableField(fill = FieldFill.INSERT_UPDATE) private Date updateTime; /** * 设备号 */ private String deviceNumber; /** * 微信订单号 */ private String wechatOrderNo; /** * 商户订单号 */ private String merchantOrderNumber; /** * 用户标识 */ private String userId; /** * 交易类型 */ private String transactionType; /** * 交易状态 */ private String tradingStatus; /** * 付款银行 */ private String payingBank; /** * 货币种类 */ private String currencyType; /** * 应结订单金额 */ private BigDecimal orderAmountToBeSettled; /** * 代金券金额 */ private BigDecimal voucherAmount; /** * 微信退款单号 */ private String wechatRefundNo; /** * 商户退款单号 */ private String merchantRefundNo; /** * 退款金额 */ private BigDecimal refundAmount; /** * 充值券退款金额 */ private BigDecimal refundAmountOfRechargeVoucher; /** * 退款类型 */ private String refundType; /** * 退款状态 */ private String refundStatus; /** * 商品名称 */ private String tradeName; /** * 商户数据包 */ private String merchantPacket; /** * 手续费 */ private BigDecimal serviceCharge; /** * 费率 */ private String rate; /** * 订单金额 */ private BigDecimal orderAmount; /** * 申请退款金额 */ private BigDecimal applicationForRefundAmount; /** * 费率备注 */ private String rateNotes; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Date getWxTransactionTime() { return wxTransactionTime; } public void setWxTransactionTime(Date wxTransactionTime) { this.wxTransactionTime = wxTransactionTime; } public String getPublicAccountId() { return publicAccountId; } public void setPublicAccountId(String publicAccountId) { this.publicAccountId = publicAccountId; } public String getMerchantNumber() { return merchantNumber; } public void setMerchantNumber(String merchantNumber) { this.merchantNumber = merchantNumber; } public String getSpecialMerchantNo() { return specialMerchantNo; } public void setSpecialMerchantNo(String specialMerchantNo) { this.specialMerchantNo = specialMerchantNo; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } public String getDeviceNumber() { return deviceNumber; } public void setDeviceNumber(String deviceNumber) { this.deviceNumber = deviceNumber; } public String getWechatOrderNo() { return wechatOrderNo; } public void setWechatOrderNo(String wechatOrderNo) { this.wechatOrderNo = wechatOrderNo; } public String getMerchantOrderNumber() { return merchantOrderNumber; } public void setMerchantOrderNumber(String merchantOrderNumber) { this.merchantOrderNumber = merchantOrderNumber; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getTransactionType() { return transactionType; } public void setTransactionType(String transactionType) { this.transactionType = transactionType; } public String getTradingStatus() { return tradingStatus; } public void setTradingStatus(String tradingStatus) { this.tradingStatus = tradingStatus; } public String getPayingBank() { return payingBank; } public void setPayingBank(String payingBank) { this.payingBank = payingBank; } public String getCurrencyType() { return currencyType; } public void setCurrencyType(String currencyType) { this.currencyType = currencyType; } public BigDecimal getOrderAmountToBeSettled() { return orderAmountToBeSettled; } public void setOrderAmountToBeSettled(BigDecimal orderAmountToBeSettled) { this.orderAmountToBeSettled = orderAmountToBeSettled; } public BigDecimal getVoucherAmount() { return voucherAmount; } public void setVoucherAmount(BigDecimal voucherAmount) { this.voucherAmount = voucherAmount; } public String getWechatRefundNo() { return wechatRefundNo; } public void setWechatRefundNo(String wechatRefundNo) { this.wechatRefundNo = wechatRefundNo; } public String getMerchantRefundNo() { return merchantRefundNo; } public void setMerchantRefundNo(String merchantRefundNo) { this.merchantRefundNo = merchantRefundNo; } public BigDecimal getRefundAmount() { return refundAmount; } public void setRefundAmount(BigDecimal refundAmount) { this.refundAmount = refundAmount; } public BigDecimal getRefundAmountOfRechargeVoucher() { return refundAmountOfRechargeVoucher; } public void setRefundAmountOfRechargeVoucher(BigDecimal refundAmountOfRechargeVoucher) { this.refundAmountOfRechargeVoucher = refundAmountOfRechargeVoucher; } public String getRefundType() { return refundType; } public void setRefundType(String refundType) { this.refundType = refundType; } public String getRefundStatus() { return refundStatus; } public void setRefundStatus(String refundStatus) { this.refundStatus = refundStatus; } public String getTradeName() { return tradeName; } public void setTradeName(String tradeName) { this.tradeName = tradeName; } public String getMerchantPacket() { return merchantPacket; } public void setMerchantPacket(String merchantPacket) { this.merchantPacket = merchantPacket; } public BigDecimal getServiceCharge() { return serviceCharge; } public void setServiceCharge(BigDecimal serviceCharge) { this.serviceCharge = serviceCharge; } public String getRate() { return rate; } public void setRate(String rate) { this.rate = rate; } public BigDecimal getOrderAmount() { return orderAmount; } public void setOrderAmount(BigDecimal orderAmount) { this.orderAmount = orderAmount; } public BigDecimal getApplicationForRefundAmount() { return applicationForRefundAmount; } public void setApplicationForRefundAmount(BigDecimal applicationForRefundAmount) { this.applicationForRefundAmount = applicationForRefundAmount; } public String getRateNotes() { return rateNotes; } public void setRateNotes(String rateNotes) { this.rateNotes = rateNotes; } @Override public String toString() { return "WxReconciliationEntity [id=" + id + ", wxTransactionTime=" + wxTransactionTime + ", publicAccountId=" + publicAccountId + ", merchantNumber=" + merchantNumber + ", specialMerchantNo=" + specialMerchantNo + ", createTime=" + createTime + ", updateTime=" + updateTime + ", deviceNumber=" + deviceNumber + ", wechatOrderNo=" + wechatOrderNo + ", merchantOrderNumber=" + merchantOrderNumber + ", userId=" + userId + ", transactionType=" + transactionType + ", tradingStatus=" + tradingStatus + ", payingBank=" + payingBank + ", currencyType=" + currencyType + ", orderAmountToBeSettled=" + orderAmountToBeSettled + ", voucherAmount=" + voucherAmount + ", wechatRefundNo=" + wechatRefundNo + ", merchantRefundNo=" + merchantRefundNo + ", refundAmount=" + refundAmount + ", refundAmountOfRechargeVoucher=" + refundAmountOfRechargeVoucher + ", refundType=" + refundType + ", refundStatus=" + refundStatus + ", tradeName=" + tradeName + ", merchantPacket=" + merchantPacket + ", serviceCharge=" + serviceCharge + ", rate=" + rate + ", orderAmount=" + orderAmount + ", applicationForRefundAmount=" + applicationForRefundAmount + ", rateNotes=" + rateNotes + "]"; } }
分解数据方法
public void analyze(String result) throws ParseException { System.err.println("微信对账处理中................."); String firstString = "费率备注"; // 把第一行表头去掉 String tradeMsg = result.substring(result.indexOf(firstString) + firstString.length()); String secendString = "总交易单数"; // 去掉汇总数据,去掉` String tradeInfo = tradeMsg.substring(0, tradeMsg.indexOf(secendString)); // System.err.println(tradeInfo); // 按行读取数据 String[] str = tradeInfo.split("\r\n"); int len = str.length; if (len > 0) { for (int i = 0; i < len; i++) { if (str[i].length() > 0) { String[] order = str[i].split(","); WxReconciliationEntity wxReconciliationEntity = new WxReconciliationEntity(); wxReconciliationEntity.setWxTransactionTime(stringToDate(order[0].replace("`", ""))); wxReconciliationEntity.setPublicAccountId(order[1].replace("`", "")); wxReconciliationEntity.setMerchantNumber(order[2].replace("`", "")); wxReconciliationEntity.setSpecialMerchantNo(order[3].replace("`", "")); wxReconciliationEntity.setDeviceNumber(order[4].replace("`", "")); wxReconciliationEntity.setWechatOrderNo(order[5].replace("`", "")); wxReconciliationEntity.setMerchantOrderNumber(order[6].replace("`", "")); wxReconciliationEntity.setUserId(order[7].replace("`", "")); wxReconciliationEntity.setTransactionType(order[8].replace("`", "")); wxReconciliationEntity.setTradingStatus(order[9].replace("`", "")); wxReconciliationEntity.setPayingBank(order[10].replace("`", "")); wxReconciliationEntity.setCurrencyType(order[11].replace("`", "")); wxReconciliationEntity.setOrderAmountToBeSettled(stringToBigDecimal(order[12].replace("`", ""))); wxReconciliationEntity.setVoucherAmount(stringToBigDecimal(order[13].replace("`", ""))); wxReconciliationEntity.setWechatRefundNo(order[14].replace("`", "")); wxReconciliationEntity.setMerchantRefundNo(order[15].replace("`", "")); wxReconciliationEntity.setRefundAmount(stringToBigDecimal(order[16].replace("`", ""))); wxReconciliationEntity .setRefundAmountOfRechargeVoucher(stringToBigDecimal(order[17].replace("`", ""))); wxReconciliationEntity.setRefundType(order[18].replace("`", "")); wxReconciliationEntity.setRefundStatus(order[19].replace("`", "")); wxReconciliationEntity.setTradeName(order[20].replace("`", "")); wxReconciliationEntity.setMerchantPacket(order[21].replace("`", "")); wxReconciliationEntity.setServiceCharge(stringToBigDecimal(order[22].replace("`", ""))); wxReconciliationEntity.setRate(order[23].replace("`", "")); wxReconciliationEntity.setOrderAmount(stringToBigDecimal(order[24].replace("`", ""))); wxReconciliationEntity .setApplicationForRefundAmount(stringToBigDecimal(order[25].replace("`", ""))); wxReconciliationEntity.setRateNotes(order[26].replace("`", "")); EntityWrapper<WxReconciliationEntity> wrapper = new EntityWrapper<>(); wrapper.eq("merchant_order_number", wxReconciliationEntity.getMerchantOrderNumber()); int selectCount = this.selectCount(wrapper); if (selectCount > 0) { WxReconciliationEntity selectOne = this.selectOne(wrapper); wxReconciliationEntity.setId(selectOne.getId()); } this.insertOrUpdate(wxReconciliationEntity); } } } }
对微信支付开发者文档中给出的API进行了封装。 com.github.wxpay.sdk.WXPay类下提供了对应的方法: 方法名 说明 microPay 刷卡支付 unifiedOrder 统一下单 orderQuery 查询订单 reverse 撤销订单 closeOrder 关闭订单 refund 申请退款 refundQuery 查询退款 downloadBill 下载对账单 report 交易保障 shortUrl 转换短链接 authCodeToOpenid 授权码查询openid 注意: 证书文件不能放在web服务器虚拟目录,应放在有访问权限控制的目录中,防止被他人下载 建议将证书文件名改为复杂且不容易猜测的文件名 商户服务器要做好病毒和木马防护工作,不被非法侵入者窃取证书文件 请妥善保管商户支付密钥、公众帐号SECRET,避免密钥泄露 参数为Map<String, String>对象,返回类型也是Map<String, String> 方法内部会将参数会转换成含有appid、mch_id、nonce_str、sign\_type和sign的XML 可选HMAC-SHA256算法和MD5算法签名 通过HTTPS请求得到返回数据后会对其做必要的处理(例如验证签名,签名错误则抛出异常) 对于downloadBill,无论是否成功都返回Map,且都含有return_code和return_msg,若成功,其中return_code为SUCCESS,另外data对应对账单数据 示例 配置类MyConfig: package com.dlc.common.config; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; import org.springframework.util.ResourceUtils; import com.github.wxpay.sdk.IWXPayDomain; import com.github.wxpay.sdk.WXPayConfig; import com.github.wxpay.sdk.WXPayConstants; @Configuration public class MyWxPayConfig extends WXPayConfig { @Autowired private Environment env; private byte[] certData; private String appId = ""; private String mchId = ""; private String mchKey = ""; private String certPath = ""; @Override public String getAppID() { // return env.getProperty("weiXin.appid"); return this.appId; } @Override public String getMchID() { // return env.getProperty("weiXin.mch-id"); return this.mchId; } @Override public String getKey() { // return env.getProperty("weiXin.mch-key"); return this.mchKey; } //这里不写在构造方法是因为开发和线上的秘钥文件地址不同 @Override public InputStream getCertStream() { try { String certPath = this.certPath; File file = new File(certPath); InputStream certStream = new FileInputStream(file); this.certData = new byte[(int) file.length()]; certStream.read(this.certData); certStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData); return certBis; } @Override public IWXPayDomain getWXPayDomain() { IWXPayDomain iwxPayDomain = new IWXPayDomain() { public void report(String domain, long elapsedTimeMillis, Exception ex) { } public DomainInfo getDomain(WXPayConfig config) { return new IWXPayDomain.DomainInfo(WXPayConstants.DOMAIN_API, true); } }; return iwxPayDomain; } } 统一下单: 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(); } } } 订单查询: 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(); } } } 退款查询: 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.refundQuery(data); System.out.println(resp); } catch (Exception e) { e.printStackTrace(); } } } 下载对账单: 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("bill_date", "20140603"); data.put("bill_type", "ALL"); try { Map<String, String> resp = wxpay.downloadBill(data); System.out.println(resp); } catch (Exception e) { e.printStackTrace(); } } } 其他API的使用和上面类似。 暂时不支持下载压缩格式的对账单,但可以使用该SDK生成请求用的XML数据: import com.github.wxpay.sdk.WXPay; import com.github.wxpay.sdk.WXPayUtil; 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("bill_date", "20140603"); data.put("bill_type", "ALL"); data.put("tar_type", "GZIP"); try { data = wxpay.fillRequestData(data); System.out.println(WXPayUtil.mapToXml(data)); } catch (Exception e) { e.printStackTrace(); } } } 收到支付结果通知时,需要验证签名,可以这样做: import com.github.wxpay.sdk.WXPay; import com.github.wxpay.sdk.WXPayUtil; import java.util.Map; public class WXPayExample { public static void main(String[] args) throws Exception { String notifyData = "...."; // 支付结果通知的xml格式数据 MyConfig config = new MyConfig(); WXPay wxpay = new WXPay(config); Map<String, String> notifyMap = WXPayUtil.xmlToMap(notifyData); // 转换成map if (wxpay.isPayResultNotifySignatureValid(notifyMap)) { // 签名正确 // 进行处理。 // 注意特殊情况:订单已经退款,但收到了支付结果成功的通知,不应把商户侧订单状态从退款改成支付成功 } else { // 签名错误,如果数据里没有sign字段,也认为是签名错误 } } } HTTPS请求可选HMAC-SHA256算法和MD5算法签名: import com.github.wxpay.sdk.WXPay; import com.github.wxpay.sdk.WXPayConstants; public class WXPayExample { public static void main(String[] args) throws Exception { MyConfig config = new MyConfig(); WXPay wxpay = new WXPay(config, WXPayConstants.SignType.HMACSHA256); // ...... } } 若需要使用sandbox环境: import com.github.wxpay.sdk.WXPay; import com.github.wxpay.sdk.WXPayConstants; public class WXPayExample { public static void main(String[] args) throws Exception { MyConfig config = new MyConfig(); WXPay wxpay = new WXPay(config, WXPayConstants.SignType.MD5, true); // ...... } }