微信公众平台注册-申请微信支付
package com.cn.config;
import com.cn.controller.WXPayController;
import com.github.wxpay.sdk.WXPayUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
/**
* @Descripation 微信支付
* @Date 2019-08-02 9:26
* @since 1.0
**/
public class WXUtils {
private static final Logger logger = LoggerFactory.getLogger(WXPayController.class);
//从你的微信平台获取
private static final String appid = "";
private static final String mch_id = " ";
private static final String partnerkey = ""; //商户秘钥
/* *
* @Description 调用统一下单接口
* param 1 商品订单号 2 total_fee 支付金额 订单号前端传,金额必须后端查 订单号32位
*/
public static Map unifyPay(String out_trade_no, String total_fee) {
String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
Map<String, String> param = new HashMap<>();
param.put("appid", appid);
param.put("mch_id", mch_id);
String nonce_str = WXPayUtil.generateNonceStr();
param.put("nonce_str", nonce_str);
param.put("body", "");
param.put("out_trade_no", out_trade_no);
param.put("total_fee", total_fee);
param.put("spbill_create_ip", "127.0.0.1");//随便写
param.put("notify_url", "");//回调地址随便写
param.put("trade_type", "NATIVE");
Map<String, String> resultMap = null;
try {
String xmlParam = WXPayUtil.generateSignedXml(param, partnerkey);//生成带签名的xml
logger.error("这是生成的xml:{}", xmlParam);
String result = HttpUtils.postJsonRequest(url, xmlParam);
resultMap = WXPayUtil.xmlToMap(result);
logger.error("统一下单返回状态:{}", resultMap.get("return_code"));
logger.error("统一下单返回信息:{}", resultMap.get("return_msg"));
logger.error("二维码链接地址:{}", resultMap.get("code_url"));
} catch (Exception e) {
e.printStackTrace();
}
return resultMap;
}
/* *
* @Description 查询订单接口
*/
public static Map<String, String> queryOrder(String out_trade_no) {
String url = "https://api.mch.weixin.qq.com/pay/orderquery";
Map<String, String> param = new HashMap<>();
param.put("appid", appid);
param.put("mch_id", mch_id);
String nonce_str = WXPayUtil.generateNonceStr();
param.put("nonce_str", nonce_str);
param.put("out_trade_no", out_trade_no);
Map<String, String> resultMap = null;
try {
String xmlParam = WXPayUtil.generateSignedXml(param, partnerkey);
String result = HttpUtils.postJsonRequest(url, xmlParam);
resultMap = WXPayUtil.xmlToMap(result);
logger.error("查询订单返回状态:{}", resultMap.get("return_code"));
logger.error("查询订单返回信息:{}", resultMap.get("return_msg"));
logger.error("支付返回状态:{}", resultMap.get("trade_state"));
} catch (Exception e) {
e.printStackTrace();
}
return resultMap;
}
}
HttpUtils有需要的可以从我的资源里下载
package com.cn.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* @Descripation
* @since 1.0
**/
@RestController
@RequestMapping("/WXPay")
@CrossOrigin
@Api(description = "微信支付")
public class WXPayController {
@RequestMapping(value = "/OrderWXPay", method = RequestMethod.POST)
@ApiOperation("微信支付 参数 1 订单号 订单号必须32位 ")
@ApiImplicitParams({
@ApiImplicitParam("订单号")
})
public Object orderWXPay(String out_trade_no) {
//根据订单号去数据库查询金额 如果查不到 说明订单号非法return
//支付金额
Double order_limit = payOrder.getOrder_limit();
int order_money = (int) (order_limit * 100);
String total_fee = String.valueOf(order_money);
Map map = WXUtils.unifyPay(out_trade_no, total_fee);
if ("SUCCESS".equals(map.get("return_code"))) {
map.put("money", String.valueOf(order_limit));
return ResultMsg.ResultMsg(map,200,"下单成功");
}
return ResultMsg.ResultMsg(null,500, "订单异常");
}
@RequestMapping(value = "/OrderWXPayStatus", method = RequestMethod.POST)
@ApiOperation("微信支付状态 查询订单接口 参数 订单号 ")
@ApiImplicitParams({
@ApiImplicitParam("订单号")
})
public Object orderWXPayStatus(String out_trade_no) {
ResultMsg rm = null;
int num = 0;
while (true) {
Map<String, String> resultMap = WXUtils.queryOrder(out_trade_no);
if (resultMap == null) {
return ResultMsg.ResultMsg(false,100, "支付错误");
}
if (resultMap.get("trade_state").equals("SUCCESS")) {
//成功后的逻辑
return ResultMsg.ResultMsg(true,200, "支付成功");
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
num++;
if (num > 200) {
return ResultMsg.ResultMsg(false,300, "支付超时");
}
}
}
}