一 前言
讀本文前請確保商戶號已開通免充值立減與折扣。否則請看 微信支付-免充值立減與折扣(上篇)。
本文主要講述如何創建優惠券,統一下單和支付通知怎樣支持使用優惠券。
二 準備
2.1 必要前提
你要有微信公衆號支付/小程序支付/H5支付的開發經驗(本文以公衆號支付爲例),否則先看我的上一篇文章 移動支付--微信公衆號支付開發;
2.2 必讀文檔
三 過程
3.1 創建優惠券
3.1.1 哪裏創建
步驟:微信商戶平臺 - 營銷中心 - 代金券/立減與折扣
3.1.2 創建一張 立減與折扣 - 全場立減優惠券
1、 設置優惠券規則。
2、設置優惠券規則。
3、高級設置截圖 。
4、確認創建。
5、 創建成功,需要激活後才能使用。
3.1.3 創建一張 立減與折扣 - 創建單品立減優惠券
1、設置優惠券規則
2、設置單品優惠規則。
3、設置優惠券規則。
4、創建優惠券。
5、 創建成功,需要激活後才能使用。
3.2 統一下單接口修改
根據文檔 統一下單API(支持單品優惠) ,統一下單接口做了以下修改,附修改代碼:
//元轉分
Double moneyd = Double.parseDouble(money) * 100;
Integer moneyi = moneyd.intValue();
String amount = moneyi.toString();
parameters.put("appid", appId);
parameters.put("mch_id", mchId);
parameters.put("nonce_str", WeixinUtil.create_nonce_str());
parameters.put("body", orderDetail);
parameters.put("out_trade_no", orderId);
parameters.put("total_fee", amount);
parameters.put("spbill_create_ip", spbill_create_ip);
parameters.put("notify_url", notify_url);
parameters.put("trade_type", "JSAPI");
// 微信支付-優惠券-參數配置,start--------------------------------------------------------------
// 單品信息,使用Json數組格式提交
JSONObject goodsDetailObj = new JSONObject();
goodsDetailObj.put("goods_id", goodsId); // 商品編碼(必填)
goodsDetailObj.put("wxpay_goods_id", ""); // 微信支付定義的統一商品編號(沒有可不傳)
goodsDetailObj.put("goods_name", orderDetail); // 商品的實際名稱
goodsDetailObj.put("quantity", 1); // 用戶購買的數量(必填)
goodsDetailObj.put("price", amount); // 商品單價(必填),單位爲:分。如果商戶有優惠,需傳輸商戶優惠後的單價(例如:用戶對一筆100元的訂單使用了商場發的紙質優惠券100-50,則活動商品的單價應爲原單價-50)
List<JSONObject> goodsDetailList = new ArrayList<>();
goodsDetailList.add(goodsDetailObj);
// 單品優惠活動該字段
JSONObject detailObj = new JSONObject();
detailObj.put("cost_price", Integer.parseInt(amount)); // (單品優惠必填)1.商戶側一張小票訂單可能被分多次支付,訂單原價用於記錄整張小票的交易金額。2.當訂單原價與支付金額不相等,則不享受優惠。3.該字段主要用於防止同一張小票分多次支付,以享受多次優惠的情況,正常支付訂單不必上傳此參數。
detailObj.put("receipt_id", ""); // 商家小票ID
detailObj.put("goods_detail", goodsDetailList); // 單品信息(必填),使用Json數組格式提交
parameters.put("version", "1.0"); // 接口版本號(必填),區分原接口,默認填寫1.0。入參新增version後,則支付通知接口也將返回單品優惠信息字段promotion_detail,請確保支付通知的簽名驗證能通過。
parameters.put("goods_tag",goodsTag); // 訂單優惠標記,用於區分訂單是否可以享受優惠
parameters.put("detail", detailObj.toJSONString()); // 單品優惠活動該字段必傳(必填)
// 微信支付-優惠券-參數配置,end----------------------------------------------------------------
parameters.put("openid", openid);
注意:對於單品優惠優惠券,單品優惠活動detail字段列表 中 cost_price 字段必填(這個字段文檔中爲非必填~~),否則無法使用單品優惠。
3.3 支付通知代碼修改
爲了方便以後統計分析,支付通知代碼中增加了記錄使用的優惠券信息。下面附上獲取這些信息的代碼:
String orderId = (String) map.get("out_trade_no"); // 獲取訂單ID
String transactionId = (String) map.get("transaction_id"); // 獲取微信訂單號
String totalFee = (String) map.get("total_fee"); // 獲取訂單總額,單位爲分
String timeEnd = (String) map.get("time_end"); // 獲取訂單支付完成時間
String cashFee = (String) map.get("cash_fee"); // 現金支付金額,單位爲分
//String couponFee = (String) map.get("coupon_fee"); // 支付優惠總額,單位爲分
String payBank = (String) map.get("bank_type"); // 付款銀行,付款方式,詳情請看:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_2
String promotionDetail = (String) map.get("promotion_detail"); // 營銷詳情。統一下單API上傳參數version後纔有返回,返回值爲Json格式。
JSONObject marketingObj = new JSONObject();
String promotion_name = ""; // 優惠券名稱
String promotion_amount = ""; // 用戶享受優惠的金額
String promotion_activityId = ""; // 在微信商戶後臺配置的批次ID,用於辨別使用的哪張優惠券
// 判斷是否使用支付優惠券
if (StringUtils.isNotBlank(promotionDetail)) {
JSONObject promotionObj = JSONObject.parseObject(promotionDetail);
if (promotionObj != null) {
String detail = promotionObj.getString("promotion_detail");
if (StringUtils.isNotBlank(detail)) {
JSONArray promotionDetailArray= JSONArray.parseArray(detail);
if (promotionDetailArray != null && promotionDetailArray.size() > 0) {
// 本業務只有一條子商品信息,故直接獲取第一條
JSONObject promotionDetailObj = promotionDetailArray.getJSONObject(0);
promotion_activityId = promotionDetailObj.getString("activity_id");
promotion_name = promotionDetailObj.getString("name");
promotion_amount = promotionDetailObj.getString("amount");
}
}
}
}
求解:對於有使用優惠的訂單,微信會在支付通知中返回 promotion_detail字段列表,該參數中有一個promotion_id(券ID) 字段,可這個字段在 商戶平臺中找不到記錄(也就是使用該字段無法查詢優惠券詳細信息),不明白有什麼意義,希望有知道的同學可以解答下。
四、問題:
1)假如有兩張優惠券,分別是 全場立減券A 和 單品立減券B,這兩張優惠券設置的訂單優惠標記(即goods_tag)一樣,不同點是優惠券B同時設置了商品編碼(即goods_id),在同一下單接口同時傳了 goods_tag 和 goods_id 參數,此時微信在用戶支付時會優先使用哪張優惠券?
經測試會優先使用單品立減券B,且在支付界面中不會展示 優惠券A。
2)假如又有兩張優惠券,分別是 全場立減券A 和 全場代金券B,這兩張優惠券設置的訂單優惠標記(即goods_tag)一樣,在用戶領取了優惠券B的前提下,微信在用戶支付時會優先使用哪張券?
經測試會優先使用全場代金券B。
五、補充:
5.1 爲方便小夥伴們開發,附上各種優惠券支付通知接收參數:
1)立減與折扣 - 全場立減 通知回調接收參數:
{
"appid": "test",
"bank_type": "CFT",
"cash_fee": "1",
"fee_type": "CNY",
"is_subscribe": "Y",
"mch_id": "1234567890",
"nonce_str": "0b3743772a2a4d8b8b9005c64d47623f",
"openid": "test",
"out_trade_no": "010100032018111400067818",
"promotion_detail": "{\"promotion_detail\":[{\"promotion_id\":\"12345678\",\"name\":\"立減與折扣-全場滿減\",\"scope\":\"GLOBAL\",\"type\":\"DISCOUNT\",\"amount\":1000,\"activity_id\":\"1234567\",\"wxpay_contribute\":0,\"merchant_contribute\":1000,\"other_contribute\":0}]}",
"result_code": "SUCCESS",
"return_code": "SUCCESS",
"sign": "6EE6BB2EF05C702FD00C550D82E8DE31",
"time_end": "20181116170811",
"total_fee": "1001",
"trade_type": "JSAPI",
"transaction_id": "4200000206201811165319882354",
"version": "1.0"
}
2)立減與折扣 - 單品立減通知回調接收參數:
{
"appid": "test",
"bank_type": "PSBC_DEBIT",
"cash_fee": "1",
"fee_type": "CNY",
"is_subscribe": "Y",
"mch_id": "test",
"nonce_str": "0bd1e91037834c658b30081241bc6460",
"openid": "test_openid",
"out_trade_no": "0101000320181114000627",
"promotion_detail": "{\"promotion_detail\":[{\"promotion_id\":\"test\",\"name\":\"立減與折扣-單品滿減\",\"scope\":\"SINGLE\",
\"type\":\"DISCOUNT\",\"amount\":3000,\"activity_id\":\"test\",\"wxpay_contribute\":0,
\"merchant_contribute\":3000,\"other_contribute\":0,\"goods_detail\":[{\"goods_id\":\"010003\",\"quantity\":1,\"price\":3001,\"discount_amount\":3000}]}]}",
"result_code": "SUCCESS",
"return_code": "SUCCESS",
"sign": "73195D1130F14C3797858BD23883B492",
"time_end": "20181114165912",
"total_fee": "3001",
"trade_type": "JSAPI",
"transaction_id": "4200000215201811145730796824",
"version": "1.0"
}
2)立減與折扣 - 全場折扣通知回調接收參數:
{
"appid": "test",
"bank_type": "CFT",
"cash_fee": "1",
"fee_type": "CNY",
"is_subscribe": "Y",
"mch_id": "1234567890",
"nonce_str": "66f3049df96b4176953911e5bad40f3b",
"openid": "test",
"out_trade_no": "010100032018111400067823",
"promotion_detail": "{\"promotion_detail\":[{\"promotion_id\":\"1234567890\",\"name\":\"全場折扣優惠\",\"scope\":\"GLOBAL\",\"type\":\"DISCOUNT\",\"amount\":99,\"activity_id\":\"1234567\",\"wxpay_contribute\":0,\"merchant_contribute\":99,\"other_contribute\":0}]}",
"result_code": "SUCCESS",
"return_code": "SUCCESS",
"sign": "E59747EACFE5410FEC5205CA292F9D31",
"time_end": "20181116181640",
"total_fee": "100",
"trade_type": "JSAPI",
"transaction_id": "4200000205201811169229040652",
"version": "1.0"
}
4)代金券 - 全場券通知回調接收參數:
{
"appid": "test",
"bank_type": "CFT",
"cash_fee": "1",
"fee_type": "CNY",
"is_subscribe": "Y",
"mch_id": "1234567890",
"nonce_str": "ad3dc6346e894667851a2d6b9d888109",
"openid": "test",
"out_trade_no": "010100032018111400067820",
"promotion_detail": "{\"promotion_detail\":[{\"promotion_id\":\"1234567890\",\"name\":\"代金券-全場券測試\",\"scope\":\"GLOBAL\",\"type\":\"DISCOUNT\",\"amount\":1000,\"activity_id\":\"1234567\",\"wxpay_contribute\":0,\"merchant_contribute\":1000,\"other_contribute\":0}]}",
"result_code": "SUCCESS",
"return_code": "SUCCESS",
"sign": "B557FFD756E05FFA8E6A3DFF216715A5",
"time_end": "20181116175129",
"total_fee": "1001",
"trade_type": "JSAPI",
"transaction_id": "4200000206201811167791965300",
"version": "1.0"
}
5)代金券 - 單品券通知回調接收參數:
{
"appid": "test",
"bank_type": "CFT",
"cash_fee": "1",
"fee_type": "CNY",
"is_subscribe": "Y",
"mch_id": "1234567890",
"nonce_str": "c1caf40c84844af09bb8698fc4193d66",
"openid": "test",
"out_trade_no": "010100032018111400067821",
"promotion_detail": "{\"promotion_detail\":[{\"promotion_id\":\"1234567890\",\"name\":\"單品券測試\",\"scope\":\"SINGLE\",\"type\":\"DISCOUNT\",\"amount\":1000,\"activity_id\":\"1234567\",\"wxpay_contribute\":0,\"merchant_contribute\":1000,\"other_contribute\":0,\"goods_detail\":[{\"goods_id\":\"020001\",\"quantity\":1,\"price\":1001,\"discount_amount\":1000}]}]}",
"result_code": "SUCCESS",
"return_code": "SUCCESS",
"sign": "E04FC81B9D38B0208111F5FDCF18BB5E",
"time_end": "20181116175726",
"total_fee": "1001",
"trade_type": "JSAPI",
"transaction_id": "4200000208201811167067822806",
"version": "1.0"
}