先去官网下载 sdk ,下载完成后可以用idea打开看一下有哪些东西,然后直接 maven 发布一下(install)到本地仓库,然后就可以在项目中用maven导包:
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay‐sdk</artifactId>
<version>3.0.9</version>
</dependency>
创建支付类
package com.qingcheng.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.wxpay.sdk.Config;
import com.github.wxpay.sdk.WXPayRequest;
import com.github.wxpay.sdk.WXPayUtil;
import com.qingcheng.service.order.OrderService;
import com.qingcheng.service.order.WxPayService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap;
import java.util.Map;
@Service
public class WxPayServiceImpl implements WxPayService {
@Autowired
private Config config;
@Override
public Map createNative(String orderId, Integer money, String notifyUrl) {
try {
//1.封装请求参数
Map<String,String> map=new HashMap();
map.put("appid",config.getAppID());//公众账号ID
map.put("mch_id",config.getMchID());//商户号
map.put("nonce_str", WXPayUtil.generateNonceStr());//随机字符串
map.put("body","测试");//商品描述
map.put("out_trade_no",orderId);//订单号
map.put("total_fee",money+"");//金额
map.put("spbill_create_ip","127.0.0.1");//终端IP
map.put("notify_url",notifyUrl);//回调地址
map.put("trade_type","NATIVE");//交易类型
String xmlParam = WXPayUtil.generateSignedXml(map, config.getKey());
System.out.println("参数:"+xmlParam);
//2.发送请求
WXPayRequest wxPayRequest=new WXPayRequest(config);
String xmlResult = wxPayRequest.requestWithCert("/pay/unifiedorder", null, xmlParam, false);
System.out.println("结果:"+xmlResult);
//3.解析返回结果
Map<String, String> mapResult = WXPayUtil.xmlToMap(xmlResult);
Map m=new HashMap();
m.put("code_url", mapResult.get("code_url"));
m.put("total_fee",money+"");
m.put("out_trade_no",orderId);
return m;
} catch (Exception e) {
e.printStackTrace();
return new HashMap();
}
}
@Autowired
private OrderService orderService;
@Autowired
private RabbitTemplate rabbitTemplate;
@Override
public void notifyLogic(String xml) {
try {
//1.对xml进行解析 map
Map<String, String> map = WXPayUtil.xmlToMap(xml);
//2.验证签名
boolean signatureValid = WXPayUtil.isSignatureValid(map, config.getKey());// key是商户密钥
System.out.println("验证签名是否正确:"+signatureValid);
System.out.println(map.get("out_trade_no"));
System.out.println(map.get("result_code"));
//3.修改订单状态
if(signatureValid){
if("SUCCESS".equals(map.get("result_code"))){
orderService.updatePayStatus( map.get("out_trade_no"),map.get("transaction_id") );
//发送消息给mq
rabbitTemplate.convertAndSend("paynotify","",map.get("out_trade_no"));
}else{
//记录日志
}
}else{
//记录日志
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public Map queryPayStatus(String orderId) {
try {
//1.封装参数
Map<String,String> param=new HashMap<>();
param.put("appid",config.getAppID());
param.put("mch_id",config.getMchID());
param.put("out_trade_no",orderId);
param.put("nonce_str",WXPayUtil.generateNonceStr());
String xmlParam = WXPayUtil.generateSignedXml(param, config.getKey());
//2.调用接口
WXPayRequest wxPayRequest=new WXPayRequest(config);
String result = wxPayRequest.requestWithCert("/pay/orderquery", null, xmlParam, false);
//3.解析结果
Map<String, String> map = WXPayUtil.xmlToMap(result);
return map;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
这里说一下接收回调参数:
/**
* 通知
* @return
*/
@RequestMapping("/notify")
public Map notifyLogic(HttpServletRequest request){
System.out.println("回调了.....");
InputStream inStream;
try {
inStream = (InputStream) request.getInputStream();
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != ‐1) {
outSteam.write(buffer, 0, len);
}
outSteam.close();
inStream.close();
String result = new String(outSteam.toByteArray(), "utf‐8");
System.out.println(result);
WxPayServiceImpl.notifyLogic(result ) ;
} catch (IOException e) {
e.printStackTrace();
}
return new HashMap();
}
或者
/**
* 微信支付回调(post方式)
*/
@PostMapping("/order/callback")
public void orderCallback(HttpServletRequest request) throws Exception {
// 获取微信回调参数
InputStream inputStream= request.getInputStream();// 读取字节流
InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");// 字节流转字符流
BufferedReader in = new BufferedReader(reader);// 吧字符流放到字符缓冲流中读取,比 InputStreamReader 更快
StringBuffer buffer = new StringBuffer();
String line;
while ((line = in.readLine()) != null){
buffer.append(line);
}
// 关闭流
in.close();
inputStream.close();
Map<String, String> params = WXPayUtil.xmlToMap(buffer.toString());
}











网友评论