微信支付开发文档地址
SDK与DEMO下载--JAVA .NET C# PHP
微信支付参数配置
public class WXPayConfig {
//应用ID
public final static String APP_ID = "yourAppId";
//API密钥
public final static String APP_KEY = "yourAppKey";
//商户号
public final static String MCH_ID = "yourMchId";
//统一下单地址
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";
//统一下单回调地址
public final static String NOTIFY_URL = "yourNotifyUrl";
}
统一下单
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并转化成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:--- 微信统一下单请求交易成功");
} else {
String message = result.get("err_code_des");
ret.put("code", -1);
ret.put("msg", "微信统一下单请求交易解析失败");
logger.error("errorMsg:--- 微信统一下单请求交易解析失败" + message);
}
} else {
ret.put("code", -1);
ret.put("msg", "微信统一下单请求交易解析失败");
logger.error("errorMsg:--- 微信统一下单请求交易解析失败");
}
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:--- 微信支付订单查询结束");
} catch (Exception e) {
// System.out.println("erroroMsg:--- 微信支付订单查询失败" + e.getMessage()
// + message);
}
return message;
}
支付结果异步回调
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);
// 验签
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;
}
过期时间格式转化
private static String timeExpire(long time) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyyMMddHHmmss");
Date date = new Date(time * 1000);
return simpleDateFormat.format(date);
}