微信支付
- 自己以前没做过微信支付的时候很是迷茫,现把2020最新附上,直接上代码
吊起支付
public JsonResult wxPay(String ids) throws Exception {
double total = 0;
//这里更具自己需求自定义就好可忽略
String[] orderIds = ids.split(",");
for(int i = 0;i<orderIds.length;i++) {
Order order = this.orderService.getOrderById(Integer.valueOf(orderIds[i]));
if(order != null) {
total += order.getTotalMoney();
}
}
//total = 0.01;
String attach = "{\"order_id\":\"" + ids + "\"}";
//请求预支付订单
Map<String, String> result = wxPayService.dounifiedOrder(total, attach);
Map<String, String> map = new HashMap<String, String>();
WxMD5Util md5Util = new WxMD5Util();
//返回APP端的数据
//参加调起支付的签名字段有且只能是6个,分别为appid、partnerid、prepayid、package、noncestr和timestamp,而且都必须是小写
//参加调起支付的签名字段有且只能是6个,分别为appid、partnerid、prepayid、package、noncestr和timestamp,而且都必须是小写
//参加调起支付的签名字段有且只能是6个,分别为appid、partnerid、prepayid、package、noncestr和timestamp,而且都必须是小写
map.put("appid", result.get("appid"));
map.put("partnerid", result.get("mch_id"));
map.put("prepayid", result.get("prepay_id"));
map.put("package", "Sign=WXPay");
map.put("noncestr", result.get("nonce_str"));
map.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));//单位为秒
// 这里不要使用请求预支付订单时返回的签名
map.put("sign", md5Util.getSign(map));
map.put("extdata", attach);
return new JsonResult(0, "支付成功", map);
//return map;
}
public static final String SPBILL_CREATE_IP = "60.205.253.31";
public static final String NOTIFY_URL = "http://60.205.253.31/hf/v1/weixin/notify.json";
public static final String TRADE_TYPE_APP = "APP";
/**
* 调用官方SDK 获取预支付订单等参数
* @param attach 额外参数
* @param total_fee 总价
* @return
* @throws Exception
*/
public Map<String, String> dounifiedOrder(double total, String attach) throws Exception {
String total_fee = (int)(total*100)+"";
WxMD5Util md5Util = new WxMD5Util();
Map<String, String> returnMap = new HashMap<String, String>();
WXConfigUtil config = new WXConfigUtil();
WXPay wxpay = new WXPay(config);
Map<String, String> data = new HashMap<String, String>();
//生成商户订单号,不可重复
String out_trade_no = "wxpay" + System.currentTimeMillis();
data.put("appid", config.getAppID());
data.put("mch_id", config.getMchID());
data.put("nonce_str", WXPayUtil.generateNonceStr());
String body = "订单支付";
data.put("body", body);
data.put("out_trade_no", out_trade_no);
data.put("total_fee", total_fee);
//自己的服务器IP地址
data.put("spbill_create_ip", SPBILL_CREATE_IP);
//异步通知地址(请注意必须是外网)
data.put("notify_url", NOTIFY_URL);
//交易类型
data.put("trade_type", TRADE_TYPE_APP);
//附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据
data.put("attach", attach);
String sign1 = md5Util.getSign(data);
data.put("sign", sign1);
try {
//使用官方API请求预付订单
Map<String, String> response = wxpay.unifiedOrder(data);
System.out.println(response);
String returnCode = response.get("return_code"); //获取返回码
//若返回码为SUCCESS,则会返回一个result_code,再对该result_code进行判断
if (returnCode.equals("SUCCESS")) {//主要返回以下5个参数
String resultCode = response.get("result_code");
returnMap.put("appid", response.get("appid"));
returnMap.put("mch_id", response.get("mch_id"));
returnMap.put("nonce_str", response.get("nonce_str"));
returnMap.put("sign", response.get("sign"));
if ("SUCCESS".equals(resultCode)) {//resultCode 为SUCCESS,才会返回prepay_id和trade_type
//获取预支付交易回话标志
returnMap.put("trade_type", response.get("trade_type"));
returnMap.put("prepay_id", response.get("prepay_id"));
return returnMap;
} else {
//此时返回没有预付订单的数据
return returnMap;
}
} else {
return returnMap;
}
} catch (Exception e) {
System.out.println(e);
//系统等其他错误的时候
}
return returnMap;
}
public class WxMD5Util {
public String getSign(Map<String, String> data) throws Exception {
WXConfigUtil config = new WXConfigUtil();
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) // 参数值为空,则不参与签名
sb.append(k).append("=").append(data.get(k).trim()).append("&");
}
sb.append("key=").append(config.getKey());
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
byte[] array = new byte[0];
try {
array = md.digest(sb.toString().getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
StringBuilder sb2 = new StringBuilder();
for (byte item : array) {
sb2.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
}
return sb2.toString().toUpperCase();
}
}
以上是微信支付的源代码,这次够详细了吧!望等帮到各位小伙伴。
后续有时间会更新银联、号码验签登录等一些第三方的代码(本人对第三方很感兴趣)
有问题的小伙伴可嘉主页Q或V进行交流