java对接微信代金券V3

微信代金券官方文档中有对接文档,但是根据官方文档很多地方不清晰,接下来给大家介绍一种简单的对接步骤。
官方文档:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/index.shtml
对接步骤:
准备工作:商户创建商品,激活商品等,提供证书等略过。

今天我们主要讲解java对接代金券发放接口和代金券查询接口https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter9_1_1.shtml
1、maven依赖

<dependency><groupId>com.github.wechatpay-apiv3</groupId><artifactId>wechatpay-apache-httpclient</artifactId><version>0.4.5</version></dependency>

2、对接发放代金券接口

@Service
public class WxTest {private static final Logger log = Logger.getLogger(UserClubCardServiceImpl.class);//商户id@Value("${mch_id}")private String mchId;//证书密码@Value("${serialno}")private String mchSerialNo;//访问微信的私钥文件地址 E:/file/key/wx_coupon/apiclient_key.pem@Value("${private_key_file}")private String merchantPrivateKeyFile;//证书地址 E:/file/key/wx_coupon/wechatpay_XXXXXpem@Value("${cert_file}")private String certificateFile;//改微信号的appid@Value("${appid}")private String appId;/*** 发放微信代金券*/public String sendCoupon(FavorCouponsCreateReq favorCouponsCreateReq) {FileInputStream privateKeyInputStream = null;FileInputStream certificateInputStream = null;try {privateKeyInputStream = new FileInputStream(new File(merchantPrivateKeyFile));certificateInputStream = new FileInputStream(new File(certificateFile));} catch (FileNotFoundException e) {e.printStackTrace();log.error("read merchantPrivateKeyFile or certificateFile faild, errmsg:" + e.getMessage());return null;}PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(privateKeyInputStream);X509Certificate wechatpayCertificate = PemUtil.loadCertificate(certificateInputStream);ArrayList<X509Certificate> wechatpayCertificates = new ArrayList<>();wechatpayCertificates.add(wechatpayCertificate);CloseableHttpClient httpClient = WechatPayHttpClientBuilder.create().withMerchant(mchId, mchSerialNo, merchantPrivateKey).withWechatPay(wechatpayCertificates).build();String sendCouponUrl = String.format("https://api.mch.weixin.qq.com/v3/marketing/favor/users/%s/coupons", favorCouponsCreateReq.getOpenId());HashMap<String, String> paraMap = new HashMap<>();paraMap.put("stock_id", favorCouponsCreateReq.getStock_id());paraMap.put("out_request_no", favorCouponsCreateReq.getOut_request_no());paraMap.put("appid", appId);paraMap.put("stock_creator_mchid", mchId);log.info("request url=" + sendCouponUrl);log.info("request params=" + JSONObject.toJSONString(paraMap));HttpPost httpPost = new HttpPost(sendCouponUrl);StringEntity reqEntity = new StringEntity(JSONObject.toJSONString(paraMap),ContentType.create("application/json", "utf-8"));httpPost.setEntity(reqEntity);httpPost.addHeader("Accept", "application/json");httpPost.addHeader("Content-Type", "application/json");CloseableHttpResponse response = null;try {log.info("微信发券接口地址:"+sendCouponUrl+",入参:"+JSONObject.toJSONString(httpPost));response = httpClient.execute(httpPost);log.info("微信发券接口返回结果code:"+response.getStatusLine().getStatusCode()+",内容:"+ EntityUtils.toString(response.getEntity()));if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {String result = EntityUtils.toString(response.getEntity());log.info("response=" + result);return result;} else {String result = EntityUtils.toString(response.getEntity());log.error("status_code=" + response.getStatusLine().getStatusCode() + ", response=" + result);return result;}} catch (Exception e) {log.error("excepiton:" + e.getMessage());e.printStackTrace();} finally {if (response != null) {try {response.close();} catch (IOException e) {e.printStackTrace();}}}return null;}public String getCouponDetails(WxCouponsDetailsReq wxCouponsDetailsReq) throws Exception {FileInputStream privateKeyInputStream = null;FileInputStream certificateInputStream = null;try {privateKeyInputStream = new FileInputStream(new File(merchantPrivateKeyFile));certificateInputStream = new FileInputStream(new File(certificateFile));} catch (FileNotFoundException e) {e.printStackTrace();log.error("read merchantPrivateKeyFile or certificateFile faild, errmsg:" + e.getMessage());return null;}PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(privateKeyInputStream);X509Certificate wechatpayCertificate = PemUtil.loadCertificate(certificateInputStream);ArrayList<X509Certificate> wechatpayCertificates = new ArrayList<>();wechatpayCertificates.add(wechatpayCertificate);CloseableHttpClient httpClient = WechatPayHttpClientBuilder.create().withMerchant(mchId, mchSerialNo, merchantPrivateKey).withWechatPay(wechatpayCertificates).build();//get请求String sendCouponUrl = String.format("https://api.mch.weixin.qq.com/v3/marketing/favor/users/%s/coupons/%s?appid="+appId,wxCouponsDetailsReq.getOpenid(),wxCouponsDetailsReq.getCoupon_id());log.info("request url=" + sendCouponUrl);log.info("request params=" + JSONObject.toJSONString(wxCouponsDetailsReq));HttpGet httpGet = new HttpGet(sendCouponUrl);httpGet.addHeader("Accept", "application/json");httpGet.addHeader("Content-Type", "application/json");CloseableHttpResponse response = null;try {log.info("微信查询券详情接口地址:"+sendCouponUrl);response = httpClient.execute(httpGet);log.info("微信查询券详情返回结果code:"+response.getStatusLine().getStatusCode()+",内容:"+EntityUtils.toString(response.getEntity()));if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {String result = EntityUtils.toString(response.getEntity());log.info("response=" + result);return result;} else {String result = EntityUtils.toString(response.getEntity());log.error("status_code=" + response.getStatusLine().getStatusCode() + ", response=" + result);return result;}} catch (Exception e) {log.error("查询微信代金券详情异常:" + e.getMessage());e.printStackTrace();} finally {if (response != null) {try {response.close();} catch (IOException e) {e.printStackTrace();}}}return null;}
}请求类
public class FavorCouponsCreateRequest {/*** <pre>* 字段名:批次号* 变量名:stock_id* 是否必填:是* 类型:string[1,20]* 描述:*  微信为每个批次分配的唯一id。*  校验规则:必须为代金券(全场券或单品券)批次号,不支持立减与折扣。*  示例值:9856000* </pre>*/private String stock_id;/*** <pre>* 字段名:商户单据号* 变量名:out_request_no* 是否必填:是* 类型:string[1,128]* 描述:*  商户此次发放凭据号(格式:商户id+日期+流水号),可包含英文字母,数字,|,_,*,-等内容,不允许出现其他不合法符号,商户侧需保持唯一性。*  示例值: 89560002019101000121* </pre>*/private String out_request_no;/*** <pre>* 字段名:公众账号ID* 变量名:appid* 是否必填:是* 类型:string[1,128]* 描述:*  微信为发券方商户分配的公众账号ID,接口传入的所有appid应该为公众号的appid或者小程序的appid(在mp.weixin.qq.com申请的),不能为APP的appid(在open.weixin.qq.com申请的)。。*  校验规则:*  1、该appid需要与接口传入中的openid有对应关系;*  2、该appid需要与调用接口的商户号(即请求头中的商户号)有绑定关系,若未绑定,可参考该指引完成绑定(商家商户号与AppID账号关联管理)*  示例值:wx233544546545989* </pre>*/private String appid;/*** <pre>* 字段名:创建批次的商户号* 变量名:stock_creator_mchid* 是否必填:是* 类型:string[1,20]* 描述:*  批次创建方商户号。*  示例值:8956000* </pre>*/private String stock_creator_mchid;/*** <pre>* 字段名:指定面额发券,面额* 变量名:coupon_value* 是否必填:否* 类型:uint64* 描述:*  指定面额发券场景,券面额,其他场景不需要填,单位:分。*  校验规则:仅在发券时指定面额及门槛的场景才生效,常规发券场景请勿传入该信息。*  示例值:100* </pre>*/private Integer coupon_value;/*** <pre>* 字段名:指定面额发券,券门槛* 变量名:coupon_minimum* 是否必填:是* 类型:uint64* 描述:*  指定面额发券批次门槛,其他场景不需要,单位:分。*  校验规则:仅在发券时指定面额及门槛的场景才生效,常规发券场景请勿传入该信息。*  示例值:100* </pre>*/private Integer coupon_minimum;private String openid;}public class WxCouponsDetailsReq {private String coupon_id;private String openid;
}

controller类

调用接口,参数如下

返回结果

调用代金券详情接口


参数含义请参考官方文档

java对接微信代金券功能相关推荐

  1. java对接微信电子小票功能

    电子小票与纸质小票都是线下购物的有效凭证,是纸质小票的电子化形态.电子小票易于存储.查看,便于消费追溯.获取售后服务等. 目前微信官方推出电子小票功能方案分为两种 1.小程序接入方案:微信电子小票通过 ...

  2. 最全的java对接微信小程序客服功能实现(包含自动回复文本消息、图片消息,进入人工客服)

    java对接微信小程序客服功能实现(包含自动回复文本消息.图片消息,进入人工客服) 第一步:请求校验(确认请求来自微信服务器) 代码如下: @ApiOperation(value = " 微 ...

  3. 秒杀微服务实现抢购代金券功能

    文章目录 需求分析 秒杀场景的解决方案 数据库表设计 代金券表 抢购活动表 订单表 创建秒杀服务 pom依赖 配置文件 关系型数据库实现代金券秒杀 相关实体引入 抢购代金券活动信息 代金券订单信息 R ...

  4. Java对接微信公众平台详解

    Java对接微信公众平台详解 1.公众平台概述 1.1 公众平台概述 1.2 入门指引 2.对接流程 2.1 接入概述 2.2 填写服务器配置 2.3 接口域名说明 2.4 获取Access toke ...

  5. java对接微信支付收不到支付通知问题(亲身实践)

    问题描述: 用java对接微信支付时,统一下单接口正常.但是用户扫码付款成功后,设置用于回调的notify_url对应的接口并没有收到请求(这个url测试过,是正常的且外网能访问的). 由于官方文档没 ...

  6. Java对接微信支付实现微信APP支付

    Java对接微信实现微信APP支付 之前对接过第三方的支付方式,也有接入微信jsapi的支付方式,这次项目需求要求对接微信APP支付,找了很多,几乎都没有最新版的微信支付v3的对接相关的详细博客,真的 ...

  7. Java对接微信支付(完整全流程)

    Java对接微信支付及支付回调通知的全流程 一.所用框架.对接微信支付我们技术组用的是payment框架,因为该框架已整合springboot因此很方便快捷 <dependency>< ...

  8. PHP创建微信代金券流程

    创建微信代金券接口文档地址 https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/marketing/convention/chapter3_1.shtml 注 ...

  9. java对接支付宝实现支付功能

    ** java对接支付宝实现支付功能 ** Controller /*** 支付功能* @return*/ @RequestMapping("/test") public Mode ...

  10. java对接微信分享_Java编程调用微信分享功能示例

    本文实例讲述了Java编程调用微信分享功能.分享给大家供大家参考,具体如下: 这篇文章介绍如何使用java开发微信分享功能,因为工作,已经开发完成,可使用. 如果想要自定义微信的分享功能,首先在自己的 ...

最新文章

  1. 德国阿尔迪成功启示录(转载)
  2. 26期20180628 shell 命令操作 通配符 输出输入重定向
  3. 应该始终以PreparedStatement代替Statement
  4. redis shutdown (error) ERR Errors trying to SHUTDOWN. Check logs.
  5. 云电脑是什么_云电脑为什么发布新1代5G无影?带你了解PC
  6. 利用绝对定位和相对定位对CSS中区块进行位置调整
  7. 周庄不买门票攻略_广东佛山旅游攻略好玩的地方景点推荐
  8. 使用windows2003架设邮件服务器
  9. 计算机操作系统知识点总结
  10. imdisk虚拟光驱安装linux,ImDisk Virtual Disk Driver
  11. Android 地图跳转到百度、高德、腾讯地图导航
  12. 拉依达准则python实现
  13. UI设计中标签设计总结
  14. css+html中div和img对齐
  15. 视频播放器是如何播放音视频的?
  16. android elevation 白色,使用android:elevation在LinearLayout上投射阴影
  17. 华东师大计算机软件6,华东师大计算机应用于技术题库 .doc
  18. 黑客必读:蜜罐技术的前世今生
  19. 贪食蛇java源码_JAVA 贪食蛇 源码
  20. C语言连接符号##和#解析

热门文章

  1. 小程序汉字转码以及倒计时
  2. MATLAB从fig文件中获取数据
  3. macOS Mojave 夜神模拟器打不开解决办法
  4. IEEE期刊参考文献中的会议缩写
  5. java实习两个月总结
  6. 如何清除Excel2007中的网格线?
  7. 【ps-course】layer 图层
  8. java通过winrm实现remote powershell
  9. w10计算机用户名密码忘了,电脑w10系统开机密码忘了
  10. 月活8.89亿背后:微信工程师细数兼容测试经验