https://pay.weixin.qq.com/wiki/doc/api/index.html
提示:在这边选择想要使用的支付方式点击查看api文档,我这边开发的是web项目,所以就以Native支付来讲解。
提示:以下是本篇文章正文内容,下面案例可供参考
点击进入Native支付—Api列表
在这里可以选择想要的接口进行使用,并且查看需要带的参数,发送请求路径等
接口链接就是我们往此链接发送请求,即可得到我们想要的数据,比如二维码,订单状态等
URL地址:https://api.mch.weixin.qq.com/pay/unifiedorder
URL地址:https://api2.mch.weixin.qq.com/pay/unifiedorder(备用域名)见跨城冗灾方案
这两个接口都可以使用,推荐使用第一个
我们只要关注必填参数即可,感兴趣可以自己去官网看一下,这边不一一介绍了
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency> <dependency> <groupId>com.github.wxpay</groupId> <artifactId>wxpay-sdk</artifactId> <version>0.0.3</version> </dependency>
HttpClient:模仿网页发送http请求,wx提供的工具包
CommonResult:返回参数
@Data @NoArgsConstructor @AllArgsConstructor @ApiModel("响应的结果") public class CommonResult { @ApiModelProperty("响应的状态码") private Integer code; @ApiModelProperty("响应的信息") private String msg; @ApiModelProperty("响应的数据") private Object result; }
我们在使用之前要明确一些参数
这些在开发时会有人提供
public class ConfigUtil { public final static String APPID = "wx8087d8149331d27c";//服务号的应用号 public final static String MCH_ID = "1532192611";//商户号 public final static String APP_SECRECT = "e8cb3f526ac67e41dffb8fb4201873da";//服务号的应用密码 public final static String API_KEY = "Cc158380629071583806290715838062";//API密钥 }
@GetMapping("erweima") public Result createNative(String orderno){ Map testorder = payService.createNative(orderno); Result result=new Result(); result.setCode(100); result.setMsg(""); result.setT(testorder); return result; }
创建二维码的时候需要知道将钱付给谁,也就是商户的信息,由于什么原因发起的支付,所以这个service里面需要传递的有商户的信息还有订单的信息
各个参数的意思请参考微信支付开发文档
public Map createNative(String orderNo) { try { Map m = new HashMap(); //1、设置支付参数 m.put("appid", ConfigUtil.APPID);//"wx74862e0dfcf69954" m.put("mch_id",ConfigUtil.MCH_ID );//"1558950191" m.put("nonce_str", WXPayUtil.generateNonceStr()); m.put("body", "课程的信息"); m.put("out_trade_no", orderNo); m.put("total_fee", new BigDecimal("0.01").multiply(new BigDecimal("100")).longValue() + ""); m.put("spbill_create_ip", "127.0.0.1"); //设置的回调的路径 m.put("notify_url","http://www.chenkaixiang.top/notifyWeiXinPay\n");// //支付类型 native 扫码支付 m.put("trade_type", "NATIVE"); //2、HTTPClient来根据URL访问第三方接口并且传递参数 //https://api.mch.weixin.qq.com/v3/pay/transactions/native HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder"); //client设置参数 client.setXmlParam(WXPayUtil.generateSignedXml(m, ConfigUtil.API_KEY));//"Cc158380629071583806290715838062"));//"T6m9iK73b0kn9g5v426MKfHQH7X8rKwb" client.setHttps(true); client.post(); //3、返回第三方的数据 String xml = client.getContent(); Map<String, String> resultMap = WXPayUtil.xmlToMap(xml); //4、封装返回结果集 Map map = new HashMap<>(); map.put("out_trade_no", orderNo); map.put("course_id", 1); map.put("total_fee", 0.01); map.put("result_code", resultMap.get("result_code")); map.put("code_url", resultMap.get("code_url")); //微信支付二维码2小时过期,可采取2小时未支付取消订单 //redisTemplate.opsForValue().set(orderNo, map, 120, TimeUnit.MINUTES); return map; } catch (Exception e) { e.printStackTrace(); return new HashMap<>(); } }
支付完成之后需要查询一下订单的状态,到底是否支付成功,成功支付要进行操作
controller:
@GetMapping("getPayStatus/{orderNo}") public Result getPayStatus(@PathVariable("orderNo")String orderNo) throws AAAException { Map<String,String> map=payService.getPayStatus(orderNo); System.out.println(map); Result result=new Result(); if(map==null){ result.setMsg("支付失败"); return result; } else if(map.get("trade_state").equals("SUCCESS")){ //修改订单状态 并添加支付记录 result.setMsg("支付"); result.setT(map); return result; }else{ result.setCode(2500); result.setMsg("支付中"); result.setT(map); return result; }
对应的service
public Map<String, String> getPayStatus(String orderNo) throws AAAException { try { String url = "https://api.mch.weixin.qq.com/pay/orderquery"; Map<String, String> map = new HashMap<>(); map.put("appid",ConfigUtil.APPID); map.put("mch_id", ConfigUtil.MCH_ID); map.put("out_trade_no", orderNo); map.put("nonce_str", WXPayUtil.generateNonceStr()); HttpClient httpClient = new HttpClient(url); httpClient.setXmlParam(WXPayUtil.generateSignedXml(map,ConfigUtil.API_KEY ));//"Cc158380629071583806290715838062"));//"T6m9iK73b0kn9g5v426MKfHQH7X8rKwb")); httpClient.setHttps(true); httpClient.post(); String xml = httpClient.getContent(); Map<String,String> resultMap=WXPayUtil.xmlToMap(xml); System.out.println(resultMap); return resultMap; }catch (Exception e){ e.printStackTrace(); throw new AAAException(20001,"支付失败"); } }