• 微信支付v2


    文档: https://pay.weixin.qq.com/wiki/doc/api/index.html

    微信小程序https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1

    需要一个微信认证后的小程序,,还需要一个,在微信商户平台,,开通的商户,,将商户和小程序绑定到一起,,

    用户在微信小程序上下单:

    • 需要知道是哪个用户下的单,,也就是openid
    • 需要知道小程序id,,商户id,,根据商户的秘钥key,生成一个签名

    签名是为了防止数据别篡改,,,

    怎么获取openid:

      @PostMapping("/login")
        public Map<String,String> login(@RequestBody @Valid WxLoginForm loginForm){
    
            String url = "https://api.weixin.qq.com/sns/jscode2session";
    
            HashMap<String, Object> map = new HashMap<>();
            map.put("appid",appid);
            map.put("secret",secret);
            map.put("js_code",loginForm.getCode());
            map.put("grant_type","authorization_code");
    
            String response = HttpUtil.post(url, map);
    
            JSONObject obj = JSONUtil.parseObj(response);
            String openid = obj.getStr("openid");
    
    
            if (openid == null || openid.length()==0){
                throw new ParameterException(99999);
            }
    
            User user = userRepository.findByOpenid(openid);
            Long uid;
            if (user ==null){
                // 注册
                User newUser = new User();
                BeanUtils.copyProperties(loginForm,newUser);
                newUser.setOpenid(openid);
                userRepository.save(newUser);
                uid = newUser.getId();
            }else{
                // 登录
    
                 uid = user.getId();
    
            }
            // 生成令牌
    
            String token = JwtToken.makeToken(uid);
    
            HashMap<String, String> map1 = new HashMap<>();
            map1.put("token",token);
    
            // 根据 主键id生成token
    
    
    
            return map1;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    怎么向微信统一下单:
    微信SDK中有一个 WXPayConfig抽象类,,继承这个类,,将你的wx支付的配置写进去

    // ccWxPayConfig自己的wx配置
     WXPay wxPay = new WXPay(ccWxPayConfig);
     // 下单成功会返回一个 prepay_id
     Map<String, String> result = wxPay.unifiedOrder(map);
    // 将prepay_id更新到order中
    // 生成前端的数字签名,, 前端生成签名比较麻烦,后端生成之后传递给前端
     String paySign = WXPayUtil.generateSignature(map2, key);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    前端根据回传的信息,请求微信服务器获取支付信息,,,用户确认金额后,选择支付或者取消支付

    支付完成后,微信会支付结果通知在这里插入图片描述

    如果没有接到通知,,也可以自己主动发出通知:

    
        /**
         * 主动 去 微信 查询支付状态
         */
        @PostMapping("/updateOrderStatus")
        @ScopeLevel
        @Transactional
        public void updateOrderStatus(String orderNo) throws Exception {
            // 验证这个订单是不是这个用户的
    
            Long uid = LocalUser.getUser().getId();
            Long orderId = 1L;
    
            Order order = orderRepository.findFirstByUserIdAndId(uid, orderId).orElseThrow(() -> new ServerErrorException(9999));
    
    
            HashMap<String, String> map = new HashMap<>();
            map.put("appid",appid);
            map.put("mch_id",mchId);
            map.put("out_trade_no",order.getOrderNo());
            map.put("nonce_str",WXPayUtil.generateNonceStr());
            String sign = WXPayUtil.generateSignature(map, key);
            map.put("sign",sign);
    
            WXPay wxPay = new WXPay(ccWxPayConfig);
            Map<String, String> result = wxPay.orderQuery(map);
    
    
            String returnCode = result.get("return_code");
            String resultCode = result.get("result_code");
            if ("SUCCESS".equals(returnCode) && "SUCCESS".equals(resultCode)){
                String tradeState = result.get("trade_state");
                if ("SUCCESS".equals(tradeState)){
                    // 交易成功,,更新状态
    //                orderRepository.
                    orderRepository.updateOrderStatus(order.getOrderNo(),2);
    
                }else{
    //                return new UnifyResponse()
                    throw new ServerErrorException(9999);
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
  • 相关阅读:
    权限提升:Mysql 数据库 .(UDF || 启动项 || 反弹)
    JDK8函数式编程
    8月23日
    HTML简单实现v-if与v-for与v-model
    重温以太坊的升级之路
    Linux开发讲课18--- “>file 2>&1“ 和 “2>&1 >file“ 的区别
    以软硬协同最佳实践构建智能算力,燧原科技WAIC大会上发布云燧智算机
    opencv 入门学习
    淘宝/天猫API:item_cat_get-获得淘宝商品类目
    OpenAI推出首个AI视频模型Sora:重塑视频创作与体验
  • 原文地址:https://blog.csdn.net/qq_36022463/article/details/133757388