🎨 🐛 #1898 【公众号】微信商户电子发票代码规范化及优化,并修复设置商户联系方式参数问题

This commit is contained in:
Binary Wang 2020-12-21 00:45:57 +08:00
parent 06d45dc5fe
commit 155501152a
4 changed files with 76 additions and 35 deletions

View File

@ -14,16 +14,26 @@ import me.chanjar.weixin.mp.bean.invoice.merchant.*;
* <p>
* 流程文档: 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 {
* <p>
* 场景: 用户授权填写数据无效
* 结果: 用户会收到一条开票失败提示
*
* @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;
}

View File

@ -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<String, String> 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> T doCommonInvoiceHttpPost(WxMpApiUrl url, Object data, Class<T> 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);
}
}

View File

@ -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;
}

View File

@ -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;
}