实战:第八章:支付宝Native,JSAPI支付与微信Native,JSAPI,MWEB支付实现

首先是H5的支付,先看控制层的代码

代码语言:javascript
复制
    @Autowired
    AliPayH5Bean aliPayH5Bean;
@Autowired
WxPayH5MWEB wxPayH5MWEB;

@Autowired
WxPayH5JSAPI wxPayH5JSAPI;

@LoginRequired(isNeedLogin = true)
@ApiOperation("H5去支付,支付主接口,用于控制支付流程")
@GetMapping(value = "/h5/pay" ,produces = { "application/json;charset=UTF-8" })
@ResponseBody
public Object wxPrepay(String orderSn,BigDecimal totalAmount,String code,String channelId){
    //正常开发情况下是根据用户登录的id和订单编号查询该订单是否为登录用户的订单并获取订单信息,订单价格是不会作为参数传入的,这里只是演示支付不做那么麻烦
    String result = "";
    if(PayConstant.CHANNELALIH5.equals(channelId)){
        Map<String, Object> wxMap = aliPayH5Bean.getPayMap();
        wxMap.put("out_trade_no", orderSn);
        wxMap.put("total_fee", "1".equals(aliPayH5Bean.getPayMap().get("is_real_pay")) ? totalAmount : 0.01);
        //手机网站支付,调用支付宝(网页类支付接口),这里没有写(系统调用类支付接口)
        result = PayUtils.alipayh5(wxMap);
        if (StringUtils.isEmpty(result)) {
            throw new AppException("凭证生成失败!");
        }
        return result;
    }else if(PayConstant.CHANNELWXMWEB.equals(channelId)){
        Map<String, Object> wxMap = wxPayH5MWEB.getPayMap();
        wxMap.put("out_trade_no", orderSn);
        wxMap.put("total_fee", "1".equals(wxPayH5MWEB.getPayMap().get("is_real_pay")) ? MoneyUtil.convertYuanToFen(String.valueOf(totalAmount)): 1);
        //MWEB支付是微信之外的浏览器,访问手机网站时使用的支付手段
        Map<String, String> map = PayUtils.wxh5MWEBPay(wxMap);
        if (map == null) {
            throw new AppException("支付凭证生成失败!");
        }
        return map;
    }else if(PayConstant.CHANNELWXJSAPI.equals(channelId)){
        Map<String, Object> wxMap = wxPayH5JSAPI.getPayMap();
        wxMap.put("out_trade_no", orderSn);
        wxMap.put("total_fee", "1".equals(wxPayH5MWEB.getPayMap().get("is_real_pay")) ? MoneyUtil.convertYuanToFen(String.valueOf(totalAmount)): 1);
        //JSAPI支付需要微信授权,前端引导用户到指定页面获取到微信给code,code五分钟内只能用一次,
        // 后端根据code获取openid,微信公众平台配置时需要配置支付目录与授权目录要注意域名是否与后台配置一致
        String openId = PayUtils.getOpenId(code, wxMap);
        wxMap.put("openId",openId);
        Map<String, String> map = PayUtils.wxJSAPIPay(wxMap);
        if (map == null) {
            throw new AppException("支付凭证生成失败!");
        }
        return map;
    }
    return null;
}

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152

以上三种支付方式都是需要和前端交互的网页类支付接口

更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/120583621