微信支付服务商模式说明

微信支付分为普通商户版,服务商版以及银行服务商版,我们主讲服务商版。

官方地址:https://pay.weixin.qq.com/wiki/doc/api/sl.html

微信支付服务商模式说明_第1张图片

微信支付服务商模式

如果把服务商模式比作公司的话,普通商户就好比个人。公司可以聘请很多“个人”(员工)。也就是说,服务商可以发展很多自己的子商户。并且在费率上面服务商也是有优势的。

服务商模式比普通商户模式,从下单参数上面来看,主要多了子商户号:sub_mch_id(必填),sub_appid和sub_openId(非必填)。

这里我们选择JSAPI支付。

 

1. 支付主要会用到5个参数

appid:微信公众号(服务号)appid

appsecret:微信公众平台的 AppSecret(应用密钥),用于获取用户openId

mchId:微信商户ID

apiKey: 微信商户平台设置的 API密钥-用于签名验证

sub_mchId:子商户ID

点击查看如何获取支付参数,未更新,仅供参考

子商户从通过商户平台,进件审核通过之后,微信分配。

 

准备请求参数

ScanPayReqDataSp.java

package com.pay.wechat.protocol.pay_protocol;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import com.pay.wechat.util.Config;
import com.pay.wechat.util.RandomStringGenerator;
import com.pay.wechat.util.Signature;
import com.pay.wechat.util.xmlstream.XStreamCDATA;
import com.tenet.util.DateUtil;

/**
 * 统一下单请求数据模型-- 服务商版本
* * API接口地址说明:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1 * * 是否需要证书: 不需要 * * @author libaibai * @version 1.0 2017年11月2日 */ public class ScanPayReqDataSp { // 更多详情查看API接口地址说明 private String appid = ""; // 微信分配的公众账号ID(企业号corpid即为此appId) private String mch_id = ""; // 微信支付分配的商户号 private String sub_appid; // 否 String(32) wxd678efh567hg6999 // // 微信分配的子商户公众账号ID,如需在支付完成后获取sub_openid则此参数必传。 private String sub_mch_id;// 是 String(32) 1900000109 微信支付分配的子商户号 private String device_info = ""; // 设备号(非必填) private String nonce_str = ""; // 随机字符串,不长于32位 private String sign = ""; // 签名 private String body = ""; // 商品描述 private String detail = ""; // 商品详情(非必填) @XStreamCDATA private String attach = ""; // 附加数据(非必填) - 目前存放车牌号或卡号 private String out_trade_no = ""; // 商户订单号,调用方产生,32个字符(必填***) private String fee_type = ""; // 货币类型(非必填) private int total_fee = 0; // 总金额(单位:分,必填***) private String spbill_create_ip = ""; // 终端IP private String time_start = ""; // 交易起始时间(格式为yyyyMMddHHmmss) private String time_expire = ""; // 交易结束时间(格式为yyyyMMddHHmmss) private String goods_tag = ""; // 商品标记(非必填) // 通知地址,接收微信支付异步通知回调地址,通知url必须为直接可访问的url,不能携带参数。(以Config.java为准) private String notify_url = Config.NOTIFY_URL; private String trade_type = "JSAPI"; // 交易类型,默认:JSAPI(JSAPI,NATIVE,APP) private String product_id = ""; // 商品ID(trade_type=NATIVE,此参数必传) private String limit_pay = ""; // 指定支付方式(no_credit--指定不能使用信用卡支付) private String openid = ""; // 用户标识(trade_type=JSAPI,此参数必传) private String sub_openid = ""; // trade_type=JSAPI,此参数必传,用户在子商户appid下的唯一标识。openid和sub_openid可以选传其中之一,如果选择传sub_openid,则必须传sub_appid。 public ScanPayReqDataSp(String appid, String mch_id, String sub_appid, String sub_mch_id, String key, String body, String detail, String attach, String out_trade_no, int total_fee, String spbill_create_ip, String trade_type, String product_id, String openid, String sub_openid) { this.appid = appid; this.mch_id = mch_id; this.sub_appid = sub_appid; this.sub_mch_id = sub_mch_id; this.body = body; this.detail = detail; this.attach = attach; this.out_trade_no = out_trade_no; this.total_fee = total_fee; this.spbill_create_ip = spbill_create_ip; this.trade_type = trade_type; this.product_id = product_id; this.openid = openid; this.sub_openid = sub_openid; // 需要附属值字段,可在此处统一生成 this.nonce_str = RandomStringGenerator.getRandomStringByLength(32); // 交易时间限制 this.time_start = DateUtil.getDateTimeStr(null); // 当前时间 long time_expireLong = DateUtil.getTimeStampLong() + Config.ORDER_EXPIRE_TIME; this.time_expire = DateUtil.getDateTimeStr(DateUtil.getTimeStampToDate(time_expireLong)); // 当前时间+600秒 // sign签名 // 根据API给的签名规则进行签名 this.sign = Signature.getSign(toMap(), key); } public String getAppid() { return appid; } public void setAppid(String appid) { this.appid = appid; } public String getMch_id() { return mch_id; } public void setMch_id(String mch_id) { this.mch_id = mch_id; } public String getDevice_info() { return device_info; } public void setDevice_info(String device_info) { this.device_info = device_info; } public String getNonce_str() { return nonce_str; } public void setNonce_str(String nonce_str) { this.nonce_str = nonce_str; } public String getSign() { return sign; } public void setSign(String sign) { this.sign = sign; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail; } public String getAttach() { return attach; } public void setAttach(String attach) { this.attach = attach; } public String getOut_trade_no() { return out_trade_no; } public void setOut_trade_no(String out_trade_no) { this.out_trade_no = out_trade_no; } public String getFee_type() { return fee_type; } public void setFee_type(String fee_type) { this.fee_type = fee_type; } public int getTotal_fee() { return total_fee; } public void setTotal_fee(int total_fee) { this.total_fee = total_fee; } public String getSpbill_create_ip() { return spbill_create_ip; } public void setSpbill_create_ip(String spbill_create_ip) { this.spbill_create_ip = spbill_create_ip; } public String getTime_start() { return time_start; } public void setTime_start(String time_start) { this.time_start = time_start; } public String getTime_expire() { return time_expire; } public void setTime_expire(String time_expire) { this.time_expire = time_expire; } public String getGoods_tag() { return goods_tag; } public void setGoods_tag(String goods_tag) { this.goods_tag = goods_tag; } public String getNotify_url() { return notify_url; } public void setNotify_url(String notify_url) { this.notify_url = notify_url; } public String getTrade_type() { return trade_type; } public void setTrade_type(String trade_type) { this.trade_type = trade_type; } public String getProduct_id() { return product_id; } public void setProduct_id(String product_id) { this.product_id = product_id; } public String getLimit_pay() { return limit_pay; } public void setLimit_pay(String limit_pay) { this.limit_pay = limit_pay; } public String getOpenid() { return openid; } public void setOpenid(String openid) { this.openid = openid; } public String getSub_mch_id() { return sub_mch_id; } public void setSub_mch_id(String sub_mch_id) { this.sub_mch_id = sub_mch_id; } public String getSub_appid() { return sub_appid; } public void setSub_appid(String sub_appid) { this.sub_appid = sub_appid; } public String getSub_openid() { return sub_openid; } public void setSub_openid(String sub_openid) { this.sub_openid = sub_openid; } public Map toMap() { Map map = new HashMap(); Field[] fields = this.getClass().getDeclaredFields(); for (Field field : fields) { Object obj; try { obj = field.get(this); if (obj != null) { map.put(field.getName(), obj); } } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } } return map; } }

签名工具类

package com.pay.wechat.util;

import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
import org.xml.sax.SAXException;

import com.wsimpl.bo.wx.einvoice.HMacShaUtil;

import javax.xml.parsers.ParserConfigurationException;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

/**
 * 签名util 类
 *
 * 
 * @depc 将key修改成传入,不从Configure类获取
 * @author libaibai
 * @version 2.0 2016年2月22日
 */
public class Signature {
	private static final Logger LOG = LogManager.getLogger(Signature.class);

	/**
	 * 签名算法
	 * 
	 * @param o 要参与签名的数据对象
	 * @return 签名
	 * @throws IllegalAccessException
	 */
	public static String getSign(Object o, String key) throws IllegalAccessException {
		ArrayList list = new ArrayList();
		Class cls = o.getClass();
		Field[] fields = cls.getDeclaredFields();
		for (Field f : fields) {
			f.setAccessible(true);
			if (f.get(o) != null && f.get(o) != "") {
				list.add(f.getName() + "=" + f.get(o) + "&");
			}
		}
		int size = list.size();
		String[] arrayToSort = list.toArray(new String[size]);
		Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < size; i++) {
			sb.append(arrayToSort[i]);
		}
		String result = sb.toString();
		result += "key=" + key;
		result = MD5.MD5Encode(result).toUpperCase();
		return result;
	}

	/**
	 * 签名算法
	 * 
	 * @param map
	 * @param key
	 * @return
	 */
	public static String getSign(Map map, String key) {
		ArrayList list = new ArrayList();
		for (Map.Entry entry : map.entrySet()) {
			if (entry.getValue() != "") {
				list.add(entry.getKey() + "=" + entry.getValue() + "&");
			}
		}
		int size = list.size();
		String[] arrayToSort = list.toArray(new String[size]);
		Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < size; i++) {
			sb.append(arrayToSort[i]);
		}
		String result = sb.toString();
		result += "key=" + key;
		result = MD5.MD5Encode(result).toUpperCase();
		return result;
	}
	
	/**
	 * 签名算法( HMAC-SHA256)
	 * 
	 * @param map
	 * @param key
	 * @return
	 */
	public static String getSignSha(Map map, String key) {
		ArrayList list = new ArrayList();
		for (Map.Entry entry : map.entrySet()) {
			if (entry.getValue() != "") {
				list.add(entry.getKey() + "=" + entry.getValue() + "&");
			}
		}
		int size = list.size();
		String[] arrayToSort = list.toArray(new String[size]);
		Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < size; i++) {
			sb.append(arrayToSort[i]);
		}
		String result = sb.toString();
		result += "key=" + key;
		result = HMacShaUtil.sha256_HMAC(result, key).toUpperCase();
		return result;
	}

	/**
	 * 从API返回的XML数据里面重新计算一次签名
	 * 
	 * @param responseString API返回的XML数据
	 * @return 新鲜出炉的签名
	 * @throws ParserConfigurationException
	 * @throws IOException
	 * @throws SAXException
	 */
	public static String getSignFromResponseString(String responseString, String key)
			throws IOException, SAXException, ParserConfigurationException {
		Map map = XMLParser.getMapFromXML(responseString);
		// 清掉返回数据对象里面的Sign数据(不能把这个数据也加进去进行签名),然后用签名算法进行签名
		map.put("sign", "");
		// 将API返回的数据根据用签名算法进行计算新的签名,用来跟API返回的签名进行比较
		return Signature.getSign(map, key);
	}

	/**
	 * 检验API返回的数据里面的签名是否合法,避免数据在传输的过程中被第三方篡改
	 * 
	 * @param responseString API返回的XML数据字符串
	 * @return API签名是否合法
	 * @throws ParserConfigurationException
	 * @throws IOException
	 * @throws SAXException
	 */
	public static boolean checkIsSignValidFromResponseString(String responseString, String key)
			throws ParserConfigurationException, IOException, SAXException {

		Map map = XMLParser.getMapFromXML(responseString);

		String signFromAPIResponse = map.get("sign").toString();
		if (signFromAPIResponse == "" || signFromAPIResponse == null) {
			LOG.error("API返回的数据签名数据不存在,有可能被第三方篡改!!!");
			return false;
		}
		// 清掉返回数据对象里面的Sign数据(不能把这个数据也加进去进行签名),然后用签名算法进行签名
		map.put("sign", "");
		// 将API返回的数据根据用签名算法进行计算新的签名,用来跟API返回的签名进行比较
		String signForAPIResponse = Signature.getSign(map, key);

		if (!signForAPIResponse.equals(signFromAPIResponse)) {
			// 签名验不过,表示这个API返回的数据有可能已经被篡改了
			LOG.error("API返回的数据签名数据不存在,有可能被第三方篡改!!!");
			return false;
		}
		return true;
	}

}

支付请求类

ScanPayService .java

package com.pay.wechat.service;

import org.springframework.stereotype.Component;

import com.pay.wechat.protocol.pay_protocol.ScanPayReqDataSp;
import com.pay.wechat.util.Config;

/**
 * 支付请求
 *
 * @author libaibai
 * @version 1.0 2015年8月31日
 */
@Component
public class ScanPayService extends BaseService {

	/**
	 * 请求支付服务-- 服务商版
	 * 
	 * @param scanPayReqData 这个数据对象里面包含了API要求提交的各种数据字段
	 * @return API返回的数据
	 * @throws Exception
	 */
	public String requestSp(ScanPayReqDataSp scanPayReqDataSp) throws Exception {

		super.apiURL = Config.UNIFIEDORDER_API;
		// --------------------------------------------------------------------
		// 发送HTTPS的Post请求到API地址
		// --------------------------------------------------------------------
		String responseString = sendPost(scanPayReqDataSp);

		return responseString;
	}



}

BaseService.java

package com.pay.wechat.service;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;

import javax.annotation.Resource;

import com.dlys.pay.wechat.util.HttpsRequest;

/**
 * 
 * @author libaibai
 *
 */
public class BaseService {

	// API的地址
	public String apiURL;

	// 发请求的HTTPS请求器

	@Resource
	private HttpsRequest httpsRequest;

	protected String sendPost(Object xmlObj) throws UnrecoverableKeyException, IOException,
			NoSuchAlgorithmException, KeyStoreException, KeyManagementException,
			ClassNotFoundException, InstantiationException, IllegalAccessException {
		return httpsRequest.sendPost(apiURL, xmlObj);
	}

}

HttpsRequest.java

package com.pay.wechat.util;

import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;

import javax.net.ssl.SSLContext;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import com.pay.wechat.protocol.pay_protocol.ScanPayReqDataSp;
import com.pay.wechat.util.xmlstream.XStreamFactory;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;

/**
 * HTTP 请求类
 *
 * @author libaibai
 * @version 1.0 2015年8月31日
 */
@Component
public class HttpsRequest {
	public interface ResultListener {
		public void onConnectionPoolTimeoutError();
	}

	private static Logger LOG = LogManager.getLogger(HttpsRequest.class);

	private boolean hasInit = false;

	// 连接超时时间,默认10秒
	private int socketTimeout = 10000;

	// 传输超时时间,默认30秒
	private int connectTimeout = 30000;

	// 请求器的配置
	private RequestConfig requestConfig;

	// HTTP请求器
	private CloseableHttpClient httpClient;

	/**
	 * 证书初始化
	 * 
	 * @throws UnrecoverableKeyException
	 * @throws KeyManagementException
	 * @throws NoSuchAlgorithmException
	 * @throws KeyStoreException
	 * @throws IOException
	 */
	public HttpsRequest() throws UnrecoverableKeyException, KeyManagementException, NoSuchAlgorithmException, KeyStoreException, IOException {
		init();
	}

	private void init() throws IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException {

		KeyStore keyStore = KeyStore.getInstance("PKCS12");
		InputStream instream = HttpsRequest.class.getClassLoader().getResourceAsStream(Configure.CERTLOCAL_PATHx);// 加载本地的证书进行https加密传输
		try {
			keyStore.load(instream, Configure.CERTPASSWORD.toCharArray());// 设置证书密码
		} catch (Exception e) {
			LOG.error("加载证书异常", e);
		} finally {
			instream.close();
		}
		SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, Configure.CERTPASSWORD.toCharArray()).build();
		SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null,
				SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);

		httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

		// 根据默认超时限制初始化requestConfig
		requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();

		hasInit = true;
	}

	/**
	 * 通过Https往API GET
	 *
	 * @param url API地址
	 * @return API回包的实际数据
	 * @throws IOException
	 * @throws KeyStoreException
	 * @throws UnrecoverableKeyException
	 * @throws NoSuchAlgorithmException
	 * @throws KeyManagementException
	 */

	public String sendGET(String url)
			throws IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException {

		if (!hasInit) {
			init();
		}

		String result = null;

		HttpGet httpGet = new HttpGet(url);

		// 得指明使用UTF-8编码,否则到API服务器XML的中文不能被成功识别
		httpGet.addHeader("Content-Type", "text/xml");

		// 设置请求器的配置
		httpGet.setConfig(requestConfig);

		try {
			HttpResponse response = httpClient.execute(httpGet);

			HttpEntity entity = response.getEntity();

			result = EntityUtils.toString(entity, "UTF-8");

		} catch (Exception e) {
			LOG.error("HTTP Get请示异常", e);
		} finally {
			httpGet.abort();
		}
		return result;
	}

	/**
	 * 通过Https往API post
	 *
	 * @param url API地址
	 * @param Object 要提交的Object数据对象
	 * @return API回包的实际数据
	 * @throws IOException
	 * @throws KeyStoreException
	 * @throws UnrecoverableKeyException
	 * @throws NoSuchAlgorithmException
	 * @throws KeyManagementException
	 */

	public String sendPostObject(String url, HttpEntity postEntity)
			throws IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException {

		if (!hasInit) {
			init();
		}

		String result = null;
		HttpPost httpPost = new HttpPost(url);
		httpPost.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.MULTIPART_FORM_DATA.getMimeType());
		httpPost.setEntity(postEntity);

		// 设置请求器的配置
		httpPost.setConfig(requestConfig);
		try {
			HttpResponse response = httpClient.execute(httpPost);
			HttpEntity entity = response.getEntity();

			result = EntityUtils.toString(entity, "UTF-8");

		} catch (Exception e) {
			LOG.error("HTTP POST 请求异常", e);
		} finally {
			httpPost.abort();
		}

		return result;
	}

	/**
	 * 通过Https往API post xml数据
	 *
	 * @param url API地址
	 * @param xmlObj 要提交的XML数据对象
	 * @return API回包的实际数据
	 * @throws IOException
	 * @throws KeyStoreException
	 * @throws UnrecoverableKeyException
	 * @throws NoSuchAlgorithmException
	 * @throws KeyManagementException
	 */

	public String sendPost(String url, Object xmlObj)
			throws IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException {

		if (!hasInit) {
			init();
		}

		String result = null;
		HttpPost httpPost = new HttpPost(url);

		// 解决XStream对出现双下划线的bug
		// XStream xStreamForRequestPostData = new XStream(new DomDriver("UTF-8",
		// new XmlFriendlyNameCoder("-_", "_")));

		XStream xStream = XStreamFactory.getXStream(new XmlFriendlyNameCoder("_-", "_"));

		// 将要提交给API的数据对象转换成XML格式数据Post给API
		String postDataXML = xStream.toXML(xmlObj);
		LOG.info("请求微信接口->url=" + url + ",data=" + postDataXML);
		// LOG.info("data="+StringEscapeUtils.unescapeXml(postDataXML));// 转义字符

		// 得指明使用UTF-8编码,否则到API服务器XML的中文不能被成功识别
		StringEntity postEntity = new StringEntity(postDataXML, "UTF-8");
		httpPost.addHeader("Content-Type", "text/xml");
		httpPost.setEntity(postEntity);

		// 设置请求器的配置
		httpPost.setConfig(requestConfig);

		try {
			HttpResponse response = httpClient.execute(httpPost);

			HttpEntity entity = response.getEntity();

			result = EntityUtils.toString(entity, "UTF-8");

		} catch (Exception e) {
			LOG.error("HTTP POST 请求异常", e);

		} finally {
			// httpPost.abort();
			httpPost.releaseConnection();
		}

		return result;
	}

	/**
	 * 设置连接超时时间
	 *
	 * @param socketTimeout 连接时长,默认10秒
	 */
	public void setSocketTimeout(int socketTimeout) {
		this.socketTimeout = socketTimeout;
		resetRequestConfig();
	}

	/**
	 * 设置传输超时时间
	 *
	 * @param connectTimeout 传输时长,默认30秒
	 */
	public void setConnectTimeout(int connectTimeout) {
		this.connectTimeout = connectTimeout;
		resetRequestConfig();
	}

	private void resetRequestConfig() {
		requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
	}

	/**
	 * 允许商户自己做更高级更复杂的请求器配置
	 *
	 * @param requestConfig 设置HttpsRequest的请求器配置
	 */
	public void setRequestConfig(RequestConfig requestConfig) {
		this.requestConfig = requestConfig;
	}

}

UnifiedOrderSp .java

package com.dlys.pay.wechat;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dlys.pay.wechat.protocol.pay_protocol.ScanPayReqDataSp;
import com.dlys.pay.wechat.service.ScanPayService;
import com.dlys.pay.wechat.util.Util;
import com.dlys.pay.wechat.util.XMLParser;

/**
 * 微信支付统一下单公用入口--服务商版本
 * 
 * @author libaibai
 * @version 1.0 2017年11月2日
 */
@Component
public class UnifiedOrderSp {

	private static final Logger LOG = LogManager.getLogger(UnifiedOrderSp.class);

	@Resource
	private ScanPayService scanPayService; // wechat sdk

	/**
	 * 微信支付统一下单(JSAPI)
	 */
	public Map unifiedOrderJSAPI(String appid, String mch_id,String sub_appid, String sub_mch_id, String key,
			String body, String detail, String attach, String out_trade_no, int total_fee, String spbill_create_ip,
			String openid, String sub_openid) {

		return this.unifiedOrder(appid, mch_id, sub_appid, sub_mch_id, key, body, detail, attach, out_trade_no, total_fee,
				spbill_create_ip, "JSAPI", Util.getProduct_id(), openid, sub_openid);
	}

	
	/**
	 * 微信支付统一下单(JSAPI/NATIVE/APP方法均可)
	 * 
	 * @param appid 公众号的唯一标识
	 * @param mch_id 微信支付分配的商户号
	 * @param key 微信应用秘钥
	 * @param body 商品名称
	 * @param detail 商品详情(非必填)
	 * @param attach 附加数据(非必填) - 目前存放车牌号或卡号
	 * @param out_trade_no 商户订单号
	 * @param total_fee 支付金额(单位:分)
	 * @param spbill_create_ip 终端IP
	 * @param trade_type 交易类型(JSAPI/NATIVE/APP)
	 * @param product_id 商品ID(trade_type=NATIVE时,必填)
	 * @param openid 用户标识(trade_type=JSAPI时,必填)
	 * @return 响应信息
	 */
	private Map unifiedOrder(String appid, String mch_id, String sub_appid, String sub_mch_id,
			String key, String body, String detail, String attach, String out_trade_no, int total_fee,
			String spbill_create_ip, String trade_type, String product_id, String openid, String sub_openid) {

		ScanPayReqDataSp data = new ScanPayReqDataSp(appid, mch_id, sub_appid, sub_mch_id, key, body, detail, attach,
				out_trade_no, total_fee, spbill_create_ip, trade_type, product_id, openid, sub_openid);
		// LOG.info("微信支付统一下单参数:" + JSONObject.fromObject(data).toString());
		try {
			String xmlMsg = scanPayService.requestSp(data);
//			LOG.info("微信支付统一下单返回数据:" + XMLParser.getMapFromXML(xmlMsg));
			return XMLParser.getMapFromXML(xmlMsg);

		} catch (Exception e) {
			LOG.error("微信支付统一下单时出错!", e);
			return null;
		}
	}
}

以上是支付的工具类,基本上只要是微信支付API,都是通用的

 

以下是业务代码,大家自行修改

GetPayOrder.java

package com.pay.wechat.bo;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.fastquery.service.FQuery;
import org.springframework.stereotype.Component;

import com.db.ParkingCarInfoDBService;
import com.db.PayOrderDBService;
import com.db.PunitDBService;
import com.db.PunitParamDBService;
import com.pay.bo.ParkingPayBo;
import com.pay.bo.PayOrderBo;
import com.pay.bo.PayOrderSpBo;
import com.pay.wechat.UnifiedOrder;
import com.pay.wechat.UnifiedOrderSp;
import com.pay.wechat.WxPayBo;
import com.pay.wechat.protocol.JsapiRequestData;
import com.wsimpl.bo.operate.InvoiceBusinessBo;
import com.wsimpl.bo.wx.WxCardBo;
import com.bean.ParkingCarInfo;
import com.bean.PayOrder;
import com.bean.Punit;
import com.bean.WxCard;
import com.util.ChargeMoneyUtil;
import com.util.Config;
import com.util.Const;
import com.util.bean.ChargeMoneyBean;
import com.util.DateUtil;
import com.util.lang.StringUtil;

/**
 * 生成支付订单,微信统一下单,并返回调取JSAPI所需参数
 * 
 * @author libaibai
 * @version 1.0 2016年3月4日
 */
@Component
public class GetPayOrder {

	private static final Logger LOG = LogManager.getLogger(GetPayOrder.class);

	/* db */
	private PunitDBService punitDBService = FQuery.getRepository(PunitDBService.class); // 物业单位
	private PunitParamDBService punitParamDBService = FQuery
			.getRepository(PunitParamDBService.class); // 物业单位
	private PayOrderDBService payOrderDBService = FQuery.getRepository(PayOrderDBService.class); // 物业单位

	private ParkingCarInfoDBService parkingCarInfoDBService = FQuery
			.getRepository(ParkingCarInfoDBService.class); // 在场车辆信息

	/* 微信支付相关 */
	@Resource
	private PayOrderBo payOrderBo; // 支付总订单

	@Resource
	private PayOrderSpBo payOrderSpBo; // 支付总订单-- 服务商

	@Resource
	private WxPayBo weChatBo; // 微信支付共用类

	@Resource
	private UnifiedOrder unifiedOrder; // 微信支付统一下单

	@Resource
	private UnifiedOrderSp unifiedOrderSp; // 微信支付统一下单 -- 服务商

	@Resource
	private ParkingPayBo parkingPayBo;

	@Resource
	private ChargeMoneyUtil chargeMoneyUtil;
	
	@Resource
	private InvoiceBusinessBo invoiceBusinessBo;

	/**
	 * 执行方法
	 * 
	 * @param code
	 * @param punitId
	 * @param iden
	 * @param chargeMoney
	 * @return
	 */
	public Map get(String code, Long punitId, String iden, Double chargeMoney,
			Long channelId, Long ruid, Long outTime, String cardId, String encryptCode) {
		LOG.info("weixin_下单请求,punitId=" + punitId + ",iden=" + iden + ",chargeMoney=" + chargeMoney + 
				",channelId=" + channelId + ",ruid=" + ruid + ",outTime=" + outTime);
		
		Map returnMap = new HashMap(); // 返回Map

		// 参数不完整
		if (punitId == null || punitId.longValue() <= 0 || code == null || "".equals(code.trim())) {
			LOG.error("信息不完整,请返回重新进入,punitId=" + punitId + ",code=" + code);
			returnMap.put("isPay", "0");
			returnMap.put("errorMsg", "信息不完整,请返回重新进入");
			return returnMap;
		}

		// 获取停车场微信支付所需参数appid,mch_id,appSecret,key -- begin
		Punit punit = punitDBService.findById(punitId);
		if (punit == null) {
			LOG.error("信息不完整,请返回重新进入,未找到停车场信息,punitId=" + punitId);
			returnMap.put("isPay", "0");
			returnMap.put("errorMsg", "信息不完整,请返回重新进入");
			return returnMap;
		}

		// 停车场对应公众号基本参数(云平台填入),如果是泊链支付,则直接使用德立云停车参数
		String appid = punit.getAppId();
		String mch_id = punit.getMchId();
		String appSecret = punit.getAppSecret();
		String key = punit.getWx_key();
		String sub_mchId = punit.getSub_mchId();

		// 微信支付所需参数不完整
		if (StringUtils.isEmpty(mch_id) && StringUtils.isEmpty(sub_mchId)) {
			LOG.error("信息不完整,请返回重新进入,必须参数不完整");
			returnMap.put("isPay", "0");
			returnMap.put("errorMsg", "信息不完整,请返回重新进入");
			return returnMap;
		}
		// 获取停车场微信支付所需参数appid,mch_id,appSecret,key -- end

		// 判断是否启用服务商
		Map map = punitParamDBService.findParam(punitId);
		int isSpPay = 0;
		if (map != null && !map.isEmpty()) {
			isSpPay = Const.getInteger(map.get("isSpPay"));
		}

		// 使用德立云停车为服务商
		String subappid = null;
		String openid = null;
		String sub_openid = null;
		if (isSpPay == 1) {
			appid = Config.APPID;
			mch_id = Config.MCHIDSP;
			appSecret = Config.APPSECRET;
			key = Config.APIKEY;
			openid = weChatBo.getOauth2Openid(appid, appSecret, code);
		} else if (isSpPay == 2) {
			subappid = Config.APPID;
			sub_openid = weChatBo.getOauth2Openid(subappid, Config.APPSECRET, code);
		} else {
			openid = weChatBo.getOauth2Openid(appid, appSecret, code);
		}
		if (StringUtils.isEmpty(openid) && StringUtils.isEmpty(sub_openid)) {
			LOG.error("信息不完整,请返回重新进入,openId为空, appid=" + appid + ",appSecret=" + appSecret + ",isSpPay=" + isSpPay + ",code=" + code);
			returnMap.put("isPay", "0");
			returnMap.put("errorMsg", "信息不完整,请返回重新进入");
			return returnMap;
		}

		// db查询-用户停车信息
		ParkingCarInfo pcInfo = null;
		try {
			pcInfo = parkingCarInfoDBService.findByIden(iden);
		} catch (Exception e) {
			pcInfo = null;
			LOG.error("根据iden查询停车信息时出错!", e);
		}
		// 无停车信息
		if (pcInfo == null) {
			LOG.error("信息不完整,请返回重新进入,未找到在场车辆信息,iden=" + iden);
			returnMap.put("isPay", "0");
			returnMap.put("errorMsg", "信息不完整,请返回重新进入");
			return returnMap;
		}

		// 判断是否二次缴费,0元不需要缴费
		byte isEmptyPay = Const.getByte(map.get("isEmptyPay")); // 支持0元支付
		if (pcInfo.getPayTime() != null && pcInfo.getPayTime() > 0 || isEmptyPay == 0) {
			if (chargeMoney == null || chargeMoney.doubleValue() <= 0) {
				returnMap.put("isPay", "0");
				returnMap.put("errorMsg", "0元不需缴费");
				return returnMap;
			}
		}
		
		// 重新计算缴费金额
		ChargeMoneyBean chargeMoneyBean = null;
		long orderDate = 0;
		// 如果存在出场时间,而且在5分钟之内(考虑到存在时间差)
		if (outTime != null && outTime > 0 && (Math.abs(DateUtil.getTimeStampLong() - outTime.longValue()) <= 5 * 60)) {
			chargeMoneyBean = chargeMoneyUtil.getMoney(pcInfo, punit, outTime);
			orderDate = outTime;
		} else {
			chargeMoneyBean = chargeMoneyUtil.getMoney(pcInfo, punit);
			orderDate = DateUtil.getTimeStampLong();
		}
		double chargeMoneydb = StringUtil.getDoubleDecimal(Const.getDouble(chargeMoneyBean.getRealMoney()), 2);
		if (chargeMoney.doubleValue() != chargeMoneydb) {
			LOG.warn("weixin_下单失败,punitId=" + punitId + ", plateNum=" + pcInfo.getPlate() + ",页面金额chargeMoney=" + chargeMoney
					+ ",重新计算金额chargeMoneydb=" + chargeMoneydb);
			returnMap.put("isPay", "0");
			returnMap.put("errorMsg", "下单失败,请返回重新进入");
			return returnMap;
		}
		double saleMoney = chargeMoneyBean.getSaleMoney();
		Double shouldPay = chargeMoney;// 实付金额(可能会有优惠卷)
		LOG.info("GetPayOrder-微信支付开始保存订单,punitId=" +punitId + ",plateNum=" +  pcInfo.getPlate() + ",iden=" + iden );
		// 保存支付总订单表- (byte) 1
		PayOrder payOrder = null;
		if (isSpPay == 0) {
			byte payway = 0; // 支付途径
			payOrder = payOrderBo.savePayOrderToParkingPay(payway, (byte) 1, iden, punit.getTenantId(), punitId, ruid, pcInfo.getPlate(),
					pcInfo.getCardNo(), pcInfo.getEntranceTime(), chargeMoney, appid, mch_id, openid, saleMoney, channelId, 0L, orderDate);
		} else {
			payOrder = payOrderSpBo.savePayOrderToParkingPay((byte) 1, iden, punit.getTenantId(), punitId, ruid, pcInfo.getPlate(),
					pcInfo.getCardNo(), chargeMoney, saleMoney, appid, mch_id, openid, sub_mchId, pcInfo.getEntranceTime(), channelId, 0L, (byte) 0, orderDate);
		}
		if (payOrder == null) {
			LOG.error("weixin_保存订单失败");
			returnMap.put("isPay", "0");
			returnMap.put("errorMsg", "下单失败,请返回重新进入");
			return returnMap;
		}
		
		// 0元支付
		if (chargeMoney.doubleValue() == 0) {
			LOG.info("weixin-0元支付成功,punitId=" + punitId + ",plateNum=" + pcInfo.getPlate());
			// 修改订单信息
			PayOrder temp = FQuery.reset(PayOrder.class);
			temp.setId(payOrder.getId());
			temp.setPayState((byte) 1);
			temp.setPayTime(DateUtil.getTimeStampLong());
			payOrder = payOrderDBService.update(temp);
			
			// 处理在场车辆信息
			parkingPayBo.handle(payOrder);
			returnMap.put("isPay", "2");
			returnMap.put("errorMsg", "支付成功");
			return returnMap;
		}

		// 微信统一下单,调用公用入口
		String body = punit.getUnitName() + "-停车费("
				+ (pcInfo.getPlate() == null ? "" : pcInfo.getPlate()) + ")";
		String detail = body; // 商品详情
		String attach = pcInfo.getPlate(); // 车牌号
		if (attach == null || "".equals(attach)) {
			attach = pcInfo.getCardNo();
		}
		// 判断是否开通微信无感支付
		if (Const.getByte(punit.getIsWxFreePay()) == 1) {
			attach = "#*#{\"pn\":\""+pcInfo.getPlate()+"\",\"aid\":\""+appid+"\", \"vm\":\"true\"}#*#"; 
			LOG.info("wx-判断是否开通无感支付,attach=" + attach + ",plateNum=" + pcInfo.getPlate());
		}

		String out_trade_no = payOrder.getPayOrderId();
		Double payMoney = shouldPay * 100;
		int total_fee = payMoney.intValue();
		String spbill_create_ip = "127.0.0.1";
		
		//开发票参数
		String receipt = null;
		if(isSpPay==0){
			boolean openWxFaPiaoByAfterPay = invoiceBusinessBo.isOpenWxFaPiaoByAfterPay(punitId, "LTC");
			if(openWxFaPiaoByAfterPay){
				receipt = "Y";
			}
		}

		Map unifiedOrderMap = null;
		if (isSpPay == 0) {
			unifiedOrderMap = unifiedOrder.unifiedOrderJSAPI(appid, mch_id, key, body, detail,
					attach, out_trade_no, total_fee, spbill_create_ip, openid,receipt);
		} else {
			unifiedOrderMap = unifiedOrderSp.unifiedOrderJSAPI(appid, mch_id, subappid, sub_mchId, key, body,
					detail, attach, out_trade_no, total_fee, spbill_create_ip, openid, sub_openid);
		}
		if (unifiedOrderMap == null) {
			returnMap.put("isPay", "0");
			returnMap.put("errorMsg", "下单失败,请返回重新进入");
			return returnMap;
		}

		// 下单成功,组装JSAPI调用所需参数
		try {
			if ("SUCCESS".equals(unifiedOrderMap.get("result_code"))) {

				// 组装JSAPI调用所需参数
				String timeStamp = DateUtil.getTimeStamp();
				String nonceStr = Const.getStr(unifiedOrderMap.get("nonce_str"));

				String package1 = "prepay_id=" + Const.getStr(unifiedOrderMap.get("prepay_id"));

				JsapiRequestData jsapiRequestData = new JsapiRequestData(appid, key, timeStamp,
						nonceStr, package1);
				String paySign = jsapiRequestData.getPaySign();

				// 组装页面响应Map
				returnMap.put("isPay", "1");
				returnMap.put("appid", appid);
				returnMap.put("timeStamp", timeStamp);
				returnMap.put("nonceStr", Const.getStr(unifiedOrderMap.get("nonce_str")));
				returnMap.put("package1", package1);
				returnMap.put("signType", "MD5");
				returnMap.put("paySign", paySign);
				returnMap.put("payOrderId", payOrder.getPayOrderId());
			}
		} catch (Exception e) {
			LOG.error("下单成功,组装JSAPI调用所需参数时出错!", e);
		}

		return returnMap;
	}

	/**
	 * 乘 2位小数 四舍五入
	 * 
	 * @param value1
	 * @param value2
	 * @return
	 */
	private Double mul(Double value1, Double value2) {
		BigDecimal b1 = new BigDecimal(Double.toString(value1));
		BigDecimal b2 = new BigDecimal(Double.toString(value2));
		BigDecimal multiply = b1.multiply(b2);
		return multiply.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
	}

	/**
	 * 减 2位小数 四舍五入
	 * 
	 * @param value1
	 * @param value2
	 * @return
	 */
	public Double sub(Double value1, Double value2) {
		BigDecimal b1 = new BigDecimal(Double.toString(value1));
		BigDecimal b2 = new BigDecimal(Double.toString(value2));
		BigDecimal sub = b1.subtract(b2);
		return sub.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
	}
}

后台下单成功之后,我们在jsp页面就可以调起微信支付了

// 微信公众号支付
function weixinpay (wxCode, punitId, iden, chargeMoney, channelId) {
	var encryptCode=$("#encryptCode").val();
	var cardId=$("#cardId").val();
	var outTime=$("#outTime").val();
	
	// 统一下单返回JS调取支付所需参数
	$.tenetAjax({
		url: PATH + "/punitWS/getPayOrder",
		data: {'code':wxCode,'punitId':punitId,'iden':iden,'chargeMoney':chargeMoney,'channelId':channelId, 'outTime':outTime, 'cardId':cardId,'encryptCode':encryptCode},//卡券传值
		async: false,
		success: function(data){
			//alert(JSON.stringify(data));
			if(data.payOrderMap==null){
				$("#iformbtndID").html('支付失败,请返回重新进入');
				return;
			}
			
			var isPay = data.payOrderMap.isPay;
			var errorMsg = data.payOrderMap.errorMsg;
			if(isPay==null || isPay==0){
				alert(errorMsg);
				$("#iformbtndID").html(''+errorMsg+'');
				return;
			} else if (isPay==2) {
				$("#iformbtndID").html('

支付成功

'); return; } // JS调取微信支付 var appid = data.payOrderMap.appid; var timeStamp = data.payOrderMap.timeStamp; var nonceStr = data.payOrderMap.nonceStr; var package1 = data.payOrderMap.package1; var signType = data.payOrderMap.signType; var paySign = data.payOrderMap.paySign; var payOrderId = data.payOrderMap.payOrderId; if (typeof window.WeixinJSBridge == "undefined"){ $(document).on('WeixinJSBridgeReady',function(){ onBridgeReady(appid, timeStamp, nonceStr, package1, signType, paySign, payOrderId); }) } else { onBridgeReady(appid, timeStamp, nonceStr, package1, signType, paySign, payOrderId); } } }); } // JS调取微信支付 function onBridgeReady(appId, timeStamp, nonceStr, package1, signType, paySign, payOrderId) { WeixinJSBridge.invoke('getBrandWCPayRequest', { "appId" : appId, //公众号名称,由商户传入 "timeStamp" : timeStamp, //时间戳,自1970年以来的秒数 "nonceStr" : nonceStr, //随机串 "package" : package1, //订单详情扩展字符串 "signType" : signType, //微信签名方式MD5 "paySign" : paySign //微信签名 }, function(res) { // alert(res.err_msg); if (res.err_msg == null) { $("#iformbtndID").html('支付失败,请返回重新进入'); return; } // 支付成功 if (res.err_msg == "get_brand_wcpay_request:ok") { var punitId = $("#punitId").val(); var stayTime = $("#outDelayID").text(); var unitName= $("#unitNameID").text(); var chargeMoney = $("#chargeMoney").val(); window.location.href="paysuc?punitId="+punitId+"&payOrderId="+payOrderId+"&stayTime="+stayTime+"&unitName="+unitName+"&payMoney="+chargeMoney; return; } else { $("#iformbtndID").html('支付失败,请返回重新进入'); return; } }); }

 

ok,微信支付就到此结束了

你可能感兴趣的:(微信支付服务商模式说明)