com.github.binarywang.wxpay.util.SignUtils Java Examples

The following examples show how to use com.github.binarywang.wxpay.util.SignUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaseWxPayServiceImpl.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
@Override
public String createScanPayQrcodeMode1(String productId) {
  //weixin://wxpay/bizpayurl?sign=XXXXX&appid=XXXXX&mch_id=XXXXX&product_id=XXXXXX&time_stamp=XXXXXX&nonce_str=XXXXX
  StringBuilder codeUrl = new StringBuilder("weixin://wxpay/bizpayurl?");
  Map<String, String> params = Maps.newHashMap();
  params.put("appid", this.getConfig().getAppId());
  params.put("mch_id", this.getConfig().getMchId());
  params.put("product_id", productId);
  //这里需要秒,10位数字
  params.put("time_stamp", String.valueOf(System.currentTimeMillis() / 1000));
  params.put("nonce_str", String.valueOf(System.currentTimeMillis()));

  String sign = SignUtils.createSign(params, null, this.getConfig().getMchKey(), false);
  params.put("sign", sign);

  for (String key : params.keySet()) {
    codeUrl.append(key).append("=").append(params.get(key)).append("&");
  }

  String content = codeUrl.toString().substring(0, codeUrl.length() - 1);
  log.debug("扫码支付模式一生成二维码的URL:{}", content);
  return content;
}
 
Example #2
Source File: EntPayServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public String getPublicKey() throws WxPayException {
  WxPayDefaultRequest request = new WxPayDefaultRequest();
  request.setMchId(this.payService.getConfig().getMchId());
  request.setNonceStr(String.valueOf(System.currentTimeMillis()));
  request.setSign(SignUtils.createSign(request, null, this.payService.getConfig().getMchKey(),
    true));

  String url = "https://fraud.mch.weixin.qq.com/risk/getpublickey";
  String responseContent = this.payService.post(url, request.toXML(), true);
  GetPublicKeyResult result = BaseWxPayResult.fromXML(responseContent, GetPublicKeyResult.class);
  result.checkResult(this.payService, request.getSignType(), true);
  return result.getPubKey();
}
 
Example #3
Source File: WxPayOrderNotifyResult.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, String> toMap() {
  Map<String, String> resultMap = SignUtils.xmlBean2Map(this);
  if (this.getCouponCount() != null && this.getCouponCount() > 0) {
    for (int i = 0; i < this.getCouponCount(); i++) {
      WxPayOrderNotifyCoupon coupon = couponList.get(i);
      resultMap.putAll(coupon.toMap(i));
    }
  }
  return resultMap;
}
 
Example #4
Source File: BaseWxPayResult.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
/**
 * 校验返回结果签名.
 *
 * @param signType     签名类型
 * @param checkSuccess 是否同时检查结果是否成功
 */
public void checkResult(WxPayService wxPayService, String signType, boolean checkSuccess) throws WxPayException {
  //校验返回结果签名
  Map<String, String> map = toMap();
  if (getSign() != null && !SignUtils.checkSign(map, signType, wxPayService.getConfig().getMchKey())) {
    this.getLogger().debug("校验结果签名失败,参数:{}", map);
    throw new WxPayException("参数格式校验错误!");
  }

  //校验结果是否成功
  if (checkSuccess) {
    List<String> successStrings = Lists.newArrayList("SUCCESS", "");
    if (!successStrings.contains(StringUtils.trimToEmpty(getReturnCode()).toUpperCase())
      || !successStrings.contains(StringUtils.trimToEmpty(getResultCode()).toUpperCase())) {
      StringBuilder errorMsg = new StringBuilder();
      if (getReturnCode() != null) {
        errorMsg.append("返回代码:").append(getReturnCode());
      }
      if (getReturnMsg() != null) {
        errorMsg.append(",返回信息:").append(getReturnMsg());
      }
      if (getResultCode() != null) {
        errorMsg.append(",结果代码:").append(getResultCode());
      }
      if (getErrCode() != null) {
        errorMsg.append(",错误代码:").append(getErrCode());
      }
      if (getErrCodeDes() != null) {
        errorMsg.append(",错误详情:").append(getErrCodeDes());
      }

      this.getLogger().error("\n结果业务代码异常,返回结果:{},\n{}", map, errorMsg.toString());
      throw WxPayException.from(this);
    }
  }
}
 
Example #5
Source File: BaseWxPayServiceImpl.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException {
  WxPayUnifiedOrderResult unifiedOrderResult = this.unifiedOrder(request);
  String prepayId = unifiedOrderResult.getPrepayId();
  if (StringUtils.isBlank(prepayId)) {
    throw new WxPayException(String.format("无法获取prepay id,错误代码: '%s',信息:%s。",
      unifiedOrderResult.getErrCode(), unifiedOrderResult.getErrCodeDes()));
  }

  String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
  String nonceStr = String.valueOf(System.currentTimeMillis());
  switch (request.getTradeType()) {
    case TradeType.MWEB: {
      return (T) new WxPayMwebOrderResult(unifiedOrderResult.getMwebUrl());
    }

    case TradeType.NATIVE: {
      return (T) new WxPayNativeOrderResult(unifiedOrderResult.getCodeURL());
    }

    case TradeType.APP: {
      // APP支付绑定的是微信开放平台上的账号,APPID为开放平台上绑定APP后发放的参数
      String appId = unifiedOrderResult.getAppid();
      if (StringUtils.isNotEmpty(unifiedOrderResult.getSubAppId())) {
        appId = unifiedOrderResult.getSubAppId();
      }

      Map<String, String> configMap = new HashMap<>(8);
      // 此map用于参与调起sdk支付的二次签名,格式全小写,timestamp只能是10位,格式固定,切勿修改
      String partnerId;
      if (StringUtils.isEmpty(request.getMchId())) {
        partnerId = this.getConfig().getMchId();
      } else {
        partnerId = request.getMchId();
      }

      configMap.put("prepayid", prepayId);
      configMap.put("partnerid", partnerId);
      String packageValue = "Sign=WXPay";
      configMap.put("package", packageValue);
      configMap.put("timestamp", timestamp);
      configMap.put("noncestr", nonceStr);
      configMap.put("appid", appId);

      final WxPayAppOrderResult result = WxPayAppOrderResult.builder()
        .sign(SignUtils.createSign(configMap, null, this.getConfig().getMchKey(), false))
        .prepayId(prepayId)
        .partnerId(partnerId)
        .appId(appId)
        .packageValue(packageValue)
        .timeStamp(timestamp)
        .nonceStr(nonceStr)
        .build();
      return (T) result;
    }

    case TradeType.JSAPI: {
      String signType = SignType.MD5;
      String appid = unifiedOrderResult.getAppid();
      if (StringUtils.isNotEmpty(unifiedOrderResult.getSubAppId())) {
        appid = unifiedOrderResult.getSubAppId();
      }

      WxPayMpOrderResult payResult = WxPayMpOrderResult.builder()
        .appId(appid)
        .timeStamp(timestamp)
        .nonceStr(nonceStr)
        .packageValue("prepay_id=" + prepayId)
        .signType(signType)
        .build();

      payResult.setPaySign(SignUtils.createSign(payResult, signType, this.getConfig().getMchKey(), false));
      return (T) payResult;
    }

    default: {
      throw new WxPayException("该交易类型暂不支持");
    }
  }

}
 
Example #6
Source File: BaseWxPayServiceImpl.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
@Deprecated
public Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws WxPayException {
  WxPayUnifiedOrderResult unifiedOrderResult = this.unifiedOrder(request);
  String prepayId = unifiedOrderResult.getPrepayId();
  if (StringUtils.isBlank(prepayId)) {
    throw new RuntimeException(String.format("无法获取prepay id,错误代码: '%s',信息:%s。",
      unifiedOrderResult.getErrCode(), unifiedOrderResult.getErrCodeDes()));
  }

  Map<String, String> payInfo = new HashMap<>();
  String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
  String nonceStr = String.valueOf(System.currentTimeMillis());
  if (TradeType.NATIVE.equals(request.getTradeType())) {
    payInfo.put("codeUrl", unifiedOrderResult.getCodeURL());
  } else if (TradeType.APP.equals(request.getTradeType())) {
    // APP支付绑定的是微信开放平台上的账号,APPID为开放平台上绑定APP后发放的参数
    String appId = getConfig().getAppId();
    Map<String, String> configMap = new HashMap<>();
    // 此map用于参与调起sdk支付的二次签名,格式全小写,timestamp只能是10位,格式固定,切勿修改
    String partnerId = getConfig().getMchId();
    configMap.put("prepayid", prepayId);
    configMap.put("partnerid", partnerId);
    String packageValue = "Sign=WXPay";
    configMap.put("package", packageValue);
    configMap.put("timestamp", timestamp);
    configMap.put("noncestr", nonceStr);
    configMap.put("appid", appId);
    // 此map用于客户端与微信服务器交互
    payInfo.put("sign", SignUtils.createSign(configMap, null, this.getConfig().getMchKey(), false));
    payInfo.put("prepayId", prepayId);
    payInfo.put("partnerId", partnerId);
    payInfo.put("appId", appId);
    payInfo.put("packageValue", packageValue);
    payInfo.put("timeStamp", timestamp);
    payInfo.put("nonceStr", nonceStr);
  } else if (TradeType.JSAPI.equals(request.getTradeType())) {
    payInfo.put("appId", unifiedOrderResult.getAppid());
    // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
    payInfo.put("timeStamp", timestamp);
    payInfo.put("nonceStr", nonceStr);
    payInfo.put("package", "prepay_id=" + prepayId);
    payInfo.put("signType", SignType.MD5);
    payInfo.put("paySign", SignUtils.createSign(payInfo, null, this.getConfig().getMchKey(), false));
  }

  return payInfo;
}
 
Example #7
Source File: BaseWxPayRequest.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
/**
 * <pre>
 * 检查参数,并设置签名
 * 1、检查参数(注意:子类实现需要检查参数的而外功能时,请在调用父类的方法前进行相应判断)
 * 2、补充系统参数,如果未传入则从配置里读取
 * 3、生成签名,并设置进去
 * </pre>
 *
 * @param config 支付配置对象,用于读取相应系统配置信息
 */
public void checkAndSign(WxPayConfig config) throws WxPayException {
  this.checkFields();

  if (!ignoreAppid()) {
    if (StringUtils.isBlank(getAppid())) {
      this.setAppid(config.getAppId());
    }
  }

  if (StringUtils.isBlank(getMchId())) {
    this.setMchId(config.getMchId());
  }

  if (StringUtils.isBlank(getSubAppId())) {
    this.setSubAppId(config.getSubAppId());
  }

  if (StringUtils.isBlank(getSubMchId())) {
    this.setSubMchId(config.getSubMchId());
  }

  if (StringUtils.isBlank(getSignType())) {
    if (config.getSignType() != null && !ALL_SIGN_TYPES.contains(config.getSignType())) {
      throw new WxPayException("非法的signType配置:" + config.getSignType() + ",请检查配置!");
    }
    this.setSignType(StringUtils.trimToNull(config.getSignType()));
  } else {
    if (!ALL_SIGN_TYPES.contains(this.getSignType())) {
      throw new WxPayException("非法的sign_type参数:" + this.getSignType());
    }
  }

  if (StringUtils.isBlank(getNonceStr())) {
    this.setNonceStr(String.valueOf(System.currentTimeMillis()));
  }

  //设置签名字段的值
  this.setSign(SignUtils.createSign(this, this.getSignType(), config.getMchKey(),
    this.ignoreSignType()));
}