微信卡券的领取签名参照 https://blog.youkuaiyun.com/SongJingzhou/article/details/105517841
package com.yzf.mall.services.support.wxpay.service.impl;
import cn.hutool.core.lang.Dict;
import com.google.common.collect.Lists;
import com.yzf.mall.core.utils.JsonUtils;
import com.yzf.mall.services.support.util.HttpClient;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class WxCoupon {
private String logoUrl = "http://mmbiz.qpic.cn/mmbiz/iaL1LJM1mF9aRKPZJkmG8xXhiaHqkKSVMMWeN3hLut7X7hicFNjakmxibMLGWpXrEXB33367o7zHN0CwngnQY7zb7g/0";
private String brandName = "某某商家";
private static CloseableHttpClient httpclient = HttpClients.createDefault();
private static String CHAR_SET = "UTF-8";
/**
* 解密微信卡券code
*/
public String _decryptWxCardCode(String code) throws IOException {
String url = "https://api.weixin.qq.com/card/code/decrypt?access_token="
+ "获取微信accessToken"; //_getAccessToken();
try {
String res = doPostString(url, "{\"encrypt_code\":\"" + code + "\"}",
ContentType.APPLICATION_JSON);
return Objects.requireNonNull(JsonUtils.parseBean(res, Dict.class)).getStr("code");
} catch (IOException e) {
throw e;
}
}
/**
* 创建微信卡券
*
* @param wxCardInfo 需要的参数信息
* @return 微信端生成的卡券Id,经过重新签名可用于前端领取优惠券,签名参考 https://blog.youkuaiyun.com/SongJingzhou/article/details/105517841
*/
public String createWxCard(WxCardInfo wxCardInfo) throws IOException {
String url = "https://api.weixin.qq.com/card/create?access_token="
+ "获取微信accessToken"; //_getAccessToken();
try {
String body = _packageWxCardInfo(wxCardInfo);
String res = doPostString(url, body, ContentType.APPLICATION_JSON);
return Objects.requireNonNull(JsonUtils.parseBean(res, Dict.class)).getStr("card_id");
} catch (IOException e) {
throw e;
}
}
private String _packageWxCardInfo(WxCardInfo wxCardInfo) {
Dict coupon = Dict.create();
coupon.put("base_info", _packageWxCardBaseInfo(wxCardInfo));
coupon.put("advanced_info", _packageWxCardAdvancedInfo());
coupon.put("default_detail", wxCardInfo.getDefaultDetail());
Dict card = Dict.create();
card.put("card_type", "GENERAL_COUPON");
card.put("general_coupon", coupon);
return JsonUtils.toString(Dict.create().set("card", card));
}
private Dict _packageWxCardBaseInfo(WxCardInfo wxCardInfo) {
Dict baseInfo = Dict.create();
// 可以通过接口上传至微信服务器
baseInfo.put("logo_url", logoUrl);
baseInfo.put("brand_name", brandName);
/*
* 码型: "CODE_TYPE_TEXT"文 本 "CODE_TYPE_BARCODE"一维码 "CODE_TYPE_QRCODE"二维码
* "CODE_TYPE_ONLY_QRCODE",二维码无code显示 "CODE_TYPE_ONLY_BARCODE" 一维码无code显示 CODE_TYPE_NONE,
* 不显示code和条形码类型
*/
baseInfo.put("code_type", "CODE_TYPE_ONLY_BARCODE");
baseInfo.put("title", wxCardInfo.getTitle());
baseInfo.put("color", "Color010");
baseInfo.put("notice", "使用提示");
baseInfo.put("description", wxCardInfo.getDescription());
Dict dateInfo = Dict.create();
dateInfo.put("type", wxCardInfo.getTimeType());
dateInfo.put("begin_timestamp", wxCardInfo.getStartTimeStamp());
dateInfo.put("end_timestamp", wxCardInfo.getEndTimeStamp());
dateInfo.put("fixed_term", wxCardInfo.getFixedTerm());
dateInfo.put("fixed_begin_term", wxCardInfo.getFixedBeginTerm());
baseInfo.put("date_info", dateInfo);
baseInfo.put("sku", Dict.create().set("quantity", wxCardInfo.getNum()));
// 每人领取数量由系统控制,此处不限制每人可领取数量,非零时限制每人的领取数量
baseInfo.put("get_limit", 0);
baseInfo.put("use_custom_code", false);
baseInfo.put("bind_openid", false);
baseInfo.put("can_share", false);
baseInfo.put("can_give_friend", false);
return baseInfo;
}
/**
* 为了让用户领取的优惠券能看到使用有效期设置,也可不设置
*/
private Dict _packageWxCardAdvancedInfo() {
Dict advanced = Dict.create();
List<String> days = Lists
.newArrayList("MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY");
List<Dict> timeLimit = Lists.newArrayList();
for (String day : days) {
Dict item = Dict.create();
item.put("begin_hour", 0);
item.put("begin_minute", 0);
item.put("end_hour", 59);
item.put("end_minute", 59);
item.put("type", day);
timeLimit.add(item);
}
advanced.put("time_limit", timeLimit);
return advanced;
}
/**
* 核销客户领取后的优惠券
*
* @param code 客户领取时的优惠券code
*/
public void consumeWxCardCode(String code) throws IOException {
String url = "https://api.weixin.qq.com/card/code/consume?access_token="
+ "获取微信accessToken"; //_getAccessToken();
try {
String res = HttpClient.doPostString(url, "{\"code\":\"" + _decryptWxCardCode(code) + "\"}",
ContentType.APPLICATION_JSON);
} catch (IOException e) {
throw e;
}
}
public static String doGetString(String url) throws IOException {
HttpGet httpGet = new HttpGet(url);
try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, CHAR_SET);
}
}
public static String doPostString(String url, String body, ContentType contentType)
throws IOException {
HttpPost httpPost = new HttpPost(url);
if (body != null) {
httpPost.setEntity(new StringEntity(body, contentType));
}
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, CHAR_SET);
}
}
}
class WxCardInfo {
/**
* 最长支持九个汉字
*/
private String title;
private String description;
/**
* DATE_TYPE_FIX_TIME_RANGE 表示固定日期区间,DATE_TYPE_FIX_TERM 表示固定时长,自领取后按天算。
*/
private String timeType;
private Integer fixedTerm;
private Integer fixedBeginTerm;
private Long startTimeStamp;
private Long endTimeStamp;
/**
* 优惠券数量
*/
private Integer num;
private String defaultDetail;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTimeType() {
return timeType;
}
public void setTimeType(String timeType) {
this.timeType = timeType;
}
public Integer getFixedTerm() {
return fixedTerm;
}
public void setFixedTerm(Integer fixedTerm) {
this.fixedTerm = fixedTerm;
}
public Integer getFixedBeginTerm() {
return fixedBeginTerm;
}
public void setFixedBeginTerm(Integer fixedBeginTerm) {
this.fixedBeginTerm = fixedBeginTerm;
}
public Long getStartTimeStamp() {
return startTimeStamp;
}
public void setStartTimeStamp(Long startTimeStamp) {
this.startTimeStamp = startTimeStamp;
}
public Long getEndTimeStamp() {
return endTimeStamp;
}
public void setEndTimeStamp(Long endTimeStamp) {
this.endTimeStamp = endTimeStamp;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
public String getDefaultDetail() {
return defaultDetail;
}
public void setDefaultDetail(String defaultDetail) {
this.defaultDetail = defaultDetail;
}
}
测试数据示例:
{
"card": {
"card_type": "GENERAL_COUPON",
"general_coupon": {
"base_info": {
"logo_url": "http://mmbiz.qpic.cn/mmbiz/iaL1LJM1mF9aRKPZJkmG8xXhiaHqkKSVMMWeN3hLut7X7hicFNjakmxibMLGWpXrEXB33367o7zHN0CwngnQY7zb7g/0",
"brand_name": "主题",
"code_type": "CODE_TYPE_NONE",
"title": "当天生效",
"sub_title": "无门槛",
"color": "Color010",
"notice": "请尽快使用",
"description": "描述呗 ",
"date_info": {
"type": "DATE_TYPE_FIX_TERM",
"begin_timestamp": null,
"end_timestamp": null,
"fixed_term": 1,
"fixed_begin_term": 0
},
"sku": {
"quantity": 10
},
"get_limit": 1,
"use_custom_code": false,
"bind_openid": false,
"can_share": false,
"can_give_friend": false
},
"advanced_info": {
"time_limit": [
{
"type": "MONDAY",
"begin_hour":0,
"end_hour":10,
"begin_minute":10,
"end_minute":59
},
{
"type": "TUESDAY",
"begin_hour":0,
"end_hour":10,
"begin_minute":10,
"end_minute":59
},
{
"type": "WEDNESDAY",
"begin_hour":0,
"end_hour":10,
"begin_minute":10,
"end_minute":59
},
{
"type": "THURSDAY",
"begin_hour":0,
"end_hour":10,
"begin_minute":10,
"end_minute":59
},
{
"type": "FRIDAY",
"begin_hour":0,
"end_hour":10,
"begin_minute":10,
"end_minute":59
},
{
"type": "SATURDAY",
"begin_hour":0,
"end_hour":10,
"begin_minute":10,
"end_minute":59
},
{
"type": "SUNDAY",
"begin_hour":0,
"end_hour":10,
"begin_minute":10,
"end_minute":59
}
]
},
"default_detail": "优惠券详情说明"
}
}
}