收藏
回答

H5纯签约 根据文档拼接得url 请求是空白页面?

public String wxH5Sign(String merNo, String pkgCode, String plan_id,String contractOuterid, RequestHandler requestHandler, String returnUrl, String clientip) {
    try {
        List<ConfigEntity> configEntityList = configService.findList(merNo, pkgCode);
        MainEntity merchantMainEntity = mainService.find(configEntityList.get(0).getProviderCode(), configEntityList.get(0).getMainNo());
        WxMerchantEntity wxMerchantEntity = wxMerchantService.queryWxMerchantInfo(merchantMainEntity.getMainNo());
        String appid = wxMerchantEntity.getAppId();
        String mch_id = wxMerchantEntity.getPartner();
        String contract_code = orderNoService.getContractCode();
        String request_serial = orderNoService.getRequestSerial();
        String contract_display_account = "正隆保险";
        String orderNo = orderNoService.getOrderNo();
        String notify_url = "https://testpp.bxlsj.com/sign/wechat/callback/" + orderNo;
        String version = "1.0";
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        SortedMap<String, String> packageParams = new TreeMap<>();
        packageParams.put("appid", appid);
        packageParams.put("mch_id", mch_id);
        packageParams.put("plan_id", plan_id);
        packageParams.put("contract_code", contract_code);
        packageParams.put("request_serial", request_serial);
        packageParams.put("contract_display_account", contract_display_account);
        packageParams.put("notify_url", notify_url);
        packageParams.put("version", version);
        packageParams.put("timestamp", timestamp);
        packageParams.put("clientip", clientip);
        packageParams.put("outerid", contractOuterid);
        requestHandler.init(appid, wxMerchantEntity.getAppsecret(), wxMerchantEntity.getPartnerkey());

        String sign = requestHandler.createHmacSign(packageParams);

        notify_url = URLEncoder.encode(notify_url);
        String url = "https://api.mch.weixin.qq.com/papay/h5entrustweb?appid=" + appid
                + "&contract_code=" + contract_code + "&contract_display_account="
                + contract_display_account + "&mch_id=" + mch_id + "&notify_url=" +
                notify_url + "&plan_id=" + plan_id + "&request_serial=" + request_serial + "&clientip=" + clientip
                + "&timestamp=" + timestamp + "&version=" + version + "&sign=" + sign+"&outerid="+contractOuterid;
        log.info("wxH5Sign H5纯签约  merNo:[{}],pkgCode:[{}] url[{}]", merNo, pkgCode, url);
        return url;
    } catch (Exception e) {
        log.error("wxH5Sign H5纯签约  merNo:[{}],pkgCode:[{}] clientip[{}]", merNo, pkgCode, clientip);
    }
    return null;
}


/**
 * 创建HmacSHA256摘要,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
 */
public String createHmacSign(SortedMap<String, String> packageParams) throws Exception {
   StringBuffer sb = new StringBuffer();
   Set es = packageParams.entrySet();
   Iterator it = es.iterator();
   while (it.hasNext()) {
      Map.Entry entry = (Map.Entry) it.next();
      String k = (String) entry.getKey();
      String v = (String) entry.getValue();
      if (null != v && !"".equals(v) && !"sign".equals(k)
            && !"key".equals(k)) {
         sb.append(k + "=" + v + "&");
      }
   }
   //sb.append("key=" + this.getKey());
   return WXPayUtil.HMACSHA256(sb.toString(),this.key);

}


/**
 * 生成 HMACSHA256
 *
 * @param data 待处理数据
 * @param key  密钥
 * @return 加密结果
 * @throws Exception
 */
public static String HMACSHA256(String data, String key) throws Exception {
    Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
    SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
    sha256_HMAC.init(secret_key);
    byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
    StringBuilder sb = new StringBuilder();
    for (byte item : array) {
        sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
    }
    return sb.toString().toUpperCase();
}



回答关注问题邀请回答
收藏
登录 后发表内容
问题标签