mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-09-21 02:58:09 +08:00
重构已有支付代码
This commit is contained in:
@@ -30,20 +30,25 @@ public interface WxMpPayService {
|
||||
* @param tradeType 交易类型 JSAPI,NATIVE,APP,WAP
|
||||
* @param ip 发起支付的客户端IP
|
||||
* @param notifyUrl 通知地址
|
||||
* @throws WxErrorException
|
||||
* @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getPrepayId(Map<String, String>) instead
|
||||
*/
|
||||
@Deprecated
|
||||
WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String notifyUrl);
|
||||
WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double amt,
|
||||
String body, String tradeType, String ip, String notifyUrl)
|
||||
throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1)
|
||||
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识"
|
||||
*
|
||||
* @param parameters All required/optional parameters for weixin payment
|
||||
* @throws WxErrorException
|
||||
* @deprecated use me.chanjar.weixin.mp.api.WxMpPayService.unifiedOrder(WxUnifiedOrderRequest) instead
|
||||
*/
|
||||
@Deprecated
|
||||
WxMpPrepayIdResult getPrepayId(Map<String, String> parameters);
|
||||
WxMpPrepayIdResult getPrepayId(Map<String, String> parameters)
|
||||
throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1)
|
||||
@@ -98,9 +103,11 @@ public interface WxMpPayService {
|
||||
/**
|
||||
* 该接口提供所有微信支付订单的查询,当支付通知处理异常戒丢失的情冴,商户可以通过该接口查询订单支付状态。
|
||||
* 详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2
|
||||
* @throws WxErrorException
|
||||
*
|
||||
*/
|
||||
WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo);
|
||||
WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo)
|
||||
throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 读取支付结果通知
|
||||
|
@@ -1,6 +1,5 @@
|
||||
package me.chanjar.weixin.mp.api.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -10,16 +9,9 @@ import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.joor.Reflect;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.slf4j.helpers.MessageFormatter;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
@@ -29,9 +21,9 @@ import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import me.chanjar.weixin.common.annotation.Required;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.Utf8ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
|
||||
import me.chanjar.weixin.mp.api.WxMpPayService;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.bean.pay.WxMpPayCallback;
|
||||
import me.chanjar.weixin.mp.bean.pay.WxMpPayRefundResult;
|
||||
import me.chanjar.weixin.mp.bean.pay.WxMpPayResult;
|
||||
@@ -54,19 +46,17 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
||||
private final String[] REQUIRED_ORDER_PARAMETERS = new String[] { "appid",
|
||||
"mch_id", "body", "out_trade_no", "total_fee", "spbill_create_ip",
|
||||
"notify_url", "trade_type" };
|
||||
private HttpHost httpProxy;
|
||||
private WxMpServiceImpl wxMpService;
|
||||
private WxMpService wxMpService;
|
||||
|
||||
public WxMpPayServiceImpl(WxMpServiceImpl wxMpService) {
|
||||
public WxMpPayServiceImpl(WxMpService wxMpService) {
|
||||
this.wxMpService = wxMpService;
|
||||
this.httpProxy = wxMpService.getHttpProxy();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo,
|
||||
double amt, String body, String tradeType, String ip,
|
||||
String callbackUrl) {
|
||||
String callbackUrl) throws WxErrorException {
|
||||
Map<String, String> packageParams = new HashMap<>();
|
||||
packageParams.put("appid",
|
||||
this.wxMpService.getWxMpConfigStorage().getAppId());
|
||||
@@ -85,7 +75,8 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters) {
|
||||
public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters)
|
||||
throws WxErrorException {
|
||||
final SortedMap<String, String> packageParams = new TreeMap<>(parameters);
|
||||
packageParams.put("appid",
|
||||
this.wxMpService.getWxMpConfigStorage().getAppId());
|
||||
@@ -106,29 +97,11 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
||||
|
||||
request.append("</xml>");
|
||||
|
||||
HttpPost httpPost = new HttpPost(
|
||||
"https://api.mch.weixin.qq.com/pay/unifiedorder");
|
||||
if (this.httpProxy != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy)
|
||||
.build();
|
||||
httpPost.setConfig(config);
|
||||
}
|
||||
|
||||
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
|
||||
httpPost.setEntity(entity);
|
||||
try (CloseableHttpResponse response = this.wxMpService.getHttpclient()
|
||||
.execute(httpPost)) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE
|
||||
.handleResponse(response);
|
||||
String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
|
||||
String responseContent = this.wxMpService.post(url, request.toString());
|
||||
XStream xstream = XStreamInitializer.getInstance();
|
||||
xstream.alias("xml", WxMpPrepayIdResult.class);
|
||||
return (WxMpPrepayIdResult) xstream.fromXML(responseContent);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to get prepay id due to IO exception.",
|
||||
e);
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
}
|
||||
|
||||
private void checkParameters(Map<String, String> parameters) {
|
||||
@@ -238,7 +211,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
||||
|
||||
@Override
|
||||
public WxMpPayResult getJSSDKPayResult(String transactionId,
|
||||
String outTradeNo) {
|
||||
String outTradeNo) throws WxErrorException {
|
||||
String nonce_str = System.currentTimeMillis() + "";
|
||||
|
||||
SortedMap<String, String> packageParams = new TreeMap<>();
|
||||
@@ -267,27 +240,11 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
||||
}
|
||||
request.append("</xml>");
|
||||
|
||||
HttpPost httpPost = new HttpPost(
|
||||
"https://api.mch.weixin.qq.com/pay/orderquery");
|
||||
if (this.httpProxy != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy)
|
||||
.build();
|
||||
httpPost.setConfig(config);
|
||||
}
|
||||
|
||||
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
|
||||
httpPost.setEntity(entity);
|
||||
try (CloseableHttpResponse response = this.wxMpService.getHttpclient()
|
||||
.execute(httpPost)) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE
|
||||
.handleResponse(response);
|
||||
String url = "https://api.mch.weixin.qq.com/pay/orderquery";
|
||||
String responseContent = this.wxMpService.post(url, request.toString());
|
||||
XStream xstream = XStreamInitializer.getInstance();
|
||||
xstream.alias("xml", WxMpPayResult.class);
|
||||
return (WxMpPayResult) xstream.fromXML(responseContent);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to query order due to IO exception.",
|
||||
e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -325,20 +282,8 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
||||
}
|
||||
request.append("</xml>");
|
||||
|
||||
HttpPost httpPost = new HttpPost(
|
||||
"https://api.mch.weixin.qq.com/secapi/pay/refund");
|
||||
if (this.httpProxy != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy)
|
||||
.build();
|
||||
httpPost.setConfig(config);
|
||||
}
|
||||
|
||||
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
|
||||
httpPost.setEntity(entity);
|
||||
try (CloseableHttpResponse response = this.wxMpService.getHttpclient()
|
||||
.execute(httpPost)) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE
|
||||
.handleResponse(response);
|
||||
String url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
|
||||
String responseContent = this.wxMpService.post(url, request.toString());
|
||||
XStream xstream = XStreamInitializer.getInstance();
|
||||
xstream.processAnnotations(WxMpPayRefundResult.class);
|
||||
WxMpPayRefundResult wxMpPayRefundResult = (WxMpPayRefundResult) xstream
|
||||
@@ -350,24 +295,13 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
||||
error.setErrorCode(-1);
|
||||
error.setErrorMsg("return_code:" + wxMpPayRefundResult.getReturnCode()
|
||||
+ ";return_msg:" + wxMpPayRefundResult.getReturnMsg()
|
||||
+ ";result_code:" + wxMpPayRefundResult.getResultCode()
|
||||
+ ";err_code" + wxMpPayRefundResult.getErrCode() + ";err_code_des"
|
||||
+ ";result_code:" + wxMpPayRefundResult.getResultCode() + ";err_code"
|
||||
+ wxMpPayRefundResult.getErrCode() + ";err_code_des"
|
||||
+ wxMpPayRefundResult.getErrCodeDes());
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
|
||||
return wxMpPayRefundResult;
|
||||
} catch (IOException e) {
|
||||
String message = MessageFormatter
|
||||
.format("Exception happened when sending refund '{}'.",
|
||||
request.toString())
|
||||
.getMessage();
|
||||
this.log.error(message, e);
|
||||
throw new WxErrorException(
|
||||
WxError.newBuilder().setErrorMsg(message).build());
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -400,34 +334,12 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
||||
|
||||
request.append("</xml>");
|
||||
|
||||
HttpPost httpPost = new HttpPost(
|
||||
"https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack");
|
||||
if (this.httpProxy != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy)
|
||||
.build();
|
||||
httpPost.setConfig(config);
|
||||
}
|
||||
String url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
|
||||
|
||||
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
|
||||
httpPost.setEntity(entity);
|
||||
try (CloseableHttpResponse response = this.wxMpService.getHttpclient()
|
||||
.execute(httpPost)) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE
|
||||
.handleResponse(response);
|
||||
String responseContent = this.wxMpService.post(url, request.toString());
|
||||
XStream xstream = XStreamInitializer.getInstance();
|
||||
xstream.processAnnotations(WxRedpackResult.class);
|
||||
return (WxRedpackResult) xstream.fromXML(responseContent);
|
||||
} catch (IOException e) {
|
||||
String message = MessageFormatter
|
||||
.format("Exception occured when sending redpack '{}'.",
|
||||
request.toString())
|
||||
.getMessage();
|
||||
this.log.error(message, e);
|
||||
throw new WxErrorException(
|
||||
WxError.newBuilder().setErrorMsg(message).build());
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -544,7 +456,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
||||
|
||||
private void checkParameters(WxUnifiedOrderRequest request) {
|
||||
|
||||
List<String> nullFields = com.google.common.collect.Lists.newArrayList();
|
||||
List<String> nullFields = Lists.newArrayList();
|
||||
for (Entry<String, Reflect> entry : Reflect.on(request).fields()
|
||||
.entrySet()) {
|
||||
Reflect reflect = entry.getValue();
|
||||
|
Reference in New Issue
Block a user