diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java index 9bfa69d7..91281038 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java @@ -66,6 +66,16 @@ public interface WxPayService { */ String postV3(String url, String requestStr) throws WxPayException; + /** + * 发送patch请求,得到响应字符串. + * + * @param url 请求地址 + * @param requestStr 请求信息 + * @return 返回请求结果字符串 string + * @throws WxPayException the wx pay exception + */ + String patchV3(String url, String requestStr) throws WxPayException; + /** * 发送post请求,得到响应字符串. *

diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/MemberCardServiceImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/MemberCardServiceImpl.java index 6e503e99..27b9429d 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/MemberCardServiceImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/MemberCardServiceImpl.java @@ -74,14 +74,14 @@ public class MemberCardServiceImpl implements MemberCardService { public void updMemberCard(String card_id, String code, MemberCardUpdRequest request) throws WxPayException { String url = String.format("%s/v3/marketing/membercard-open/cards/%s/codes/%s", this.payService.getPayBaseUrl(),card_id,code); RsaCryptoUtil.encryptFields(request, this.payService.getConfig().getVerifier().getValidCertificate()); - this.payService.postV3WithWechatpaySerial(url, GSON.toJson(request)); + this.payService.patchV3(url, GSON.toJson(request)); } @Override public void setMemberCardRights(String card_id, String code, MemberCardRightsRequest request) throws WxPayException { String url = String.format("%s/v3/marketing/membercard-open/cards/%s/codes/%s/rights", this.payService.getPayBaseUrl(),card_id,code); RsaCryptoUtil.encryptFields(request, this.payService.getConfig().getVerifier().getValidCertificate()); - this.payService.postV3WithWechatpaySerial(url, GSON.toJson(request)); + this.payService.patchV3(url, GSON.toJson(request)); } } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java index 36d358a6..62b047af 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java @@ -19,10 +19,7 @@ import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.client.methods.*; import org.apache.http.conn.ssl.DefaultHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.entity.ContentType; @@ -122,8 +119,46 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { } finally { httpPost.releaseConnection(); } + } + + @Override + public String patchV3(String url, String requestStr) throws WxPayException { + CloseableHttpClient httpClient = this.createApiV3HttpClient(); + HttpPatch httpPatch = new HttpPatch(url); + httpPatch.setEntity(this.createEntry(requestStr)); + httpPatch.setConfig(RequestConfig.custom() + .setConnectionRequestTimeout(this.getConfig().getHttpConnectionTimeout()) + .setConnectTimeout(this.getConfig().getHttpConnectionTimeout()) + .setSocketTimeout(this.getConfig().getHttpTimeout()) + .build()); + httpPatch.addHeader("Accept", "application/json"); + httpPatch.addHeader("Content-Type", "application/json"); + try (CloseableHttpResponse response = httpClient.execute(httpPatch)) { + //v3已经改为通过状态码判断200 204 成功 + int statusCode = response.getStatusLine().getStatusCode(); + //post方法有可能会没有返回值的情况 + String responseString; + if (response.getEntity() == null) { + responseString = null; + } else { + responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); + } + if (HttpStatus.SC_OK == statusCode || HttpStatus.SC_NO_CONTENT == statusCode) { + this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString); + return responseString; + } else { + //有错误提示信息返回 + JsonObject jsonObject = GsonParser.parse(responseString); + throw convertException(jsonObject); + } + } catch (Exception e) { + this.log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage()); + throw (e instanceof WxPayException) ? (WxPayException) e : new WxPayException(e.getMessage(), e); + } finally { + httpPatch.releaseConnection(); + } } @Override @@ -137,11 +172,9 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { try (CloseableHttpResponse response = httpClient.execute(httpPost)) { //v3已经改为通过状态码判断200 204 成功 int statusCode = response.getStatusLine().getStatusCode(); - this.log.error("====="+statusCode); String responseString = "{}"; HttpEntity entity = response.getEntity(); if (entity != null) { - this.log.error("====="+entity.toString()); responseString = EntityUtils.toString(entity, StandardCharsets.UTF_8); } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceJoddHttpImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceJoddHttpImpl.java index 86801ca2..2acd168c 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceJoddHttpImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceJoddHttpImpl.java @@ -75,6 +75,11 @@ public class WxPayServiceJoddHttpImpl extends BaseWxPayServiceImpl { return null; } + @Override + public String patchV3(String url, String requestStr) throws WxPayException { + return null; + } + @Override public String postV3WithWechatpaySerial(String url, String requestStr) throws WxPayException { return null;