The introduction

According to the business requirements of the company, users can receive partner card coupons to the card package in our small program. In the process of development, I found that wechat’s documentation for card coupons was too simple and messy, so that there were many pits in the middle. Here is a record to sort out.

demand

The main demand this time is for the partner to create card coupons and collect them in our small program. This article was developed based on the successful creation of the card coupon. For how to create a card coupon, please refer to the wechat document – Creating a card coupon.

Tips: The card is not a membership card. If you need to get an active membership card, refer to other articles to get an active membership card

The body of the

When I first received this requirement, I checked the wechat document and found that wechat provided an API for adding card coupons

wx.addCard({
  cardList: [{cardId: ' '.cardExt: '{"code": "", "openid": "", "timestamp": "", "signature":""}'
    }, {
      cardId: ' '.cardExt: '{"code": "", "openid": "", "timestamp": "", "signature":""}'
    }
  ],
  success (res) {
    console.log(res.cardList) // Add the result of the card}})Copy the code

At first glance, it seems simple, just pass cardId and cardExt.

But what is cardExt? The documentation simply mentions that cardExt is an extended parameter to the card coupon and its value is a JSON string.

I was so bewildered that I went to the official document of the official account and found the description of cardExt in Appendix 4.

The other fields are easier to understand, but how to get signature? Don’t worry. It’s all documented

1. Sort the values of api_ticket, TIMESTAMP, card_id, code, openID, and nonce_str in alphabetical order. 2. Concatenate all parameter strings into a string for SHA1 encryption to obtain signature. 3. The timestamp and nonce fields in signature must be consistent with the timestamp and nonce_str fields in card_ext.Copy the code

When you finally know how to generate the signature, the api_ticket parameter appears out of nowhere. Fortunately, the documentation also explains this

The card api_ticket is a temporary ticket used to invoke the card cart-related interface, valid for 7200 seconds and obtained via an access_token. This should be distinguished from jSAPi_ticket. Because the NUMBER of API calls for obtaining the api_ticket is limited, frequently refreshing the API_ticket will restrict API calls and affect services. Therefore, developers must cache the API_ticket globally in their own services. 1. Refer to the following documents to obtain the access_token (valid for 7200 seconds, developers must cache the access_token globally in their own services) : /15/54ce45d8d30b6bf6758f68d2e95bc627.html 2. Api_ticket (valid for 7200 seconds, developers must cache the api_ticket globally in their own services) using the access_token obtained in step 1: https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=wx_cardCopy the code

Simply put, get requests this address to get the API_ticket

https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token= create card voucher public ACCESSTOKEN&type = wx_card,

Ok,api_ticket has been picked up, now it’s time to generate the signature. Here wechat provides an official verification tool for debugging, the parameter (note here, the unit of timestamp is seconds, not milliseconds) can be filled in to generate signatures

Once you’ve got the signature, serialize it into a cardExt object, and then put it into wx.addCard

Here basic add card coupons required parameters can be obtained, if successful, should be able to add card coupons in the small program smoothly call API, get the following page

Common mistakes

If according to the above steps still can not be successfully called, don’t worry, wechat also issued a document summarizing the common errors: card signature error troubleshooting method we follow the article to the problem slowly troubleshooting, basic can be successful.