Developing Public-Facing APIs

Here’s the requirement:
There’s an external app that promotes our mini program by handing out coupon codes on their side. Then users come to our mini program to claim the coupon and place an order with it. At the same time, we need to push orders to them, and also accept the coupon status changes on their side.

The flow diagram is as follows:
Flow diagram

So, based on the flow above, I need to develop three interfaces here

1.Coupon list

The third party calls the interface to get coupon information

2.Coupon status change

The third party calls to change the coupon status

3.Coupon order push

The third party provides a URL, and on my side I push order information based on orders generated from using coupons

First, design an open application table, mainly storing the application id and application secret, as shown in the figure

Application table design

We need to sign the data and encrypt the important data

For the signature, use the conventional format (you can also change the format yourself): timestamp + application secret + transmitted data
Apply md5 to the format above, then encode it to base64. The timestamp and signature should be placed in the request header for transmission. As shown in the figure

Signature code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
func CouponSigin(appId, jsonData string, sid int, def ...string) (*string, *string, *string, error) {
openApp, err := GetStoreApp(appId, sid, 0)
if err != nil {
logs.Error("获取应用失败", err)
return nil, nil, nil, err
}
apiKey := openApp.ApiOne
getTime := Util.Int64ToString(time.Now().UnixNano())
if len(def) > 0 && len(def[0]) > 0 {
getTime = def[0]
}
strData := getTime + apiKey + jsonData + getTime
md := md5.New()
md.Write([]byte(strData))
md5Str := hex.EncodeToString(md.Sum(nil))
SystemSign := base64.StdEncoding.EncodeToString([]byte(md5Str))
logs.Error("==签名==", SystemSign)
logs.Error("==时间戳==", getTime)
return &SystemSign, &getTime, &apiKey, nil
}

For encryption and decryption, just use conventional aes encryption, encrypting with the application secret yourself. I won’t go into it here, it’s very common.

With that, the foundation is done.

Once we develop the coupon list, coupon status change and coupon order push, we can verify the data through the signature, and encrypt the sensitive data at the same time