微信支付開發(fā)文檔地址
SDK與DEMO下載--JAVA .NET C# PHP
微信支付參數(shù)配置
public class WXPayConfig {
//應(yīng)用ID
public final static String APP_ID = "yourAppId";
//API密鑰
public final static String APP_KEY = "yourAppKey";
//商戶號(hào)
public final static String MCH_ID = "yourMchId";
//統(tǒng)一下單地址
public final static String URL_UNIFIED_ORDER = "https://api.mch.weixin.qq.com/pay/unifiedorder";
//查詢賬單地址
public final static String URL_ORDER_QUERY = "https://api.mch.weixin.qq.com/pay/orderquery";
//統(tǒng)一下單回調(diào)地址
public final static String NOTIFY_URL = "yourNotifyUrl";
}
統(tǒng)一下單
public static JSONObject wxUnifiedOrder(String orderId, String cost,
String body, String ip, long expireTime) {
JSONObject ret = new JSONObject();
SortedMap<String, String> param = new TreeMap<String, String>();
param.put("appid", WXPayConfig.APP_ID);
param.put("mch_id", WXPayConfig.MCH_ID);
param.put("nonce_str", WXPayUtil.generateNonceStr());
param.put("body", body);
param.put("out_trade_no", orderId);
param.put("fee_type", "CNY");
param.put("total_fee", cost);
param.put("spbill_create_ip", ip);
param.put("notify_url", WXPayConfig.NOTIFY_URL);
param.put("trade_type", "APP");
param.put("time_expire", timeExpire(expireTime));
// 生成sign并轉(zhuǎn)化成xml格式
String xml = "";
try {
xml = WXPayUtil.generateSignedXml(param, WXPayConfig.APP_KEY);
} catch (Exception e) {
ret.put("code", -1);
ret.put("msg", e.getMessage());
}
String response = "";
try {
response = HttpRequest.post(WXPayConfig.URL_UNIFIED_ORDER, xml);
} catch (Exception e) {
ret.put("code", -1);
ret.put("msg", e.getMessage());
}
Map<String, String> result = null;
try {
result = WXPayUtil.xmlToMap(response);
} catch (Exception e) {
ret.put("code", -1);
ret.put("msg", e.getMessage());
}
if (!result.isEmpty() && result.get("return_code").equals("SUCCESS")) {
if (result.get("result_code").equals("SUCCESS")) {
try {
ret = wxAppPay(result);
} catch (Exception e) {
}
ret.put("code", 0);
logger.error("InfoMsg:--- 微信統(tǒng)一下單請(qǐng)求交易成功");
} else {
String message = result.get("err_code_des");
ret.put("code", -1);
ret.put("msg", "微信統(tǒng)一下單請(qǐng)求交易解析失敗");
logger.error("errorMsg:--- 微信統(tǒng)一下單請(qǐng)求交易解析失敗" + message);
}
} else {
ret.put("code", -1);
ret.put("msg", "微信統(tǒng)一下單請(qǐng)求交易解析失敗");
logger.error("errorMsg:--- 微信統(tǒng)一下單請(qǐng)求交易解析失敗");
}
return ret;
}
賬單查詢
public static String wxQueryOrder() {
System.out.println("infoMsg:--- 微信支付訂單查詢開始");
String message = "";
try {
SortedMap<String, String> param = new TreeMap<String, String>();
param.put("appid", WXPayConfig.APP_ID);
param.put("mch_id", WXPayConfig.MCH_ID);
param.put("nonce_str", WXPayUtil.generateNonceStr());
param.put("out_trade_no", "20171205");
String xml = WXPayUtil
.generateSignedXml(param, WXPayConfig.APP_KEY);
String response = HttpRequest
.post(WXPayConfig.URL_ORDER_QUERY, xml);
if (response != null && response != "") {
Map<String, String> result = WXPayUtil.xmlToMap(response);
// System.out.println(result);
if (!result.isEmpty()) {
String return_code = result.get("return_code");
if (return_code.equals("SUCCESS")) {
String result_code = result.get("return_code");
if (result_code.equals("SUCCESS")) {
message = (String) result.get("trade_state_desc");
}
} else {
message = (String) result.get("err_code_des");
}
} else {
}
} else {
}
// System.out.println("infoMsg:--- 微信支付訂單查詢結(jié)束");
} catch (Exception e) {
// System.out.println("erroroMsg:--- 微信支付訂單查詢失敗" + e.getMessage()
// + message);
}
return message;
}
支付結(jié)果異步回調(diào)
public static JSONObject wxPayNotify(String body) {
JSONObject result = new JSONObject();
System.out.println("infoMsg:--- 微信異步通知開始");
String wx_map = "";
try {
String xml = body;
if (xml != null && xml != "") {
Map<String, String> resultMap = WXPayUtil.xmlToMap(xml);
// 驗(yàn)簽
if (!WXPayUtil.isSignatureValid(xml, WXPayConfig.APP_KEY)) {
if (resultMap.get("return_code").equals("SUCCESS")) {
Double amount = Double.parseDouble(resultMap
.get("total_fee"));
String passbackParams = resultMap.get("total_fee");
String outOrderNo = resultMap.get("out_trade_no");
Map<String, String> map = new HashMap<>();
map.put("return_code", "SUCCESS");
map.put("return_msg", "OK");
wx_map = WXPayUtil.mapToXml(map);
result.put("out_trade_no", outOrderNo);
result.put("code", 0);
}
}
} else {
result.put("code", -1);
result.put("msg", "微信異步通知失敗");
logger.error("infoMsg:--- 微信異步通知失敗");
}
} catch (Exception e) {
result.put("code", -1);
result.put("msg", "微信異步通知失敗" + e.getMessage());
logger.error("msg", "微信異步通知失敗" + e.getMessage());
}
result.put("wx_map", wx_map);
return result;
}
支付二次簽名
public static JSONObject wxAppPay(Map<String, String> param)
throws Exception {
JSONObject result = new JSONObject();
SortedMap<String, String> sortedMap = new TreeMap<String, String>();
sortedMap.put("appid", WXPayConfig.APP_ID);
sortedMap.put("partnerid", WXPayConfig.MCH_ID);
sortedMap.put("prepayid", param.get("prepay_id"));
sortedMap.put("package", "Sign=WXPay");
String nonceStr = WXPayUtil.generateNonceStr();
long timestamp = System.currentTimeMillis() / 1000;
sortedMap.put("noncestr", nonceStr);
sortedMap.put("timestamp", String.valueOf(timestamp));
String sign = WXPayUtil.generateSignature(sortedMap,
WXPayConfig.APP_KEY);
result.put("appid", WXPayConfig.APP_ID);
result.put("partnerid", WXPayConfig.MCH_ID);
result.put("prepayid", param.get("prepay_id"));
result.put("package", "Sign=WXPay");
result.put("noncestr", nonceStr);
result.put("timestamp", String.valueOf(timestamp));
result.put("sign", sign);
return result;
}
過期時(shí)間格式轉(zhuǎn)化
private static String timeExpire(long time) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyyMMddHHmmss");
Date date = new Date(time * 1000);
return simpleDateFormat.format(date);
}