
更新时间:2019年08月20日 14:43:10 缀不努力读书长窜当吴
这篇文章珠绍了Java开发微信Navicat支付完整版java版微信,文中通过实例代码介绍的十分详尽,对茨学习护仔一定的参考学习价值,需要的徘下面随着小编来一瓢学习吧
一:准备工譼rong>

1:先去注册一岗号java版微信,选?p>
2:去注册一抚号,用于收款
3:在商户号中配置对应的公众号的APPID

4:支付结果异步通知(需要重点注意)
注意:请先简要查看官方文档按流程开发,一切以官网文档为謘p;
5:测试的时酣要使用内网穿透软件,坟滇报错

二:开发代码
WeChatPayConfig:
public class WeChatPayConfig {
//公众号APPID
private String APPID = "";
//商户号KEY
private String KEY = "";
//商户号ID
private String MCHID = "";
//支付完成号回捣,需要外网能访问要是蝇不能是127.0.0.1跟localhost
private String NOTIFY_URL = "";
}

WeChatPayServcie:
public interface WeChatPayServcie {
//微信支付下单
public Map<String,String> getWxpayUrl(Map<String, String> sourceMap);
//订单查询
public String orderQuery(String out_trade_no);
}
@Service
public class WeChatPayServiceImpl implements WeChatPayServcie {
/**
* 微信支付请求
* @param sourceMap
* @return
*/
public Map<String,String> getWxpayUrl(Map<String, String> sourceMap) {
SortedMap<String, Object> signParams = new TreeMap<String, Object>();
String nonce_str = UUID.randomUUID().toString().trim().replaceAll("-", "");
signParams.put("appid", PayConfig.APPID);
signParams.put("mch_id",PayConfig.MCHID);
signParams.put("nonce_str",sourceMap.get("nonce_str"));
signParams.put("product_id",sourceMap.get("prod_id"));
signParams.put("body",sourceMap.get("body"));
signParams.put("out_trade_no",sourceMap.get("out_trade_no"));
signParams.put("total_fee",sourceMap.get("total_fee"));
signParams.put("spbill_create_ip", WxUtil.getIp());
signParams.put("notify_url",PayConfig.NOTYFLY_URL);
signParams.put("trade_type","NATIVE");
String sign = WxUtil.createSign(signParams,PayConfig.KEY);
signParams.put("sign",sign);
String xmlPackage = WxUtil.parseMapXML(signParams);
Map<String, Object> resultMap = new HashMap();
try {
String result = HttpUtil.post("https://api.mch.weixin.qq.com/pay/unifiedorder",xmlPackage);
resultMap = WxUtil.parseXmlMap(result);
} catch (Exception e) {
e.printStackTrace();
}
String returnCode = (String) resultMap.get("return_code");
String returnMsg = (String) resultMap.get("return_msg");
if (returnCode.equalsIgnoreCase("FAIL")) {
throw new RuntimeException(returnMsg);
}
String result_code = (String) resultMap.get("result_code");
if (result_code.equalsIgnoreCase("FAIL")) {
throw new RuntimeException(resultMap.get("err_code_des"));
}
String code_url = (String) resultMap.get("code_url");
Map<String,String> map = new HashMap<>();
map.put("code_url",code_url);
map.put("out_trade_no",sourceMap.get("out_trade_no"));
return map;
}
/**
* 微信支付订单查询
* @param out_trade_no
* @return
*/
public String orderQuery(String out_trade_no) {
String nonce_str = UUID.randomUUID().toString().trim().replaceAll("-", "");
SortedMap<String, Object> signParams = new TreeMap<>();
signParams.put("appid", payConfig.getWeChatPorpetties().getAppId());
signParams.put("mch_id",payConfig.getWeChatPorpetties().getMchId());
signParams.put("out_trade_no",out_trade_no);
signParams.put("nonce_str",nonce_str);
String sign = WxUtil.createSign(signParams,payConfig.getWeChatPorpetties().getApiKey());
signParams.put("sign",sign);
String xmlPackage = WxUtil.parseMapXML(signParams);
Map<String, Object> resultMap = new HashMap();
try {
String result = HttpUtil.post("https://api.mch.weixin.qq.com/pay/orderquery",xmlPackage);
resultMap = WxUtil.parseXmlMap(result);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("渠道网络异常");
}
String returnCode = (String) resultMap.get("return_code");
String returnMsg = (String) resultMap.get("return_msg");
if (returnCode.equalsIgnoreCase(PayContants.FAIL)) {
throw new RuntimeException(returnMsg);
}
String result_code = (String) resultMap.get("result_code");
if (result_code.equalsIgnoreCase(PayContants.FAIL)) {
throw new RuntimeException(resultMap.get("err_code_des"));
}
String trade_state = (String) resultMap.get("trade_state");
return trade_state;
}
}
WeChatPayController:
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/sanxing/article-134595-1.html
不想摊牌
鄙视