Skip to content

Commit

Permalink
添加微信沙盒模式的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
Javen205 committed Aug 11, 2017
1 parent b42eddc commit 80d3f6d
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 168 deletions.
158 changes: 0 additions & 158 deletions src/main/java/com/jpay/weixin/api/QrCodeScanPayApi.java

This file was deleted.

62 changes: 55 additions & 7 deletions src/main/java/com/jpay/weixin/api/WxPayApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ public class WxPayApi {
//查询企业付款
private static final String GETTRANSFERINFO_URL = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo";

//获取沙箱秘钥
private static final String GETSINGKEY= "https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey";
//统一下单接口
private static final String UNIFIEDORDER_SANDBOXNEW_URL = "https://api.mch.weixin.qq.com/sandboxnew/pay/unifiedorder";
//刷卡支付
private static final String MICROPAY_SANDBOXNEW_RUL = "https://api.mch.weixin.qq.com/sandboxnew/pay/micropay";
//订单查询
private static final String ORDERQUERY_SANDBOXNEW_URL = "https://api.mch.weixin.qq.com/sandboxnew/pay/orderquery";
//申请退款
private static final String REFUND_SANDBOXNEW_URL = "https://api.mch.weixin.qq.com/sandboxnew/secapi/pay/refund";
//查询退款
private static final String REFUNDQUERY_SANDBOXNEW_URL = "https://api.mch.weixin.qq.com/sandboxnew/pay/refundquery";
//下载对账单
private static final String DOWNLOADBILLY_SANDBOXNEW_URL = "https://api.mch.weixin.qq.com/sandboxnew/pay/downloadbill";

private WxPayApi() {}
/**
* 交易类型枚举
Expand All @@ -51,25 +66,47 @@ public static enum TradeType {
JSAPI, NATIVE, APP, WAP , MICROPAY
}


/**
* 获取验签秘钥API
* @param mch_id 商户号
* @param paternerKey Api 密钥
* @return
*/
public static String getsignkey(String mch_id,String partnerKey){
Map<String, String> map =new HashMap<String, String>();
String nonce_str = String.valueOf(System.currentTimeMillis());
map.put("mch_id", mch_id);
map.put("nonce_str", nonce_str);
map.put("sign", PaymentKit.createSign(map, partnerKey));
return doPost(GETSINGKEY, map);
}

/**
* 统一下单
* 服务商模式接入文档:https://pay.weixin.qq.com/wiki/doc/api/native_sl.php?chapter=9_1
* 商户模式接入文档:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1
* @param isSandbox 是否是沙盒环境
* @param params
* @return
*/
public static String pushOrder(Map<String, String> params){
public static String pushOrder(boolean isSandbox,Map<String, String> params){
if(isSandbox)
return doPost(UNIFIEDORDER_SANDBOXNEW_URL, params);
return doPost(UNIFIEDORDER_URL, params);
}

/**
* 订单查询
* 服务商模式接入文档:https://pay.weixin.qq.com/wiki/doc/api/micropay_sl.php?chapter=9_2
* 商户模式接入文档:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_2
* @param isSandbox 是否是沙盒环境
* @param params 请求参数
* @return
*/
public static String orderQuery(Map<String, String> params){
public static String orderQuery(boolean isSandbox,Map<String, String> params){
if (isSandbox)
return doPost(ORDERQUERY_SANDBOXNEW_URL, params);
return doPost(ORDERQUERY_URL, params);
}
/**
Expand Down Expand Up @@ -99,32 +136,41 @@ public static String orderReverse(Map<String, String> params, String certPath, S
* 申请退款
* 服务商模式接入文档:https://pay.weixin.qq.com/wiki/doc/api/micropay_sl.php?chapter=9_4
* 商户模式接入文档:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_4
* @param isSandbox 是否是沙盒环境
* @param params 请求参数
* @param certPath 证书文件目录
* @param certPass 证书密码
* @return
*/
public static String orderRefund(Map<String, String> params, String certPath, String certPass){
public static String orderRefund(boolean isSandbox, Map<String, String> params, String certPath, String certPass){
if (isSandbox)
return doPostSSL(REFUND_SANDBOXNEW_URL,params , certPath, certPass);
return doPostSSL(REFUND_URL,params , certPath, certPass);
}
/**
* 查询退款
* 服务商模式接入文档:https://pay.weixin.qq.com/wiki/doc/api/micropay_sl.php?chapter=9_5
* 商户模式接入文档:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_5
* @param isSandbox 是否是沙盒环境
* @param params 请求参数
* @return
*/
public static String orderRefundQuery(Map<String, String> params){
public static String orderRefundQuery(boolean isSandbox, Map<String, String> params){
if(isSandbox)
return doPost(REFUNDQUERY_SANDBOXNEW_URL,params);
return doPost(REFUNDQUERY_URL,params);
}
/**
* 下载对账单
* 服务商模式接入文档:https://pay.weixin.qq.com/wiki/doc/api/micropay_sl.php?chapter=9_6
* 商户模式接入文档:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_6
* @param isSandbox 是否是沙盒环境
* @param params 请求参数
* @return
*/
public static String downloadBill(Map<String, String> params){
public static String downloadBill(boolean isSandbox, Map<String, String> params){
if (isSandbox)
return doPost(DOWNLOADBILLY_SANDBOXNEW_URL,params);
return doPost(DOWNLOADBILLY_URL,params);
}

Expand Down Expand Up @@ -163,11 +209,13 @@ public static String authCodeToOpenid(Map<String, String> params){
* 刷卡支付
* 服务商模式接入文档:https://pay.weixin.qq.com/wiki/doc/api/micropay_sl.php?chapter=9_10&index=1
* 商户模式接入文档: https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_10&index=1
*
* @param isSandbox 是否是沙盒环境
* @param params 请求参数
* @return
*/
public static String micropay(Map<String, String> params){
public static String micropay(boolean isSandbox, Map<String, String> params){
if (isSandbox)
return WxPayApi.doPost(MICROPAY_SANDBOXNEW_RUL, params);
return WxPayApi.doPost(MICROPAY_URL, params);
}
/**
Expand Down
45 changes: 42 additions & 3 deletions src/main/java/com/jpay/weixin/api/WxPayApiConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class WxPayApiConfig implements Serializable {
private String nonceStr;
private String body;
private String attach;
private String transactionId;
private String outTradeNo;
private String totalFee;
private String spbillCreateIp;
Expand All @@ -32,6 +33,7 @@ public class WxPayApiConfig implements Serializable {
private String subOpenId;
private String authCode;


private PayModel payModel;

/**
Expand Down Expand Up @@ -99,6 +101,34 @@ public Map<String, String> build(){

return map;
}
/**
* 构建查询订单参数
* @return
*/
public Map<String, String> orderQueryBuild(){
Map<String, String> map = new HashMap<String, String>();
if (getPayModel().equals(PayModel.SERVICEMODE)) {
System.out.println("服务商上模式...");
map.put("sub_mch_id", getSubMchId());
map.put("sub_appid", getSubAppId());
}

map.put("appid", getAppId());
map.put("mch_id", getMchId());

if (StrKit.notBlank(getTransactionId())) {
map.put("transaction_id", getTransactionId());
}else {
if (StrKit.isBlank(getOutTradeNo())) {
throw new IllegalArgumentException("out_trade_no,transaction_id 不能同时为空");
}
map.put("out_trade_no", getOutTradeNo());
}
map.put("nonce_str", String.valueOf(System.currentTimeMillis()));
map.put("sign", PaymentKit.createSign(map, getPaternerKey()));
return map;
}


public String getAppId() {
if (StrKit.isBlank(appId))
Expand Down Expand Up @@ -189,13 +219,11 @@ public WxPayApiConfig setAttach(String attach) {

public String getOutTradeNo() {
if (StrKit.isBlank(outTradeNo))
outTradeNo = String.valueOf(System.currentTimeMillis());
throw new IllegalArgumentException("outTradeNo 未被赋值");
return outTradeNo;
}

public WxPayApiConfig setOutTradeNo(String outTradeNo) {
if (StrKit.isBlank(outTradeNo))
throw new IllegalArgumentException("outTradeNo 值不能为空");
this.outTradeNo = outTradeNo;
return this;
}
Expand Down Expand Up @@ -316,6 +344,17 @@ public WxPayApiConfig setAuthCode(String authCode) {
this.authCode = authCode;
return this;
}

public String getTransactionId() {
return transactionId;
}

public WxPayApiConfig setTransactionId(String transactionId) {
if (StrKit.isBlank(transactionId))
throw new IllegalArgumentException("transactionId 值不能为空");
this.transactionId = transactionId;
return this;
}


}

0 comments on commit 80d3f6d

Please sign in to comment.