抖音下单:
取sign
var paramsArr []string for k, v := range paramsMap { if k == "other_settle_params" { continue } value := strings.TrimSpace(fmt.Sprintf("%v", v)) if strings.HasPrefix(value, "\"") && strings.HasSuffix(value, "\"") && len(value) > 1 { value = value[1 : len(value)-1] } value = strings.TrimSpace(value) if value == "" || value == "null" { continue } switch k { // app_id, thirdparty_id, sign 字段用于标识身份,不参与签名 case "app_id", "thirdparty_id", "sign": default: paramsArr = append(paramsArr, value) } } paramsArr = append(paramsArr, salt) sort.Strings(paramsArr) return fmt.Sprintf("%x", md5.Sum([]byte(strings.Join(paramsArr, "&"))))
调起支付:
sign := payService.DyPayText() url := "https://developer.toutiao.com/api/apps/ecpay/v1/create_order" // 给 map 赋值 paramsMap := map[string]interface{}{ "app_id": "xxx", "out_order_no": "xxx ", "total_amount":xxx, "subject": "xxx", "body": "3333", "valid_time": xxx, "sign": sign, "notify_url": "https://xxx/", } jsonData, err := json.Marshal(paramsMap) if err != nil { fmt.Println("Error:", err) return } payload := strings.NewReader(string(jsonData)) 请求下单: req, _ := http.NewRequest("POST", url, payload) req.Header.Add("content-type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := ioutil.ReadAll(res.Body) fmt.Println(string(body)) var response map[string]interface{} err = json.Unmarshal(body, &response) if err != nil { fmt.Println("Error parsing JSON:", err) return } // 提取 data 字段下的 field1 字段值 data, dataExists := response["data"].(map[string]interface{}) if !dataExists { fmt.Println("No 'data' field found in the response") return } orderId, field1Exists := data["order_id"].(string) if !field1Exists { fmt.Println("No 'order_id' field found in the 'data' field") return } orderToken, field1Exists := data["order_token"].(string) if !field1Exists { fmt.Println("No 'order_token' field found in the 'data' field") return } 得到orderId, orderToken,
验证签名:
back := model.CallBack{} err := c.ShouldBindJSON(&back) if err != nil { fmt.Println(err) c.JSON(400, gin.H{"error": "Invalid2 JSON"}) return } callbackToken := "xxxxxx" //callbackToken, timestamp, nonce, msg, code callbackSign := payService.CallbackSignTest(callbackToken, back.Timestamp, back.Nonce, back.Msg, back.MsgSignature) if callbackSign { c.JSON(200, gin.H{"success": "sign解析成功"}) return } else { c.JSON(400, gin.H{"error": "sign解析失败"}) return }
CallbackSignTest:
sort.Strings(strArr) h := sha1.New() h.Write([]byte(strings.Join(strArr, ""))) return fmt.Sprintf("%x", h.Sum(nil))