一、请求与应答

1、请求报文

1.1 请求方式:POST

1.2 字符编码:UTF-8

1.3 Headers配置

AppID = {{appid}}

Content-Type = application/json;charset=UTF-8

1.4 Body报文结构

  1. {
  2. "coupon_id": "100175",
  3. "order_id": "123456",
  4. "brand_id": "10010001",
  5. "member_id": "100000049",
  6. "send_flag": "2",
  7. "notice_phone": "13800000001",
  8. "verify_code": "123456",
  9. "outer_str": "123456",
  10. "timestamp": 1576127771,
  11. "sign": "93daf319f792332e011a8dc26e8b9e7955fb6ead60855f8672c4e859696a7bd4"
  12. }

PS.timestampsign为所有接口必传节点,所以API接口文档不再赘述!

1.4 Curl方式请求示例

  1. curl -X POST \
  2. https://xxxxx/api/member/queryPoint \
  3. -H 'AppID: 8888888' \
  4. -H 'Content-Type: application/json;charset=UTF-8' \
  5. -d '{"member_id":"703232323232","timestamp":1570899661,"sign":"b1d4a90dab7e4f1608e22a72aca53eb5f9bec81f80d3948391562b45e73af2b9"}'

2、应答报文结构

2.1 成功返回:code === 0,通常data节点包含数据

  1. {
  2. "code": 0,
  3. "data": {
  4. "verify_code": "23006296189188",
  5. "order_id": "123456",
  6. "seq": "10000320191212120741197848693"
  7. },
  8. "msg": "",
  9. "timestamp": 1576123670,
  10. "sign": "af58c0c9633d6b66eb923733520ae4572a5a84979e219464dbb04cca852e279f"
  11. }

2.2 错误返回:code !== 0,通常data节点无返回

  1. {
  2. "code": 40001,
  3. "data": null,
  4. "msg": "缺少必须的参数",
  5. "timestamp": 1575431387,
  6. "sign": "245b7f43f586ffae3df885ce4a8af38c8673c5014ed8430de70887b3287ff322"
  7. }

PS.所有接口报文结构一致,所以API接口文档不再赘述,仅说明data以下的节点!

二、签名算法

1、示例参数:

  1. $key = 'B6RluAgaBGHAs8s0WmyRmUUzxfJav48d';//接入密码
  2. $input = [
  3. "company_id" => "THEORY",
  4. "trans_type" => "2",
  5. "order_id" => "221322232422131",
  6. "order_time" => "2019-11-13 18:00:00",
  7. "from_channel" => "POS",
  8. "order_amt" => -100,
  9. "store_id" => "0999",
  10. "member_id" => "100000047",
  11. "currency" => "CNY",
  12. "taobao_nick" => "大树",
  13. "receiver_phone" => "1380000000",
  14. "receiver_address" => "xx路xx号",
  15. "receiver_province" => "福建省",
  16. "receiver_city" => "福州市",
  17. "receiver_name" => "张三",
  18. "receiver_district" => "鼓楼区",
  19. "goods_detail" => [
  20. [
  21. "line_no" => 1,
  22. "barcode" => "190789856223",
  23. "org_order_id" => "2423444321234323266",
  24. "org_line_no" => "33443332",
  25. "unit_price" => 199,
  26. "sale_price" => -50,
  27. "quantity" => 1,
  28. ],
  29. [
  30. "line_no" => 2,
  31. "barcode" => "190789856224",
  32. "org_order_id" => "24233123131123266",
  33. "org_line_no" => "4444342",
  34. "unit_price" => 99,
  35. "sale_price" => -50,
  36. "quantity" => 2,
  37. ]
  38. ]
  39. ];

2、筛选参数并排序

首先将数组中的键按字母的ASCII码顺序重新排序 得到如下新的数组(注意:goods_detail里面的不需要排序,只需要排序最外层的字段。外层字段为空的(""、null、[]、false)会被忽略不参加排序,因此计算时为空的字段要去掉,goods_detail中为空的不会被忽略,参与计算)

  1. $input = [
  2. "company_id" => "THEORY",
  3. "currency" => "CNY",
  4. "from_channel" => "POS",
  5. "goods_detail" => "[{"line_no":1,"barcode":"190789856223","org_order_id":"2423444321234323266","org_line_no":"33443332","unit_price":199,"sale_price":-50,"quantity":1},{"line_no":2,"barcode":"190789856224","org_order_id":"24233123131123266","org_line_no":"4444342","unit_price":99,"sale_price":-50,"quantity":2}]",
  6. "member_id" => "100000047",
  7. "order_amt" => -100,
  8. "order_id" => "221322232422131",
  9. "order_time" => "2019-11-13 18:00:00",
  10. "receiver_address" => "xx路xx号",
  11. "receiver_city" => "福州市",
  12. "receiver_district" => "鼓楼区",
  13. "receiver_name" => "张三",
  14. "receiver_phone" => "1380000000",
  15. "receiver_province" => "福建省",
  16. "store_id" => "0999",
  17. "taobao_nick" => "大树",
  18. "timestamp" => 1575878166,
  19. "trans_type" => "2"
  20. ];

3、拼接参数

将数组的值转换成url参数形式的字符串,并且将结果url_decode得到待签名字符串($str)

  1. $str = "company_id=THEORY¤cy=CNY&from_channel=POS&goods_detail=[{"line_no":1,"barcode":"190789856223","org_order_id":"2423444321234323266","org_line_no":"33443332","unit_price":199,"sale_price":-50,"quantity":1},{"line_no":2,"barcode":"190789856224","org_order_id":"24233123131123266","org_line_no":"4444342","unit_price":99,"sale_price":-50,"quantity":2}]&member_id=100000047&order_amt=-100&order_id=221322232422131&order_time=2019-11-13 18:00:00&receiver_address=xx路xx号&receiver_city=福州市&receiver_district=鼓楼区&receiver_name=张三&receiver_phone=1380000000&receiver_province=福建省&store_id=0999&taobao_nick=大树×tamp=1575878166&trans_type=2";

4、生成摘要

待签名字符串($str)进行sha256哈希加密得到摘要($signStep1)

PHP示例:

  1. $signStep1 = hash('sha256', $str, false);

5、计算签名

摘要($signStep1)首尾拼接接入密钥($appsecret)后,进行sha256哈希加密

PHP示例:

  1. $newStr = $key . $signStep1 . $key;
  2. $sign = hash('sha256', $newStr, false);

三、附录:

1、采用Postman调试接口

1.1 Headers配置

AppID = {{appid}}

Content-Type = application/json

1.2 Body配置:raw

1.3 Pre-request Scripts

【建议】创建一个New CollectionEdit,在Pre-request Scripts填入以下脚本:

  1. //接入参数
  2. const appid = '100003';
  3. const appsecret = '--翼码接入密码--';
  4. ////////// 以下代码不可修改 //////////
  5. //剔除空参数
  6. String.prototype.trim = function () {
  7. return this.replace(/(^\s*)|(\s*$)/g, "");
  8. }
  9. //获取当前时间
  10. var timestamp = Math.round(new Date().getTime());
  11. var requestData = request.data.replace('{{timestamp}}', timestamp);
  12. var requestObj = JSON.parse(requestData);
  13. //将sign排除排序
  14. delete requestObj["sign"];
  15. console.log(requestObj);
  16. //根据key经行排序
  17. var keys = Object.keys(requestObj),
  18. i, len = keys.length;
  19. keys.sort();
  20. console.log(keys)
  21. var requestBody = "";
  22. var firstpass = true;
  23. // 构造数据为 key=param&key=param....字符串
  24. for (var index in keys) {
  25. if (!firstpass) {
  26. requestBody += "&";
  27. }
  28. var val = requestObj[keys[index]];
  29. // console.log(keys[index]);
  30. // console.log(Object.prototype.toString.call(val));
  31. // console.log(val);
  32. if (Object.prototype.toString.call(val) === '[object Object]' || Object.prototype.toString.call(val[0]) === '[object Object]') {
  33. requestBody += keys[index] + "=" + JSON.stringify(val);
  34. } else if ("" !== val.toString().trim()) {
  35. requestBody += keys[index] + "=" + val;
  36. }
  37. firstpass = false;
  38. }
  39. console.log(requestBody);
  40. //将数组的值转换成url参数形式的字符串,并且将结果url_decode得到
  41. var encodeURIdata = decodeURIComponent(requestBody);
  42. console.log(encodeURIdata);
  43. //sha256加密生成摘要
  44. var signStep1 = CryptoJS.SHA256(encodeURIdata).toString();
  45. console.log(signStep1);
  46. //前后拼接接入密钥,sha256加密生成签名
  47. var newStr = appsecret + signStep1 + appsecret;
  48. var sign = CryptoJS.SHA256(newStr).toString();
  49. console.log(sign);
  50. //替换变量
  51. postman.setGlobalVariable('appid', appid);
  52. postman.setGlobalVariable('timestamp', timestamp);
  53. postman.setGlobalVariable('sign', sign);

2、接口文档地址:

2.1 微服务卡券

https://open.wangcaio2o.com/apidoc/coupon/index.html