QQ小程序支付 调起微信支付

前言(如果做过微信小程序支付对接其实就是修改参数)

由于公司业务需要,最近这段时间对接了QQ小程序支付QQ小程序内微信支付,在微信H5统一下单和qq小程序当中反复横跳,求助官方、论坛里面遨游、发送邮件(邮件已经发送三天,但是没有任何回复)。

准备工作

语言java

# 微信支付配置
wxpay:
  config:
    # 微信公众号身份的唯一标识
    wechatAppid: 
    # 微信公钥
    wechatMchid: 
    # 商户支付密钥Key
    wechatAppkey: 
    # JSAPI接口中获取openid
    wechatAppsecret: 
    # 微信通讯token值
    wechatToken: 
    # 回调地址
    notifyUrl: 
    # domain地址
    domain: 
https://api.q.qq.com/wxpay/unifiedorder?appid=APPID6&access_token=access_token&&real_notify_url=wxpayCallback

domain参数说明:

1、access_token可以通过获取小程序全局唯一后台接口调用凭据(access_token)。调调用绝大多数后台接口时都需使用 access_token,开发者需要进行妥善保存

GET https://api.q.qq.com/api/getToken?grant_type=client_credential&appid=APPID&secret=APPSECRET

2、real_notify_url 支付成功后 qq小程序异步回调的地址 (记住使用 URLEncoder.encode()方法转化url地址)


wx:
  open:
    # 微信开放平台 appid
    app_id: 
    # 微信开放平台 appsecret
    app_secret: 
    # 微信开放平台 重定向url
    redirect_url:

代码

@Override
public ApiResponse<String> createPayQrCode(String orderNo, String subject, String amount, String payType, int bizType, String ip, String hbFqNum, String isBalance) {
    final String huabei = "huabei";
    if (StrUtil.isBlank(orderNo)) {
        return ApiResponse.fail("订单不存在");
    }
    PayBizDataParam payBizDataParam = new PayBizDataParam(bizType);
    payBizDataParam.setIsBalance(isBalance);
    payBizDataParam.setPayType(payType);
    ApiResponse<String> payQrCodeResponse = null;
//支付宝支付
    if (OrderPayTypeEnum.ALIPAY.getCode().equals(payType)) {
        payQrCodeResponse = alipayConsumerService.getPayQrCode(subject, orderNo,
                amount, null, new HashMap<>(1), payBizDataParam);
    } else if (huabei.equals(payType)) {
//支付宝花呗支付
        Map<String, String> params = new HashMap<>(1);
        params.put("hbFqNum", hbFqNum);
        payQrCodeResponse = huabeiConsumerService.getPayQrCode(subject, orderNo,
                amount, null, params, payBizDataParam);
    } else if (OrderPayTypeEnum.WXPAY.getCode().equals(payType)) {
//微信扫码支付
        Map<String, String> params = new HashMap<>(1);
        params.put("spbill_create_ip", ip);
        params.put("trade_type", "NATIVE");
        payQrCodeResponse = wxpayConsumerService.getPayQrCode(subject, orderNo, amount, null, params, payBizDataParam);
    }else if (OrderPayTypeEnum.WXH5.getCode().equals(payType)) {
//微信H5 qq小程序调用
        Map<String, String> params = new HashMap<>(2);
        params.put("spbill_create_ip", ip);
        params.put("trade_type", "MWEB");
        payQrCodeResponse = wxpayConsumerService.getPayQrCode(subject, orderNo, amount, null, params, payBizDataParam);
    }
    return payQrCodeResponse;
}
(二维码支付和H5都在里面 extMap里面还设置了不同的参数)
public ApiResponse<String> getPayQrCode(String subject, String outTradeNo, String totalAmount, String notifyUrl, Map<String, String> extMap, PayBizDataParam payBizDataParam) {
    // 元转分
    int totalFee = PriceUtil.yuanToFen(totalAmount).intValue();
    Map<String, String> reqData = new HashMap<>(8);
    reqData.put("body", subject);
    outTradeNo = outTradeNo + "XCY" + OrderUtil.getNumberRandomStr(4);
    reqData.put("out_trade_no", outTradeNo);
    reqData.put("total_fee", totalFee + "");
    reqData.put("attach", payBizDataParam.toParams());

    if (OrderPayTypeEnum.WXH5.getCode().equals(payBizDataParam.getPayType())) { 
         //获取access_token
        Map<String, Object> accessTokenMap = JSON.parseObject(HttpUtil.get("https://api.q.qq.com/api/getToken?grant_type=client_credential&appid=" + appId + "&secret=" + secret), Map.class);
        //判断是否获取成功
   if (!((Integer) accessTokenMap.get("errcode") == 0)) {
            return ApiResponse.fail(RespCodeConstant.PAY_WXPAY_ERROR);
        }
        String accessToken = (String) accessTokenMap.get("access_token");
        Map<String, String> h5Map = new HashMap<>(3);
        Map<String, String> h5InfoMap = new HashMap<>(5);
        h5Map.put("payer_client_ip", extMap.get("spbill_create_ip"));
        h5InfoMap.put("type", "Wap");
        h5InfoMap.put("wap_url", "开通H5支付绑定的IP地址");
        h5InfoMap.put("wap_name", "绑定应用的名称");
        h5Map.put("h5_info", JSONObject.toJSONString(h5InfoMap));
        reqData.put("scene_info", JSONObject.toJSONString(h5Map));
        reqData.putAll(extMap);
        try {
            Map<String, String> respData = unifiedOrderH5(reqData, WxPayConstants.H5NOTIFY_URL, accessToken);
            log.info("微信H5支付信息{}", JSONObject.toJSONString(respData));
            String returnCode = respData.get(RETURN_CODE);
            String resultCode = respData.get(RESULT_CODE);
            if (returnCode.equals(WxPayConstants.SUCCESS) && resultCode.equals(WxPayConstants.SUCCESS)) {
                String h5Url = respData.get("mweb_url");
                return ApiResponse.ok(h5Url);
            } else if (returnCode.equals(WxPayConstants.FAIL)) {
                return ApiResponse.fail(RespCodeConstant.PAY_WXPAY_ERROR);
            }
        } catch (Exception e) {
            log.error("微信支付异常", e);
        }
        return ApiResponse.fail(RespCodeConstant.PAY_WXPAY_ERROR);

    } else {
        reqData.putAll(extMap);
        try {
            Map<String, String> respData = unifiedOrder(reqData, notifyUrl);
            log.info(JSONObject.toJSONString(respData));
            String returnCode = respData.get(RETURN_CODE);
            String resultCode = respData.get(RESULT_CODE);
            if (returnCode.equals(WxPayConstants.SUCCESS) && resultCode.equals(WxPayConstants.SUCCESS)) {
                String codeUrl = respData.get("code_url");
                byte[] qrCodes = QrCodeUtil.generatePng(codeUrl, 200, 200);
                return ApiResponse.ok(Base64.encode(qrCodes));
            } else if (returnCode.equals(WxPayConstants.FAIL)) {
                return ApiResponse.fail(RespCodeConstant.PAY_WXPAY_ERROR);
            }
        } catch (Exception e) {
            log.error("微信支付异常", e);
        }
        return ApiResponse.fail(RespCodeConstant.PAY_WXPAY_ERROR);
    }
}

如果想学习支付调用可以访问gitee上的一个开源项目:https://gitee.com/javen205/IJPay?_from=gitee_search

聚合支付,IJPay 让支付触手可及,封装了微信支付、QQ支付、支付宝支付、京东支付、银联支付、PayPal支付等常用的支付方式以及各种常用的接口。不依赖任何第三方 mvc 框架,仅仅作为工具使用简单快速完成支付模块的开发,可轻松嵌入到任何系统里

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值