package com.duke.learn.weixinpay.util;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.duke.learn.weixinpay.util.WxPayApi.TradeType;
public class WxPayApiConfig implements Serializable {
private static final long serialVersionUID = -6447075676732210047L;
private String appId;
private String mchId;
private String subAppId;
private String subMchId;
private String paternerKey;
private String nonceStr;
private String body;
private String attach;
private String transactionId;
private String outTradeNo;
private String totalFee;
private String spbillCreateIp;
private String notifyUrl;
private TradeType tradeType;
private String openId;
private String subOpenId;
private String authCode;
private String sceneInfo;
private String planId;
private String contractCode;
private String requestSerial;
private String contractDisplayAccount;
private String version;
private String timestamp;
private String returnApp;
private String returnWeb;
private String contractNotifyUrl;
private String contractId;
private PayModel payModel;
/**
* 分别对应商户模式、服务商模式
*/
public static enum PayModel {
BUSINESSMODEL, SERVICEMODE
}
private WxPayApiConfig() {
}
public static WxPayApiConfig New() {
return new WxPayApiConfig();
}
/**
* 构建请求参数
*
* @return Map<String, String>
*/
public Map<String, String> build() {
Map<String, String> map = new HashMap<String, String>();
if (getPayModel().equals(PayModel.SERVICEMODE)) {
//服务商上模式
map.put("sub_mch_id", getSubMchId());
if (StrKit.notBlank(getSubAppId())) {
map.put("sub_appid", subAppId);
}
}
/**
* openId和sub_openid可以选传其中之一,如果选择传sub_openid,则必须传sub_appid
*/
if (getTradeType().equals(TradeType.JSAPI)) {
//公众号支付
if (StrKit.notBlank(getSubAppId())) {
map.put("sub_appid", subAppId);
map.put("sub_openid", getSubOpenId());
} else {
map.put("openid", getOpenId());
}
}
/**
* H5支付必填scene_info
*/
if (getTradeType().equals(TradeType.MWEB)) {
if (StrKit.isBlank(getSceneInfo())) {
throw new IllegalArgumentException("微信H5支付 scene_info 不能同时为空");
}
map.put("scene_info", getSceneInfo());
}
map.put("appid", getAppId());
map.put("mch_id", getMchId());
map.put("nonce_str", getNonceStr());
map.put("body", getBody());
map.put("out_trade_no", getOutTradeNo());
map.put("total_fee", getTotalFee());
map.put("spbill_create_ip", getSpbillCreateIp());
map.put("trade_type", getTradeType().name());
map.put("attach", getAttach());
if (getTradeType().equals(TradeType.MICROPAY)) {
map.put("auth_code", getAuthCode());
map.remove("trade_type");
} else {
map.put("notify_url", getNotifyUrl());
}
map.put("sign", PaymentKit.createSign(map, getPaternerKey()));
return map;
}
/**
* 构建查询订单参数
*
* @return <Map<String, String>>
*/
public Map<String, String> orderQueryBuild() {
Map<String, String> map = new HashMap<String, String>();
if (getPayModel().equals(PayModel.SERVICEMODE)) {
//服务商上模式
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;
}
/**
* 构建申请签约Map
*
* @return 申请签约Map
* @throws UnsupportedEncodingException
*/
public Map<String, String> entrustwebBuild() throws UnsupportedEncodingException {
Map<String, String> map = new HashMap<String, String>();
map.put("appid", getAppId());
map.put("mch_id", getMchId());
map.put("plan_id", getPlanId());
map.put("contract_code", getContractCode());
map.put("request_serial", getRequestSerial());
map.put("contract_display_account", getContractDisplayAccount());
map.put("notify_url", getNotifyUrl());
map.put("version", getVersion());
map.put("timestamp", getTimestamp());
map.put("sign", PaymentKit.createSign(map, getPaternerKey()));
for (Entry<String, String> param : map.entrySet()) {
String key = param.getKey();
String value = param.getValue();
value = PaymentKit.urlEncode(value);
map.put(key, value);
}
return map;
}
/**
* 构建支付中签约Map
*
* @return 支付中签约Map
*/
public Map<String, String> contractorderBuild() {
Map<String, String> map = new HashMap<String, String>();
map.put("appid", getAppId());
map.put("mch_id", getMchId());
map.put("contract_appid", getAppId());
map.put("contract_mchid", getMchId());
map.put("out_trade_no", getOutTradeNo());
map.put("nonce_str", getNonceStr());
map.put("body", getBody());
map.put("attach", getAttach());
map.put("notify_url", getNotifyUrl());
map.put("total_fee", getTotalFee());
map.put("spbill_create_ip", getSpbillCreateIp());
map.put("trade_type", getTradeType().name());
if (getTradeType().equals(TradeType.JSAPI)) {
map.put("openid", getOpenId());
}
map.put("plan_id", getPlanId());
map.put("contract_code", getContractCode());
map.put("request_serial", getRequestSerial());
map.put("contract_display_account", getContractDisplayAccount());
map.put("contract_notify_url", getContractNotifyUrl());
map.put("sign", PaymentKit.createSign(map, getPaternerKey()));
return map;
}
/**
* 构建查询签约关系的Map
*
* @return 查询签约关系的Map
*/
public Map<String, String> querycontractBuild() {
Map<String, String> map = new HashMap<String, String>();
map.put("appid", getAppId());
map.put("mch_id", getMchId());
if (StrKit.notBlank(getPlanId())) {
map.put("plan_id", getPlanId());
map.put("contract_code", getContractCode());
} else {
map.put("contract_id", getContractId());
}
map.put("version", getVersion());
map.put("sign", PaymentKit.createSign(map, getPaternerKey()));
return map;
}
/**
* 构建申请扣款的Map
*
* @return 申请扣款的Map
*/
public Map<String, String> pappayapplyBuild() {
Map<String, String> map = new HashMap<String, String>();
map.put("appid", getAppId());
map.put("mch_id", getMchId());
map.put("nonce_str", getNonceStr());
map.put("body", getBody());
map.put("attach", getAttach());
map.put("out_trade_no", getOutTradeNo());
map.put("total_fee", getTotalFee());
map.put("spbill_create_ip", getSpbillCreateIp());
map.put("notify_url", getNotifyUrl());
map.put("trade_type", getTradeType().name());
map.put("contract_id", getContractId());
map.put("sign", PaymentKit.createSign(map, getPaternerKey()));
return map;
}
public String getAppId() {
if (StrKit.isBlank(appId))
throw new IllegalArgumentException("appId 未被赋值");
return appId;
}
public WxPayApiConfig setAppId(String appId) {
if (StrKit.isBlank(appId))
throw new IllegalArgumentException("appId 值不能为空");
this.appId = appId;
return this;
}
public String getMchId() {
if (StrKit.isBlank(mchId))
throw new IllegalArgumentException("mchId 未被赋值");
return mchId;
}
public WxPayApiConfig setMchId(String mchId) {
if (StrKit.isBlank(mchId))
throw new IllegalArgumentException("mchId 值不能为空");
this.mchId = mchId;
return this;
}
public String getSubAppId() {
return subAppId;
}
public WxPayApiConfig setSubAppId(String subAppId) {
if (StrKit.isBlank(subAppId))
throw new IllegalArgumentException("subAppId 值不能为空");
this.subAppId =