com.github.binarywang.wxpay.bean.result.BaseWxPayResult Java Examples

The following examples show how to use com.github.binarywang.wxpay.bean.result.BaseWxPayResult. 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: WxPayRefundNotifyResult.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
/**
 * 从xml字符串创建bean对象
 *
 * @param xmlString xml字符串
 * @param mchKey    商户密钥
 */
public static WxPayRefundNotifyResult fromXML(String xmlString, String mchKey) throws WxPayException {
  WxPayRefundNotifyResult result = BaseWxPayResult.fromXML(xmlString, WxPayRefundNotifyResult.class);
  String reqInfoString = result.getReqInfoString();
  try {
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

    final MessageDigest md5 = MessageDigest.getInstance("MD5");
    md5.update(mchKey.getBytes());
    final String keyMd5String = new BigInteger(1, md5.digest()).toString(16).toLowerCase();
    SecretKeySpec key = new SecretKeySpec(keyMd5String.getBytes(), "AES");
    cipher.init(Cipher.DECRYPT_MODE, key);
    result.setReqInfo(ReqInfo.fromXML(new String(cipher.doFinal(Base64.decodeBase64(reqInfoString)))));
  } catch (Exception e) {
    throw new WxPayException("解密退款通知加密信息时出错", e);
  }

  return result;
}
 
Example #2
Source File: WxScanPayNotifyResultTest.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
@Test
public void testFromXML() {
  String xmlString = "<xml>\n" +
    "  <appid><![CDATA[wx8888888888888888]]></appid>\n" +
    "  <openid><![CDATA[o8GeHuLAsgefS_80exEr1cTqekUs]]></openid>\n" +
    "  <mch_id><![CDATA[1900000109]]></mch_id>\n" +
    "  <is_subscribe><![CDATA[Y]]></is_subscribe>\n" +
    "  <nonce_str><![CDATA[5K8264ILTKCH16CQ2502SI8ZNMTM67VS]]></nonce_str>\n" +
    "  <product_id><![CDATA[88888]]></product_id>\n" +
    "  <sign><![CDATA[C380BEC2BFD727A4B6845133519F3AD6]]></sign>\n" +
    "</xml>";

  WxScanPayNotifyResult result = BaseWxPayResult.fromXML(xmlString, WxScanPayNotifyResult.class);

  assertThat(result).isNotNull();

  assertThat(result.getAppid()).isEqualTo("wx8888888888888888");
  assertThat(result.getOpenid()).isEqualTo("o8GeHuLAsgefS_80exEr1cTqekUs");
  assertThat(result.getMchId()).isEqualTo("1900000109");
  assertThat(result.getNonceStr()).isEqualTo("5K8264ILTKCH16CQ2502SI8ZNMTM67VS");
  assertThat(result.getProductId()).isEqualTo("88888");
  assertThat(result.getSign()).isEqualTo("C380BEC2BFD727A4B6845133519F3AD6");
}
 
Example #3
Source File: WxPayException.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
/**
 * 通过BaseWxPayResult生成异常对象.
 */
public static WxPayException from(BaseWxPayResult payBaseResult) {
  return WxPayException.newBuilder()
    .xmlString(payBaseResult.getXmlString())
    .returnMsg(payBaseResult.getReturnMsg())
    .returnCode(payBaseResult.getReturnCode())
    .resultCode(payBaseResult.getResultCode())
    .errCode(payBaseResult.getErrCode())
    .errCodeDes(payBaseResult.getErrCodeDes())
    .build();
}
 
Example #4
Source File: EntPayServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public EntPayResult entPay(EntPayRequest request) throws WxPayException {
  request.checkAndSign(this.payService.getConfig());
  String url = this.payService.getPayBaseUrl() + "/mmpaymkttransfers/promotion/transfers";

  String responseContent = this.payService.post(url, request.toXML(), true);
  EntPayResult result = BaseWxPayResult.fromXML(responseContent, EntPayResult.class);
  result.checkResult(this.payService, request.getSignType(), true);
  return result;
}
 
Example #5
Source File: EntPayServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public EntPayQueryResult queryEntPay(String partnerTradeNo) throws WxPayException {
  EntPayQueryRequest request = new EntPayQueryRequest();
  request.setPartnerTradeNo(partnerTradeNo);
  request.checkAndSign(this.payService.getConfig());

  String url = this.payService.getPayBaseUrl() + "/mmpaymkttransfers/gettransferinfo";
  String responseContent = this.payService.post(url, request.toXML(), true);
  EntPayQueryResult result = BaseWxPayResult.fromXML(responseContent, EntPayQueryResult.class);
  result.checkResult(this.payService, request.getSignType(), true);
  return result;
}
 
Example #6
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 #7
Source File: EntPayServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public EntPayBankResult payBank(EntPayBankRequest request) throws WxPayException {
  File publicKeyFile = this.buildPublicKeyFile();
  request.setEncBankNo(this.encryptRSA(publicKeyFile, request.getEncBankNo()));
  request.setEncTrueName(this.encryptRSA(publicKeyFile, request.getEncTrueName()));
  publicKeyFile.deleteOnExit();

  request.checkAndSign(this.payService.getConfig());

  String url = this.payService.getPayBaseUrl() + "/mmpaysptrans/pay_bank";
  String responseContent = this.payService.post(url, request.toXML(), true);
  EntPayBankResult result = BaseWxPayResult.fromXML(responseContent, EntPayBankResult.class);
  result.checkResult(this.payService, request.getSignType(), true);
  return result;
}
 
Example #8
Source File: EntPayServiceImpl.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public EntPayBankQueryResult queryPayBank(String partnerTradeNo) throws WxPayException {
  EntPayBankQueryRequest request = new EntPayBankQueryRequest();
  request.setPartnerTradeNo(partnerTradeNo);
  request.checkAndSign(this.payService.getConfig());

  String url = this.payService.getPayBaseUrl() + "/mmpaysptrans/query_bank";
  String responseContent = this.payService.post(url, request.toXML(), true);
  EntPayBankQueryResult result = BaseWxPayResult.fromXML(responseContent, EntPayBankQueryResult.class);
  result.checkResult(this.payService, request.getSignType(), true);
  return result;
}
 
Example #9
Source File: SignUtils.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
/**
 * 将bean按照@XStreamAlias标识的字符串内容生成以之为key的map对象
 *
 * @param bean 包含@XStreamAlias的xml bean对象
 * @return map对象
 */
public static Map<String, String> xmlBean2Map(Object bean) {
  Map<String, String> result = Maps.newHashMap();
  List<Field> fields = new ArrayList<>(Arrays.asList(bean.getClass().getDeclaredFields()));
  fields.addAll(Arrays.asList(bean.getClass().getSuperclass().getDeclaredFields()));
  if(bean.getClass().getSuperclass().getSuperclass() == BaseWxPayRequest.class){
    fields.addAll(Arrays.asList(BaseWxPayRequest.class.getDeclaredFields()));
  }

  if(bean.getClass().getSuperclass().getSuperclass() == BaseWxPayResult.class){
    fields.addAll(Arrays.asList(BaseWxPayResult.class.getDeclaredFields()));
  }

  for (Field field : fields) {
    try {
      boolean isAccessible = field.isAccessible();
      field.setAccessible(true);
      if (field.get(bean) == null) {
        field.setAccessible(isAccessible);
        continue;
      }

      if (field.isAnnotationPresent(XStreamAlias.class)) {
        result.put(field.getAnnotation(XStreamAlias.class).value(), field.get(bean).toString());
      } else if (!Modifier.isStatic(field.getModifiers())) {
        //忽略掉静态成员变量
        result.put(field.getName(), field.get(bean).toString());
      }

      field.setAccessible(isAccessible);
    } catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
      log.error(e.getMessage(), e);
    }

  }

  return result;
}