From aae0dfe9ab63c54d717e66d16d1f989be82552a3 Mon Sep 17 00:00:00 2001 From: egzosn Date: Mon, 29 Jan 2018 10:39:53 +0800 Subject: [PATCH] =?UTF-8?q?Payoneer=20=E6=94=AF=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pay/common/http/ClientHttpRequest.java | 53 +++++++++++++++---- .../handler/PayoneerMessageHandler.java | 3 +- .../pay/payoneer/api/PayoneerPayService.java | 6 +-- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/pay-java-common/src/main/java/com/egzosn/pay/common/http/ClientHttpRequest.java b/pay-java-common/src/main/java/com/egzosn/pay/common/http/ClientHttpRequest.java index f872fe7..efae468 100644 --- a/pay-java-common/src/main/java/com/egzosn/pay/common/http/ClientHttpRequest.java +++ b/pay-java-common/src/main/java/com/egzosn/pay/common/http/ClientHttpRequest.java @@ -185,14 +185,6 @@ public class ClientHttpRequest extends HttpEntityEnclosingRequestBase impleme final StatusLine statusLine = response.getStatusLine(); final HttpEntity entity = response.getEntity(); - if (statusLine.getStatusCode() >= 300 && statusLine.getStatusCode() != 304) { - EntityUtils.consume(entity); - throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); - } - if (null == responseType){ - responseType = (Class) String.class; - } - String[] value = null; if (null == entity.getContentType()){ value = new String[]{"application/x-www-form-urlencoded"}; @@ -200,6 +192,24 @@ public class ClientHttpRequest extends HttpEntityEnclosingRequestBase impleme value = entity.getContentType().getValue().split(";"); } + if (statusLine.getStatusCode() >= 300 && statusLine.getStatusCode() != 304) { + if (isJson(value[0], "") || isXml(value[0], "") ){ + return toBean(entity, value); + } + + EntityUtils.consume(entity); + throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); + } + if (null == responseType){ + responseType = (Class) String.class; + } + + + return toBean(entity, value); + + } + + private T toBean(HttpEntity entity, String[] value) throws IOException { if (ContentType.APPLICATION_OCTET_STREAM.getMimeType().equals(value[0])){ if (responseType.isAssignableFrom(InputStream.class)){ @@ -228,7 +238,7 @@ public class ClientHttpRequest extends HttpEntityEnclosingRequestBase impleme } String first = result.substring(0, 1); - if ( ContentType.APPLICATION_JSON.getMimeType().equals( value[0]) || "{[".indexOf(first) >= 0 ){ + if ( isJson(value[0], first) ){ try { return JSON.parseObject(result, responseType); }catch (JSONException e){ @@ -236,11 +246,34 @@ public class ClientHttpRequest extends HttpEntityEnclosingRequestBase impleme } } - if (ContentType.APPLICATION_XML.getMimeType().equals( value[0]) || "<".indexOf(first) >= 0){ + if (isXml(value[0], first)){ return XML.toJSONObject(result).toJavaObject(responseType); } throw new PayErrorException(new PayException("failure", "类型转化异常,contentType:" + entity.getContentType().getValue(), result)); + } + + /** + * 检测响应类型是否为json + * @param contentType 内容类型 + * @param textFirst 文本第一个字符 + * @return 布尔型, true为json内容类型 + */ + private boolean isJson(String contentType, String textFirst){ + return( ContentType.APPLICATION_JSON.getMimeType().equals(contentType) || "{[".indexOf(textFirst) >= 0 ); + } + + /** + * 检测响应类型是否为xml + * @param contentType 内容类型 + * @param textFirst 文本第一个字符 + * @return 布尔型, true为xml内容类型 + */ + private boolean isXml(String contentType, String textFirst){ + return( ContentType.APPLICATION_XML.getMimeType().equals(contentType) || "<".indexOf(textFirst) >= 0 ); + } + + } diff --git a/pay-java-demo/src/main/java/com/egzosn/pay/demo/service/handler/PayoneerMessageHandler.java b/pay-java-demo/src/main/java/com/egzosn/pay/demo/service/handler/PayoneerMessageHandler.java index 3c235a1..d1ee66c 100644 --- a/pay-java-demo/src/main/java/com/egzosn/pay/demo/service/handler/PayoneerMessageHandler.java +++ b/pay-java-demo/src/main/java/com/egzosn/pay/demo/service/handler/PayoneerMessageHandler.java @@ -4,6 +4,7 @@ import com.egzosn.pay.common.api.PayService; import com.egzosn.pay.common.bean.PayMessage; import com.egzosn.pay.common.bean.PayOutMessage; import com.egzosn.pay.common.exception.PayErrorException; +import com.egzosn.pay.payoneer.api.PayoneerPayService; import java.util.Map; @@ -23,7 +24,7 @@ public class PayoneerMessageHandler extends BasePayMessageHandler { @Override public PayOutMessage handle(PayMessage payMessage, Map context, PayService payService) throws PayErrorException { //交易状态 - if ("0".equals(payMessage.getPayMessage().get("code"))) { + if ("0".equals(payMessage.getPayMessage().get(PayoneerPayService.CODE))) { /////这里进行成功的处理 return payService.successPayOutMessage(payMessage); diff --git a/pay-java-payoneer/src/main/java/com/egzosn/pay/payoneer/api/PayoneerPayService.java b/pay-java-payoneer/src/main/java/com/egzosn/pay/payoneer/api/PayoneerPayService.java index 82488d1..7472dfd 100644 --- a/pay-java-payoneer/src/main/java/com/egzosn/pay/payoneer/api/PayoneerPayService.java +++ b/pay-java-payoneer/src/main/java/com/egzosn/pay/payoneer/api/PayoneerPayService.java @@ -40,7 +40,7 @@ public class PayoneerPayService extends BasePayService implements AdvancedPaySer /** * 响应状态码 */ - private final static String CODE = "code"; + public final static String CODE = "code"; /** * 响应状态码 */ @@ -214,10 +214,10 @@ public class PayoneerPayService extends BasePayService implements AdvancedPaySer public Map microPay(PayOrder order) { HttpStringEntity entity = new HttpStringEntity(JSON.toJSONString(orderInfo(order)), ContentType.APPLICATION_JSON); JSONObject response = getHttpRequestTemplate().postForObject(getReqUrl(PayoneerTransactionType.charge), entity, JSONObject.class); - if (response != null && 0 == response.getIntValue(CODE)) { + if (response != null) { return response; } - throw new PayErrorException(new PayException("fail", "Payoneer申请收款失败,原因:" + response.getString("hint"), response.toJSONString())); + throw new PayErrorException(new PayException("fail", "Payoneer申请收款失败,原因:" + response.getString("description"), response.toJSONString())); } /**