微信卡券 java_微信小程序领取卡券(java)

最近做了个领取微信卡券的小程序,看了很多文档资料以及花了很多时间才算搞定的,不过也算是好事多磨,这边记录分享一下,也算给一点提升。

一、开发前准备

1:申请微信公众号 和 微信小程序,这是两个不同的东西,都需要单独申请、不同的帐号;

2:微信公众号需要开通微信卡券的功能;

3:在微信公众号里面去绑定小程序;

4:申请微信开放平台,并将微信公众号 和 微信小程序绑定到该开放平台。(注:绑定到开发平台下的作用只是为了获取unionid,因为同一用户在 公众号 和 小程序下获得的openid是不一样的,如果公众号 和 小程序都需要领取卡券,则最好通过unionid来跟踪用户;如果你只是开发微信小程序的领取卡券,则完全可以忽略第4点,博主本人也没有去绑定到微信开放平台,感觉步骤好多,特别麻烦!)

二、开始开发

1:获取微信卡券

https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1451025272

这边可以直接通过微信公众号提供的接口获取或者创建微信的卡券,此处不过多介绍,只是提一下这边要获取的access_token,网址如下https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183,代码直接如下:

[java] view plaincopy

privatestaticString grantType ="client_credential";

publicstaticString appId ="";//微信公众号appid

publicstaticString secret ="";//微信公众号密钥

publicstaticAccessToken token =null;//微信公众号的accessToken对象,由于请求次数有限制,这里使用全局静态变量保存起来

publicstaticAccessToken getToken()throwsWeixinException, JsonParseException, JsonMappingException, IOException{

if(token ==null|| token.getExpires_in() 

//拼接参数

String param = "?grant_type="+ grantType +"&appid="+ appId +"&secret="+ secret;

//创建请求对象

HttpsClient http = newHttpsClient();

//调用获取access_token接口

Response res = http.get("https://api.weixin.qq.com/cgi-bin/token"+ param);

System.out.println(res.asString());

ObjectMapper mapper = newObjectMapper();

对于微信卡券的开发,需要先进行微信公众号或小程序的授权认证,获取到相关的 API 信息。然后根据不同的卡券类型,进行相应的开发。 下面以微信小程序为例,介绍一下卡券的开发流程: 1. 配置小程序的接口权限,包括卡券相关的接口权限。 2. 创建卡券,可以在微信公众平台或小程序后台进行创建。创建时需要填写卡券的基本信息,包括卡券的类型、商户信息等。 3. 使用 API 接口进行卡券的管理,包括卡券的发放、核销等。需要对 API 进行相应的封装,方便开发人员进行调用。 4. 在小程序中展示卡券,可以通过卡券列表、卡券详情页等方式进行展示。 这里提供一个简单的 PHP 微信卡券 demo,供参考: ```php <?php // 设置公众号或小程序的 appid 和 appsecret $appid = 'your_appid'; $appsecret = 'your_appsecret'; // 获取 access_token $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret; $response = file_get_contents($url); $result = json_decode($response, true); $access_token = $result['access_token']; // 创建卡券 $url = 'https://api.weixin.qq.com/card/create?access_token='.$access_token; $data = array( 'card' => array( 'card_type' => 'GROUPON', 'groupon' => array( 'base_info' => array( 'logo_url' => 'http://mmbiz.qpic.cn/mmbiz_jpg/xxxxxxxx/0', 'brand_name' => '测试商户', 'code_type' => 'CODE_TYPE_QRCODE', 'title' => '测试优惠券', 'sub_title' => '测试商户', 'color' => 'Color010', 'notice' => '测试优惠券', 'service_phone' => '18888888888', 'description' => '测试优惠券', 'date_info' => array( 'type' => 'DATE_TYPE_FIX_TIME_RANGE', 'begin_timestamp' => strtotime('2021-01-01'), 'end_timestamp' => strtotime('2021-12-31') ), 'sku' => array( 'quantity' => 1000000 ), 'get_limit' => 1, 'use_custom_code' => false, 'bind_openid' => false, 'can_share' => true, 'can_give_friend' => true, 'location_id_list' => array(), 'url_name_type' => 'URL_NAME_TYPE_RESERVATION', 'custom_url' => 'http://www.xxx.com', 'source' => '测试商户' ), 'deal_detail' => '测试详情' ) ) ); $data = json_encode($data, JSON_UNESCAPED_UNICODE); $response = http_post_data($url, $data); $result = json_decode($response, true); $card_id = $result['card_id']; // 发放卡券 $url = 'https://api.weixin.qq.com/card/groupon/add?access_token='.$access_token; $data = array( 'card_id' => $card_id, 'quantity' => 10 ); $data = json_encode($data, JSON_UNESCAPED_UNICODE); $response = http_post_data($url, $data); $result = json_decode($response, true); echo $result['errcode'] == 0 ? '发放成功' : '发放失败'; // 核销卡券 $url = 'https://api.weixin.qq.com/card/code/consume?access_token='.$access_token; $data = array( 'code' => 'xxxxxxxx', 'card_id' => $card_id ); $data = json_encode($data, JSON_UNESCAPED_UNICODE); $response = http_post_data($url, $data); $result = json_decode($response, true); echo $result['errcode'] == 0 ? '核销成功' : '核销失败'; // 查询卡券详情 $url = 'https://api.weixin.qq.com/card/get?access_token='.$access_token; $data = array( 'card_id' => $card_id ); $data = json_encode($data, JSON_UNESCAPED_UNICODE); $response = http_post_data($url, $data); $result = json_decode($response, true); print_r($result); function http_post_data($url, $data_string) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string) )); $response = curl_exec($ch); curl_close($ch); return $response; } ?> ``` 需要注意的是,此 demo 中的 access_token 获取方式不够安全,建议使用官方 SDK 或者其他安全的方式进行获取。另外,卡券的创建、发放、核销等操作需要根据实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值