springboot整合微信小程序支付

1 篇文章 0 订阅
1 篇文章 0 订阅

springboot整合微信小程序支付

/**
     * 发起支付
     *
     * @param request request
     * @return R
     */
    @PostMapping("/doPay")
    public R doPay(@RequestBody TradeRequest request) throws Exception {
        request.setUserId(getAppUserId());
        return R.success(orderService.pay(request));
    }
@Override
    @Transactional(rollbackFor = Exception.class)
    public PayPackage pay(TradeRequest request) throws Exception {
        ResourceCodeEntity codeEntity = resourceCodeMapper.selectOne(Wrappers.lambdaQuery(ResourceCodeEntity.class)
                .eq(ResourceCodeEntity::getId, request.getResourceId()));
        if (Objects.isNull(codeEntity)) {
            throw new ServiceException("该源码已下架");
        }
        // 查询用户信息
        UserEntity userEntity = userMapper.selectById(request.getUserId());
        if (Objects.isNull(userEntity)) {
            throw new ServiceException("该用户已被封禁,请联系管理员");
        }
        // 创建订单
        OrderEntity order = createAppOrder(request.getUserId(), codeEntity);
        //  一元=100分
        int totalMoney = new BigDecimal(100).multiply(order.getTotalFee()).intValue();
        // 生成预支付ID
        String prepayId = wxPayManager.unified(order.getOrderNo(), totalMoney, userEntity.getOpenId(), codeEntity.getTitle());
        PayPackage payPackage = wxPayManager.miniProgramPay(prepayId);
        payPackage.setOrderNo(order.getOrderNo());
        log.info("payPackage===>" + JSONObject.toJSONString(payPackage));
        return payPackage;
    }
    ```

public String unified(String orderNo, Integer total, String openId, String title) throws IOException {
        HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi");
        httpPost.addHeader("Accept", "application/json");
        httpPost.addHeader("Content-type", "application/json; charset=utf-8");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode rootNode = objectMapper.createObjectNode();
        rootNode.put("mchid", config.getMchId())
                .put("appid", config.getAppId())
                .put("description", title)
                .put("notify_url", config.getNotifyUrl())
                .put("out_trade_no", orderNo);
        rootNode.putObject("amount")
                .put("total", total);
        rootNode.putObject("payer")
                .put("openid", openId);
        objectMapper.writeValue(bos, rootNode);
        httpPost.setEntity(new StringEntity(bos.toString("UTF-8"), "UTF-8"));
        CloseableHttpResponse response = httpClient().execute(httpPost);
        String bodyAsString = EntityUtils.toString(response.getEntity());
        log.info("统一下单:{}", bodyAsString);
        JSONObject jsonObject = JSONObject.parseObject(bodyAsString);
        String code = jsonObject.getString("code");
        if (StringUtils.hasLength(code) && "ORDERPAID".equals(code)) {
            throw new ServiceException(RS.ACCOUNT_DISABLED);
        }
        return jsonObject.getString("prepay_id");
    }
 /**
     * 小程序支付
     *
     * @param prepayId 预支付ID
     * @return PayPackage
     * @throws Exception 异常
     */
    public PayPackage miniProgramPay(String prepayId) throws Exception {
        PayPackage payPackage = new PayPackage();
        String timeStamp = System.currentTimeMillis() / 1000 + "";
        payPackage.setTimeStamp(timeStamp);
        payPackage.set_package("prepay_id=" + prepayId);
        payPackage.setSignType("RSA");
        String randomStr = RandomUtils.randomStr(30);
        String signStr = config.getAppId() + "\n" + timeStamp + "\n" + randomStr + "\n" + payPackage.get_package() + "\n";
        payPackage.setPaySign(sign(signStr));
        payPackage.setAppId(config.getAppId());
        payPackage.setNonceStr(randomStr);
        return payPackage;
    }


具体项目代码如下:https://gitee.com/Marsfactory/source-byte

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值