微信小程序添加卡券,领取到卡包

微信小程序开发交流qq群   173683895

   承接微信小程序开发。扫码加微信。

小程序实现代码:

  onLoad: function(option) {
    wx.addCard({
      cardList: [{
        "cardExt": '{"nonce_str":"42a34c6e84334c69961f5294643ad331","code":"892237947","signature":"6211eb80ec26b4054c566b3a7d1868fa304183f4","timestamp":"1541723819"}',
        "cardId": "pCS2i1DQQWELTTEcORmb-irOTRqo"
      }],
      success(res) {
        console.log(res.cardList) // 卡券添加结果
      }
    })
}

 

页面加载的时候,调用 wx.addCard API ,如图:领取,然后打开微信卡包页面,领取成功后可点击立即使用,

把优惠券添加微信卡包中需要使用微信支付提供的API,而且需要申请微信支付的商户号和相关的证书,因此下面提供的Java代码只是示例代码,实际使用时需要根据具体情况进行修改。 1. 首先需要引入相关的包: ```java import java.util.HashMap; import java.util.Map; import java.security.KeyStore; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.SSLContextBuilder; import org.apache.http.conn.ssl.TrustSelfSignedStrategy; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContexts; import org.apache.http.ssl.TrustStrategy; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; ``` 2. 然后定义相关的参数: ```java String apiUrl = "https://api.weixin.qq.com/card/qrcode/create?access_token="; String accessToken = "YOUR_ACCESS_TOKEN"; String cardId = "YOUR_CARD_ID"; String code = "YOUR_COUPON_CODE"; String openId = "USER_OPENID"; String keyPath = "YOUR_KEY_PATH"; String keyPassword = "YOUR_KEY_PASSWORD"; ``` 其中,accessToken 是通过调用微信支付提供的获取 access_token 的 API 获取的,cardId 是要添加的优惠券的卡券 ID,code 是要添加的优惠券的 code,openId 是要添加优惠券的用户的 openid,keyPath 是商户证书的路径,keyPassword 是商户证书的密码。 3. 构造 HTTP 请求: ```java String url = apiUrl + accessToken; RestTemplate restTemplate = getRestTemplate(keyPath, keyPassword); HttpHeaders headers = new HttpHeaders(); headers.add("Content-Type", "application/json"); String json = "{\"action_name\": \"QR_CARD\", \"action_info\": {\"card\": {\"card_id\": \"" + cardId + "\", \"code\": \"" + code + "\"}}}"; HttpEntity<String> request = new HttpEntity<String>(json, headers); ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class); if (response.getStatusCode() != HttpStatus.OK) { // 请求失败 } String responseBody = response.getBody(); ``` 其中,getRestTemplate 方法用来构造带有商户证书的 RestTemplate。 4. 解析返回结果: ```java ObjectMapper mapper = new ObjectMapper(); JsonNode node = mapper.readTree(responseBody); if (node.has("errcode") && node.get("errcode").asInt() != 0) { // 添加失败 } String ticket = node.get("ticket").asText(); ``` 其中,使用了 Jackson 库来解析 JSON 数据。 5. 最后,将 ticket 添加到用户的卡包中: ```java String addUrl = "https://api.weixin.qq.com/card/user/addcard?access_token=" + accessToken; json = "{\"card_id\":\"" + cardId + "\",\"code\":\"" + code + "\",\"openid\":\"" + openId + "\",\"is_unique_code\":false,\"outer_str\":\"" +
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

a_靖

对你有帮助吗?打赏鼓励一下?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值