mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
#733 微信卡券增加会员卡新增,卡券二维码,添加白名单,新增卡券货架等接口
This commit is contained in:
@@ -2,12 +2,16 @@ package me.chanjar.weixin.mp.api;
|
||||
|
||||
import me.chanjar.weixin.common.bean.WxCardApiSignature;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.mp.bean.card.WxMpCardLandingPageCreateRequest;
|
||||
import me.chanjar.weixin.mp.bean.card.WxMpCardLandingPageCreateResult;
|
||||
import me.chanjar.weixin.mp.bean.card.WxMpCardQrcodeCreateResult;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpCardResult;
|
||||
|
||||
/**
|
||||
* 卡券相关接口
|
||||
*
|
||||
* @author YuJian(mgcnrx11@hotmail.com) on 01/11/2016
|
||||
* @author yuanqixun 2018-08-29
|
||||
*/
|
||||
public interface WxMpCardService {
|
||||
String CARD_GET = "https://api.weixin.qq.com/card/get";
|
||||
@@ -16,6 +20,9 @@ public interface WxMpCardService {
|
||||
String CARD_CODE_GET = "https://api.weixin.qq.com/card/code/get";
|
||||
String CARD_CODE_CONSUME = "https://api.weixin.qq.com/card/code/consume";
|
||||
String CARD_CODE_MARK = "https://api.weixin.qq.com/card/code/mark";
|
||||
String CARD_TEST_WHITELIST = "https://api.weixin.qq.com/card/testwhitelist/set";
|
||||
String CARD_QRCODE_CREAET = "https://api.weixin.qq.com/card/qrcode/create";
|
||||
String CARD_LANDING_PAGE_CREAET = "https://api.weixin.qq.com/card/landingpage/create";
|
||||
|
||||
/**
|
||||
* 得到WxMpService
|
||||
@@ -122,4 +129,27 @@ public interface WxMpCardService {
|
||||
* <br> 可由 com.google.gson.JsonParser#parse 等方法直接取JSON串中的某个字段。
|
||||
*/
|
||||
String getCardDetail(String cardId) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 添加测试白名单
|
||||
* @param openid 用户的openid
|
||||
* @return
|
||||
*/
|
||||
String addTestWhiteList(String openid) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 创建卡券二维码
|
||||
* @param cardId 卡券编号
|
||||
* @param outerStr 二维码标识
|
||||
* @return WxMpCardQrcodeCreateResult
|
||||
*/
|
||||
WxMpCardQrcodeCreateResult createQrcodeCard(String cardId, String outerStr) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 创建卡券货架
|
||||
* @param createRequest 货架创建参数
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
*/
|
||||
WxMpCardLandingPageCreateResult createLandingPage(WxMpCardLandingPageCreateRequest createRequest) throws WxErrorException;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.mp.bean.membercard.WxMpMemberCardActivatedMessage;
|
||||
import me.chanjar.weixin.mp.bean.membercard.WxMpMemberCardUpdateMessage;
|
||||
import me.chanjar.weixin.mp.bean.membercard.WxMpMemberCardUpdateResult;
|
||||
import me.chanjar.weixin.mp.bean.membercard.WxMpMemberCardUserInfoResult;
|
||||
import me.chanjar.weixin.mp.bean.card.WxMpCardCreateResult;
|
||||
import me.chanjar.weixin.mp.bean.card.WxMpCardQrcodeCreateResult;
|
||||
import me.chanjar.weixin.mp.bean.membercard.*;
|
||||
|
||||
/**
|
||||
* 会员卡相关接口
|
||||
@@ -19,11 +18,27 @@ public interface WxMpMemberCardService {
|
||||
*/
|
||||
WxMpService getWxMpService();
|
||||
|
||||
/**
|
||||
* 会员卡创建接口
|
||||
* @param createJson
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
*/
|
||||
WxMpCardCreateResult createMemberCard(String createJson) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 会员卡创建接口
|
||||
* @param createMessageMessage
|
||||
* @return WxMpCardCreateResult
|
||||
* @throws WxErrorException
|
||||
*/
|
||||
WxMpCardCreateResult createMemberCard(WxMpMemberCardCreateMessage createMessageMessage) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 会员卡激活接口
|
||||
*
|
||||
* @param activatedMessage 激活所需参数
|
||||
* @return 调用返回的JSON字符串。
|
||||
* @return 返回json字符串
|
||||
* @throws WxErrorException 接口调用失败抛出的异常
|
||||
*/
|
||||
String activateMemberCard(WxMpMemberCardActivatedMessage activatedMessage) throws WxErrorException;
|
||||
@@ -50,4 +65,5 @@ public interface WxMpMemberCardService {
|
||||
* @throws WxErrorException 接口调用失败抛出的异常
|
||||
*/
|
||||
WxMpMemberCardUpdateResult updateUserMemberCard(WxMpMemberCardUpdateMessage updateUserMessage) throws WxErrorException;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
package me.chanjar.weixin.mp.api.impl;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.*;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import me.chanjar.weixin.common.bean.WxCardApiSignature;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
@@ -13,6 +10,9 @@ import me.chanjar.weixin.common.util.crypto.SHA1;
|
||||
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
|
||||
import me.chanjar.weixin.mp.api.WxMpCardService;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.bean.card.WxMpCardLandingPageCreateRequest;
|
||||
import me.chanjar.weixin.mp.bean.card.WxMpCardLandingPageCreateResult;
|
||||
import me.chanjar.weixin.mp.bean.card.WxMpCardQrcodeCreateResult;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpCardResult;
|
||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
|
||||
import org.slf4j.Logger;
|
||||
@@ -30,6 +30,8 @@ public class WxMpCardServiceImpl implements WxMpCardService {
|
||||
|
||||
private WxMpService wxMpService;
|
||||
|
||||
private static final Gson GSON = new Gson();
|
||||
|
||||
public WxMpCardServiceImpl(WxMpService wxMpService) {
|
||||
this.wxMpService = wxMpService;
|
||||
}
|
||||
@@ -236,4 +238,51 @@ public class WxMpCardServiceImpl implements WxMpCardService {
|
||||
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加测试白名单
|
||||
*
|
||||
* @param openid 用户的openid
|
||||
* @return
|
||||
*/
|
||||
public String addTestWhiteList(String openid) throws WxErrorException {
|
||||
JsonArray array = new JsonArray();
|
||||
array.add(openid);
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.add("openid", array);
|
||||
String respone = this.wxMpService.post(CARD_TEST_WHITELIST, GSON.toJson(jsonObject));
|
||||
return respone;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建卡券二维码
|
||||
* @param cardId
|
||||
* @param outerStr
|
||||
* @return
|
||||
*/
|
||||
public WxMpCardQrcodeCreateResult createQrcodeCard(String cardId, String outerStr) throws WxErrorException {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("action_name", "QR_CARD");
|
||||
jsonObject.addProperty("expire_seconds", 1800);
|
||||
JsonObject actionInfoJson = new JsonObject();
|
||||
JsonObject cardJson = new JsonObject();
|
||||
cardJson.addProperty("card_id", cardId);
|
||||
cardJson.addProperty("outer_str", outerStr);
|
||||
actionInfoJson.add("card", cardJson);
|
||||
jsonObject.add("action_info", actionInfoJson);
|
||||
String response = this.wxMpService.post(CARD_QRCODE_CREAET, GSON.toJson(jsonObject));
|
||||
return WxMpCardQrcodeCreateResult.fromJson(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建卡券货架接口
|
||||
* @param request
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
*/
|
||||
@Override
|
||||
public WxMpCardLandingPageCreateResult createLandingPage(WxMpCardLandingPageCreateRequest request) throws WxErrorException {
|
||||
String response = this.wxMpService.post(CARD_LANDING_PAGE_CREAET,GSON.toJson(request));
|
||||
return WxMpCardLandingPageCreateResult.fromJson(response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +1,29 @@
|
||||
package me.chanjar.weixin.mp.api.impl;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.gson.*;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
import me.chanjar.weixin.mp.api.WxMpMemberCardService;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.bean.membercard.WxMpMemberCardActivatedMessage;
|
||||
import me.chanjar.weixin.mp.bean.membercard.WxMpMemberCardUpdateMessage;
|
||||
import me.chanjar.weixin.mp.bean.membercard.WxMpMemberCardUpdateResult;
|
||||
import me.chanjar.weixin.mp.bean.membercard.WxMpMemberCardUserInfoResult;
|
||||
import me.chanjar.weixin.mp.bean.card.*;
|
||||
import me.chanjar.weixin.mp.bean.membercard.*;
|
||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 会员卡相关接口的实现类
|
||||
*
|
||||
* @author YuJian(mgcnrx11@gmail.com)
|
||||
* @author YuJian(mgcnrx11 @ gmail.com)
|
||||
* @version 2017/7/8
|
||||
*/
|
||||
public class WxMpMemberCardServiceImpl implements WxMpMemberCardService {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(WxMpMemberCardServiceImpl.class);
|
||||
|
||||
private static final String MEMBER_CARD_CREAET = "https://api.weixin.qq.com/card/create";
|
||||
private static final String MEMBER_CARD_ACTIVATE = "https://api.weixin.qq.com/card/membercard/activate";
|
||||
private static final String MEMBER_CARD_USER_INFO_GET = "https://api.weixin.qq.com/card/membercard/userinfo/get";
|
||||
private static final String MEMBER_CARD_UPDATE_USER = "https://api.weixin.qq.com/card/membercard/updateuser";
|
||||
@@ -46,11 +44,164 @@ public class WxMpMemberCardServiceImpl implements WxMpMemberCardService {
|
||||
return this.wxMpService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员卡创建接口
|
||||
*
|
||||
* @param createJson 创建json
|
||||
* @return 调用返回的JSON字符串。
|
||||
* @throws WxErrorException 接口调用失败抛出的异常
|
||||
*/
|
||||
@Override
|
||||
public WxMpCardCreateResult createMemberCard(String createJson) throws WxErrorException {
|
||||
WxMpMemberCardCreateMessage createMessage = WxGsonBuilder.create().fromJson(createJson, WxMpMemberCardCreateMessage.class);
|
||||
return createMemberCard(createMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员卡创建接口
|
||||
*
|
||||
* @param createMessageMessage 创建所需参数
|
||||
* @return WxMpCardCreateResult。
|
||||
* @throws WxErrorException 接口调用失败抛出的异常
|
||||
*/
|
||||
@Override
|
||||
public WxMpCardCreateResult createMemberCard(WxMpMemberCardCreateMessage createMessageMessage) throws WxErrorException {
|
||||
//校验请求对象合法性
|
||||
WxMpCardCreateResult validResult = validCheck(createMessageMessage);
|
||||
if (!validResult.isSuccess())
|
||||
return validResult;
|
||||
String response = this.wxMpService.post(MEMBER_CARD_CREAET, GSON.toJson(createMessageMessage));
|
||||
return WxMpCardCreateResult.fromJson(response);
|
||||
}
|
||||
|
||||
private WxMpCardCreateResult validCheck(WxMpMemberCardCreateMessage createMessageMessage) throws WxErrorException {
|
||||
if (createMessageMessage == null) {
|
||||
return WxMpCardCreateResult.failure("对象不能为空");
|
||||
}
|
||||
MemberCardCreateRequest cardCreateRequest = createMessageMessage.getCardCreateRequest();
|
||||
if (createMessageMessage == null) {
|
||||
return WxMpCardCreateResult.failure("会员卡对象不能为空");
|
||||
}
|
||||
String cardType = cardCreateRequest.getCardType();
|
||||
if (!StringUtils.equals(cardType, "MEMBER_CARD")) {
|
||||
return WxMpCardCreateResult.failure("卡券类型必须等于MEMBER_CARD");
|
||||
}
|
||||
MemberCard memberCard = cardCreateRequest.getMemberCard();
|
||||
|
||||
if (StringUtils.isEmpty(memberCard.getPrerogative())) {
|
||||
return WxMpCardCreateResult.failure("会员卡特权说明不能为空:prerogative");
|
||||
}
|
||||
//卡片激活规则
|
||||
if (!memberCard.isAutoActivate() && !memberCard.isWxActivate() && StringUtils.isEmpty(memberCard.getActivateUrl())) {
|
||||
return WxMpCardCreateResult.failure("会员卡激活方式为接口激活,activate_url不能为空");
|
||||
}
|
||||
|
||||
//积分支持
|
||||
// if(memberCard.isSupplyBonus() && StringUtils.isEmpty(memberCard.getBonusUrl())){
|
||||
// return WxMpCardCreateResult.failure("会员卡支持积分,bonus_url不能为空");
|
||||
// }
|
||||
// if(memberCard.isSupplyBonus() && memberCard.getBonusRule() == null){
|
||||
// return WxMpCardCreateResult.failure("会员卡支持积分,bonus_rule不能为空");
|
||||
// }
|
||||
BaseInfo baseInfo = memberCard.getBaseInfo();
|
||||
if (baseInfo == null) {
|
||||
return WxMpCardCreateResult.failure("会员卡基本信息对象base_info不能为空");
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(baseInfo.getLogoUrl())) {
|
||||
return WxMpCardCreateResult.failure("会员卡基本信息的商户logo:logo_url不能为空");
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(baseInfo.getCodeType())) {
|
||||
return WxMpCardCreateResult.failure("会员卡基本信息的条码类型:code_type不能为空");
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(baseInfo.getBrandName())) {
|
||||
return WxMpCardCreateResult.failure("会员卡基本信息的商户名字:brand_name不能为空");
|
||||
}
|
||||
|
||||
if (StringUtils.length(baseInfo.getBrandName()) > 12) {
|
||||
return WxMpCardCreateResult.failure("会员卡基本信息的商户名字:brand_name长度不能大于12个汉字");
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(baseInfo.getTitle())) {
|
||||
return WxMpCardCreateResult.failure("会员卡基本信息的卡券名称:title不能为空");
|
||||
}
|
||||
|
||||
if (StringUtils.length(baseInfo.getTitle()) > 9) {
|
||||
return WxMpCardCreateResult.failure("会员卡基本信息的卡券名称:title长度不能大于9个汉字");
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(baseInfo.getColor())) {
|
||||
return WxMpCardCreateResult.failure("会员卡基本信息的卡颜色:color不能为空");
|
||||
}
|
||||
|
||||
CardColor cardColor = null;
|
||||
try {
|
||||
cardColor = CardColor.valueOf(baseInfo.getColor());
|
||||
} catch (IllegalArgumentException ex) {
|
||||
|
||||
}
|
||||
if (cardColor == null) {
|
||||
return WxMpCardCreateResult.failure("会员卡基本信息的卡颜色:" + baseInfo.getColor() + "不支持");
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(baseInfo.getNotice())) {
|
||||
return WxMpCardCreateResult.failure("会员卡基本信息的使用提醒:notice不能为空");
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(baseInfo.getDescription())) {
|
||||
return WxMpCardCreateResult.failure("会员卡基本信息的使用说明:description不能为空");
|
||||
}
|
||||
|
||||
if (baseInfo.getSku() == null) {
|
||||
return WxMpCardCreateResult.failure("会员卡基本信息的商品信息:sku不能为空");
|
||||
}
|
||||
|
||||
DateInfo dateInfo = baseInfo.getDateInfo();
|
||||
if (dateInfo == null) {
|
||||
return WxMpCardCreateResult.failure("会员卡基本信息的使用日期:date_info不能为空");
|
||||
}
|
||||
|
||||
DateInfoType dateInfoType = null;
|
||||
try {
|
||||
dateInfoType = DateInfoType.valueOf(dateInfo.getType());
|
||||
} catch (IllegalArgumentException ex) {
|
||||
|
||||
}
|
||||
|
||||
if (dateInfoType == null) {
|
||||
return WxMpCardCreateResult.failure("会员卡基本信息的使用日期类型:" + dateInfo.getType() + "不合法");
|
||||
}
|
||||
|
||||
//固定时长
|
||||
if (dateInfoType == DateInfoType.DATE_TYPE_FIX_TERM && (dateInfo.getFixedTerm() == null || dateInfo.getFixedBeginTerm() == null)) {
|
||||
return WxMpCardCreateResult.failure("会员卡基本信息的使用日期为:" + dateInfoType.getDescription() + ",fixedTerm和fixedBeginTerm不能为空");
|
||||
}
|
||||
|
||||
//固定期限
|
||||
if (dateInfoType == DateInfoType.DATE_TYPE_FIX_TIME_RANGE && (dateInfo.getBeginTimestamp() == null || dateInfo.getEndTimestamp() == null)) {
|
||||
return WxMpCardCreateResult.failure("会员卡基本信息的使用日期为:" + dateInfoType.getDescription() + ",beginTimestamp 和 endTimestamp 不能为空");
|
||||
}
|
||||
if (dateInfoType == DateInfoType.DATE_TYPE_FIX_TIME_RANGE && (dateInfo.getBeginTimestamp() < System.currentTimeMillis() || dateInfo.getEndTimestamp() < System.currentTimeMillis() || dateInfo.getBeginTimestamp() > dateInfo.getEndTimestamp())) {
|
||||
return WxMpCardCreateResult.failure("会员卡基本信息的使用日期为:" + dateInfoType.getDescription() + ",beginTimestamp和endTimestamp的值不合法,请检查");
|
||||
}
|
||||
|
||||
if (!baseInfo.isUseAllLocations() && StringUtils.isBlank(baseInfo.getLocationIdList())) {
|
||||
return WxMpCardCreateResult.failure("会员卡基本信息的门店使用范围选择指定门店,门店列表:locationIdList不能为空");
|
||||
}
|
||||
|
||||
//TODO 高级信息
|
||||
|
||||
|
||||
return WxMpCardCreateResult.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员卡激活接口
|
||||
*
|
||||
* @param activatedMessage 激活所需参数
|
||||
* @return 调用返回的JSON字符串。
|
||||
* @return WxMpCardCreateResult。
|
||||
* @throws WxErrorException 接口调用失败抛出的异常
|
||||
*/
|
||||
@Override
|
||||
@@ -70,7 +221,7 @@ public class WxMpMemberCardServiceImpl implements WxMpMemberCardService {
|
||||
public WxMpMemberCardUserInfoResult getUserInfo(String cardId, String code) throws WxErrorException {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("card_id", cardId);
|
||||
jsonObject.addProperty("code",code);
|
||||
jsonObject.addProperty("code", code);
|
||||
|
||||
String responseContent = this.getWxMpService().post(MEMBER_CARD_USER_INFO_GET, jsonObject.toString());
|
||||
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
|
||||
@@ -81,7 +232,7 @@ public class WxMpMemberCardServiceImpl implements WxMpMemberCardService {
|
||||
|
||||
/**
|
||||
* 当会员持卡消费后,支持开发者调用该接口更新会员信息。会员卡交易后的每次信息变更需通过该接口通知微信,便于后续消息通知及其他扩展功能。
|
||||
*
|
||||
* <p>
|
||||
* 1.开发者可以同时传入add_bonus和bonus解决由于同步失败带来的幂等性问题。同时传入add_bonus和bonus时
|
||||
* add_bonus作为积分变动消息中的变量值,而bonus作为卡面上的总积分额度显示。余额变动同理。
|
||||
* 2.开发者可以传入is_notify_bonus控制特殊的积分对账变动不发送消息,余额变动同理。
|
||||
@@ -101,4 +252,5 @@ public class WxMpMemberCardServiceImpl implements WxMpMemberCardService {
|
||||
new TypeToken<WxMpMemberCardUpdateResult>() {
|
||||
}.getType());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user