SpringBoot+UniApp+微信商家券使用

Reference

微信官方文档

微信支付商家券API简介: https://pay.weixin.qq.com/wik...

后端

第三方集成服务

Github-WxJava: https://github.com/Wechat-Gro...

前端

小程序发券插件

官方小程序发券插件: https://pay.weixin.qq.com/wik...
uniapp 引用小程序发券插件的使用经验: https://ask.dcloud.net.cn/art...

后端

API: https://pay.weixin.qq.com/wik...

引入

pom.xml


    com.github.binarywang
    weixin-java-pay
    4.1.0

application.yml

注意: wx.pay.mchKey 是微信支付apiv2的signkey, 加密的时候V2和V3的key都可能用到,详细情况看文档要求
wx:
  pay:
    appId: wxoinweoih3098340h #微信公众号或者小程序等的appid
    mchId: 1234456789 #微信支付商户号
    certSerialNo: KOKLJB45BB45KB45OIBOB45OUBB45 #商户在微信公众平台申请服务号对应的APPID
    apiv3Key: kljbawerkuoin324ointoin34 #微信支付商户密钥V3秘钥Key
    mchKey: oeairbngonegnoINoijOIHNoino #微信支付商户密钥V2秘钥Key
    certificates: https://api.mch.weixin.qq.com/v3/certificates #微信获取平台证书列表地址
    #微信统一下单的API地址,用于二维码支付
    payScoreNotifyUrl: https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi
    callbackUrl: https://api.sampleabc.com/api/pay/callback/    #异步接收微信支付结果通知的回调地址
    subAppId: #服务商模式下的子商户公众账号ID
    subMchId: #服务商模式下的子商户号
    privateKeyPath:  D:/samplePathabc/apiclient_key.pem
    privateCertPath:  D:/samplePathabc/apiclient_cert.pem
    # p12证书的位置,可以指定绝对路径,也可以指定类路径(以classpath:开头)
    keyPath: D:/samplePathabc/apiclient_cert.p12

相关类

以下类可以在项目中找到,git clone下来后寻找即可,通过测试类可以知道用法

接口: https://github.com/Wechat-Gro...

实现类: https://github.com/Wechat-Gro...

测试类: https://github.com/Wechat-Gro...

生成商家券

// 生成券
@PostMapping(value = "/port/to/create")
public String createCoupon(@RequestBody Map body) throws Exception {

    String stockId = this.testCreateFavorStocksV3();

    return stockId;
}


// 生成商户券
public String testCreateFavorStocksV3() throws WxPayException {
    BusiFavorStocksCreateRequest request = new BusiFavorStocksCreateRequest();
    request.setStockName("这就是商家券666");
    request.setBelongMerchant(wxPayService.getConfig().getMchId());
    request.setComment("这就是评论了");
    request.setGoodsName("仅供leonard使用");
    request.setCouponCodeMode("WECHATPAY_MODE");
    request.setOutRequestNo(wxPayService.getConfig().getMchId() + "20210204" + "1024102410");

    //核销规则
    CouponUseRule couponUseRule = new CouponUseRule();

    //线下核销
    couponUseRule.setUseMethod("OFF_LINE");
    // 小程序核销
    // couponUseRule.setUseMethod("MINI_PROGRAMS");
    // couponUseRule.setMiniProgramsAppid("小程序APPID");
    //couponUseRule.setMiniProgramsPath("/pages/index/index/abc.html?sadf=xxx");

    //券可核销时间
    CouponAvailableTime couponAvailableTime = new CouponAvailableTime();
    couponAvailableTime.setAvailableBeginTime("2021-10-22T13:29:35+08:00");
    couponAvailableTime.setAvailableEndTime("2021-10-29T13:29:35+08:00");
    couponUseRule.setCouponAvailableTime(couponAvailableTime);

    //固定面额满减券
    request.setStockType(StockTypeEnum.NORMAL);
    FixedNormalCoupon fixedNormalCoupon = new FixedNormalCoupon();
    fixedNormalCoupon.setDiscountAmount(31600);
    fixedNormalCoupon.setTransactionMinimum(98400);
    couponUseRule.setFixedNormalCoupon(fixedNormalCoupon);
    request.setCouponUseRule(couponUseRule);

    //发放规则
    StockSendRule stockSendRule = new StockSendRule();
    stockSendRule.setMaxCoupons(108);
    stockSendRule.setMaxCouponsPerUser(5);
    request.setStockSendRule(stockSendRule);

    BusiFavorStocksCreateResult result = wxPayService.getMarketingBusiFavorService().createBusiFavorStocksV3(request);
    String stockId = result.getStockId();


    System.out.println("stockId: [{}]" + stockId);

    return stockId;
}

查询商家券详情

// 获取券详情
@PostMapping(value = "/port/to/get")
public AjaxResult getCouponDetails(@RequestBody Map body) throws Exception {
    String stockId = body.get("stockId");

    BusiFavorStocksGetResult result = this.testGetBusiFavorStocksV3(stockId);

    return AjaxResult.success(result);
}

//  获取商家券详细信息
public BusiFavorStocksGetResult testGetBusiFavorStocksV3(String stockId) throws WxPayException {
    BusiFavorStocksGetResult result = wxPayService.getMarketingBusiFavorService().getBusiFavorStocksV3(stockId);
    System.out.println(result);

    return result;
}

生成小程序版本 发商户券 请求所需参数

@Resource
private WxPayService wxPayService;

// 生成小程序版本 发商户券 请求所需参数
@GetMapping(value = "/get/coupon/params/{stockId}")
public Map generateCouponParams(@PathVariable("stockId") String stockId) throws Exception {
    Coupon coupon = couponMapper.selectCouponByStockId(stockId);

    BusiFavorCouponsUrlRequest request = new BusiFavorCouponsUrlRequest();
    request.setOutRequestNo(coupon.getOutRequestNo);
    request.setSendCouponMerchant(this.wxPayService.getConfig().getMchKey());
    request.setStockId(stockId);
    // 签名
    String sign = this.buildBusiFavorCouponinfoMiniapp(request);

    Map result = new HashMap<>();
    result.put("stock_id", stockId);
    result.put("out_request_no", request.getOutRequestNo());
    result.put("sign", request.getSign());
    result.put("send_coupon_merchant", request.getSendCouponMerchant());

    return result;
}

// 签名
public String buildBusiFavorCouponinfoMiniapp(BusiFavorCouponsUrlRequest request) throws WxPayException {

    // 官方签名拼接要求(注意out_request_no0和stock_id0字段最后有0,即使是1张券)
    // out_request_no0=abc123&send_coupon_merchant=10016226&stock_id0=1234567&key=xxxxx

    Map signMap = new HashMap<>(8);
    signMap.put("out_request_no0", request.getOutRequestNo());
    signMap.put("send_coupon_merchant", request.getSendCouponMerchant());
    signMap.put("stock_id0", request.getStockId());

    String sign = SignUtils.createSign(signMap, WxPayConstants.SignType.HMAC_SHA256, this.wxPayService.getConfig().getMchKey(), null);

    return sign;
}

前端

引入

step 1:

在 manifest.json 中点击源码视图, 在 mp-weixin 里添加:

"plugins": {  
    "sendCoupon": {  
        "version": "latest",  
        "provider": "wxf3f436ba9bd4be7b"  
    }  
}

step2:

在 pages.json 的 globalStyle 里添加

"usingComponents": {  
    "send-coupon": "plugin://sendCoupon/send-coupon"  
}

step3:

在需要调用插件的页面中调用, 按照微信的对接文档对接即可. 唯一需要注意的是, 发券的事件bindcustomevent改为 @customevent



END

你可能感兴趣的:(前端springboot)