(demo為native支付)
流程:
業(yè)務(wù)流程說明:
(1)商戶后臺系統(tǒng)根據(jù)用戶選購的商品生成訂單。
(2)用戶確認(rèn)支付后調(diào)用微信支付【統(tǒng)一下單API】生成預(yù)支付交易;
(3)微信支付系統(tǒng)收到請求后生成預(yù)支付交易單,并返回交易會話的二維碼鏈接code_url。
(4)商戶后臺系統(tǒng)根據(jù)返回的code_url生成二維碼。
(5)用戶打開微信“掃一掃”掃描二維碼,微信客戶端將掃碼內(nèi)容發(fā)送到微信支付系統(tǒng)。
(6)微信支付系統(tǒng)收到客戶端請求,驗(yàn)證鏈接有效性后發(fā)起用戶支付,要求用戶授權(quán)。
(7)用戶在微信客戶端輸入密碼,確認(rèn)支付后,微信客戶端提交授權(quán)。
(8)微信支付系統(tǒng)根據(jù)用戶授權(quán)完成支付交易。
(9)微信支付系統(tǒng)完成支付交易后給微信客戶端返回交易結(jié)果,并將交易結(jié)果通過短信、微信消息提示用戶。微信客戶端展示支付交易結(jié)果頁面。
(10)微信支付系統(tǒng)通過發(fā)送異步消息通知商戶后臺系統(tǒng)支付結(jié)果。商戶后臺系統(tǒng)需回復(fù)接收情況,通知微信后臺系統(tǒng)不再發(fā)送該單的支付通知。
(11)未收到支付通知的情況,商戶后臺系統(tǒng)調(diào)用【查詢訂單API】。
(12)商戶確認(rèn)訂單已支付后給用戶發(fā)貨。
相關(guān)工具類
/package cn.tx.utils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* http工具類
* @author Administrator
*/
public class HttpClient {
private String url;
private Map<String, String> param;
private int statusCode;
private String content;
private String xmlParam;
private boolean isHttps;
public boolean isHttps() {
return isHttps;
}
public void setHttps(boolean isHttps) {
this.isHttps = isHttps;
}
public String getXmlParam() {
return xmlParam;
}
public void setXmlParam(String xmlParam) {
this.xmlParam = xmlParam;
}
public HttpClient(String url, Map<String, String> param) {
this.url = url;
this.param = param;
}
public HttpClient(String url) {
this.url = url;
}
public void setParameter(Map<String, String> map) {
param = map;
}
public void addParameter(String key, String value) {
if (param == null)
param = new HashMap<String, String>();
param.put(key, value);
}
public void post() throws ClientProtocolException, IOException {
HttpPost http = new HttpPost(url);
setEntity(http);
execute(http);
}
public void put() throws ClientProtocolException, IOException {
HttpPut http = new HttpPut(url);
setEntity(http);
execute(http);
}
public void get() throws ClientProtocolException, IOException {
if (param != null) {
StringBuilder url = new StringBuilder(this.url);
boolean isFirst = true;
for (String key : param.keySet()) {
if (isFirst)
url.append("?");
else
url.append("&");
url.append(key).append("=").append(param.get(key));
}
this.url = url.toString();
}
HttpGet http = new HttpGet(url);
execute(http);
}
/**
* set http post,put param
*/
private void setEntity(HttpEntityEnclosingRequestBase http) {
if (param != null) {
List<NameValuePair> nvps = new LinkedList<NameValuePair>();
for (String key : param.keySet())
nvps.add(new BasicNameValuePair(key, param.get(key))); // 參數(shù)
http.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); // 設(shè)置參數(shù)
}
if (xmlParam != null) {
http.setEntity(new StringEntity(xmlParam, Consts.UTF_8));
}
}
private void execute(HttpUriRequest http) throws ClientProtocolException,
IOException {
CloseableHttpClient httpClient = null;
try {
if (isHttps) {
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(null, new TrustStrategy() {
// 信任所有
public boolean isTrusted(X509Certificate[] chain,
String authType)
throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslContext);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf)
.build();
} else {
httpClient = HttpClients.createDefault();
}
CloseableHttpResponse response = httpClient.execute(http);
try {
if (response != null) {
if (response.getStatusLine() != null)
statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
// 響應(yīng)內(nèi)容
content = EntityUtils.toString(entity, Consts.UTF_8);
}
} finally {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpClient.close();
}
}
public int getStatusCode() {
return statusCode;
}
public String getContent() throws ParseException, IOException {
return content;
}
}
唯一訂單號工具類(使用推特雪花算法
)
package cn.tx.utils;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.NetworkInterface;
/**
* <p>名稱:IdWorker.java</p>
* <p>描述:分布式自增長ID</p>
* <pre>
* Twitter的 Snowflake JAVA實(shí)現(xiàn)方案
* </pre>
* 核心代碼為其IdWorker這個類實(shí)現(xiàn),其原理結(jié)構(gòu)如下,我分別用一個0表示一位,用—分割開部分的作用:
* 1||0---0000000000 0000000000 0000000000 0000000000 0 --- 00000 ---00000 ---000000000000
* 在上面的字符串中,第一位為未使用(實(shí)際上也可作為long的符號位),接下來的41位為毫秒級時間,
* 然后5位datacenter標(biāo)識位,5位機(jī)器ID(并不算標(biāo)識符,實(shí)際是為線程標(biāo)識),
* 然后12位該毫秒內(nèi)的當(dāng)前毫秒內(nèi)的計(jì)數(shù),加起來剛好64位,為一個Long型。
* 這樣的好處是,整體上按照時間自增排序,并且整個分布式系統(tǒng)內(nèi)不會產(chǎn)生ID碰撞(由datacenter和機(jī)器ID作區(qū)分),
* 并且效率較高,經(jīng)測試,snowflake每秒能夠產(chǎn)生26萬ID左右,完全滿足需要。
* <p>
* 64位ID (42(毫秒)+5(機(jī)器ID)+5(業(yè)務(wù)編碼)+12(重復(fù)累加))
*
* @author Polim
*/
public class IdWorker {
// 時間起始標(biāo)記點(diǎn),作為基準(zhǔn),一般取系統(tǒng)的最近時間(一旦確定不能變動)
private final static long twepoch = 1288834974657L;
// 機(jī)器標(biāo)識位數(shù)
private final static long workerIdBits = 5L;
// 數(shù)據(jù)中心標(biāo)識位數(shù)
private final static long datacenterIdBits = 5L;
// 機(jī)器ID最大值
private final static long maxWorkerId = -1L ^ (-1L << workerIdBits);
// 數(shù)據(jù)中心ID最大值
private final static long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
// 毫秒內(nèi)自增位
private final static long sequenceBits = 12L;
// 機(jī)器ID偏左移12位
private final static long workerIdShift = sequenceBits;
// 數(shù)據(jù)中心ID左移17位
private final static long datacenterIdShift = sequenceBits + workerIdBits;
// 時間毫秒左移22位
private final static long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
private final static long sequenceMask = -1L ^ (-1L << sequenceBits);
/* 上次生產(chǎn)id時間戳 */
private static long lastTimestamp = -1L;
// 0,并發(fā)控制
private long sequence = 0L;
private final long workerId;
// 數(shù)據(jù)標(biāo)識id部分
private final long datacenterId;
public IdWorker(){
this.datacenterId = getDatacenterId(maxDatacenterId);
this.workerId = getMaxWorkerId(datacenterId, maxWorkerId);
}
/**
* @param workerId
* 工作機(jī)器ID
* @param datacenterId
* 序列號
*/
public IdWorker(long workerId, long datacenterId) {
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
}
this.workerId = workerId;
this.datacenterId = datacenterId;
}
/**
* 獲取下一個ID
*
* @return
*/
public synchronized long nextId() {
long timestamp = timeGen();
if (timestamp < lastTimestamp) {
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}
if (lastTimestamp == timestamp) {
// 當(dāng)前毫秒內(nèi),則+1
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) {
// 當(dāng)前毫秒內(nèi)計(jì)數(shù)滿了,則等待下一秒
timestamp = tilNextMillis(lastTimestamp);
}
} else {
sequence = 0L;
}
lastTimestamp = timestamp;
// ID偏移組合生成最終的ID,并返回ID
long nextId = ((timestamp - twepoch) << timestampLeftShift)
| (datacenterId << datacenterIdShift)
| (workerId << workerIdShift) | sequence;
return nextId;
}
private long tilNextMillis(final long lastTimestamp) {
long timestamp = this.timeGen();
while (timestamp <= lastTimestamp) {
timestamp = this.timeGen();
}
return timestamp;
}
private long timeGen() {
return System.currentTimeMillis();
}
/**
* <p>
* 獲取 maxWorkerId
* </p>
*/
protected static long getMaxWorkerId(long datacenterId, long maxWorkerId) {
StringBuffer mpid = new StringBuffer();
mpid.append(datacenterId);
String name = ManagementFactory.getRuntimeMXBean().getName();
if (!name.isEmpty()) {
/*
* GET jvmPid
*/
mpid.append(name.split("@")[0]);
}
/*
* MAC + PID 的 hashcode 獲取16個低位
*/
return (mpid.toString().hashCode() & 0xffff) % (maxWorkerId + 1);
}
/**
* <p>
* 數(shù)據(jù)標(biāo)識id部分
* </p>
*/
protected static long getDatacenterId(long maxDatacenterId) {
long id = 0L;
try {
InetAddress ip = InetAddress.getLocalHost();
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
if (network == null) {
id = 1L;
} else {
byte[] mac = network.getHardwareAddress();
id = ((0x000000FF & (long) mac[mac.length - 1])
| (0x0000FF00 & (((long) mac[mac.length - 2]) << 8))) >> 6;
id = id % (maxDatacenterId + 1);
}
} catch (Exception e) {
System.out.println(" getDatacenterId: " + e.getMessage());
}
return id;
}
}
調(diào)用微信下單接口
package cn.tx.servlet;
import cn.tx.utils.HttpClient;
import cn.tx.utils.IdWorker;
import com.alibaba.fastjson.JSON;
import com.github.wxpay.sdk.WXPayUtil;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
*
* 調(diào)用統(tǒng)一下單的接口,根據(jù)微信支付服務(wù)器返回的結(jié)果生成支付二維碼
*/
public class CreateNativeServlet extends HttpServlet {
/**
* 調(diào)用微信統(tǒng)一下單接口,在微信端產(chǎn)生預(yù)支付訂單
* 根據(jù)返回的結(jié)果,生成二維碼
* HttpClient 發(fā)送請求 參數(shù)
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
// 創(chuàng)建HttpClient對象
HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
// 設(shè)置https
client.setHttps(true);
// 創(chuàng)建map集合
Map<String, String> param = new HashMap();
// 公眾號
param.put("appid", "");
// 商戶號
param.put("mch_id", "");
// 隨機(jī)字符串
param.put("nonce_str", );
// 商品描述
param.put("body", "");
// 訂單的編號
String orderNo = new IdWorker().nextId()+"";
// 商戶訂單號
param.put("out_trade_no", orderNo);
// 支付的金額
String totalFee = "1";
// 總金額(分)
param.put("total_fee",totalFee);
// IP
param.put("spbill_create_ip", "127.0.0.1");
// 回調(diào)地址
param.put("notify_url", "");
// 交易類型
param.put("trade_type", "NATIVE");
// 生成簽名,把map集合轉(zhuǎn)換成xml格式的字符串
// 1. 存放數(shù)據(jù)map 2. 申請的時候預(yù)留的密鑰
String xmlParam = WXPayUtil.generateSignedXml(param, "");
// 請求https://api.mch.weixin.qq.com/pay/unifiedorder,傳很多請求參數(shù)
client.setXmlParam(xmlParam);
// 發(fā)送請求
client.post();
// 獲取到返回結(jié)果
String content = client.getContent();
// 使用工具類,解析content數(shù)據(jù)
Map<String, String> resultMap = WXPayUtil.xmlToMap(content);
// 把訂單編號 和 支付金額存入到resultMap
resultMap.put("out_trade_no",orderNo);
resultMap.put("total_fee",totalFee);
// 響應(yīng)
resp.setContentType("application/json;charset=UTF-8");
String resultStr = JSON.toJSONString(resultMap);
resp.getWriter().print(resultStr);
} catch (Exception e) {
e.printStackTrace();
}
}
}
查詢訂單是否支付成功
package cn.tx.servlet;
import cn.tx.utils.HttpClient;
import com.alibaba.fastjson.JSON;
import com.github.wxpay.sdk.WXPayUtil;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* 查詢訂單是否支付成功
*/
public class QueryPayStatusServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
// 設(shè)置響應(yīng)編碼
resp.setContentType("application/json;charset=UTF-8");
// 獲取到訂單編號
String out_trade_no = req.getParameter("out_trade_no");
// 定義map集合
Map<String,Object > resultMap = new HashMap<String,Object>();
// 定義次數(shù)
int times = 0;
// 寫一個循環(huán)的查詢
while(times <= 10){
// 每循環(huán)一次,去調(diào)用一次查詢的接口
times++;
// 加睡眠的操作
Thread.sleep(5000);
// 調(diào)用查詢接口方法
Map<String, String> map = getPayStatus(out_trade_no);
// 根據(jù)map返回值,判斷支付是否成功
if("SUCCESS".equals(map.get("result_code")) && "SUCCESS".equals(map.get("trade_state"))){
resultMap.put("success",true);
String result = JSON.toJSONString(resultMap);
resp.getWriter().print(result);
return;
}
}
// 支付超時
resultMap.put("success",false);
resultMap.put("message","支付超時");
String result = JSON.toJSONString(resultMap);
resp.getWriter().print(result);
return;
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 發(fā)送請求,查詢支付狀態(tài)
*
* @param out_trade_no
* @return
*/
public Map<String, String> getPayStatus(String out_trade_no) {
// 查詢支付狀態(tài)的請求地址
HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
// 調(diào)用微信的查詢訂單支付狀態(tài)API
Map<String, String> paramMap = new HashMap<String, String>();
// 公眾賬號ID
paramMap.put("appid", "");
// 商戶號
paramMap.put("mch_id", "");
// 商戶的訂單號
paramMap.put("out_trade_no", out_trade_no);
// 隨機(jī)字符串
paramMap.put("nonce_str", );
try {
// 把paramMap轉(zhuǎn)成xml 轉(zhuǎn)xml時帶有簽名
String paramXml = WXPayUtil.generateSignedXml(paramMap, "txjavayingmulaoshi01234567891234");
// 設(shè)置參數(shù)
httpClient.setXmlParam(paramXml);
// 發(fā)送post請求
httpClient.post();
// 獲取返回結(jié)果
String content = httpClient.getContent();
// 根據(jù)返回結(jié)果生成map集合
Map<String, String> resultMap = WXPayUtil.xmlToMap(content);
return resultMap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
依賴
<dependencies>
<!--微信工具類-->
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
<!--HttpClient類-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
</dependencies>
文檔地址
https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_2
微信回調(diào)接口
1.獲取微信發(fā)來的xml字符串
2.根據(jù)字符串判斷是否支付成功(成功做業(yè)務(wù)邏輯),返回對應(yīng)的xml字符串給微信
public String notifyWeiXinPay(HttpServletRequest request, HttpServletResponse response) throws IOException, JDOMException {
System.out.println("微信支付回調(diào)");
InputStream inStream = request.getInputStream();
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outSteam.write(buffer, 0, len);
}
String resultxml = new String(outSteam.toByteArray(), "utf-8");
Map<String, String> params = PayCommonUtil.doXMLParse(resultxml);
outSteam.close();
inStream.close();
Map<String,String> return_data = new HashMap<String,String>();
if (!PayCommonUtil.isTenpaySign(params)) { //判斷是否支付成功
// 支付失敗
return_data.put("return_code", "FAIL");
return_data.put("return_msg", "return_code不正確");
return StringUtil.GetMapToXML(return_data);
} else {
System.out.println("===============付款成功==============");
// ------------------------------
// 處理業(yè)務(wù)開始
// ------------------------------
// 此處處理訂單狀態(tài),結(jié)合自己的訂單數(shù)據(jù)完成訂單狀態(tài)的更新
// ------------------------------
String total_fee = params.get("total_fee");
double v = Double.valueOf(total_fee) / 100;
String out_trade_no = String.valueOf(Long.parseLong(params.get("out_trade_no").split("O")[0]));
Date accountTime = DateUtil.stringtoDate(params.get("time_end"), "yyyyMMddHHmmss");
String ordertime = DateUtil.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss");
String totalAmount = String.valueOf(v);
String appId = params.get("appid");
String tradeNo = params.get("transaction_id");
return_data.put("return_code", "SUCCESS");
return_data.put("return_msg", "OK");
return StringUtil.GetMapToXML(return_data); //返回給微信的xml
}
}
}
參考
https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_7&index=8