From 155501152af0561b9cad5a39e6a27ca94b3ad612 Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Mon, 21 Dec 2020 00:45:57 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20:bug:=20#1898=20=E3=80=90=E5=85=AC?= =?UTF-8?q?=E4=BC=97=E5=8F=B7=E3=80=91=E5=BE=AE=E4=BF=A1=E5=95=86=E6=88=B7?= =?UTF-8?q?=E7=94=B5=E5=AD=90=E5=8F=91=E7=A5=A8=E4=BB=A3=E7=A0=81=E8=A7=84?= =?UTF-8?q?=E8=8C=83=E5=8C=96=E5=8F=8A=E4=BC=98=E5=8C=96=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AE=BE=E7=BD=AE=E5=95=86=E6=88=B7=E8=81=94?= =?UTF-8?q?=E7=B3=BB=E6=96=B9=E5=BC=8F=E5=8F=82=E6=95=B0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mp/api/WxMpMerchantInvoiceService.java | 40 +++++++++++++ .../impl/WxMpMerchantInvoiceServiceImpl.java | 58 ++++++++----------- .../invoice/merchant/MerchantContactInfo.java | 6 ++ .../merchant/MerchantContactInfoWrapper.java | 7 ++- 4 files changed, 76 insertions(+), 35 deletions(-) diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMerchantInvoiceService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMerchantInvoiceService.java index 294fba85b..795c848b3 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMerchantInvoiceService.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMerchantInvoiceService.java @@ -14,16 +14,26 @@ import me.chanjar.weixin.mp.bean.invoice.merchant.*; *

* 流程文档: https://developers.weixin.qq.com/doc/offiaccount/WeChat_Invoice/E_Invoice/Vendor_and_Invoicing_Platform_Mode_Instruction.html * 接口文档: https://developers.weixin.qq.com/doc/offiaccount/WeChat_Invoice/E_Invoice/Vendor_API_List.html + * + * @author Mario Luo */ public interface WxMpMerchantInvoiceService { /** * 获取开票授权页链接 + * + * @param params the params + * @return the auth page url + * @throws WxErrorException the wx error exception */ InvoiceAuthPageResult getAuthPageUrl(InvoiceAuthPageRequest params) throws WxErrorException; /** * 获得用户授权数据 + * + * @param params the params + * @return the auth data + * @throws WxErrorException the wx error exception */ InvoiceAuthDataResult getAuthData(InvoiceAuthDataRequest params) throws WxErrorException; @@ -32,16 +42,25 @@ public interface WxMpMerchantInvoiceService { *

* 场景: 用户授权填写数据无效 * 结果: 用户会收到一条开票失败提示 + * + * @param params the params + * @throws WxErrorException the wx error exception */ void rejectInvoice(InvoiceRejectRequest params) throws WxErrorException; /** * 开具电子发票 + * + * @param params the params + * @throws WxErrorException the wx error exception */ void makeOutInvoice(MakeOutInvoiceRequest params) throws WxErrorException; /** * 发票冲红 + * + * @param params the params + * @throws WxErrorException the wx error exception */ void clearOutInvoice(ClearOutInvoiceRequest params) throws WxErrorException; @@ -50,36 +69,57 @@ public interface WxMpMerchantInvoiceService { * * @param fpqqlsh 发票请求流水号 * @param nsrsbh 纳税人识别号 + * @return the invoice result + * @throws WxErrorException the wx error exception */ InvoiceResult queryInvoiceInfo(String fpqqlsh, String nsrsbh) throws WxErrorException; /** * 设置商户联系方式, 获取授权链接前需要设置商户联系信息 + * + * @param contact the contact + * @throws WxErrorException the wx error exception */ void setMerchantContactInfo(MerchantContactInfo contact) throws WxErrorException; /** * 获取商户联系方式 + * + * @return the merchant contact info + * @throws WxErrorException the wx error exception */ MerchantContactInfo getMerchantContactInfo() throws WxErrorException; /** * 配置授权页面字段 + * + * @param authPageSetting the auth page setting + * @throws WxErrorException the wx error exception */ void setAuthPageSetting(InvoiceAuthPageSetting authPageSetting) throws WxErrorException; /** * 获取授权页面配置 + * + * @return the auth page setting + * @throws WxErrorException the wx error exception */ InvoiceAuthPageSetting getAuthPageSetting() throws WxErrorException; /** * 设置商户开票平台信息 + * + * @param merchantInvoicePlatformInfo the merchant invoice platform info + * @throws WxErrorException the wx error exception */ void setMerchantInvoicePlatform(MerchantInvoicePlatformInfo merchantInvoicePlatformInfo) throws WxErrorException; /** * 获取商户开票平台信息 + * + * @param merchantInvoicePlatformInfo the merchant invoice platform info + * @return the merchant invoice platform + * @throws WxErrorException the wx error exception */ MerchantInvoicePlatformInfo getMerchantInvoicePlatform(MerchantInvoicePlatformInfo merchantInvoicePlatformInfo) throws WxErrorException; } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMerchantInvoiceServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMerchantInvoiceServiceImpl.java index ffae3ddf1..11883cded 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMerchantInvoiceServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMerchantInvoiceServiceImpl.java @@ -1,8 +1,7 @@ package me.chanjar.weixin.mp.api.impl; -import com.google.gson.FieldNamingPolicy; +import com.google.common.collect.ImmutableMap; import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import lombok.AllArgsConstructor; import me.chanjar.weixin.common.error.WxErrorException; @@ -11,96 +10,85 @@ import me.chanjar.weixin.mp.api.WxMpMerchantInvoiceService; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.invoice.merchant.*; import me.chanjar.weixin.mp.enums.WxMpApiUrl; +import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; -import java.util.HashMap; import java.util.Map; import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Invoice.*; +/** + * @author Mario Luo + */ @AllArgsConstructor public class WxMpMerchantInvoiceServiceImpl implements WxMpMerchantInvoiceService { - - private WxMpService wxMpService; - private WxMpCardService wxMpCardService; - - private final static Gson gson; - - static { - gson = new GsonBuilder() - .disableHtmlEscaping() - .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) - .create(); - } + private final WxMpService wxMpService; + private final WxMpCardService wxMpCardService; @Override public InvoiceAuthPageResult getAuthPageUrl(InvoiceAuthPageRequest params) throws WxErrorException { String ticket = wxMpCardService.getCardApiTicket(); params.setTicket(ticket); - return doCommonInvoiceHttpPost(GET_AUTH_URL, params, InvoiceAuthPageResult.class); + return this.doCommonInvoiceHttpPost(GET_AUTH_URL, params, InvoiceAuthPageResult.class); } @Override public InvoiceAuthDataResult getAuthData(InvoiceAuthDataRequest params) throws WxErrorException { - return doCommonInvoiceHttpPost(GET_AUTH_DATA, params, InvoiceAuthDataResult.class); + return this.doCommonInvoiceHttpPost(GET_AUTH_DATA, params, InvoiceAuthDataResult.class); } @Override public void rejectInvoice(InvoiceRejectRequest params) throws WxErrorException { - doCommonInvoiceHttpPost(REJECT_INSERT, params, null); + this.doCommonInvoiceHttpPost(REJECT_INSERT, params, null); } @Override public void makeOutInvoice(MakeOutInvoiceRequest params) throws WxErrorException { - doCommonInvoiceHttpPost(MAKE_OUT_INVOICE, params, null); + this.doCommonInvoiceHttpPost(MAKE_OUT_INVOICE, params, null); } @Override public void clearOutInvoice(ClearOutInvoiceRequest params) throws WxErrorException { - doCommonInvoiceHttpPost(CLEAR_OUT_INVOICE, params, null); + this.doCommonInvoiceHttpPost(CLEAR_OUT_INVOICE, params, null); } @Override public InvoiceResult queryInvoiceInfo(String fpqqlsh, String nsrsbh) throws WxErrorException { - Map data = new HashMap(); - data.put("fpqqlsh", fpqqlsh); - data.put("nsrsbh", nsrsbh); - return doCommonInvoiceHttpPost(QUERY_INVOICE_INFO, data, InvoiceResult.class); + Map data = ImmutableMap.of("fpqqlsh", fpqqlsh, "nsrsbh", nsrsbh); + return this.doCommonInvoiceHttpPost(QUERY_INVOICE_INFO, data, InvoiceResult.class); } @Override public void setMerchantContactInfo(MerchantContactInfo contact) throws WxErrorException { - MerchantContactInfoWrapper data = new MerchantContactInfoWrapper(); - data.setContact(contact); - doCommonInvoiceHttpPost(SET_CONTACT_SET_BIZ_ATTR, data, null); + this.doCommonInvoiceHttpPost(SET_CONTACT_SET_BIZ_ATTR, new MerchantContactInfoWrapper(contact), null); } @Override public MerchantContactInfo getMerchantContactInfo() throws WxErrorException { - MerchantContactInfoWrapper merchantContactInfoWrapper = doCommonInvoiceHttpPost(GET_CONTACT_SET_BIZ_ATTR, null, MerchantContactInfoWrapper.class); + MerchantContactInfoWrapper merchantContactInfoWrapper = this.doCommonInvoiceHttpPost(GET_CONTACT_SET_BIZ_ATTR, null, MerchantContactInfoWrapper.class); return merchantContactInfoWrapper == null ? null : merchantContactInfoWrapper.getContact(); } @Override public void setAuthPageSetting(InvoiceAuthPageSetting authPageSetting) throws WxErrorException { - doCommonInvoiceHttpPost(SET_AUTH_FIELD_SET_BIZ_ATTR, authPageSetting, null); + this.doCommonInvoiceHttpPost(SET_AUTH_FIELD_SET_BIZ_ATTR, authPageSetting, null); } @Override public InvoiceAuthPageSetting getAuthPageSetting() throws WxErrorException { - return doCommonInvoiceHttpPost(GET_AUTH_FIELD_SET_BIZ_ATTR, new JsonObject(), InvoiceAuthPageSetting.class); + return this.doCommonInvoiceHttpPost(GET_AUTH_FIELD_SET_BIZ_ATTR, new JsonObject(), InvoiceAuthPageSetting.class); } @Override public void setMerchantInvoicePlatform(MerchantInvoicePlatformInfo paymchInfo) throws WxErrorException { MerchantInvoicePlatformInfoWrapper data = new MerchantInvoicePlatformInfoWrapper(); data.setPaymchInfo(paymchInfo); - doCommonInvoiceHttpPost(SET_PAY_MCH_SET_BIZ_ATTR, data, null); + this.doCommonInvoiceHttpPost(SET_PAY_MCH_SET_BIZ_ATTR, data, null); } @Override public MerchantInvoicePlatformInfo getMerchantInvoicePlatform(MerchantInvoicePlatformInfo merchantInvoicePlatformInfo) throws WxErrorException { - MerchantInvoicePlatformInfoWrapper result = doCommonInvoiceHttpPost(GET_PAY_MCH_SET_BIZ_ATTR, new JsonObject(), MerchantInvoicePlatformInfoWrapper.class); + MerchantInvoicePlatformInfoWrapper result = this.doCommonInvoiceHttpPost(GET_PAY_MCH_SET_BIZ_ATTR, new JsonObject(), MerchantInvoicePlatformInfoWrapper.class); return result == null ? null : result.getPaymchInfo(); } @@ -109,11 +97,15 @@ public class WxMpMerchantInvoiceServiceImpl implements WxMpMerchantInvoiceServic */ private T doCommonInvoiceHttpPost(WxMpApiUrl url, Object data, Class resultClass) throws WxErrorException { String json = ""; + final Gson gson = WxMpGsonBuilder.create(); if (data != null) { json = gson.toJson(data); } String responseText = wxMpService.post(url, json); - if (resultClass == null) return null; + if (resultClass == null) { + return null; + } + return gson.fromJson(responseText, resultClass); } } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/invoice/merchant/MerchantContactInfo.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/invoice/merchant/MerchantContactInfo.java index 569fffa6b..c52ef5728 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/invoice/merchant/MerchantContactInfo.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/invoice/merchant/MerchantContactInfo.java @@ -1,14 +1,19 @@ package me.chanjar.weixin.mp.bean.invoice.merchant; +import com.google.gson.annotations.SerializedName; import lombok.Data; import java.io.Serializable; /** * 商户联系信息 + * + * @author Mario Luo */ @Data public class MerchantContactInfo implements Serializable { + private static final long serialVersionUID = -2008465944249686100L; + /** * 联系电话 */ @@ -17,6 +22,7 @@ public class MerchantContactInfo implements Serializable { /** * 开票超时时间 */ + @SerializedName("time_out") private Integer timeout; } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/invoice/merchant/MerchantContactInfoWrapper.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/invoice/merchant/MerchantContactInfoWrapper.java index 3ceed2b6d..27a1d147b 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/invoice/merchant/MerchantContactInfoWrapper.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/invoice/merchant/MerchantContactInfoWrapper.java @@ -1,16 +1,19 @@ package me.chanjar.weixin.mp.bean.invoice.merchant; +import lombok.AllArgsConstructor; import lombok.Data; import java.io.Serializable; /** * 设置商户联系信息和发票过时时间参数 + * + * @author Mario Luo */ @Data +@AllArgsConstructor public class MerchantContactInfoWrapper implements Serializable { + private static final long serialVersionUID = -5377979396495452212L; private MerchantContactInfo contact; - - }