Search in sources :

Example 1 with WxMpPrepayIdResult

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

the class WxMpServiceImpl method getJSSDKPayInfo.

@Override
public Map<String, String> getJSSDKPayInfo(Map<String, String> parameters) {
    WxMpPrepayIdResult wxMpPrepayIdResult = getPrepayId(parameters);
    String prepayId = wxMpPrepayIdResult.getPrepay_id();
    if (prepayId == null || prepayId.equals("")) {
        throw new RuntimeException(String.format("Failed to get prepay id due to error code '%s'(%s).", wxMpPrepayIdResult.getErr_code(), wxMpPrepayIdResult.getErr_code_des()));
    }
    Map<String, String> payInfo = new HashMap<String, String>();
    payInfo.put("appId", wxMpConfigStorage.getAppId());
    // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
    payInfo.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000));
    payInfo.put("nonceStr", System.currentTimeMillis() + "");
    payInfo.put("package", "prepay_id=" + prepayId);
    payInfo.put("signType", "MD5");
    String finalSign = WxCryptUtil.createSign(payInfo, wxMpConfigStorage.getPartnerKey());
    payInfo.put("paySign", finalSign);
    return payInfo;
}
Also used : WxMpPrepayIdResult(me.chanjar.weixin.mp.bean.result.WxMpPrepayIdResult) HashMap(java.util.HashMap)

Example 2 with WxMpPrepayIdResult

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

the class WxMpServiceImpl method getPrepayId.

public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters) {
    String nonce_str = System.currentTimeMillis() + "";
    final SortedMap<String, String> packageParams = new TreeMap<String, String>(parameters);
    packageParams.put("appid", wxMpConfigStorage.getAppId());
    packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
    packageParams.put("nonce_str", nonce_str);
    checkParameters(packageParams);
    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/pay/unifiedorder");
    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.alias("xml", WxMpPrepayIdResult.class);
        WxMpPrepayIdResult wxMpPrepayIdResult = (WxMpPrepayIdResult) xstream.fromXML(responseContent);
        return wxMpPrepayIdResult;
    } catch (IOException e) {
        throw new RuntimeException("Failed to get prepay id due to IO exception.", e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) WxMpPrepayIdResult(me.chanjar.weixin.mp.bean.result.WxMpPrepayIdResult) XStream(com.thoughtworks.xstream.XStream) IOException(java.io.IOException) TreeMap(java.util.TreeMap) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Aggregations

WxMpPrepayIdResult (me.chanjar.weixin.mp.bean.result.WxMpPrepayIdResult)2 XStream (com.thoughtworks.xstream.XStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)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