在這里插入圖片描述
這是微信下單支付的建議時(shí)序圖,我們?cè)谌粘i_發(fā)過(guò)程中結(jié)合自身的訂單業(yè)務(wù)場(chǎng)景,進(jìn)行訂單的處理。我這里就以一個(gè)簡(jiǎn)單的購(gòu)買單個(gè)產(chǎn)品的業(yè)務(wù)邏輯展示微信支付的Java后臺(tái)代碼。這里是微信官方文檔:
https://pay.weixin.qq.com/wiki/doc/api/external/jsapi.php?chapter=9_1
1.準(zhǔn)備好工具類
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
package com.demo.utils.wxpay;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.*;
public class WXPayUtil {
private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final Random RANDOM = new SecureRandom();
/**
* XML格式字符串轉(zhuǎn)換為Map
*
* @param strXML XML字符串
* @return XML數(shù)據(jù)轉(zhuǎn)換后的Map
* @throws Exception
*/
public static Map<String, String> xmlToMap(String strXML) throws Exception {
try {
Map<String, String> data = new HashMap<String, String>();
DocumentBuilder documentBuilder = WXPayXmlUtil.newDocumentBuilder();
InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8"));
org.w3c.dom.Document doc = documentBuilder.parse(stream);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getDocumentElement().getChildNodes();
for (int idx = 0; idx < nodeList.getLength(); ++idx) {
Node node = nodeList.item(idx);
if (node.getNodeType() == Node.ELEMENT_NODE) {
org.w3c.dom.Element element = (org.w3c.dom.Element) node;
data.put(element.getNodeName(), element.getTextContent());
}
}
try {
stream.close();
} catch (Exception ex) {
// do nothing
}
return data;
} catch (Exception ex) {
WXPayUtil.getLogger().warn("Invalid XML, can not convert to map. Error message: {}. XML content: {}", ex.getMessage(), strXML);
throw ex;
}
}
/**
* 將Map轉(zhuǎn)換為XML格式的字符串
*
* @param data Map類型數(shù)據(jù)
* @return XML格式的字符串
* @throws Exception
*/
public static String mapToXml(Map<String, String> data) throws Exception {
org.w3c.dom.Document document = WXPayXmlUtil.newDocument();
org.w3c.dom.Element root = document.createElement("xml");
document.appendChild(root);
for (String key: data.keySet()) {
String value = data.get(key);
if (value == null) {
value = "";
}
value = value.trim();
org.w3c.dom.Element filed = document.createElement(key);
filed.appendChild(document.createTextNode(value));
root.appendChild(filed);
}
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
transformer.transform(source, result);
String output = writer.getBuffer().toString(); //.replaceAll("\n|\r", "");
try {
writer.close();
}
catch (Exception ex) {
}
return output;
}
/**
* 生成帶有 sign 的 XML 格式字符串
*
* @param data Map類型數(shù)據(jù)
* @param key API密鑰
* @return 含有sign字段的XML
*/
public static String generateSignedXml(final Map<String, String> data, String key) throws Exception {
return generateSignedXml(data, key, WXPayConstants.SignType.MD5);
}
/**
* 生成帶有 sign 的 XML 格式字符串
*
* @param data Map類型數(shù)據(jù)
* @param key API密鑰
* @param signType 簽名類型
* @return 含有sign字段的XML
*/
public static String generateSignedXml(final Map<String, String> data, String key, WXPayConstants.SignType signType) throws Exception {
String sign = generateSignature(data, key, signType);
data.put(WXPayConstants.FIELD_SIGN, sign);
return mapToXml(data);
}
/**
* 判斷簽名是否正確
*
* @param xmlStr XML格式數(shù)據(jù)
* @param key API密鑰
* @return 簽名是否正確
* @throws Exception
*/
public static boolean isSignatureValid(String xmlStr, String key) throws Exception {
Map<String, String> data = xmlToMap(xmlStr);
if (!data.containsKey(WXPayConstants.FIELD_SIGN) ) {
return false;
}
String sign = data.get(WXPayConstants.FIELD_SIGN);
return generateSignature(data, key).equals(sign);
}
/**
* 判斷簽名是否正確,必須包含sign字段,否則返回false。使用MD5簽名。
*
* @param data Map類型數(shù)據(jù)
* @param key API密鑰
* @return 簽名是否正確
* @throws Exception
*/
public static boolean isSignatureValid(Map<String, String> data, String key) throws Exception {
return isSignatureValid(data, key, WXPayConstants.SignType.MD5);
}
/**
* 判斷簽名是否正確,必須包含sign字段,否則返回false。
*
* @param data Map類型數(shù)據(jù)
* @param key API密鑰
* @param signType 簽名方式
* @return 簽名是否正確
* @throws Exception
*/
public static boolean isSignatureValid(Map<String, String> data, String key, WXPayConstants.SignType signType) throws Exception {
if (!data.containsKey(WXPayConstants.FIELD_SIGN) ) {
return false;
}
String sign = data.get(WXPayConstants.FIELD_SIGN);
return generateSignature(data, key, signType).equals(sign);
}
/**
* 生成簽名
*
* @param data 待簽名數(shù)據(jù)
* @param key API密鑰
* @return 簽名
*/
public static String generateSignature(final Map<String, String> data, String key) throws Exception {
return generateSignature(data, key, WXPayConstants.SignType.MD5);
}
/**
* 生成簽名. 注意,若含有sign_type字段,必須和signType參數(shù)保持一致。
*
* @param data 待簽名數(shù)據(jù)
* @param key API密鑰
* @param signType 簽名方式
* @return 簽名
*/
public static String generateSignature(final Map<String, String> data, String key, WXPayConstants.SignType signType) throws Exception {
Set<String> keySet = data.keySet();
String[] keyArray = keySet.toArray(new String[keySet.size()]);
Arrays.sort(keyArray);
StringBuilder sb = new StringBuilder();
for (String k : keyArray) {
if (k.equals(WXPayConstants.FIELD_SIGN)) {
continue;
}
if (data.get(k).trim().length() > 0) // 參數(shù)值為空,則不參與簽名
sb.append(k).append("=").append(data.get(k).trim()).append("&");
}
sb.append("key=").append(key);
if (WXPayConstants.SignType.MD5.equals(signType)) {
return MD5(sb.toString()).toUpperCase();
}
else if (WXPayConstants.SignType.HMACSHA256.equals(signType)) {
return HMACSHA256(sb.toString(), key);
}
else {
throw new Exception(String.format("Invalid sign_type: %s", signType));
}
}
/**
* 獲取隨機(jī)字符串 Nonce Str
*
* @return String 隨機(jī)字符串
*/
public static String generateNonceStr() {
char[] nonceChars = new char[32];
for (int index = 0; index < nonceChars.length; ++index) {
nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
}
return new String(nonceChars);
}
/**
* 生成 MD5
*
* @param data 待處理數(shù)據(jù)
* @return MD5結(jié)果
*/
public static String MD5(String data) throws Exception {
java.security.MessageDigest md = MessageDigest.getInstance("MD5");
byte[] array = md.digest(data.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (byte item : array) {
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString().toUpperCase();
}
/**
* 生成 HMACSHA256
* @param data 待處理數(shù)據(jù)
* @param key 密鑰
* @return 加密結(jié)果
* @throws Exception
*/
public static String HMACSHA256(String data, String key) throws Exception {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
sha256_HMAC.init(secret_key);
byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (byte item : array) {
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString().toUpperCase();
}
/**
* 日志
* @return
*/
public static Logger getLogger() {
Logger logger = LoggerFactory.getLogger("wxpay java sdk");
return logger;
}
/**
* 獲取當(dāng)前時(shí)間戳,單位秒
* @return
*/
public static long getCurrentTimestamp() {
return System.currentTimeMillis()/1000;
}
/**
* 獲取當(dāng)前時(shí)間戳,單位毫秒
* @return
*/
public static long getCurrentTimestampMs() {
return System.currentTimeMillis();
}
}
package com.demo.util;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonElement;
import org.apache.commons.codec.Charsets;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import java.io.*;
import java.net.URL;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.util.ArrayList;
public final class HttpUtil {
private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
private static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36";
private HttpUtil() {
}
public static String httpClientPost(String url) {
String result = "";
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod(url);
try {
client.executeMethod(getMethod);
result = getMethod.getResponseBodyAsString();
} catch (Exception e) {
logger.error(e.toString());
} finally {
getMethod.releaseConnection();
}
return result;
}
public static String httpClientPost(String url, ArrayList<NameValuePair> list) {
String result = "";
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(url);
try {
NameValuePair[] params = new NameValuePair[list.size()];
for (int i = 0; i < list.size(); i++) {
params[i] = list.get(i);
}
postMethod.addParameters(params);
client.executeMethod(postMethod);
result = postMethod.getResponseBodyAsString();
} catch (Exception e) {
logger.error(e.toString());
} finally {
postMethod.releaseConnection();
}
return result;
}
/**
* if response == null -> code != 200
*
* @param url
* @param json
* @return
* @throws IOException
*/
public static JsonElement postJson(String url, String json) throws Exception {
return postJsonWithHeader(url, json, null);
}
public static JsonElement postJsonWithHeader(String url, String json, Header[] headers) throws Exception {
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
JsonElement response = null;
try {
StringEntity s = new StringEntity(json);
s.setContentEncoding("UTF-8");
s.setContentType("application/json");
post.setEntity(s);
if (ArrayUtils.isNotEmpty(headers)) {
post.setHeaders(headers);
}
HttpResponse res = client.execute(post);
String result = EntityUtils.toString(res.getEntity());
response = GsonUtil.jsonParser().parse(result);
if (response != null) {
if (response.isJsonObject())
response.getAsJsonObject().addProperty("statusCode", res.getStatusLine().getStatusCode());
if (response.isJsonArray()) {
if (response.getAsJsonArray() != null)
response.getAsJsonArray().get(0).getAsJsonObject()
.addProperty("statusCode", res.getStatusLine().getStatusCode());
}
}
} catch (IOException e) {
LogUtils.error(logger, "url:{}, param:{}", e, url, json);
throw e;
}
return response;
}
public static JsonElement patchJsonWithHeader(String url, String json, Header[] headers) throws Exception {
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPatch patch = new HttpPatch(url);
JsonElement response = null;
try {
StringEntity s = new StringEntity(json);
s.setContentEncoding("UTF-8");
s.setContentType("application/json");
patch.setEntity(s);
if (ArrayUtils.isNotEmpty(headers)) {
patch.setHeaders(headers);
}
HttpResponse res = client.execute(patch);
String result = EntityUtils.toString(res.getEntity());
response = GsonUtil.jsonParser().parse(result);
if (response != null) {
if (response.isJsonObject())
response.getAsJsonObject().addProperty("statusCode", res.getStatusLine().getStatusCode());
if (response.isJsonArray()) {
if (response.getAsJsonArray() != null)
response.getAsJsonArray().get(0).getAsJsonObject()
.addProperty("statusCode", res.getStatusLine().getStatusCode());
}
}
} catch (IOException e) {
LogUtils.error(logger, "url:{}, param:{}", e, url, json);
throw e;
}
return response;
}
public static <T> T postJson(String url, String json, Class<T> tClass) throws Exception {
return postJsonWithHeader(url, json, null, tClass);
}
public static <T> T postJsonWithHeader(String url, String json, Header[] headers, Class<T> tClass) throws Exception {
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
T payload;
try {
StringEntity s = new StringEntity(json);
s.setContentEncoding("UTF-8");
s.setContentType("application/json");
post.setEntity(s);
if (ArrayUtils.isNotEmpty(headers)) {
post.setHeaders(headers);
}
HttpResponse res = client.execute(post);
String result = EntityUtils.toString(res.getEntity());
payload = JSONObject.parseObject(result, tClass);
} catch (IOException e) {
LogUtils.error(logger, "url:{}, param:{}", e, url, json);
throw e;
}
return payload;
}
public static String postXml(String url, String xmlStr) throws Exception {
int timeout = 5 * 1000;
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(timeout)
.setConnectTimeout(timeout)
.setSocketTimeout(timeout)
.build();
CloseableHttpClient client = HttpClientBuilder.create()
.setDefaultRequestConfig(requestConfig)
.build();
HttpPost post = new HttpPost(url);
try {
StringEntity s = new StringEntity(xmlStr, "UTF-8");
post.setEntity(s);
post.setHeader("Content-Type", "text/xml; charset=UTF-8");
HttpResponse res = client.execute(post);
String result = EntityUtils.toString(res.getEntity(), "UTF-8");
return result;
} catch (IOException e) {
LogUtils.error(logger, "url:{}, param:{}", e, url, xmlStr);
throw e;
}
}
public static String postSSL(String url, String data, String certPath, String certPass) {
HttpsURLConnection conn = null;
OutputStream out = null;
InputStream inputStream = null;
BufferedReader reader = null;
try {
KeyStore clientStore = KeyStore.getInstance("PKCS12");
clientStore.load(new FileInputStream(certPath), certPass.toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(clientStore, certPass.toCharArray());
KeyManager[] kms = kmf.getKeyManagers();
SSLContext sslContext = SSLContext.getInstance("TLSv1");
sslContext.init(kms, null, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
URL _url = new URL(url);
conn = (HttpsURLConnection) _url.openConnection();
conn.setConnectTimeout(25000);
conn.setReadTimeout(25000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("User-Agent", DEFAULT_USER_AGENT);
conn.connect();
out = conn.getOutputStream();
out.write(data.getBytes(Charsets.toCharset("UTF-8")));
out.flush();
inputStream = conn.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream, Charsets.toCharset("UTF-8")));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
return sb.toString();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(out);
IOUtils.closeQuietly(reader);
IOUtils.closeQuietly(inputStream);
if (conn != null) {
conn.disconnect();
}
}
}
}
2.申請(qǐng)微信訂單預(yù)支付參數(shù)
/**
* 微信提交預(yù)訂單
* @author wanghzi
*/
public JSONObject submitOrder() {
JSONObject result = new JSONObject();
try {
//構(gòu)建請(qǐng)求參數(shù)
String xmlStr = produceToWXOrderXml();
//發(fā)送下單請(qǐng)求
String xmlResult = HttpUtil.postXml("https://api.mch.weixin.qq.com/pay/unifiedorder", xmlStr);
//得到微信下單結(jié)果,轉(zhuǎn)換成Map
Map<String,String> resultMap = WXPayUtil.xmlToMap(xmlResult);
if (!"SUCCESS".equals(resultMap.get("return_code"))) {
throw new Exception("Request to make wechat order returned FAIL");
}
if (resultMap.get("prepay_id") == null){
throw new Exception("prepayId is null");
}
Long timeStamp = System.currentTimeMillis()/1000;
//重新計(jì)算sign
Map<String,String> signData = new HashMap<>();
signData.put("appId","你的appid");
signData.put("timeStamp",String.valueOf(timeStamp));
signData.put("nonceStr",resultMap.get("nonce_str"));
signData.put("package","prepay_id="+resultMap.get("prepay_id").toString());
signData.put("signType","MD5");
String paySign = WXPayUtil.generateSignature(signData, "wxPayAPIKey");//你的支付apikey,計(jì)算簽名需要
//封裝返回前端的參數(shù)對(duì)象
result.put("trade_type", resultMap.get("trade_type"));
result.put("prepay_id", resultMap.get("prepay_id"));
result.put("nonce_str", resultMap.get("nonce_str"));
result.put("sign_type", "MD5");
result.put("sign", paySign);
result.put("timestamp", timeStamp);
result.put("orderId", "當(dāng)時(shí)生成的業(yè)務(wù)訂單號(hào)");
return result;
} catch (Exception e) {
logger.error("Error trying to get data from wechat.", e);
throw new DPRuntimeException(Error.WECHAT_PAY_FAILED_TO_MAKE_ORDER);
}
}
public String produceToWXOrderXml() throws Exception {
//訂單支付時(shí)間
Map<String, String> map = new HashMap();
map.put("appid", "你的appid");
map.put("body", "商品名稱");
map.put("mch_id", "你的商戶號(hào)");
map.put("nonce_str", WXPayUtil.generateNonceStr());
map.put("notify_url", "localhost:8080/ns/qk/order/paid");//支付成功后微信服務(wù)器會(huì)調(diào)用你的通知接口
map.put("out_trade_no","我們自己業(yè)務(wù)生成的全局唯一訂單號(hào)");//業(yè)務(wù)orderId
map.put("spbill_create_ip", "用戶id,根據(jù)request獲取");
map.put("total_fee", String.valueOf(100));//支付金額(單位:分)
map.put("trade_type", "MWEB");//支付方式(JSAPI、NATIVE、APP、MWEB)
//map.put("openid", openId);//trade_type=JSAPI時(shí)必傳
map.put("time_expire", "20200101121212");//yyyyMMddHHmmss格式的當(dāng)前時(shí)間
map.put("fee_type", "CNY");//幣種
return WXPayUtil.generateSignedXml(map, "wxPayAPIKey");//你的支付apikey,計(jì)算簽名需要
}
3.后臺(tái)編寫回調(diào)接口
注意:回調(diào)接口一定要外網(wǎng)能訪問(wèn)的接口,且不能帶有權(quán)限限制和參數(shù)限制。
@ApiOperation(value = "微信支付回調(diào)接口")
@PostMapping("/paid")
public String paymentNotify(HttpServletRequest request) throws Exception {
try {
ServletInputStream inputStream = request.getInputStream();
String body = IOUtils.toString(inputStream, Charsets.UTF_8);
Map<String, String> data = WXPayUtil.xmlToMap(body);
//自己的業(yè)務(wù)邏輯,比如修改訂單為成功
} catch (Exception e) {
return "ERROR";
}
Map<String, String> result = new HashMap<>();
result.put("return_code", WXPayConstants.SUCCESS);
result.put("return_msg", WXPayConstants.OK);
return WXPayUtil.mapToXml(result);
}