Search in sources :

Example 1 with WxRedpackResult

use of me.chanjar.weixin.mp.bean.result.WxRedpackResult in project weixin-java-tools by chanjarster.

the class WxMpServiceImpl method sendRedpack.

@Override
public WxRedpackResult sendRedpack(Map<String, String> parameters) throws WxErrorException {
    String nonce_str = System.currentTimeMillis() + "";
    SortedMap<String, String> packageParams = new TreeMap<String, String>(parameters);
    packageParams.put("wxappid", wxMpConfigStorage.getAppId());
    packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
    packageParams.put("nonce_str", nonce_str);
    String sign = WxCryptUtil.createSign(packageParams, wxMpConfigStorage.getPartnerKey());
    packageParams.put("sign", sign);
    StringBuilder request = new StringBuilder("<xml>");
    for (Entry<String, String> para : packageParams.entrySet()) {
        request.append(String.format("<%s>%s</%s>", para.getKey(), para.getValue(), para.getKey()));
    }
    request.append("</xml>");
    HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack");
    if (httpProxy != null) {
        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
        httpPost.setConfig(config);
    }
    StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
    httpPost.setEntity(entity);
    try {
        CloseableHttpResponse response = getHttpclient().execute(httpPost);
        String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
        XStream xstream = XStreamInitializer.getInstance();
        xstream.processAnnotations(WxRedpackResult.class);
        WxRedpackResult wxMpRedpackResult = (WxRedpackResult) xstream.fromXML(responseContent);
        return wxMpRedpackResult;
    } catch (IOException e) {
        log.error(MessageFormatter.format("The exception was happened when sending redpack '{}'.", request.toString()).getMessage(), e);
        WxError error = new WxError();
        error.setErrorCode(-1);
        throw new WxErrorException(error);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) WxError(me.chanjar.weixin.common.bean.result.WxError) XStream(com.thoughtworks.xstream.XStream) IOException(java.io.IOException) TreeMap(java.util.TreeMap) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WxRedpackResult(me.chanjar.weixin.mp.bean.result.WxRedpackResult)

Example 2 with WxRedpackResult

use of me.chanjar.weixin.mp.bean.result.WxRedpackResult in project weixin-java-tools by chanjarster.

the class WxRedpackResultTest method loadSuccessResult.

@Test
public void loadSuccessResult() {
    final String successSample = "<xml>\n" + "<return_code><![CDATA[SUCCESS]]></return_code>\n" + "<return_msg><![CDATA[发放成功.]]></return_msg>\n" + "<result_code><![CDATA[SUCCESS]]></result_code>\n" + "<err_code><![CDATA[0]]></err_code>\n" + "<err_code_des><![CDATA[发放成功.]]></err_code_des>\n" + "<mch_billno><![CDATA[0010010404201411170000046545]]></mch_billno>\n" + "<mch_id>10010404</mch_id>\n" + "<wxappid><![CDATA[wx6fa7e3bab7e15415]]></wxappid>\n" + "<re_openid><![CDATA[onqOjjmM1tad-3ROpncN-yUfa6uI]]></re_openid>\n" + "<total_amount>1</total_amount>\n" + "<send_listid>100000000020150520314766074200</send_listid>\n" + "<send_time>20150520102602</send_time>\n" + "</xml>";
    WxRedpackResult wxMpRedpackResult = (WxRedpackResult) xstream.fromXML(successSample);
    assertEquals("SUCCESS", wxMpRedpackResult.getReturnCode());
    assertEquals("SUCCESS", wxMpRedpackResult.getResultCode());
    assertEquals("20150520102602", wxMpRedpackResult.getSendTime());
}
Also used : WxRedpackResult(me.chanjar.weixin.mp.bean.result.WxRedpackResult) Test(org.junit.Test)

Example 3 with WxRedpackResult

use of me.chanjar.weixin.mp.bean.result.WxRedpackResult in project weixin-java-tools by chanjarster.

the class WxRedpackResultTest method loadFailureResult.

@Test
public void loadFailureResult() {
    final String failureSample = "<xml>\n" + "<return_code><![CDATA[FAIL]]></return_code>\n" + "<return_msg><![CDATA[系统繁忙,请稍后再试.]]></return_msg>\n" + "<result_code><![CDATA[FAIL]]></result_code>\n" + "<err_code><![CDATA[268458547]]></err_code>\n" + "<err_code_des><![CDATA[系统繁忙,请稍后再试.]]></err_code_des>\n" + "<mch_billno><![CDATA[0010010404201411170000046542]]></mch_billno>\n" + "<mch_id>10010404</mch_id>\n" + "<wxappid><![CDATA[wx6fa7e3bab7e15415]]></wxappid>\n" + "<re_openid><![CDATA[onqOjjmM1tad-3ROpncN-yUfa6uI]]></re_openid>\n" + "<total_amount>1</total_amount>\n" + "</xml>";
    WxRedpackResult wxMpRedpackResult = (WxRedpackResult) xstream.fromXML(failureSample);
    assertEquals("FAIL", wxMpRedpackResult.getReturnCode());
    assertEquals("FAIL", wxMpRedpackResult.getResultCode());
    assertEquals("onqOjjmM1tad-3ROpncN-yUfa6uI", wxMpRedpackResult.getReOpenid());
    assertEquals(1, wxMpRedpackResult.getTotalAmount());
}
Also used : WxRedpackResult(me.chanjar.weixin.mp.bean.result.WxRedpackResult) Test(org.junit.Test)

Aggregations

WxRedpackResult (me.chanjar.weixin.mp.bean.result.WxRedpackResult)3 Test (org.junit.Test)2 XStream (com.thoughtworks.xstream.XStream)1 IOException (java.io.IOException)1 TreeMap (java.util.TreeMap)1 WxError (me.chanjar.weixin.common.bean.result.WxError)1 WxErrorException (me.chanjar.weixin.common.exception.WxErrorException)1 RequestConfig (org.apache.http.client.config.RequestConfig)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 StringEntity (org.apache.http.entity.StringEntity)1