文档地址:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1
封装的工具类
package com.qf.fmall.utils;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.HttpRequest;
import org.apache.shiro.crypto.hash.Md5Hash;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;
public class WeixinPayUtils {
public static final String orderSubmit="https://api.mch.weixin.qq.com/pay/unifiedorder";
public static final String orderQueryUrl="https://api.mch.weixin.qq.com/pay/orderquery";
public static final String appkey="sbNCm1JnevqI36LrEaxFwcaT0hkGxFnC";
public static void main(String[] args) {
String s = weixinPay("sadsadasa", "wqeqweqeq");
System.out.println(s);
}
public static String weixinPay(String oid,String describe){
//1. 组织请求map,必传字段如下
//这里可有序可以无序,但是计算签名的时候必须有序,规定!按Key排序
TreeMap treeMap = new TreeMap<>();
treeMap.put("appid","wx632c8f211f8122c6");//商户的应用ID
treeMap.put("mch_id","1497984412"); //商户号
treeMap.put("notify_url","http://47.118.45.73:8080/pay/callback"); // 商户提供的回调地址
String randomstr = UUID.randomUUID().toString().replaceAll("-", ""); //生成随机数
treeMap.put("nonce_str",randomstr); // 长度32个字符的随机字符串,干扰项
treeMap.put("body",describe); // 订单描述信息
treeMap.put("out_trade_no",oid); // 订单编号
treeMap.put("total_fee","1"); // 1 分钱
treeMap.put("trade_type","NATIVE"); // 支付类型
treeMap.put("fee_type","CNY"); // 币种
treeMap.put("sign_type","MD5"); // 签名算法
//计算签名
String tempString = treeMap.toString().replace("{", "").replace("}", "").replace(", ","&")+"&key="+appkey;
String sign = new Md5Hash(tempString).toHex().toUpperCase();
treeMap.put("sign",sign);
//map转XML字符串
String xml = XmlUtil.mapToXmlStr(treeMap);
//用Hutool工具类发送post请求
String result = HttpRequest.post(orderSubmit).body(xml).execute().body();
// xml str ----> map
Map resultMap = XmlUtil.xmlToMap(result);
String url = (String) resultMap.get("code_url");
return url;
}
public static String queryStatus(String orderId) {
//1. 组织请求map,必传字段如下
//这里可有序可以无序,但是计算签名的时候必须有序,规定!按Key排序
TreeMap treeMap = new TreeMap<>();
treeMap.put("appid","wx632c8f211f8122c6");//商户的应用ID
treeMap.put("mch_id","1497984412"); //商户号
String randomstr = UUID.randomUUID().toString().replaceAll("-", ""); //生成随机数
treeMap.put("nonce_str",randomstr); // 长度32个字符的随机字符串,干扰项
treeMap.put("out_trade_no",orderId); // 订单编号
treeMap.put("sign_type","MD5"); // 签名算法
//计算签名
String tempString = treeMap.toString().replace("{", "").replace("}", "").replace(", ","&")+"&key="+appkey;
String sign = new Md5Hash(tempString).toHex().toUpperCase();
treeMap.put("sign",sign);
//map转XML字符串
String xml = XmlUtil.mapToXmlStr(treeMap);
//用Hutool工具类发送post请求
String result = HttpRequest.post(orderQueryUrl).body(xml).execute().body();
// xml str ----> map
Map resultMap = XmlUtil.xmlToMap(result);
String trade_state = (String) resultMap.get("trade_state");
return trade_state;
}
}
controller
package com.qf.fmall.controller;
import com.qf.fmall.common.ResultVo;
import com.qf.fmall.entity.Orders;
import com.qf.fmall.service.IOrdersService;
import com.qf.fmall.utils.FmallConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.HashMap;
/**
*
* 订单 前端控制器
*
*
* @author jmj
* @since 2023-08-24
*/
@RestController
@RequestMapping("/order")
@CrossOrigin
@Slf4j
public class OrdersController {
@Autowired
private IOrdersService ordersService;
@PostMapping("/add")
public ResultVo add(Integer[] cids,@RequestBody Orders orders) throws Exception {
log.info("入参为:cid={},orders={}", Arrays.toString(cids),orders);
HashMap map= ordersService.addOrder(cids,orders);
return ResultVo.vo(FmallConstants.SUCCESS_CODE,FmallConstants.SUCCESS_MSG,map);
}
@GetMapping("/status/{orderId}")
public ResultVo status(@PathVariable("orderId") String orderId){
log.info("入参为:orderId={}",orderId);
String s= ordersService.status(orderId);
log.info("传回来的状态:{}",s);
return ResultVo.vo(FmallConstants.SUCCESS_CODE,FmallConstants.SUCCESS_MSG,s);
}
}
service
package com.qf.fmall.service.impl;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.fmall.entity.*;
import com.qf.fmall.mapper.OrdersMapper;
import com.qf.fmall.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fmall.utils.WeixinPayUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
/**
*
* 订单 服务实现类
*
*
* @author jmj
* @since 2023-08-24
*/
@Service
@Slf4j
public class OrdersServiceImpl extends ServiceImpl implements IOrdersService {
@Autowired
private IShoppingCartService shoppingCartService;
@Autowired
private IProductSkuService iProductSkuService;
@Autowired
private IProductService iProductService;
@Autowired
private IOrderItemService iOrderItemService;
@Transactional(rollbackFor = Exception.class)
@Override
public synchronized HashMap addOrder(Integer[] cids, Orders orders) throws Exception {
//1. 查询用户购物车中的对应的商品套餐库存量是否还足够
List
com.github.wxpay
wxpay-sdk
0.0.3
package com.qf.fmall2302.wxpay;
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConfig;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class WxSdkUtils {
public String getPayUrl(String orderid,String desc,Long amount) throws Exception {
// 创建sdk的核心对象
WXPay wxPay = new WXPay(new MyWxConfig());
HashMap map = new HashMap<>();
String randomstr = UUID.randomUUID().toString().replaceAll("-", "");
map.put("nonce_str",randomstr); // 长度32个字符的随机字符串,干扰项
map.put("body",desc); // 订单描述信息
map.put("out_trade_no",orderid); // 订单编号
map.put("total_fee","1"); // 1 分钱
map.put("trade_type","NATIVE"); // 支付类型
map.put("fee_type","CNY"); // 币种
map.put("notify_url","http://47.118.45.73:8080/pay/callback"); // 商户提供的回调地址
map.put("sign_type","MD5"); // 签名算法
Map result = wxPay.unifiedOrder(map);
return result.get("code_url");
}
public static String queryStatus(String orderid) throws Exception {
WXPay wxPay = new WXPay(new MyWxConfig());
HashMap map = new HashMap<>();
map.put("out_trade_no",orderid);
Map result = wxPay.orderQuery(map);
return result.get("trade_state");
}
public static void main(String[] args) throws Exception {
String orderid = "202308241617987";
WXPay wxPay = new WXPay(new MyWxConfig());
HashMap map = new HashMap<>();
map.put("out_trade_no",orderid);
Map result = wxPay.orderQuery(map);
System.out.println(result);
}
}
package com.qf.fmall2302.wxpay;
import com.github.wxpay.sdk.WXPayConfig;
import java.io.InputStream;
public class MyWxConfig implements WXPayConfig {
public static final String appid = "wx632c8f211f8122c6";
public static final String mch_id = "1497984412";
public static final String appkey = "sbNCm1JnevqI36LrEaxFwcaT0hkGxFnC";
@Override
public String getAppID() {
return appid;
}
@Override
public String getMchID() {
return mch_id;
}
@Override
public String getKey() {
return appkey;
}
@Override
public InputStream getCertStream() {
return null;
}
@Override
public int getHttpConnectTimeoutMs() {
return 0;
}
@Override
public int getHttpReadTimeoutMs() {
return 0;
}
}