微信支付的实现

public void report(String domain, long elapsedTimeMillis, Exception ex) {

}

public DomainInfo getDomain(WXPayConfig config) {

return new DomainInfo(“api.mch.weixin.qq.com”,true);

}

}

}

添加依赖

org.springframework.boot

spring-boot-starter-web

org.slf4j

slf4j-log4j12

org.springframework.boot

spring-boot-starter-web

ch.qos.logback

logback-classic

org.apache.httpcomponents

httpclient

4.5.3

org.slf4j

slf4j-api

1.7.21

org.slf4j

slf4j-simple

1.7.21

com.google.zxing

core

3.3.3

com.google.zxing

javase

3.3.3

org.springframework.boot

spring-boot-starter-thymeleaf

下单、生成二维码

/**

  • 微信支付Service

*/

@Service

public class PayService {

/**

  • 下单

  • @param goodsId 商品id

  • @param price 价格

  • @return 二维码的URL

*/

public Map<String, String> makeOrder(String goodsId, Long price) throws Exception {

//创建支付对象

MyWXPayConfig config = new MyWXPayConfig();

WXPay wxPay = new WXPay(config);

Map<String,String> map = new HashMap<>();

map.put(“appid”,config.getAppID());

map.put(“mch_id”,config.getMchID());

map.put(“device_info”,“WEB”);

map.put(“nonce_str”, UUID.randomUUID().toString().replace(“-”,“”));

map.put(“body”,“商城购物”);

String tradeNo = UUID.randomUUID().toString().replace(“-”, “”);

map.put(“out_trade_no”, tradeNo);

map.put(“fee_type”,“CNY”);

map.put(“total_fee”,String.valueOf(price));

map.put(“notify_url”,“http://n538ep.natappfree.cc/order/notify”); //微信对商户后台的回调接口

map.put(“trade_type”,“NATIVE”);

map.put(“product_id”,goodsId);

//执行统一下单

Map<String, String> result = wxPay.unifiedOrder(map);

System.out.println(“result:”+result);

//保存订单号

result.put(“trade_no”,tradeNo);

return result;

}

/**

  • 生成二维码

  • @param url

  • @param response

*/

public void makeQRCode(String url, HttpServletResponse response){

//通过支付链接生成二维码

HashMap<EncodeHintType, Object> hints = new HashMap<>();

hints.put(EncodeHintType.CHARACTER_SET, “UTF-8”);

hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);

hints.put(EncodeHintType.MARGIN, 2);

try {

BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 200, 20 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 0, hints);

MatrixToImageWriter.writeToStream(bitMatrix, “PNG”, response.getOutputStream());

System.out.println(“创建二维码完成”);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

  • 检查订单状态

  • @param tradeNo

  • @return

  • @throws Exception

*/

public String checkOrder(String tradeNo) throws Exception {

MyWXPayConfig config = new MyWXPayConfig();

String str =

“”+

“”+config.getAppID()+“”+

“<mch_id>”+config.getMchID()+“</mch_id>”+

“<nonce_str>”+UUID.randomUUID().toString().replace(“-”,“”)+“</nonce_str>”+

“<out_trade_no>”+tradeNo+“</out_trade_no>”+

“5E00F9F72173C9449F802411E36208734B8138870ED3F66D8E2821D55B317078”+

“”;

WXPay pay = new WXPay(config);

Map<String,String> map = WXPayUtil.xmlToMap(str);

Map<String, String> map2 = pay.orderQuery(map);

String state = map2.get(“trade_state”);

System.out.println(“订单”+tradeNo+“,状态”+state);

return state;

}

}

订单控制器

@RestController

@RequestMapping(“/order”)

public class OrderController {

@Autowired

private PayService payService;

private String tradeNo;

//二维码生成

@GetMapping(“/code”)

public void qrcode(@RequestParam(“goodsId”)String goodsId,

@RequestParam(“price”)Long price,

HttpServletResponse response){

try {

Map<String,String> map = payService.makeOrder(goodsId, price);

payService.makeQRCode(map.get(“code_url”),response);

System.out.println(“生成订单号:” + map.get(“trade_no”));

tradeNo = map.get(“trade_no”);

} catch (Exception e) {

e.printStackTrace();

}

}

//支付后通知

@PostMapping(“/notify”)

public void notify(HttpServletRequest request,HttpServletResponse response) throws Exception {

//获得微信传来的xml字符串

String str = Streams.asString(request.getInputStream());

//将字符串xml转换为Map

Map<String, String> map = WXPayUtil.xmlToMap(str);

//读取订单号

String no = map.get(“out_trade_no”);

//模拟修改商户后台数据库订单状态

System.out.println(“更新订单状态:”+no);

//给微信发送消息

response.getWriter().println(“\n” +

" <return_code><![CDATA[SUCCESS]]></return_code>\n" +

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值