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); // ...... } }