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..f4607116e 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,8 +1,7 @@ package com.github.binarywang.wxpay.service.impl; import com.github.binarywang.wxpay.bean.complaint.*; -import com.github.binarywang.wxpay.bean.profitsharing.*; -import com.github.binarywang.wxpay.constant.WxPayConstants; +import com.github.binarywang.wxpay.bean.media.ImageUploadResult; import com.github.binarywang.wxpay.exception.WxPayException; import com.github.binarywang.wxpay.service.WxPayService; import com.github.binarywang.wxpay.testbase.ApiTestModule; @@ -13,6 +12,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 +153,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);
+    imageUploadResult.getMediaId();
+
+  }
+
 }