From 850c361611b7b0af0fd72943133199536d9339c3 Mon Sep 17 00:00:00 2001 From: hsk <245743602@qq.com> Date: Thu, 28 Apr 2022 10:16:46 +0800 Subject: [PATCH] =?UTF-8?q?1:=E4=BF=AE=E5=A4=8D=E6=9F=A5=E8=AF=A2=E6=8A=95?= =?UTF-8?q?=E8=AF=89=E5=8D=8F=E5=95=86=E5=8E=86=E5=8F=B2API=20bug,Negotiat?= =?UTF-8?q?ionHistoryResult=E7=B1=BB=E5=AD=97=E6=AE=B5(complaintMediaList)?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E4=B8=8E=E5=AE=98=E6=96=B9=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E4=B8=8D=E4=B8=80=E8=87=B4=E5=AF=BC=E8=87=B4=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=E9=9B=86=E8=BD=AC=E6=8D=A2=E5=BC=82=E5=B8=B8=202=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=95=86=E6=88=B7=E4=B8=8A=E4=BC=A0=E5=8F=8D?= =?UTF-8?q?=E9=A6=88=E5=9B=BE=E7=89=87API=EF=BC=9B=E5=AE=98=E6=96=B9api?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E5=9C=B0=E5=9D=80:https://pay.weixin.qq.com/?= =?UTF-8?q?wiki/doc/apiv3/apis/chapter10=5F2=5F10.shtml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../complaint/NegotiationHistoryResult.java | 4 +- .../wxpay/service/ComplaintService.java | 31 +++++++++++++ .../service/impl/ComplaintServiceImpl.java | 46 ++++++++++++++++++- .../impl/ComplaintServiceImplTest.java | 21 +++++++++ 4 files changed, 98 insertions(+), 4 deletions(-) diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/complaint/NegotiationHistoryResult.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/complaint/NegotiationHistoryResult.java index 4e5ab4197..b6f265fd8 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/complaint/NegotiationHistoryResult.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/complaint/NegotiationHistoryResult.java @@ -67,7 +67,7 @@ public class NegotiationHistoryResult implements Serializable { * */ @SerializedName("complaint_media_list") - private List complaintMediaList; + private List complaintMediaList; @Data public static class ComplaintMedia implements Serializable { @@ -97,7 +97,7 @@ public class NegotiationHistoryResult implements Serializable { * */ @SerializedName("media_url") - private String mediaUrl; + private List mediaUrl; } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/ComplaintService.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/ComplaintService.java index bd6a2e346..1ed2b09df 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/ComplaintService.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/ComplaintService.java @@ -1,9 +1,13 @@ package com.github.binarywang.wxpay.service; import com.github.binarywang.wxpay.bean.complaint.*; +import com.github.binarywang.wxpay.bean.media.ImageUploadResult; import com.github.binarywang.wxpay.exception.WxPayException; import javax.crypto.BadPaddingException; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; /** *
@@ -129,4 +133,31 @@ public interface ComplaintService {
    */
   void complete(CompleteRequest request) throws WxPayException;
 
+  /**
+   * 
+   * 商户上传反馈图片API
+   * 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter10_2_10.shtml
+   * 接口链接:https://api.mch.weixin.qq.com/v3/merchant-service/images/upload
+   * 
+ * + * @param imageFile 需要上传的图片文件 + * @return ImageUploadResult 微信返回的媒体文件标识Id。示例值:BB04A5DEEFEA18D4F2554C1EDD3B610B.bmp + * @throws WxPayException the wx pay exception + */ + ImageUploadResult uploadResponseImage(File imageFile) throws WxPayException, IOException; + + /** + *
+   * 商户上传反馈图片API
+   * 文档详见: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter10_2_10.shtml
+   * 接口链接:https://api.mch.weixin.qq.com/v3/merchant-service/images/upload
+   * 
+ * + * @param inputStream 需要上传的图片文件流 + * @param fileName 需要上传的图片文件名 + * @return ImageUploadResult 微信返回的媒体文件标识Id。示例值:BB04A5DEEFEA18D4F2554C1EDD3B610B.bmp + * @throws WxPayException the wx pay exception + */ + ImageUploadResult uploadResponseImage(InputStream inputStream, String fileName) throws WxPayException, IOException; + } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/ComplaintServiceImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/ComplaintServiceImpl.java index d269a8f90..51d9609c4 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/ComplaintServiceImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/ComplaintServiceImpl.java @@ -1,15 +1,20 @@ package com.github.binarywang.wxpay.service.impl; import com.github.binarywang.wxpay.bean.complaint.*; +import com.github.binarywang.wxpay.bean.media.ImageUploadResult; import com.github.binarywang.wxpay.exception.WxPayException; import com.github.binarywang.wxpay.service.ComplaintService; import com.github.binarywang.wxpay.service.WxPayService; +import com.github.binarywang.wxpay.v3.WechatPayUploadHttpPost; import com.github.binarywang.wxpay.v3.util.RsaCryptoUtil; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import lombok.RequiredArgsConstructor; +import org.apache.commons.codec.digest.DigestUtils; import javax.crypto.BadPaddingException; +import java.io.*; +import java.net.URI; import java.util.List; /** @@ -34,7 +39,7 @@ public class ComplaintServiceImpl implements ComplaintService { List data = complaintResult.getData(); for (ComplaintDetailResult complaintDetailResult : data) { // 对手机号进行解密操作 - if(complaintDetailResult.getPayerPhone() != null) { + if (complaintDetailResult.getPayerPhone() != null) { String payerPhone = RsaCryptoUtil.decryptOAEP(complaintDetailResult.getPayerPhone(), this.payService.getConfig().getPrivateKey()); complaintDetailResult.setPayerPhone(payerPhone); } @@ -49,7 +54,7 @@ public class ComplaintServiceImpl implements ComplaintService { String response = this.payService.getV3(url); ComplaintDetailResult result = GSON.fromJson(response, ComplaintDetailResult.class); // 对手机号进行解密操作 - if(result.getPayerPhone() != null) { + if (result.getPayerPhone() != null) { String payerPhone = RsaCryptoUtil.decryptOAEP(result.getPayerPhone(), this.payService.getConfig().getPrivateKey()); result.setPayerPhone(payerPhone); } @@ -107,4 +112,41 @@ public class ComplaintServiceImpl implements ComplaintService { this.payService.postV3(url, GSON.toJson(request)); } + @Override + public ImageUploadResult uploadResponseImage(File imageFile) throws WxPayException, IOException { + String url = String.format("%s/v3/merchant-service/images/upload", this.payService.getPayBaseUrl()); + + try (FileInputStream s1 = new FileInputStream(imageFile)) { + String sha256 = DigestUtils.sha256Hex(s1); + try (InputStream s2 = new FileInputStream(imageFile)) { + WechatPayUploadHttpPost request = new WechatPayUploadHttpPost.Builder(URI.create(url)) + .withImage(imageFile.getName(), sha256, s2) + .build(); + String result = this.payService.postV3(url, request); + return ImageUploadResult.fromJson(result); + } + } + } + + @Override + public ImageUploadResult uploadResponseImage(InputStream inputStream, String fileName) throws WxPayException, IOException { + String url = String.format("%s/v3/merchant-service/images/upload", this.payService.getPayBaseUrl()); + try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { + //文件大小不能超过2M + byte[] buffer = new byte[2048]; + int len; + while ((len = inputStream.read(buffer)) > -1) { + bos.write(buffer, 0, len); + } + bos.flush(); + byte[] data = bos.toByteArray(); + String sha256 = DigestUtils.sha256Hex(data); + WechatPayUploadHttpPost request = new WechatPayUploadHttpPost.Builder(URI.create(url)) + .withImage(fileName, sha256, new ByteArrayInputStream(data)) + .build(); + String result = this.payService.postV3(url, request); + return ImageUploadResult.fromJson(result); + } + } + } diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/ComplaintServiceImplTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/ComplaintServiceImplTest.java index 6014924fd..1fcc610a4 100644 --- a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/ComplaintServiceImplTest.java +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/ComplaintServiceImplTest.java @@ -1,9 +1,11 @@ package com.github.binarywang.wxpay.service.impl; import com.github.binarywang.wxpay.bean.complaint.*; +import com.github.binarywang.wxpay.bean.media.ImageUploadResult; import com.github.binarywang.wxpay.bean.profitsharing.*; import com.github.binarywang.wxpay.constant.WxPayConstants; import com.github.binarywang.wxpay.exception.WxPayException; +import com.github.binarywang.wxpay.service.MerchantMediaService; import com.github.binarywang.wxpay.service.WxPayService; import com.github.binarywang.wxpay.testbase.ApiTestModule; import com.google.inject.Inject; @@ -13,6 +15,8 @@ import org.testng.annotations.Guice; import org.testng.annotations.Test; import javax.crypto.BadPaddingException; +import java.io.File; +import java.io.IOException; /** *
@@ -152,4 +156,21 @@ public class ComplaintServiceImplTest {
     this.payService.getComplaintsService().complete(request);
   }
 
+  /**
+   *  商户上传反馈图片API
+   * @throws WxPayException
+   * @throws IOException
+   */
+  @Test
+  public  void testUploadResponseImage() throws WxPayException, IOException {
+    String filePath="你的图片文件的路径地址";
+//    String filePath="WxJava/images/banners/wiki.jpg";
+
+    File file = new File(filePath);
+
+    ImageUploadResult imageUploadResult = this.payService.getComplaintsService().uploadResponseImage(file);
+    String mediaId = imageUploadResult.getMediaId();
+
+  }
+
 }
-- 
Gitee