https://blog.csdn.net/m_sicily/article/details/82493171
博客下面有github 可以下载学习下
import back.minsu.configure.Token;
import back.minsu.entity.MallOrderDetails;
import back.minsu.result.GetHotelXqRes;
import back.minsu.result.TokenRes;
import back.minsu.service.HotelOrderDetailsService;
import back.minsu.service.MallOrderDetailsService;
import back.minsu.utils.cs.module.service.WXserviceImpl;
import back.minsu.utils.cs.module.util.WxPayTool;
import back.minsu.utils.wxdl.PayParam;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
@RestController
@RequestMapping("wxPay")
public class WXController {
@Autowired
private WXserviceImpl wxPayService;
//这里是我的一下业务操作,可以换成自己的
@Autowired
HotelOrderDetailsService orderService;
@Autowired
MallOrderDetailsService mallOrderDetailsService;
@Autowired
private HotelOrderDetailsService hotelOrderDetailsService;
/**
* 统一下单
* 官方文档:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1
*
* @param param
* @return
* @throws Exception
*/
@PostMapping("/pay")
public Map<String, String> wxPay(@RequestBody PayParam param) throws Exception {
// String attach = "{\"attach\":\"" + param.getType() + "\"}";
//attach 是唯一的 额外参数!!!重点重点重点
//所以 用json来存多个参数, 转成String然后在解析
JSONObject object = new JSONObject();
object.put("type",param.getType());
object.put("id",param.getId());
object.put("money",param.getMoney());
String attach = object.toJSONString();
//请求预支付订单 可以从自己的数据中查出订单编号
String outTradeNo = "";
Map<String, String> result = WxPayTool.wxPay(param.getMoney(), outTradeNo, "订单详情",attach);
return result;
}
/**
* 支付异步结果通知,我们在请求预支付订单时传入的地址
* 官方文档 :https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_7&index=3
*/
@PostMapping(value = "notify")
public String wxPayNotify(HttpServletRequest request) {
String resXml = "";
System.out.println("---------------");
System.out.println("---------------");
System.out.println("---------------");
try {
InputStream inputStream = request.getInputStream();
//将InputStream转换成xmlString
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
// System.out.println(e.getMessage());
} finally {
try {
inputStream.close();
} catch (IOException e) {
// e.printStackTrace();
}
}
resXml = sb.toString();
String result = wxPayService.payBack(resXml);
return result;
} catch (Exception e) {
// System.out.println("微信手机支付失败:" + e.getMessage());
String result = "" + " " + " " + " ";
return result;
}
}
}
WXservice
import java.util.Map;
public interface WXservice {
Map<String, String> dounifiedOrder(String attach, String total_fee) throws Exception;
String payBack(String notifyData);
}
WXserviceImpl
package back.minsu.utils.cs.module.service;
import back.minsu.service.HotelOrderDetailsService;
import back.minsu.service.UserService;
import back.minsu.utils.cs.module.util.OurWxConfig;
import back.minsu.utils.cs.module.util.WxMD5Util;
import com.alibaba.fastjson.JSONObject;
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class WXserviceImpl implements WXservice {
@Autowired
private HotelOrderDetailsService HotelOrderDetailsService;
@Autowired
UserService userService;
private static final Logger logger = LoggerFactory.getLogger("MainLogger");
public static final String SPBILL_CREATE_IP = "47.93.85.ip";
// public static final String NOTIFY_URL = "http://你的域名/v1/weixin/notify.json";
public static final String TRADE_TYPE_APP = "APP";
/**
* 调用官方SDK 获取预支付订单等参数
* @param attach 额外参数
* @param total_fee 总价
* @return
* @throws Exception
*/
@Override
public Map<String, String> dounifiedOrder(String attach, String total_fee) throws Exception {
WxMD5Util md5Util = new WxMD5Util();
Map<String, String> returnMap = new HashMap<>();
OurWxConfig config = new OurWxConfig();
WXPay wxpay = new WXPay(config);
Map<String, String> data = new HashMap<>();
//生成商户订单号,不可重复
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", "100");
//自己的服务器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;
}
/**
*
* @param notifyData 异步通知后的XML数据
* @return
*/
@Override
public String payBack(String notifyData) {
System.out.println("---------------");
System.out.println("---------------");
OurWxConfig config = null;
try {
config = new OurWxConfig();
} catch (Exception e) {
e.printStackTrace();
}
WXPay wxpay = new WXPay(config);
String xmlBack = "";
Map<String, String> notifyMap = null;
try {
notifyMap = WXPayUtil.xmlToMap(notifyData); // 调用官方SDK转换成map类型数据
if (wxpay.isPayResultNotifySignatureValid(notifyMap)) {//验证签名是否有效,有效则进一步处理
String return_code = notifyMap.get("return_code");//状态
String out_trade_no = notifyMap.get("out_trade_no");//商户订单号
//额外参数 接受并获取里面的 参数
//额外参数 接受并获取里面的 参数
//额外参数 接受并获取里面的 参数
String attach = notifyMap.get("attach");
JSONObject object = JSONObject.parseObject(attach);
int type = object.getInteger("type"); //订单类型
int id = object.getInteger("id");//订单id
Double money = object.getDouble("money");//订单金额
System.out.println("---------------");
System.out.println(object.toJSONString());
System.out.println("---------------");
if (return_code.equals("SUCCESS")) {
if (out_trade_no != null) {
// 注意特殊情况:订单已经退款,但收到了支付结果成功的通知,不应把商户的订单状态从退款改成支付成功
// 注意特殊情况:微信服务端同样的通知可能会多次发送给商户系统,所以数据持久化之前需要检查是否已经处理过了,处理了直接返回成功标志
//业务数据持久化
System.err.println();
System.out.println("---------------");
System.err.println("支付成功");
System.out.println("---------------");
//此处可以进行自己的业务
//此处可以进行自己的业务
//此处可以进行自己的业务
//此处可以进行自己的业务
System.out.println("---- 修改状态完成 ----");
logger.info("微信手机支付回调成功订单号:{}", out_trade_no);
xmlBack = "" + " " + " " + " ";
} else {
logger.info("微信手机支付回调失败订单号:{}", out_trade_no);
xmlBack = "" + " " + " " + " ";
}
}
return xmlBack;
} else {
// 签名错误,如果数据里没有sign字段,也认为是签名错误
//失败的数据要不要存储?
logger.error("手机支付回调通知签名错误");
xmlBack = "" + " " + " " + " ";
return xmlBack;
}
} catch (Exception e) {
logger.error("手机支付回调通知失败", e);
xmlBack = "" + " " + " " + " ";
}
return xmlBack;
}
}
OurWxConfig
import com.github.wxpay.sdk.WXPayConfig;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
/**
* @Author: zyh
* @Description: 项目描述
* @Date: 2020/6/20 19:37
*/
public class OurWxConfig implements WXPayConfig {
private byte[] certData;
public static final String APP_ID = "**********";
public static final String KEY = "**********";
public static final String MCH_ID = "**********";
public OurWxConfig() throws Exception {
//从微信商户平台下载的安全证书存放的路径
String certPath = "自己配置好放到服务器上的路径地址";
File file = new File(certPath);
InputStream certStream = new FileInputStream(file);
this.certData = new byte[(int) file.length()];
certStream.read(this.certData);
certStream.close();
}
@Override
public String getAppID() {
return "******";
}
//parnerid,商户号
@Override
public String getMchID() {
return "******";
}
@Override
public String getKey() {
return "******";
}
@Override
public InputStream getCertStream() {
ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
return certBis;
}
@Override
public int getHttpConnectTimeoutMs() {
return 8000;
}
@Override
public int getHttpReadTimeoutMs() {
return 10000;
}
}
WxMD5Util
import com.github.wxpay.sdk.WXPayConstants;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
/**
* @Author: zyh
* @Date: 2020/6/20 19:37
*/
public class WxMD5Util {
public String getSign(Map<String, String> data) throws Exception {
OurWxConfig config = new OurWxConfig();
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();
}
}
WxPayTool
import com.aliyun.oss.ServiceException;
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @Author: zyh
* @CreateDate: 2020/6/20
*/
public class WxPayTool {
/**
* 回调地址
*/
private static final String NOTIFY_URL = "http://47.93.85.82:8083/wxPay/notify";
private static final String IP = "47.93.85.82";
public static Map<String, String> wxPay(String money, String outTradeNo, String body,String attach,String uid ,String id) throws Exception {
BigDecimal totalPrice = new BigDecimal(money);
String totalFee = totalPrice.multiply(new BigDecimal(100)).toBigInteger().toString();
OurWxConfig wxConfig = new OurWxConfig();
WXPay wxPay = new WXPay(wxConfig);
Map<String, String> param = new HashMap<>();
param.put("appid", wxConfig.getAppID());
param.put("mch_id",wxConfig.getMchID());
//额外参数
param.put("attach",attach);
param.put("uid",uid);
param.put("id",id);
// 支付场景 APP 微信app支付 JSAPI 公众号支付 NATIVE 扫码支付
param.put("trade_type","APP");
// 回调地址
param.put("notify_url", NOTIFY_URL);
// 终端ip
param.put("spbill_create_ip", IP);
// 订单总金额
param.put("total_fee",totalFee);
// 默认人民币
param.put("fee_type","CNY");
// 交易订单号
param.put("out_trade_no",outTradeNo);
// 内容
param.put("body",body);
// 生成随机字符串
param.put("nonce_str", WXPayUtil.generateNonceStr());
//一次签名
WxMD5Util md5Util = new WxMD5Util();
// String oneSign = md5Util.getSign(param);
String oneSign = WXPayUtil.generateSignature(param,wxConfig.getKey());
param.put("sign",oneSign);
/**
* wxPay.unifiedOrder 这个方法中调用微信统一下单接口
*/
Map<String, String> respData = wxPay.unifiedOrder(param);
System.out.println();
System.out.println("返回结果------" + respData.toString());
System.out.println();
if (respData.get("return_code").equals("SUCCESS")){
//返回给APP端的参数,APP端再调起支付接口
Map<String, String> res = new HashMap<>();
res.put("appid",wxConfig.getAppID());
res.put("noncestr",respData.get("nonce_str"));
res.put("partnerid",wxConfig.getMchID());
res.put("timestamp",String.valueOf(System.currentTimeMillis()/1000));
res.put("package","Sign=WXPay");
res.put("prepayid",respData.get("prepay_id"));
//签名
String sign = WXPayUtil.generateSignature(res, wxConfig.getKey());
res.put("sign", sign);
System.out.println("新的验签-----------" + sign);
return res;
}
System.out.println("----------报错" + respData.get("return_msg"));
throw new Exception(respData.get("return_msg"));
}
public static String wxNotify(HttpServletRequest request) {
System.out.println("微信支付回调");
try {
// 读取参数
// 解析xml成map
Map<String, String> map = WXPayUtil.xmlToMap(getParam(request));
System.out.println("微信支付回调返回的信息:" + map);
// check(map);//该处读者自行校验(验证订单号,付款金额等是否正确)
String orderNo = map.get("out_trade_no");
String resultCode = map.get("result_code");
// 另起线程处理业务
System.out.println();
System.out.println("另起线程处理业务");
System.out.println();
if (resultCode.equals("SUCCESS")) {
return setXml("SUCCESS", "OK");
} else {
return setXml("fail", "付款失败");
}
} catch (ServiceException e) {
return setXml("fail", "付款失败");
} catch (Exception e) {
System.out.println("微信支付回调发生异常:" + e.getLocalizedMessage());
return setXml("fail", "付款失败");
}
}
private static String getParam(HttpServletRequest request) throws IOException {
// 读取参数
InputStream inputStream;
StringBuilder sb = new StringBuilder();
inputStream = request.getInputStream();
String s;
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
while ((s = in.readLine()) != null) {
sb.append(s);
}
in.close();
inputStream.close();
return sb.toString();
}
/**
* 通过xml发给微信消息
* @param return_code
* @param return_msg
* @return
*/
private static String setXml(String return_code, String return_msg) {
SortedMap<String, String> parameters = new TreeMap<>();
parameters.put("return_code", return_code);
parameters.put("return_msg", return_msg);
try {
return WXPayUtil.mapToXml(parameters);
} catch (Exception e) {
System.out.println("返回微信消息时map转xml失败");
return " + return_code + "]]>" + " + return_msg
+ "]]> ";
}
}
}