附近小店
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

2042 líneas
69 KiB

  1. package weapp
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "net/http"
  6. "net/http/httptest"
  7. "strings"
  8. "testing"
  9. )
  10. func TestAbnormalConfirm(t *testing.T) {
  11. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  12. if r.Method != "POST" {
  13. t.Error("UnExpect request method")
  14. }
  15. if r.URL.EscapedPath() != "/cgi-bin/express/local/business/order/confirm_return" {
  16. t.Error("Unexpected path")
  17. }
  18. if err := r.ParseForm(); err != nil {
  19. t.Fatal(err)
  20. }
  21. if r.Form.Get("access_token") == "" {
  22. t.Fatalf("access_token can not be empty")
  23. }
  24. params := struct {
  25. ShopID string `json:"shopid"` // 商家id, 由配送公司分配的appkey
  26. ShopOrderID string `json:"shop_order_id"` // 唯一标识订单的 ID,由商户生成
  27. ShopNo string `json:"shop_no"` // 商家门店编号, 在配送公司登记,如果只有一个门店,可以不填
  28. DeliverySign string `json:"delivery_sign"` // 用配送公司提供的appSecret加密的校验串
  29. WaybillID string `json:"waybill_id"` // 配送单id
  30. Remark string `json:"remark"` // 备注
  31. }{}
  32. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  33. t.Fatal(err)
  34. }
  35. if params.ShopID == "" {
  36. t.Error("Response column shopid can not be empty")
  37. }
  38. if params.ShopOrderID == "" {
  39. t.Error("Response column shop_order_id can not be empty")
  40. }
  41. if params.ShopNo == "" {
  42. t.Error("Response column shop_no can not be empty")
  43. }
  44. if params.DeliverySign == "" {
  45. t.Error("Response column delivery_sign can not be empty")
  46. }
  47. if params.WaybillID == "" {
  48. t.Error("Response column waybill_id can not be empty")
  49. }
  50. if params.Remark == "" {
  51. t.Error("Response column remark can not be empty")
  52. }
  53. w.WriteHeader(http.StatusOK)
  54. raw := `{
  55. "errcode": 1,
  56. "errmsg": "ok"
  57. }`
  58. if _, err := w.Write([]byte(raw)); err != nil {
  59. t.Fatal(err)
  60. }
  61. }))
  62. defer ts.Close()
  63. raw := `{
  64. "shopid": "123456",
  65. "shop_order_id": "123456",
  66. "shop_no": "shop_no_111",
  67. "waybill_id": "123456",
  68. "remark": "remark",
  69. "delivery_sign": "123456"
  70. }`
  71. confirmer := new(AbnormalConfirmer)
  72. err := json.Unmarshal([]byte(raw), confirmer)
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. _, err = confirmer.confirm(ts.URL+apiAbnormalConfirm, "mock-access-token")
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. }
  81. func TestAddDeliveryOrder(t *testing.T) {
  82. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  83. if r.Method != "POST" {
  84. t.Error("UnExpect request method")
  85. }
  86. if r.URL.EscapedPath() != "/cgi-bin/express/local/business/order/add" {
  87. t.Error("Unexpected path")
  88. }
  89. if err := r.ParseForm(); err != nil {
  90. t.Fatal(err)
  91. }
  92. if r.Form.Get("access_token") == "" {
  93. t.Fatalf("access_token can not be empty")
  94. }
  95. params := struct {
  96. DeliveryToken string `json:"delivery_token"` // 预下单接口返回的参数,配送公司可保证在一段时间内运费不变
  97. ShopID string `json:"shopid"` // 商家id, 由配送公司分配的appkey
  98. ShopOrderID string `json:"shop_order_id"` // 唯一标识订单的 ID,由商户生成
  99. ShopNo string `json:"shop_no"` // 商家门店编号, 在配送公司登记,如果只有一个门店,可以不填
  100. DeliverySign string `json:"delivery_sign"` // 用配送公司提供的appSecret加密的校验串
  101. DeliveryID string `json:"delivery_id"` // 配送公司ID
  102. OpenID string `json:"openid"` // 下单用户的openid
  103. SubBizID string `json:"sub_biz_id"` // 子商户id,区分小程序内部多个子商户
  104. Sender struct {
  105. Name string `json:"name"` // 姓名,最长不超过256个字符
  106. City string `json:"city"` // 城市名称,如广州市
  107. Address string `json:"address"` // 地址(街道、小区、大厦等,用于定位)
  108. AddressDetail string `json:"address_detail"` // 地址详情(楼号、单元号、层号)
  109. Phone string `json:"phone"` // 电话/手机号,最长不超过64个字符
  110. Lng float64 `json:"lng"` // 经度(火星坐标或百度坐标,和 coordinate_type 字段配合使用,确到小数点后6位
  111. Lat float64 `json:"lat"` // 纬度(火星坐标或百度坐标,和 coordinate_type 字段配合使用,精确到小数点后6位)
  112. CoordinateType uint8 `json:"coordinate_type"` // 坐标类型,0:火星坐标(高德,腾讯地图均采用火星坐标) 1:百度坐标
  113. } `json:"sender"` // 发件人信息,闪送、顺丰同城急送必须填写,美团配送、达达,若传了shop_no的值可不填该字段
  114. Receiver struct {
  115. Name string `json:"name"` // 姓名,最长不超过256个字符
  116. City string `json:"city"` // 城市名称,如广州市
  117. Address string `json:"address"` // 地址(街道、小区、大厦等,用于定位)
  118. AddressDetail string `json:"address_detail"` // 地址详情(楼号、单元号、层号)
  119. Phone string `json:"phone"` // 电话/手机号,最长不超过64个字符
  120. Lng float64 `json:"lng"` // 经度(火星坐标或百度坐标,和 coordinate_type 字段配合使用,确到小数点后6位
  121. Lat float64 `json:"lat"` // 纬度(火星坐标或百度坐标,和 coordinate_type 字段配合使用,精确到小数点后6位)
  122. CoordinateType uint8 `json:"coordinate_type"` // 坐标类型,0:火星坐标(高德,腾讯地图均采用火星坐标) 1:百度坐标
  123. } `json:"receiver"` // 收件人信息
  124. Cargo struct {
  125. GoodsValue float64 `json:"goods_value"` // 货物价格,单位为元,精确到小数点后两位(如果小数点后位数多于两位,则四舍五入保留两位小数),范围为(0-5000]
  126. GoodsHeight float64 `json:"goods_height"` // 货物高度,单位为cm,精确到小数点后两位(如果小数点后位数多于两位,则四舍五入保留两位小数),范围为(0-45]
  127. GoodsLength float64 `json:"goods_length"` // 货物长度,单位为cm,精确到小数点后两位(如果小数点后位数多于两位,则四舍五入保留两位小数),范围为(0-65]
  128. GoodsWidth float64 `json:"goods_width"` // 货物宽度,单位为cm,精确到小数点后两位(如果小数点后位数多于两位,则四舍五入保留两位小数),范围为(0-50]
  129. GoodsWeight float64 `json:"goods_weight"` // 货物重量,单位为kg,精确到小数点后两位(如果小数点后位数多于两位,则四舍五入保留两位小数),范围为(0-50]
  130. GoodsDetail struct {
  131. Goods []struct {
  132. Count uint `json:"good_count"` // 货物数量
  133. Name string `json:"good_name"` // 货品名称
  134. Price float32 `json:"good_price"` // 货品单价,精确到小数点后两位(如果小数点后位数多于两位,则四舍五入保留两位小数)
  135. Unit string `json:"good_unit"` // 货品单位,最长不超过20个字符
  136. } `json:"goods"` // 货物交付信息,最长不超过100个字符
  137. } `json:"goods_detail"` // 货物详情,最长不超过10240个字符
  138. GoodsPickupInfo string `json:"goods_pickup_info"` // 货物取货信息,用于骑手到店取货,最长不超过100个字符
  139. GoodsDeliveryInfo string `json:"goods_delivery_info"` // 货物交付信息,最长不超过100个字符
  140. CargoFirstClass string `json:"cargo_first_class"` // 品类一级类目
  141. CargoSecondClass string `json:"cargo_second_class"` // 品类二级类目
  142. } `json:"cargo"` // 货物信息
  143. OrderInfo struct {
  144. DeliveryServiceCode string `json:"delivery_service_code"` // 配送服务代码 不同配送公司自定义,微信侧不理解
  145. OrderType uint8 `json:"order_type"` // 订单类型, 0: 即时单 1 预约单,如预约单,需要设置expected_delivery_time或expected_finish_time或expected_pick_time
  146. ExpectedDeliveryTime uint `json:"expected_delivery_time"` // 期望派单时间(顺丰同城急送、达达、支持),unix-timestamp
  147. ExpectedFinishTime uint `json:"expected_finish_time"` // 期望送达时间(顺丰同城急送、美团配送支持),unix-timestamp
  148. ExpectedPickTime uint `json:"expected_pick_time"` // 期望取件时间(闪送支持),unix-timestamp
  149. PoiSeq string `json:"poi_seq"` // 门店订单流水号,建议提供,方便骑手门店取货,最长不超过32个字符
  150. Note string `json:"note"` // 备注,最长不超过200个字符
  151. OrderTime uint `json:"order_time"` // 用户下单付款时间
  152. IsInsured uint8 `json:"is_insured"` // 是否保价,0,非保价,1.保价
  153. DeclaredValue float64 `json:"declared_value"` // 保价金额,单位为元,精确到分
  154. Tips float64 `json:"tips"` // 小费,单位为元, 下单一般不加小费
  155. IsDirectDelivery uint `json:"is_direct_delivery"` // 是否选择直拿直送(0:不需要;1:需要。选择直拿直送后,同一时间骑手只能配送此订单至完成,配送费用也相应高一些,闪送必须选1,达达可选0或1,其余配送公司不支持直拿直送)
  156. CashOnDelivery uint `json:"cash_on_delivery"` // 骑手应付金额,单位为元,精确到分
  157. CashOnPickup uint `json:"cash_on_pickup"` // 骑手应收金额,单位为元,精确到分
  158. RiderPickMethod uint8 `json:"rider_pick_method"` // 物流流向,1:从门店取件送至用户;2:从用户取件送至门店
  159. IsFinishCodeNeeded uint8 `json:"is_finish_code_needed"` // 收货码(0:不需要;1:需要。收货码的作用是:骑手必须输入收货码才能完成订单妥投)
  160. IsPickupCodeNeeded uint8 `json:"is_pickup_code_needed"` // 取货码(0:不需要;1:需要。取货码的作用是:骑手必须输入取货码才能从商家取货)
  161. } `json:"order_info"` // 订单信息
  162. Shop struct {
  163. WxaPath string `json:"wxa_path"` // 商家小程序的路径,建议为订单页面
  164. ImgURL string `json:"img_url"` // 商品缩略图 url
  165. GoodsName string `json:"goods_name"` // 商品名称
  166. GoodsCount uint `json:"goods_count"` // 商品数量
  167. } `json:"shop"` // 商品信息,会展示到物流通知消息中
  168. }{}
  169. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  170. t.Fatal(err)
  171. }
  172. if params.DeliveryToken == "" {
  173. t.Error("Response column 'delivery_token' can not be empty")
  174. }
  175. if params.ShopID == "" {
  176. t.Error("Response column 'shopid' can not be empty")
  177. }
  178. if params.ShopOrderID == "" {
  179. t.Error("Response column 'shop_order_id' can not be empty")
  180. }
  181. if params.ShopNo == "" {
  182. t.Error("Response column 'shop_no' can not be empty")
  183. }
  184. if params.DeliverySign == "" {
  185. t.Error("Response column 'delivery_sign' can not be empty")
  186. }
  187. if params.DeliveryID == "" {
  188. t.Error("Response column 'delivery_id' can not be empty")
  189. }
  190. if params.OpenID == "" {
  191. t.Error("Response column 'openid' can not be empty")
  192. }
  193. if params.SubBizID == "" {
  194. t.Error("Response column 'sub_biz_id' can not be empty")
  195. }
  196. if params.Sender.Name == "" {
  197. t.Error("Param 'sender.name' can not be empty")
  198. }
  199. if params.Sender.City == "" {
  200. t.Error("Param 'sender.city' can not be empty")
  201. }
  202. if params.Sender.Address == "" {
  203. t.Error("Param 'sender.address' can not be empty")
  204. }
  205. if params.Sender.AddressDetail == "" {
  206. t.Error("Param 'sender.address_detail' can not be empty")
  207. }
  208. if params.Sender.Phone == "" {
  209. t.Error("Param 'sender.phone' can not be empty")
  210. }
  211. if params.Sender.Lng == 0 {
  212. t.Error("Param 'sender.lng' can not be empty")
  213. }
  214. if params.Sender.Lat == 0 {
  215. t.Error("Param 'sender.lat' can not be empty")
  216. }
  217. if params.Sender.CoordinateType == 0 {
  218. t.Error("Param 'sender.coordinate_type' can not be empty")
  219. }
  220. if params.Receiver.Name == "" {
  221. t.Error("Param 'receiver.name' can not be empty")
  222. }
  223. if params.Receiver.City == "" {
  224. t.Error("Param 'receiver.city' can not be empty")
  225. }
  226. if params.Receiver.Address == "" {
  227. t.Error("Param 'receiver.address' can not be empty")
  228. }
  229. if params.Receiver.AddressDetail == "" {
  230. t.Error("Param 'receiver.address_detail' can not be empty")
  231. }
  232. if params.Receiver.Phone == "" {
  233. t.Error("Param 'receiver.phone' can not be empty")
  234. }
  235. if params.Receiver.Lng == 0 {
  236. t.Error("Param 'receiver.lng' can not be empty")
  237. }
  238. if params.Receiver.Lat == 0 {
  239. t.Error("Param 'receiver.lat' can not be empty")
  240. }
  241. if params.Receiver.CoordinateType == 0 {
  242. t.Error("Param 'receiver.coordinate_type' can not be empty")
  243. }
  244. if params.Cargo.GoodsValue == 0 {
  245. t.Error("Param 'cargo.goods_value' can not be empty")
  246. }
  247. if params.Cargo.GoodsHeight == 0 {
  248. t.Error("Param 'cargo.goods_height' can not be empty")
  249. }
  250. if params.Cargo.GoodsLength == 0 {
  251. t.Error("Param 'cargo.goods_length' can not be empty")
  252. }
  253. if params.Cargo.GoodsWidth == 0 {
  254. t.Error("Param 'cargo.goods_width' can not be empty")
  255. }
  256. if params.Cargo.GoodsWeight == 0 {
  257. t.Error("Param 'cargo.goods_weight' can not be empty")
  258. }
  259. if params.Cargo.CargoFirstClass == "" {
  260. t.Error("Param 'cargo.cargo_first_class' can not be empty")
  261. }
  262. if params.Cargo.CargoSecondClass == "" {
  263. t.Error("Param 'cargo.cargo_second_class' can not be empty")
  264. }
  265. if len(params.Cargo.GoodsDetail.Goods) > 0 {
  266. if params.Cargo.GoodsDetail.Goods[0].Count == 0 {
  267. t.Error("Param 'cargo.goods_detail.goods.good_count' can not be empty")
  268. }
  269. if params.Cargo.GoodsDetail.Goods[0].Name == "" {
  270. t.Error("Param 'cargo.goods_detail.goods.good_name' can not be empty")
  271. }
  272. if params.Cargo.GoodsDetail.Goods[0].Price == 0 {
  273. t.Error("Param 'cargo.goods_detail.goods.good_price' can not be empty")
  274. }
  275. if params.Cargo.GoodsDetail.Goods[0].Unit == "" {
  276. t.Error("Param 'cargo.goods_detail.goods.good_unit' can not be empty")
  277. }
  278. }
  279. if params.OrderInfo.DeliveryServiceCode == "" {
  280. t.Error("Param 'order_info.delivery_service_code' can not be empty")
  281. }
  282. if params.Shop.WxaPath == "" {
  283. t.Error("Param 'shop.wxa_path' can not be empty")
  284. }
  285. if params.Shop.ImgURL == "" {
  286. t.Error("Param 'shop.img_url' can not be empty")
  287. }
  288. if params.Shop.GoodsName == "" {
  289. t.Error("Param 'shop.goods_name' can not be empty")
  290. }
  291. if params.Shop.GoodsCount == 0 {
  292. t.Error("Param 'shop.goods_count' can not be empty")
  293. }
  294. w.WriteHeader(http.StatusOK)
  295. raw := `{
  296. "resultcode": 1,
  297. "resultmsg": "ok",
  298. "fee": 11,
  299. "deliverfee": 11,
  300. "couponfee": 1,
  301. "tips": 1,
  302. "insurancefee": 1000,
  303. "insurancfee": 1,
  304. "distance": 1001,
  305. "waybill_id": "123456789",
  306. "order_status": 101,
  307. "finish_code": 1024,
  308. "pickup_code": 2048,
  309. "dispatch_duration": 300
  310. }`
  311. if _, err := w.Write([]byte(raw)); err != nil {
  312. t.Fatal(err)
  313. }
  314. }))
  315. defer ts.Close()
  316. raw := `{
  317. "cargo": {
  318. "cargo_first_class": "美食宵夜",
  319. "cargo_second_class": "零食小吃",
  320. "goods_detail": {
  321. "goods": [
  322. {
  323. "good_count": 1,
  324. "good_name": "水果",
  325. "good_price": 11,
  326. "good_unit": "元"
  327. },
  328. {
  329. "good_count": 2,
  330. "good_name": "蔬菜",
  331. "good_price": 21,
  332. "good_unit": "元"
  333. }
  334. ]
  335. },
  336. "goods_height": 1,
  337. "goods_length": 3,
  338. "goods_value": 5,
  339. "goods_weight": 1,
  340. "goods_width": 2
  341. },
  342. "delivery_id": "SFTC",
  343. "delivery_sign": "01234567890123456789",
  344. "openid": "oABC123456",
  345. "order_info": {
  346. "delivery_service_code": "xxx",
  347. "expected_delivery_time": 1,
  348. "is_direct_delivery": 1,
  349. "is_finish_code_needed": 1,
  350. "is_insured": 1,
  351. "is_pickup_code_needed": 1,
  352. "note": "test_note",
  353. "order_time": 1555220757,
  354. "order_type": 1,
  355. "poi_seq": "1111",
  356. "tips": 0
  357. },
  358. "receiver": {
  359. "address": "xxx地铁站",
  360. "address_detail": "2号楼202",
  361. "city": "北京市",
  362. "coordinate_type": 1,
  363. "lat": 40.1529600001,
  364. "lng": 116.5060300001,
  365. "name": "老王",
  366. "phone": "18512345678"
  367. },
  368. "sender": {
  369. "address": "xx大厦",
  370. "address_detail": "1号楼101",
  371. "city": "北京市",
  372. "coordinate_type": 1,
  373. "lat": 40.4486120001,
  374. "lng": 116.3830750001,
  375. "name": "刘一",
  376. "phone": "13712345678"
  377. },
  378. "shop": {
  379. "goods_count": 2,
  380. "goods_name": "宝贝",
  381. "img_url": "https://mmbiz.qpic.cn/mmbiz_png/xxxxxxxxx/0?wx_fmt=png",
  382. "wxa_path": "/page/index/index"
  383. },
  384. "shop_no": "12345678",
  385. "sub_biz_id": "sub_biz_id_1",
  386. "shop_order_id": "SFTC_001",
  387. "shopid": "122222222",
  388. "delivery_token": "xxxxxxxx"
  389. }`
  390. creator := new(DeliveryOrderCreator)
  391. err := json.Unmarshal([]byte(raw), creator)
  392. if err != nil {
  393. t.Fatal(err)
  394. }
  395. res, err := creator.create(ts.URL+apiAddDeliveryOrder, "mock-access-token")
  396. if err != nil {
  397. t.Fatal(err)
  398. }
  399. if res.Fee == 0 {
  400. t.Error("Response 'fee' can not be empty")
  401. }
  402. if res.Deliverfee == 0 {
  403. t.Error("Response 'deliverfee' can not be empty")
  404. }
  405. if res.Couponfee == 0 {
  406. t.Error("Response 'couponfee' can not be empty")
  407. }
  408. if res.Tips == 0 {
  409. t.Error("Response 'tips' can not be empty")
  410. }
  411. if res.Insurancefee == 0 {
  412. t.Error("Response 'insurancefee' can not be empty")
  413. }
  414. if res.Distance == 0 {
  415. t.Error("Response 'distance' can not be empty")
  416. }
  417. if res.WaybillID == "" {
  418. t.Error("Response 'waybill_id' can not be empty")
  419. }
  420. if res.OrderStatus == 0 {
  421. t.Error("Response 'order_status' can not be empty")
  422. }
  423. if res.FinishCode == 0 {
  424. t.Error("Response 'finish_code' can not be empty")
  425. }
  426. if res.PickupCode == 0 {
  427. t.Error("Response 'pickup_code' can not be empty")
  428. }
  429. if res.DispatchDuration == 0 {
  430. t.Error("Response 'dispatch_duration' can not be empty")
  431. }
  432. if res.ResultCode == 0 {
  433. t.Error("Response 'resultcode' can not be empty")
  434. }
  435. if res.ResultMsg == "" {
  436. t.Error("Response 'resultmsg' can not be empty")
  437. }
  438. }
  439. func TestAddDeliveryTip(t *testing.T) {
  440. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  441. if r.Method != "POST" {
  442. t.Error("UnExpect request method")
  443. }
  444. if r.URL.EscapedPath() != "/cgi-bin/express/local/business/order/addtips" {
  445. t.Error("Unexpected path")
  446. }
  447. if err := r.ParseForm(); err != nil {
  448. t.Fatal(err)
  449. }
  450. if r.Form.Get("access_token") == "" {
  451. t.Fatalf("Query 'access_token' can not be empty")
  452. }
  453. params := struct {
  454. ShopID string `json:"shopid"`
  455. ShopOrderID string `json:"shop_order_id"`
  456. ShopNo string `json:"shop_no"`
  457. DeliverySign string `json:"delivery_sign"`
  458. WaybillID string `json:"waybill_id"`
  459. OpenID string `json:"openid"`
  460. Tips float64 `json:"tips"`
  461. Remark string `json:"remark"`
  462. }{}
  463. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  464. t.Fatal(err)
  465. }
  466. if params.ShopID == "" {
  467. t.Error("Param 'shopid' can not be empty")
  468. }
  469. if params.ShopOrderID == "" {
  470. t.Error("Param 'shop_order_id' can not be empty")
  471. }
  472. if params.ShopNo == "" {
  473. t.Error("Param 'shop_no' can not be empty")
  474. }
  475. if params.DeliverySign == "" {
  476. t.Error("Param 'delivery_sign' can not be empty")
  477. }
  478. if params.WaybillID == "" {
  479. t.Error("Param 'waybill_id' can not be empty")
  480. }
  481. if params.OpenID == "" {
  482. t.Error("Param 'openid' can not be empty")
  483. }
  484. if params.Tips == 0 {
  485. t.Error("Param 'tips' can not be empty")
  486. }
  487. if params.Remark == "" {
  488. t.Error("Param 'remark' can not be empty")
  489. }
  490. w.WriteHeader(http.StatusOK)
  491. raw := `{
  492. "resultcode": 1,
  493. "resultmsg": "ok"
  494. }`
  495. if _, err := w.Write([]byte(raw)); err != nil {
  496. t.Fatal(err)
  497. }
  498. }))
  499. defer ts.Close()
  500. raw := `{
  501. "shopid": "123456",
  502. "shop_order_id": "123456",
  503. "waybill_id": "123456",
  504. "tips": 5,
  505. "openid": "mock-open-id",
  506. "remark": "gogogo",
  507. "delivery_sign": "123456",
  508. "shop_no": "shop_no_111"
  509. }`
  510. adder := new(DeliveryTipAdder)
  511. err := json.Unmarshal([]byte(raw), adder)
  512. if err != nil {
  513. t.Fatal(err)
  514. }
  515. res, err := adder.add(ts.URL+apiAddDeliveryTip, "mock-access-token")
  516. if err != nil {
  517. t.Fatal(err)
  518. }
  519. if res.ResultCode == 0 {
  520. t.Error("Response 'resultcode' can not be empty")
  521. }
  522. if res.ResultMsg == "" {
  523. t.Error("Response 'resultmsg' can not be empty")
  524. }
  525. }
  526. func TestCancelDeliveryOrder(t *testing.T) {
  527. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  528. if r.Method != "POST" {
  529. t.Error("UnExpect request method")
  530. }
  531. if r.URL.EscapedPath() != "/cgi-bin/express/local/business/order/cancel" {
  532. t.Error("Unexpected path")
  533. }
  534. if err := r.ParseForm(); err != nil {
  535. t.Fatal(err)
  536. }
  537. if r.Form.Get("access_token") == "" {
  538. t.Fatalf("Query 'access_token' can not be empty")
  539. }
  540. params := struct {
  541. ShopID string `json:"shopid"` // 商家id, 由配送公司分配的appkey
  542. ShopOrderID string `json:"shop_order_id"` // 唯一标识订单的 ID,由商户生成
  543. ShopNo string `json:"shop_no"` // 商家门店编号, 在配送公司登记,如果只有一个门店,可以不填
  544. DeliverySign string `json:"delivery_sign"` // 用配送公司提供的appSecret加密的校验串
  545. DeliveryID string `json:"delivery_id"` // 快递公司ID
  546. WaybillID string `json:"waybill_id"` // 配送单id
  547. ReasonID uint8 `json:"cancel_reason_id"` // 取消原因Id
  548. Reason string `json:"cancel_reason"` // 取消原因
  549. }{}
  550. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  551. t.Fatal(err)
  552. }
  553. if params.ShopID == "" {
  554. t.Error("Param 'shopid' can not be empty")
  555. }
  556. if params.ShopOrderID == "" {
  557. t.Error("Param 'shop_order_id' can not be empty")
  558. }
  559. if params.ShopNo == "" {
  560. t.Error("Param 'shop_no' can not be empty")
  561. }
  562. if params.DeliverySign == "" {
  563. t.Error("Param 'delivery_sign' can not be empty")
  564. }
  565. if params.WaybillID == "" {
  566. t.Error("Param 'waybill_id' can not be empty")
  567. }
  568. if params.DeliveryID == "" {
  569. t.Error("Param 'delivery_id' can not be empty")
  570. }
  571. if params.ReasonID == 0 {
  572. t.Error("Param 'cancel_reason_id' can not be empty")
  573. }
  574. if params.Reason == "" {
  575. t.Error("Param 'cancel_reason' can not be empty")
  576. }
  577. w.WriteHeader(http.StatusOK)
  578. raw := `{
  579. "resultcode": 1,
  580. "resultmsg": "ok",
  581. "deduct_fee": 5,
  582. "desc": "blabla"
  583. }`
  584. if _, err := w.Write([]byte(raw)); err != nil {
  585. t.Fatal(err)
  586. }
  587. }))
  588. defer ts.Close()
  589. raw := `{
  590. "shopid": "123456",
  591. "shop_order_id": "123456",
  592. "waybill_id": "123456",
  593. "delivery_id": "123456",
  594. "cancel_reason_id": 1,
  595. "cancel_reason": "mock-cancel-reson",
  596. "delivery_sign": "123456",
  597. "shop_no": "shop_no_111"
  598. }`
  599. canceler := new(DeliveryOrderCanceler)
  600. err := json.Unmarshal([]byte(raw), canceler)
  601. if err != nil {
  602. t.Fatal(err)
  603. }
  604. res, err := canceler.cancel(ts.URL+apiCancelDeliveryOrder, "mock-access-token")
  605. if err != nil {
  606. t.Fatal(err)
  607. }
  608. if res.ResultCode == 0 {
  609. t.Error("Response 'resultcode' can not be empty")
  610. }
  611. if res.ResultMsg == "" {
  612. t.Error("Response 'resultmsg' can not be empty")
  613. }
  614. if res.DeductFee == 0 {
  615. t.Error("Response 'deduct_fee' can not be empty")
  616. }
  617. if res.Desc == "" {
  618. t.Error("Response 'desc' can not be empty")
  619. }
  620. }
  621. func TestGetAllImmediateDelivery(t *testing.T) {
  622. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  623. if r.Method != "POST" {
  624. t.Error("UnExpect request method")
  625. }
  626. if r.URL.EscapedPath() != "/cgi-bin/express/local/business/delivery/getall" {
  627. t.Error("Unexpected path")
  628. }
  629. if err := r.ParseForm(); err != nil {
  630. t.Fatal(err)
  631. }
  632. if r.Form.Get("access_token") == "" {
  633. t.Fatalf("Query 'access_token' can not be empty")
  634. }
  635. w.WriteHeader(http.StatusOK)
  636. raw := `{
  637. "resultcode": 1,
  638. "resultmsg": "ok",
  639. "list": [
  640. {
  641. "delivery_id": "SFTC",
  642. "delivery_name": "顺发同城"
  643. },
  644. {
  645. "delivery_id": "MTPS",
  646. "delivery_name": "美团配送"
  647. }
  648. ]
  649. }`
  650. if _, err := w.Write([]byte(raw)); err != nil {
  651. t.Fatal(err)
  652. }
  653. }))
  654. res, err := getAllImmediateDelivery(ts.URL+apiGetAllImmediateDelivery, "mock-access-token")
  655. if err != nil {
  656. t.Fatal(err)
  657. }
  658. if res.ResultCode == 0 {
  659. t.Error("Response 'resultcode' can not be empty")
  660. }
  661. if res.ResultMsg == "" {
  662. t.Error("Response 'resultmsg' can not be empty")
  663. }
  664. if len(res.List) == 0 {
  665. t.Error("Response 'list' can not be empty")
  666. } else {
  667. for _, item := range res.List {
  668. if item.ID == "" {
  669. t.Error("Response 'list.delivery_id' can not be empty")
  670. }
  671. if item.Name == "" {
  672. t.Error("Response 'list.delivery_name' can not be empty")
  673. }
  674. }
  675. }
  676. }
  677. func TestGetBindAccount(t *testing.T) {
  678. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  679. if r.Method != "POST" {
  680. t.Error("UnExpect request method")
  681. }
  682. if r.URL.EscapedPath() != "/cgi-bin/express/local/business/shop/get" {
  683. t.Error("Unexpected path")
  684. }
  685. if err := r.ParseForm(); err != nil {
  686. t.Fatal(err)
  687. }
  688. if r.Form.Get("access_token") == "" {
  689. t.Fatalf("Query 'access_token' can not be empty")
  690. }
  691. w.WriteHeader(http.StatusOK)
  692. raw := `{
  693. "resultcode": 1,
  694. "resultmsg": "ok",
  695. "shop_list": [
  696. {
  697. "delivery_id": "SFTC",
  698. "shopid": "123456",
  699. "audit_result": 1
  700. },
  701. {
  702. "delivery_id": "MTPS",
  703. "shopid": "123456",
  704. "audit_result": 1
  705. }
  706. ]
  707. }`
  708. if _, err := w.Write([]byte(raw)); err != nil {
  709. t.Fatal(err)
  710. }
  711. }))
  712. res, err := getBindAccount(ts.URL+apiGetDeliveryBindAccount, "mock-access-token")
  713. if err != nil {
  714. t.Fatal(err)
  715. }
  716. if res.ResultCode == 0 {
  717. t.Error("Response 'resultcode' can not be empty")
  718. }
  719. if res.ResultMsg == "" {
  720. t.Error("Response 'resultmsg' can not be empty")
  721. }
  722. if len(res.ShopList) == 0 {
  723. t.Error("Response 'shop_list' can not be empty")
  724. } else {
  725. for _, item := range res.ShopList {
  726. if item.DeliveryID == "" {
  727. t.Error("Response 'shop_list.delivery_id' can not be empty")
  728. }
  729. if item.ShopID == "" {
  730. t.Error("Response 'shop_list.shopid' can not be empty")
  731. }
  732. if item.AuditResult == 0 {
  733. t.Error("Response 'audit_result.shopid' can not be empty")
  734. }
  735. }
  736. }
  737. }
  738. func TestGetDeliveryOrder(t *testing.T) {
  739. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  740. if r.Method != "POST" {
  741. t.Error("UnExpect request method")
  742. }
  743. if r.URL.EscapedPath() != "/cgi-bin/express/local/business/order/get" {
  744. t.Error("Unexpected path")
  745. }
  746. if err := r.ParseForm(); err != nil {
  747. t.Fatal(err)
  748. }
  749. if r.Form.Get("access_token") == "" {
  750. t.Fatalf("Query 'access_token' can not be empty")
  751. }
  752. params := struct {
  753. ShopID string `json:"shopid"`
  754. ShopOrderID string `json:"shop_order_id"`
  755. ShopNo string `json:"shop_no"`
  756. DeliverySign string `json:"delivery_sign"`
  757. }{}
  758. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  759. t.Fatal(err)
  760. }
  761. if params.ShopID == "" {
  762. t.Error("Response column shopid can not be empty")
  763. }
  764. if params.ShopOrderID == "" {
  765. t.Error("Response column shop_order_id can not be empty")
  766. }
  767. if params.ShopNo == "" {
  768. t.Error("Response column shop_no can not be empty")
  769. }
  770. if params.DeliverySign == "" {
  771. t.Error("Response column delivery_sign can not be empty")
  772. }
  773. w.WriteHeader(http.StatusOK)
  774. raw := `{
  775. "resultcode": 1,
  776. "resultmsg": "ok",
  777. "order_status": 1,
  778. "waybill_id": "string",
  779. "rider_name": "string",
  780. "rider_phone": "string",
  781. "rider_lng": 3.14,
  782. "rider_lat": 3.14
  783. }`
  784. if _, err := w.Write([]byte(raw)); err != nil {
  785. t.Fatal(err)
  786. }
  787. }))
  788. raw := `{
  789. "shopid": "xxxxxx",
  790. "shop_order_id": "xxxxxx",
  791. "shop_no": "xxxxxx",
  792. "delivery_sign": "xxxxxx"
  793. }`
  794. getter := new(DeliveryOrderGetter)
  795. err := json.Unmarshal([]byte(raw), getter)
  796. if err != nil {
  797. t.Fatal(err)
  798. }
  799. res, err := getter.get(ts.URL+apiGetDeliveryOrder, "mock-access-token")
  800. if err != nil {
  801. t.Fatal(err)
  802. }
  803. if res.ResultCode == 0 {
  804. t.Error("Response 'resultcode' can not be empty")
  805. }
  806. if res.ResultMsg == "" {
  807. t.Error("Response 'resultmsg' can not be empty")
  808. }
  809. if res.WaybillID == "" {
  810. t.Error("Response 'waybill_id' can not be empty")
  811. }
  812. if res.OrderStatus == 0 {
  813. t.Error("Response 'order_status' can not be empty")
  814. }
  815. if res.RiderName == "" {
  816. t.Error("Response 'rider_name' can not be empty")
  817. }
  818. if res.RiderPhone == "" {
  819. t.Error("Response 'rider_phone' can not be empty")
  820. }
  821. if res.RiderLng == 0 {
  822. t.Error("Response 'rider_lng' can not be empty")
  823. }
  824. if res.RiderLat == 0 {
  825. t.Error("Response 'rider_lat' can not be empty")
  826. }
  827. }
  828. func TestMockUpdateDeliveryOrder(t *testing.T) {
  829. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  830. if r.Method != "POST" {
  831. t.Error("UnExpect request method")
  832. }
  833. if r.URL.EscapedPath() != "/cgi-bin/express/local/business/test_update_order" {
  834. t.Error("Unexpected path")
  835. }
  836. if err := r.ParseForm(); err != nil {
  837. t.Fatal(err)
  838. }
  839. if r.Form.Get("access_token") == "" {
  840. t.Fatalf("Query 'access_token' can not be empty")
  841. }
  842. params := struct {
  843. ShopID string `json:"shopid"`
  844. ShopOrderID string `json:"shop_order_id"`
  845. ActionTime uint `json:"action_time"`
  846. OrderStatus int `json:"order_status"`
  847. ActionMsg string `json:"action_msg"`
  848. }{}
  849. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  850. t.Fatal(err)
  851. }
  852. if params.ShopID == "" {
  853. t.Error("Response column shopid can not be empty")
  854. }
  855. if params.ShopOrderID == "" {
  856. t.Error("Response column shop_order_id can not be empty")
  857. }
  858. if params.ActionTime == 0 {
  859. t.Error("Response column action_time can not be empty")
  860. }
  861. if params.OrderStatus == 0 {
  862. t.Error("Response column order_status can not be empty")
  863. }
  864. if params.ActionMsg == "" {
  865. t.Error("Response column action_msg can not be empty")
  866. }
  867. w.WriteHeader(http.StatusOK)
  868. raw := `{
  869. "resultcode": 1,
  870. "resultmsg": "ok"
  871. }`
  872. if _, err := w.Write([]byte(raw)); err != nil {
  873. t.Fatal(err)
  874. }
  875. }))
  876. raw := `{
  877. "shopid": "test_shop_id",
  878. "shop_order_id": "xxxxxxxxxxx",
  879. "waybill_id": "xxxxxxxxxxxxx",
  880. "action_time": 12345678,
  881. "order_status": 101,
  882. "action_msg": "xxxxxx"
  883. }`
  884. mocker := new(UpdateDeliveryOrderMocker)
  885. err := json.Unmarshal([]byte(raw), mocker)
  886. if err != nil {
  887. t.Fatal(err)
  888. }
  889. res, err := mocker.mock(ts.URL+apiMockUpdateDeliveryOrder, "mock-access-token")
  890. if err != nil {
  891. t.Fatal(err)
  892. }
  893. if res.ResultCode == 0 {
  894. t.Error("Response 'resultcode' can not be empty")
  895. }
  896. if res.ResultMsg == "" {
  897. t.Error("Response 'resultmsg' can not be empty")
  898. }
  899. }
  900. func TestPreAddDeliveryOrder(t *testing.T) {
  901. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  902. if r.Method != "POST" {
  903. t.Error("UnExpect request method")
  904. }
  905. if r.URL.EscapedPath() != "/cgi-bin/express/local/business/order/pre_add" {
  906. t.Error("Unexpected path")
  907. }
  908. if err := r.ParseForm(); err != nil {
  909. t.Fatal(err)
  910. }
  911. if r.Form.Get("access_token") == "" {
  912. t.Fatalf("access_token can not be empty")
  913. }
  914. params := struct {
  915. ShopID string `json:"shopid"` // 商家id, 由配送公司分配的appkey
  916. ShopOrderID string `json:"shop_order_id"` // 唯一标识订单的 ID,由商户生成
  917. ShopNo string `json:"shop_no"` // 商家门店编号, 在配送公司登记,如果只有一个门店,可以不填
  918. DeliverySign string `json:"delivery_sign"` // 用配送公司提供的appSecret加密的校验串
  919. DeliveryID string `json:"delivery_id"` // 配送公司ID
  920. OpenID string `json:"openid"` // 下单用户的openid
  921. SubBizID string `json:"sub_biz_id"` // 子商户id,区分小程序内部多个子商户
  922. Sender struct {
  923. Name string `json:"name"` // 姓名,最长不超过256个字符
  924. City string `json:"city"` // 城市名称,如广州市
  925. Address string `json:"address"` // 地址(街道、小区、大厦等,用于定位)
  926. AddressDetail string `json:"address_detail"` // 地址详情(楼号、单元号、层号)
  927. Phone string `json:"phone"` // 电话/手机号,最长不超过64个字符
  928. Lng float64 `json:"lng"` // 经度(火星坐标或百度坐标,和 coordinate_type 字段配合使用,确到小数点后6位
  929. Lat float64 `json:"lat"` // 纬度(火星坐标或百度坐标,和 coordinate_type 字段配合使用,精确到小数点后6位)
  930. CoordinateType uint8 `json:"coordinate_type"` // 坐标类型,0:火星坐标(高德,腾讯地图均采用火星坐标) 1:百度坐标
  931. } `json:"sender"` // 发件人信息,闪送、顺丰同城急送必须填写,美团配送、达达,若传了shop_no的值可不填该字段
  932. Receiver struct {
  933. Name string `json:"name"` // 姓名,最长不超过256个字符
  934. City string `json:"city"` // 城市名称,如广州市
  935. Address string `json:"address"` // 地址(街道、小区、大厦等,用于定位)
  936. AddressDetail string `json:"address_detail"` // 地址详情(楼号、单元号、层号)
  937. Phone string `json:"phone"` // 电话/手机号,最长不超过64个字符
  938. Lng float64 `json:"lng"` // 经度(火星坐标或百度坐标,和 coordinate_type 字段配合使用,确到小数点后6位
  939. Lat float64 `json:"lat"` // 纬度(火星坐标或百度坐标,和 coordinate_type 字段配合使用,精确到小数点后6位)
  940. CoordinateType uint8 `json:"coordinate_type"` // 坐标类型,0:火星坐标(高德,腾讯地图均采用火星坐标) 1:百度坐标
  941. } `json:"receiver"` // 收件人信息
  942. Cargo struct {
  943. GoodsValue float64 `json:"goods_value"` // 货物价格,单位为元,精确到小数点后两位(如果小数点后位数多于两位,则四舍五入保留两位小数),范围为(0-5000]
  944. GoodsHeight float64 `json:"goods_height"` // 货物高度,单位为cm,精确到小数点后两位(如果小数点后位数多于两位,则四舍五入保留两位小数),范围为(0-45]
  945. GoodsLength float64 `json:"goods_length"` // 货物长度,单位为cm,精确到小数点后两位(如果小数点后位数多于两位,则四舍五入保留两位小数),范围为(0-65]
  946. GoodsWidth float64 `json:"goods_width"` // 货物宽度,单位为cm,精确到小数点后两位(如果小数点后位数多于两位,则四舍五入保留两位小数),范围为(0-50]
  947. GoodsWeight float64 `json:"goods_weight"` // 货物重量,单位为kg,精确到小数点后两位(如果小数点后位数多于两位,则四舍五入保留两位小数),范围为(0-50]
  948. GoodsDetail struct {
  949. Goods []struct {
  950. Count uint `json:"good_count"` // 货物数量
  951. Name string `json:"good_name"` // 货品名称
  952. Price float32 `json:"good_price"` // 货品单价,精确到小数点后两位(如果小数点后位数多于两位,则四舍五入保留两位小数)
  953. Unit string `json:"good_unit"` // 货品单位,最长不超过20个字符
  954. } `json:"goods"` // 货物交付信息,最长不超过100个字符
  955. } `json:"goods_detail"` // 货物详情,最长不超过10240个字符
  956. GoodsPickupInfo string `json:"goods_pickup_info"` // 货物取货信息,用于骑手到店取货,最长不超过100个字符
  957. GoodsDeliveryInfo string `json:"goods_delivery_info"` // 货物交付信息,最长不超过100个字符
  958. CargoFirstClass string `json:"cargo_first_class"` // 品类一级类目
  959. CargoSecondClass string `json:"cargo_second_class"` // 品类二级类目
  960. } `json:"cargo"` // 货物信息
  961. OrderInfo struct {
  962. DeliveryServiceCode string `json:"delivery_service_code"` // 配送服务代码 不同配送公司自定义,微信侧不理解
  963. OrderType uint8 `json:"order_type"` // 订单类型, 0: 即时单 1 预约单,如预约单,需要设置expected_delivery_time或expected_finish_time或expected_pick_time
  964. ExpectedDeliveryTime uint `json:"expected_delivery_time"` // 期望派单时间(顺丰同城急送、达达、支持),unix-timestamp
  965. ExpectedFinishTime uint `json:"expected_finish_time"` // 期望送达时间(顺丰同城急送、美团配送支持),unix-timestamp
  966. ExpectedPickTime uint `json:"expected_pick_time"` // 期望取件时间(闪送支持),unix-timestamp
  967. PoiSeq string `json:"poi_seq"` // 门店订单流水号,建议提供,方便骑手门店取货,最长不超过32个字符
  968. Note string `json:"note"` // 备注,最长不超过200个字符
  969. OrderTime uint `json:"order_time"` // 用户下单付款时间
  970. IsInsured uint8 `json:"is_insured"` // 是否保价,0,非保价,1.保价
  971. DeclaredValue float64 `json:"declared_value"` // 保价金额,单位为元,精确到分
  972. Tips float64 `json:"tips"` // 小费,单位为元, 下单一般不加小费
  973. IsDirectDelivery uint `json:"is_direct_delivery"` // 是否选择直拿直送(0:不需要;1:需要。选择直拿直送后,同一时间骑手只能配送此订单至完成,配送费用也相应高一些,闪送必须选1,达达可选0或1,其余配送公司不支持直拿直送)
  974. CashOnDelivery uint `json:"cash_on_delivery"` // 骑手应付金额,单位为元,精确到分
  975. CashOnPickup uint `json:"cash_on_pickup"` // 骑手应收金额,单位为元,精确到分
  976. RiderPickMethod uint8 `json:"rider_pick_method"` // 物流流向,1:从门店取件送至用户;2:从用户取件送至门店
  977. IsFinishCodeNeeded uint8 `json:"is_finish_code_needed"` // 收货码(0:不需要;1:需要。收货码的作用是:骑手必须输入收货码才能完成订单妥投)
  978. IsPickupCodeNeeded uint8 `json:"is_pickup_code_needed"` // 取货码(0:不需要;1:需要。取货码的作用是:骑手必须输入取货码才能从商家取货)
  979. } `json:"order_info"` // 订单信息
  980. Shop struct {
  981. WxaPath string `json:"wxa_path"` // 商家小程序的路径,建议为订单页面
  982. ImgURL string `json:"img_url"` // 商品缩略图 url
  983. GoodsName string `json:"goods_name"` // 商品名称
  984. GoodsCount uint `json:"goods_count"` // 商品数量
  985. } `json:"shop"` // 商品信息,会展示到物流通知消息中
  986. }{}
  987. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  988. t.Fatal(err)
  989. }
  990. if params.ShopID == "" {
  991. t.Error("Response column 'shopid' can not be empty")
  992. }
  993. if params.ShopOrderID == "" {
  994. t.Error("Response column 'shop_order_id' can not be empty")
  995. }
  996. if params.ShopNo == "" {
  997. t.Error("Response column 'shop_no' can not be empty")
  998. }
  999. if params.DeliverySign == "" {
  1000. t.Error("Response column 'delivery_sign' can not be empty")
  1001. }
  1002. if params.DeliveryID == "" {
  1003. t.Error("Response column 'delivery_id' can not be empty")
  1004. }
  1005. if params.OpenID == "" {
  1006. t.Error("Response column 'openid' can not be empty")
  1007. }
  1008. if params.SubBizID == "" {
  1009. t.Error("Response column 'sub_biz_id' can not be empty")
  1010. }
  1011. if params.Sender.Name == "" {
  1012. t.Error("Param 'sender.name' can not be empty")
  1013. }
  1014. if params.Sender.City == "" {
  1015. t.Error("Param 'sender.city' can not be empty")
  1016. }
  1017. if params.Sender.Address == "" {
  1018. t.Error("Param 'sender.address' can not be empty")
  1019. }
  1020. if params.Sender.AddressDetail == "" {
  1021. t.Error("Param 'sender.address_detail' can not be empty")
  1022. }
  1023. if params.Sender.Phone == "" {
  1024. t.Error("Param 'sender.phone' can not be empty")
  1025. }
  1026. if params.Sender.Lng == 0 {
  1027. t.Error("Param 'sender.lng' can not be empty")
  1028. }
  1029. if params.Sender.Lat == 0 {
  1030. t.Error("Param 'sender.lat' can not be empty")
  1031. }
  1032. if params.Sender.CoordinateType == 0 {
  1033. t.Error("Param 'sender.coordinate_type' can not be empty")
  1034. }
  1035. if params.Receiver.Name == "" {
  1036. t.Error("Param 'receiver.name' can not be empty")
  1037. }
  1038. if params.Receiver.City == "" {
  1039. t.Error("Param 'receiver.city' can not be empty")
  1040. }
  1041. if params.Receiver.Address == "" {
  1042. t.Error("Param 'receiver.address' can not be empty")
  1043. }
  1044. if params.Receiver.AddressDetail == "" {
  1045. t.Error("Param 'receiver.address_detail' can not be empty")
  1046. }
  1047. if params.Receiver.Phone == "" {
  1048. t.Error("Param 'receiver.phone' can not be empty")
  1049. }
  1050. if params.Receiver.Lng == 0 {
  1051. t.Error("Param 'receiver.lng' can not be empty")
  1052. }
  1053. if params.Receiver.Lat == 0 {
  1054. t.Error("Param 'receiver.lat' can not be empty")
  1055. }
  1056. if params.Receiver.CoordinateType == 0 {
  1057. t.Error("Param 'receiver.coordinate_type' can not be empty")
  1058. }
  1059. if params.Cargo.GoodsValue == 0 {
  1060. t.Error("Param 'cargo.goods_value' can not be empty")
  1061. }
  1062. if params.Cargo.GoodsHeight == 0 {
  1063. t.Error("Param 'cargo.goods_height' can not be empty")
  1064. }
  1065. if params.Cargo.GoodsLength == 0 {
  1066. t.Error("Param 'cargo.goods_length' can not be empty")
  1067. }
  1068. if params.Cargo.GoodsWidth == 0 {
  1069. t.Error("Param 'cargo.goods_width' can not be empty")
  1070. }
  1071. if params.Cargo.GoodsWeight == 0 {
  1072. t.Error("Param 'cargo.goods_weight' can not be empty")
  1073. }
  1074. if params.Cargo.CargoFirstClass == "" {
  1075. t.Error("Param 'cargo.cargo_first_class' can not be empty")
  1076. }
  1077. if params.Cargo.CargoSecondClass == "" {
  1078. t.Error("Param 'cargo.cargo_second_class' can not be empty")
  1079. }
  1080. if len(params.Cargo.GoodsDetail.Goods) > 0 {
  1081. if params.Cargo.GoodsDetail.Goods[0].Count == 0 {
  1082. t.Error("Param 'cargo.goods_detail.goods.good_count' can not be empty")
  1083. }
  1084. if params.Cargo.GoodsDetail.Goods[0].Name == "" {
  1085. t.Error("Param 'cargo.goods_detail.goods.good_name' can not be empty")
  1086. }
  1087. if params.Cargo.GoodsDetail.Goods[0].Price == 0 {
  1088. t.Error("Param 'cargo.goods_detail.goods.good_price' can not be empty")
  1089. }
  1090. if params.Cargo.GoodsDetail.Goods[0].Unit == "" {
  1091. t.Error("Param 'cargo.goods_detail.goods.good_unit' can not be empty")
  1092. }
  1093. }
  1094. if params.OrderInfo.DeliveryServiceCode == "" {
  1095. t.Error("Param 'order_info.delivery_service_code' can not be empty")
  1096. }
  1097. if params.Shop.WxaPath == "" {
  1098. t.Error("Param 'shop.wxa_path' can not be empty")
  1099. }
  1100. if params.Shop.ImgURL == "" {
  1101. t.Error("Param 'shop.img_url' can not be empty")
  1102. }
  1103. if params.Shop.GoodsName == "" {
  1104. t.Error("Param 'shop.goods_name' can not be empty")
  1105. }
  1106. if params.Shop.GoodsCount == 0 {
  1107. t.Error("Param 'shop.goods_count' can not be empty")
  1108. }
  1109. w.WriteHeader(http.StatusOK)
  1110. raw := `{
  1111. "resultcode": 1,
  1112. "resultmsg": "ok",
  1113. "fee": 11,
  1114. "deliverfee": 11,
  1115. "couponfee": 1,
  1116. "insurancefee": 1000,
  1117. "tips": 1,
  1118. "insurancfee": 1,
  1119. "distance": 1001,
  1120. "dispatch_duration": 301,
  1121. "delivery_token": "1111111"
  1122. }`
  1123. if _, err := w.Write([]byte(raw)); err != nil {
  1124. t.Fatal(err)
  1125. }
  1126. }))
  1127. defer ts.Close()
  1128. raw := `{
  1129. "cargo": {
  1130. "cargo_first_class": "美食宵夜",
  1131. "cargo_second_class": "零食小吃",
  1132. "goods_detail": {
  1133. "goods": [
  1134. {
  1135. "good_count": 1,
  1136. "good_name": "水果",
  1137. "good_price": 11,
  1138. "good_unit": "元"
  1139. },
  1140. {
  1141. "good_count": 2,
  1142. "good_name": "蔬菜",
  1143. "good_price": 21,
  1144. "good_unit": "元"
  1145. }
  1146. ]
  1147. },
  1148. "goods_height": 1,
  1149. "goods_length": 3,
  1150. "goods_value": 5,
  1151. "goods_weight": 1,
  1152. "goods_width": 2
  1153. },
  1154. "delivery_id": "SFTC",
  1155. "delivery_sign": "01234567890123456789",
  1156. "openid": "oABC123456",
  1157. "order_info": {
  1158. "delivery_service_code": "xxx",
  1159. "expected_delivery_time": 1,
  1160. "is_direct_delivery": 1,
  1161. "is_finish_code_needed": 1,
  1162. "is_insured": 1,
  1163. "is_pickup_code_needed": 1,
  1164. "note": "test_note",
  1165. "order_time": 1555220757,
  1166. "order_type": 1,
  1167. "poi_seq": "1111",
  1168. "tips": 0
  1169. },
  1170. "receiver": {
  1171. "address": "xxx地铁站",
  1172. "address_detail": "2号楼202",
  1173. "city": "北京市",
  1174. "coordinate_type": 1,
  1175. "lat": 40.1529600001,
  1176. "lng": 116.5060300001,
  1177. "name": "老王",
  1178. "phone": "18512345678"
  1179. },
  1180. "sender": {
  1181. "address": "xx大厦",
  1182. "address_detail": "1号楼101",
  1183. "city": "北京市",
  1184. "coordinate_type": 1,
  1185. "lat": 40.4486120001,
  1186. "lng": 116.3830750001,
  1187. "name": "刘一",
  1188. "phone": "13712345678"
  1189. },
  1190. "shop": {
  1191. "goods_count": 2,
  1192. "goods_name": "宝贝",
  1193. "img_url": "https://mmbiz.qpic.cn/mmbiz_png/xxxxxxxxx/0?wx_fmt=png",
  1194. "wxa_path": "/page/index/index"
  1195. },
  1196. "shop_no": "12345678",
  1197. "sub_biz_id": "sub_biz_id_1",
  1198. "shop_order_id": "SFTC_001",
  1199. "shopid": "122222222"
  1200. }`
  1201. creator := new(DeliveryOrderCreator)
  1202. err := json.Unmarshal([]byte(raw), creator)
  1203. if err != nil {
  1204. t.Fatal(err)
  1205. }
  1206. res, err := creator.prepare(ts.URL+apiPreAddDeliveryOrder, "mock-access-token")
  1207. if err != nil {
  1208. t.Fatal(err)
  1209. }
  1210. if res.Fee == 0 {
  1211. t.Error("Response 'fee' can not be empty")
  1212. }
  1213. if res.Deliverfee == 0 {
  1214. t.Error("Response 'deliverfee' can not be empty")
  1215. }
  1216. if res.Couponfee == 0 {
  1217. t.Error("Response 'couponfee' can not be empty")
  1218. }
  1219. if res.Tips == 0 {
  1220. t.Error("Response 'tips' can not be empty")
  1221. }
  1222. if res.Insurancefee == 0 {
  1223. t.Error("Response 'insurancefee' can not be empty")
  1224. }
  1225. if res.Distance == 0 {
  1226. t.Error("Response 'distance' can not be empty")
  1227. }
  1228. if res.DispatchDuration == 0 {
  1229. t.Error("Response 'dispatch_duration' can not be empty")
  1230. }
  1231. }
  1232. func TestPreCancelDeliveryOrder(t *testing.T) {
  1233. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  1234. if r.Method != "POST" {
  1235. t.Error("UnExpect request method")
  1236. }
  1237. if r.URL.EscapedPath() != "/cgi-bin/express/local/business/order/precancel" {
  1238. t.Error("Unexpected path")
  1239. }
  1240. if err := r.ParseForm(); err != nil {
  1241. t.Fatal(err)
  1242. }
  1243. if r.Form.Get("access_token") == "" {
  1244. t.Fatalf("Query 'access_token' can not be empty")
  1245. }
  1246. params := struct {
  1247. ShopID string `json:"shopid"` // 商家id, 由配送公司分配的appkey
  1248. ShopOrderID string `json:"shop_order_id"` // 唯一标识订单的 ID,由商户生成
  1249. ShopNo string `json:"shop_no"` // 商家门店编号, 在配送公司登记,如果只有一个门店,可以不填
  1250. DeliverySign string `json:"delivery_sign"` // 用配送公司提供的appSecret加密的校验串
  1251. DeliveryID string `json:"delivery_id"` // 快递公司ID
  1252. WaybillID string `json:"waybill_id"` // 配送单id
  1253. ReasonID uint8 `json:"cancel_reason_id"` // 取消原因Id
  1254. Reason string `json:"cancel_reason"` // 取消原因
  1255. }{}
  1256. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  1257. t.Fatal(err)
  1258. }
  1259. if params.ShopID == "" {
  1260. t.Error("Param 'shopid' can not be empty")
  1261. }
  1262. if params.ShopOrderID == "" {
  1263. t.Error("Param 'shop_order_id' can not be empty")
  1264. }
  1265. if params.ShopNo == "" {
  1266. t.Error("Param 'shop_no' can not be empty")
  1267. }
  1268. if params.DeliverySign == "" {
  1269. t.Error("Param 'delivery_sign' can not be empty")
  1270. }
  1271. if params.WaybillID == "" {
  1272. t.Error("Param 'waybill_id' can not be empty")
  1273. }
  1274. if params.DeliveryID == "" {
  1275. t.Error("Param 'delivery_id' can not be empty")
  1276. }
  1277. if params.ReasonID == 0 {
  1278. t.Error("Param 'cancel_reason_id' can not be empty")
  1279. }
  1280. if params.Reason == "" {
  1281. t.Error("Param 'cancel_reason' can not be empty")
  1282. }
  1283. w.WriteHeader(http.StatusOK)
  1284. raw := `{
  1285. "resultcode": 1,
  1286. "resultmsg": "ok",
  1287. "deduct_fee": 5,
  1288. "desc": "blabla"
  1289. }`
  1290. if _, err := w.Write([]byte(raw)); err != nil {
  1291. t.Fatal(err)
  1292. }
  1293. }))
  1294. defer ts.Close()
  1295. raw := `{
  1296. "shopid": "123456",
  1297. "shop_order_id": "123456",
  1298. "waybill_id": "123456",
  1299. "delivery_id": "123456",
  1300. "cancel_reason_id": 1,
  1301. "cancel_reason": "xxxxxx",
  1302. "delivery_sign": "123456",
  1303. "shop_no": "shop_no_111"
  1304. }`
  1305. canceler := new(DeliveryOrderCanceler)
  1306. err := json.Unmarshal([]byte(raw), canceler)
  1307. if err != nil {
  1308. t.Fatal(err)
  1309. }
  1310. res, err := canceler.prepare(ts.URL+apiPreCancelDeliveryOrder, "mock-access-token")
  1311. if err != nil {
  1312. t.Fatal(err)
  1313. }
  1314. if res.ResultCode == 0 {
  1315. t.Error("Response 'resultcode' can not be empty")
  1316. }
  1317. if res.ResultMsg == "" {
  1318. t.Error("Response 'resultmsg' can not be empty")
  1319. }
  1320. if res.DeductFee == 0 {
  1321. t.Error("Response 'deduct_fee' can not be empty")
  1322. }
  1323. if res.Desc == "" {
  1324. t.Error("Response 'desc' can not be empty")
  1325. }
  1326. }
  1327. func TestReAddDeliveryOrder(t *testing.T) {
  1328. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  1329. if r.Method != "POST" {
  1330. t.Error("UnExpect request method")
  1331. }
  1332. if r.URL.EscapedPath() != "/cgi-bin/express/local/business/order/readd" {
  1333. t.Error("Unexpected path")
  1334. }
  1335. if err := r.ParseForm(); err != nil {
  1336. t.Fatal(err)
  1337. }
  1338. if r.Form.Get("access_token") == "" {
  1339. t.Fatalf("access_token can not be empty")
  1340. }
  1341. params := struct {
  1342. DeliveryToken string `json:"delivery_token"` // 预下单接口返回的参数,配送公司可保证在一段时间内运费不变
  1343. ShopID string `json:"shopid"` // 商家id, 由配送公司分配的appkey
  1344. ShopOrderID string `json:"shop_order_id"` // 唯一标识订单的 ID,由商户生成
  1345. ShopNo string `json:"shop_no"` // 商家门店编号, 在配送公司登记,如果只有一个门店,可以不填
  1346. DeliverySign string `json:"delivery_sign"` // 用配送公司提供的appSecret加密的校验串
  1347. DeliveryID string `json:"delivery_id"` // 配送公司ID
  1348. OpenID string `json:"openid"` // 下单用户的openid
  1349. SubBizID string `json:"sub_biz_id"` // 子商户id,区分小程序内部多个子商户
  1350. Sender struct {
  1351. Name string `json:"name"` // 姓名,最长不超过256个字符
  1352. City string `json:"city"` // 城市名称,如广州市
  1353. Address string `json:"address"` // 地址(街道、小区、大厦等,用于定位)
  1354. AddressDetail string `json:"address_detail"` // 地址详情(楼号、单元号、层号)
  1355. Phone string `json:"phone"` // 电话/手机号,最长不超过64个字符
  1356. Lng float64 `json:"lng"` // 经度(火星坐标或百度坐标,和 coordinate_type 字段配合使用,确到小数点后6位
  1357. Lat float64 `json:"lat"` // 纬度(火星坐标或百度坐标,和 coordinate_type 字段配合使用,精确到小数点后6位)
  1358. CoordinateType uint8 `json:"coordinate_type"` // 坐标类型,0:火星坐标(高德,腾讯地图均采用火星坐标) 1:百度坐标
  1359. } `json:"sender"` // 发件人信息,闪送、顺丰同城急送必须填写,美团配送、达达,若传了shop_no的值可不填该字段
  1360. Receiver struct {
  1361. Name string `json:"name"` // 姓名,最长不超过256个字符
  1362. City string `json:"city"` // 城市名称,如广州市
  1363. Address string `json:"address"` // 地址(街道、小区、大厦等,用于定位)
  1364. AddressDetail string `json:"address_detail"` // 地址详情(楼号、单元号、层号)
  1365. Phone string `json:"phone"` // 电话/手机号,最长不超过64个字符
  1366. Lng float64 `json:"lng"` // 经度(火星坐标或百度坐标,和 coordinate_type 字段配合使用,确到小数点后6位
  1367. Lat float64 `json:"lat"` // 纬度(火星坐标或百度坐标,和 coordinate_type 字段配合使用,精确到小数点后6位)
  1368. CoordinateType uint8 `json:"coordinate_type"` // 坐标类型,0:火星坐标(高德,腾讯地图均采用火星坐标) 1:百度坐标
  1369. } `json:"receiver"` // 收件人信息
  1370. Cargo struct {
  1371. GoodsValue float64 `json:"goods_value"` // 货物价格,单位为元,精确到小数点后两位(如果小数点后位数多于两位,则四舍五入保留两位小数),范围为(0-5000]
  1372. GoodsHeight float64 `json:"goods_height"` // 货物高度,单位为cm,精确到小数点后两位(如果小数点后位数多于两位,则四舍五入保留两位小数),范围为(0-45]
  1373. GoodsLength float64 `json:"goods_length"` // 货物长度,单位为cm,精确到小数点后两位(如果小数点后位数多于两位,则四舍五入保留两位小数),范围为(0-65]
  1374. GoodsWidth float64 `json:"goods_width"` // 货物宽度,单位为cm,精确到小数点后两位(如果小数点后位数多于两位,则四舍五入保留两位小数),范围为(0-50]
  1375. GoodsWeight float64 `json:"goods_weight"` // 货物重量,单位为kg,精确到小数点后两位(如果小数点后位数多于两位,则四舍五入保留两位小数),范围为(0-50]
  1376. GoodsDetail struct {
  1377. Goods []struct {
  1378. Count uint `json:"good_count"` // 货物数量
  1379. Name string `json:"good_name"` // 货品名称
  1380. Price float32 `json:"good_price"` // 货品单价,精确到小数点后两位(如果小数点后位数多于两位,则四舍五入保留两位小数)
  1381. Unit string `json:"good_unit"` // 货品单位,最长不超过20个字符
  1382. } `json:"goods"` // 货物交付信息,最长不超过100个字符
  1383. } `json:"goods_detail"` // 货物详情,最长不超过10240个字符
  1384. GoodsPickupInfo string `json:"goods_pickup_info"` // 货物取货信息,用于骑手到店取货,最长不超过100个字符
  1385. GoodsDeliveryInfo string `json:"goods_delivery_info"` // 货物交付信息,最长不超过100个字符
  1386. CargoFirstClass string `json:"cargo_first_class"` // 品类一级类目
  1387. CargoSecondClass string `json:"cargo_second_class"` // 品类二级类目
  1388. } `json:"cargo"` // 货物信息
  1389. OrderInfo struct {
  1390. DeliveryServiceCode string `json:"delivery_service_code"` // 配送服务代码 不同配送公司自定义,微信侧不理解
  1391. OrderType uint8 `json:"order_type"` // 订单类型, 0: 即时单 1 预约单,如预约单,需要设置expected_delivery_time或expected_finish_time或expected_pick_time
  1392. ExpectedDeliveryTime uint `json:"expected_delivery_time"` // 期望派单时间(顺丰同城急送、达达、支持),unix-timestamp
  1393. ExpectedFinishTime uint `json:"expected_finish_time"` // 期望送达时间(顺丰同城急送、美团配送支持),unix-timestamp
  1394. ExpectedPickTime uint `json:"expected_pick_time"` // 期望取件时间(闪送支持),unix-timestamp
  1395. PoiSeq string `json:"poi_seq"` // 门店订单流水号,建议提供,方便骑手门店取货,最长不超过32个字符
  1396. Note string `json:"note"` // 备注,最长不超过200个字符
  1397. OrderTime uint `json:"order_time"` // 用户下单付款时间
  1398. IsInsured uint8 `json:"is_insured"` // 是否保价,0,非保价,1.保价
  1399. DeclaredValue float64 `json:"declared_value"` // 保价金额,单位为元,精确到分
  1400. Tips float64 `json:"tips"` // 小费,单位为元, 下单一般不加小费
  1401. IsDirectDelivery uint `json:"is_direct_delivery"` // 是否选择直拿直送(0:不需要;1:需要。选择直拿直送后,同一时间骑手只能配送此订单至完成,配送费用也相应高一些,闪送必须选1,达达可选0或1,其余配送公司不支持直拿直送)
  1402. CashOnDelivery uint `json:"cash_on_delivery"` // 骑手应付金额,单位为元,精确到分
  1403. CashOnPickup uint `json:"cash_on_pickup"` // 骑手应收金额,单位为元,精确到分
  1404. RiderPickMethod uint8 `json:"rider_pick_method"` // 物流流向,1:从门店取件送至用户;2:从用户取件送至门店
  1405. IsFinishCodeNeeded uint8 `json:"is_finish_code_needed"` // 收货码(0:不需要;1:需要。收货码的作用是:骑手必须输入收货码才能完成订单妥投)
  1406. IsPickupCodeNeeded uint8 `json:"is_pickup_code_needed"` // 取货码(0:不需要;1:需要。取货码的作用是:骑手必须输入取货码才能从商家取货)
  1407. } `json:"order_info"` // 订单信息
  1408. Shop struct {
  1409. WxaPath string `json:"wxa_path"` // 商家小程序的路径,建议为订单页面
  1410. ImgURL string `json:"img_url"` // 商品缩略图 url
  1411. GoodsName string `json:"goods_name"` // 商品名称
  1412. GoodsCount uint `json:"goods_count"` // 商品数量
  1413. } `json:"shop"` // 商品信息,会展示到物流通知消息中
  1414. }{}
  1415. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  1416. t.Fatal(err)
  1417. }
  1418. if params.DeliveryToken == "" {
  1419. t.Error("Response column 'delivery_token' can not be empty")
  1420. }
  1421. if params.ShopID == "" {
  1422. t.Error("Response column 'shopid' can not be empty")
  1423. }
  1424. if params.ShopOrderID == "" {
  1425. t.Error("Response column 'shop_order_id' can not be empty")
  1426. }
  1427. if params.ShopNo == "" {
  1428. t.Error("Response column 'shop_no' can not be empty")
  1429. }
  1430. if params.DeliverySign == "" {
  1431. t.Error("Response column 'delivery_sign' can not be empty")
  1432. }
  1433. if params.DeliveryID == "" {
  1434. t.Error("Response column 'delivery_id' can not be empty")
  1435. }
  1436. if params.OpenID == "" {
  1437. t.Error("Response column 'openid' can not be empty")
  1438. }
  1439. if params.SubBizID == "" {
  1440. t.Error("Response column 'sub_biz_id' can not be empty")
  1441. }
  1442. if params.Sender.Name == "" {
  1443. t.Error("Param 'sender.name' can not be empty")
  1444. }
  1445. if params.Sender.City == "" {
  1446. t.Error("Param 'sender.city' can not be empty")
  1447. }
  1448. if params.Sender.Address == "" {
  1449. t.Error("Param 'sender.address' can not be empty")
  1450. }
  1451. if params.Sender.AddressDetail == "" {
  1452. t.Error("Param 'sender.address_detail' can not be empty")
  1453. }
  1454. if params.Sender.Phone == "" {
  1455. t.Error("Param 'sender.phone' can not be empty")
  1456. }
  1457. if params.Sender.Lng == 0 {
  1458. t.Error("Param 'sender.lng' can not be empty")
  1459. }
  1460. if params.Sender.Lat == 0 {
  1461. t.Error("Param 'sender.lat' can not be empty")
  1462. }
  1463. if params.Sender.CoordinateType == 0 {
  1464. t.Error("Param 'sender.coordinate_type' can not be empty")
  1465. }
  1466. if params.Receiver.Name == "" {
  1467. t.Error("Param 'receiver.name' can not be empty")
  1468. }
  1469. if params.Receiver.City == "" {
  1470. t.Error("Param 'receiver.city' can not be empty")
  1471. }
  1472. if params.Receiver.Address == "" {
  1473. t.Error("Param 'receiver.address' can not be empty")
  1474. }
  1475. if params.Receiver.AddressDetail == "" {
  1476. t.Error("Param 'receiver.address_detail' can not be empty")
  1477. }
  1478. if params.Receiver.Phone == "" {
  1479. t.Error("Param 'receiver.phone' can not be empty")
  1480. }
  1481. if params.Receiver.Lng == 0 {
  1482. t.Error("Param 'receiver.lng' can not be empty")
  1483. }
  1484. if params.Receiver.Lat == 0 {
  1485. t.Error("Param 'receiver.lat' can not be empty")
  1486. }
  1487. if params.Receiver.CoordinateType == 0 {
  1488. t.Error("Param 'receiver.coordinate_type' can not be empty")
  1489. }
  1490. if params.Cargo.GoodsValue == 0 {
  1491. t.Error("Param 'cargo.goods_value' can not be empty")
  1492. }
  1493. if params.Cargo.GoodsHeight == 0 {
  1494. t.Error("Param 'cargo.goods_height' can not be empty")
  1495. }
  1496. if params.Cargo.GoodsLength == 0 {
  1497. t.Error("Param 'cargo.goods_length' can not be empty")
  1498. }
  1499. if params.Cargo.GoodsWidth == 0 {
  1500. t.Error("Param 'cargo.goods_width' can not be empty")
  1501. }
  1502. if params.Cargo.GoodsWeight == 0 {
  1503. t.Error("Param 'cargo.goods_weight' can not be empty")
  1504. }
  1505. if params.Cargo.CargoFirstClass == "" {
  1506. t.Error("Param 'cargo.cargo_first_class' can not be empty")
  1507. }
  1508. if params.Cargo.CargoSecondClass == "" {
  1509. t.Error("Param 'cargo.cargo_second_class' can not be empty")
  1510. }
  1511. if len(params.Cargo.GoodsDetail.Goods) > 0 {
  1512. if params.Cargo.GoodsDetail.Goods[0].Count == 0 {
  1513. t.Error("Param 'cargo.goods_detail.goods.good_count' can not be empty")
  1514. }
  1515. if params.Cargo.GoodsDetail.Goods[0].Name == "" {
  1516. t.Error("Param 'cargo.goods_detail.goods.good_name' can not be empty")
  1517. }
  1518. if params.Cargo.GoodsDetail.Goods[0].Price == 0 {
  1519. t.Error("Param 'cargo.goods_detail.goods.good_price' can not be empty")
  1520. }
  1521. if params.Cargo.GoodsDetail.Goods[0].Unit == "" {
  1522. t.Error("Param 'cargo.goods_detail.goods.good_unit' can not be empty")
  1523. }
  1524. }
  1525. if params.OrderInfo.DeliveryServiceCode == "" {
  1526. t.Error("Param 'order_info.delivery_service_code' can not be empty")
  1527. }
  1528. if params.Shop.WxaPath == "" {
  1529. t.Error("Param 'shop.wxa_path' can not be empty")
  1530. }
  1531. if params.Shop.ImgURL == "" {
  1532. t.Error("Param 'shop.img_url' can not be empty")
  1533. }
  1534. if params.Shop.GoodsName == "" {
  1535. t.Error("Param 'shop.goods_name' can not be empty")
  1536. }
  1537. if params.Shop.GoodsCount == 0 {
  1538. t.Error("Param 'shop.goods_count' can not be empty")
  1539. }
  1540. w.WriteHeader(http.StatusOK)
  1541. raw := `{
  1542. "resultcode": 1,
  1543. "resultmsg": "ok",
  1544. "fee": 11,
  1545. "deliverfee": 11,
  1546. "couponfee": 1,
  1547. "tips": 1,
  1548. "insurancefee": 1000,
  1549. "insurancfee": 1,
  1550. "distance": 1001,
  1551. "waybill_id": "123456789",
  1552. "order_status": 101,
  1553. "finish_code": 1024,
  1554. "pickup_code": 2048,
  1555. "dispatch_duration": 300
  1556. }`
  1557. if _, err := w.Write([]byte(raw)); err != nil {
  1558. t.Fatal(err)
  1559. }
  1560. }))
  1561. defer ts.Close()
  1562. raw := `{
  1563. "cargo": {
  1564. "cargo_first_class": "美食宵夜",
  1565. "cargo_second_class": "零食小吃",
  1566. "goods_detail": {
  1567. "goods": [
  1568. {
  1569. "good_count": 1,
  1570. "good_name": "水果",
  1571. "good_price": 11,
  1572. "good_unit": "元"
  1573. },
  1574. {
  1575. "good_count": 2,
  1576. "good_name": "蔬菜",
  1577. "good_price": 21,
  1578. "good_unit": "元"
  1579. }
  1580. ]
  1581. },
  1582. "goods_height": 1,
  1583. "goods_length": 3,
  1584. "goods_value": 5,
  1585. "goods_weight": 1,
  1586. "goods_width": 2
  1587. },
  1588. "delivery_id": "SFTC",
  1589. "delivery_sign": "01234567890123456789",
  1590. "openid": "oABC123456",
  1591. "order_info": {
  1592. "delivery_service_code": "xxx",
  1593. "expected_delivery_time": 1,
  1594. "is_direct_delivery": 1,
  1595. "is_finish_code_needed": 1,
  1596. "is_insured": 1,
  1597. "is_pickup_code_needed": 1,
  1598. "note": "test_note",
  1599. "order_time": 1555220757,
  1600. "order_type": 1,
  1601. "poi_seq": "1111",
  1602. "tips": 0
  1603. },
  1604. "receiver": {
  1605. "address": "xxx地铁站",
  1606. "address_detail": "2号楼202",
  1607. "city": "北京市",
  1608. "coordinate_type": 1,
  1609. "lat": 40.1529600001,
  1610. "lng": 116.5060300001,
  1611. "name": "老王",
  1612. "phone": "18512345678"
  1613. },
  1614. "sender": {
  1615. "address": "xx大厦",
  1616. "address_detail": "1号楼101",
  1617. "city": "北京市",
  1618. "coordinate_type": 1,
  1619. "lat": 40.4486120001,
  1620. "lng": 116.3830750001,
  1621. "name": "刘一",
  1622. "phone": "13712345678"
  1623. },
  1624. "shop": {
  1625. "goods_count": 2,
  1626. "goods_name": "宝贝",
  1627. "img_url": "https://mmbiz.qpic.cn/mmbiz_png/xxxxxxxxx/0?wx_fmt=png",
  1628. "wxa_path": "/page/index/index"
  1629. },
  1630. "shop_no": "12345678",
  1631. "sub_biz_id": "sub_biz_id_1",
  1632. "shop_order_id": "SFTC_001",
  1633. "shopid": "122222222",
  1634. "delivery_token": "xxxxxxxx"
  1635. }`
  1636. creator := new(DeliveryOrderCreator)
  1637. err := json.Unmarshal([]byte(raw), creator)
  1638. if err != nil {
  1639. t.Fatal(err)
  1640. }
  1641. res, err := creator.recreate(ts.URL+apiReAddDeliveryOrder, "mock-access-token")
  1642. if err != nil {
  1643. t.Fatal(err)
  1644. }
  1645. if res.Fee == 0 {
  1646. t.Error("Response 'fee' can not be empty")
  1647. }
  1648. if res.Deliverfee == 0 {
  1649. t.Error("Response 'deliverfee' can not be empty")
  1650. }
  1651. if res.Couponfee == 0 {
  1652. t.Error("Response 'couponfee' can not be empty")
  1653. }
  1654. if res.Tips == 0 {
  1655. t.Error("Response 'tips' can not be empty")
  1656. }
  1657. if res.Insurancefee == 0 {
  1658. t.Error("Response 'insurancefee' can not be empty")
  1659. }
  1660. if res.Distance == 0 {
  1661. t.Error("Response 'distance' can not be empty")
  1662. }
  1663. if res.WaybillID == "" {
  1664. t.Error("Response 'waybill_id' can not be empty")
  1665. }
  1666. if res.OrderStatus == 0 {
  1667. t.Error("Response 'order_status' can not be empty")
  1668. }
  1669. if res.FinishCode == 0 {
  1670. t.Error("Response 'finish_code' can not be empty")
  1671. }
  1672. if res.PickupCode == 0 {
  1673. t.Error("Response 'pickup_code' can not be empty")
  1674. }
  1675. if res.DispatchDuration == 0 {
  1676. t.Error("Response 'dispatch_duration' can not be empty")
  1677. }
  1678. if res.ResultCode == 0 {
  1679. t.Error("Response 'resultcode' can not be empty")
  1680. }
  1681. if res.ResultMsg == "" {
  1682. t.Error("Response 'resultmsg' can not be empty")
  1683. }
  1684. }
  1685. func TestUpdateDeliveryOrder(t *testing.T) {
  1686. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  1687. if r.Method != "POST" {
  1688. t.Error("UnExpect request method")
  1689. }
  1690. if r.URL.EscapedPath() != "/cgi-bin/express/local/delivery/update_order" {
  1691. t.Error("Unexpected path")
  1692. }
  1693. if err := r.ParseForm(); err != nil {
  1694. t.Fatal(err)
  1695. }
  1696. if r.Form.Get("access_token") == "" {
  1697. t.Fatalf("Query 'access_token' can not be empty")
  1698. }
  1699. params := struct {
  1700. WxToken string `json:"wx_token"`
  1701. ShopID string `json:"shopid"`
  1702. ShopOrderID string `json:"shop_order_id"`
  1703. ShopNo string `json:"shop_no"`
  1704. WaybillID string `json:"waybill_id"`
  1705. ActionTime uint `json:"action_time"`
  1706. OrderStatus int `json:"order_status"`
  1707. ActionMsg string `json:"action_msg"`
  1708. WxaPath string `json:"wxa_path"`
  1709. Agent struct {
  1710. Name string `json:"name"`
  1711. Phone string `json:"phone"`
  1712. IsPhoneEncrypted uint8 `json:"is_phone_encrypted"`
  1713. } `json:"agent"`
  1714. ExpectedDeliveryTime uint `json:"expected_delivery_time"`
  1715. }{}
  1716. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  1717. t.Fatal(err)
  1718. }
  1719. if params.WxToken == "" {
  1720. t.Error("Response column wx_token can not be empty")
  1721. }
  1722. if params.Agent.Name == "" {
  1723. t.Error("Response column agent.name can not be empty")
  1724. }
  1725. if params.Agent.Phone == "" {
  1726. t.Error("Response column agent.phone can not be empty")
  1727. }
  1728. if params.Agent.IsPhoneEncrypted == 0 {
  1729. t.Error("Response column agent.is_phone_encrypted can not be empty")
  1730. }
  1731. if params.ShopID == "" {
  1732. t.Error("Response column shopid can not be empty")
  1733. }
  1734. if params.ShopNo == "" {
  1735. t.Error("Response column shop_no can not be empty")
  1736. }
  1737. if params.WaybillID == "" {
  1738. t.Error("Response column waybill_id can not be empty")
  1739. }
  1740. if params.ShopOrderID == "" {
  1741. t.Error("Response column expected_delivery_time can not be empty")
  1742. }
  1743. if params.ExpectedDeliveryTime == 0 {
  1744. t.Error("Response column action_time can not be empty")
  1745. }
  1746. if params.ActionTime == 0 {
  1747. t.Error("Response column action_time can not be empty")
  1748. }
  1749. if params.OrderStatus == 0 {
  1750. t.Error("Response column order_status can not be empty")
  1751. }
  1752. if params.ActionMsg == "" {
  1753. t.Error("Response column action_msg can not be empty")
  1754. }
  1755. w.WriteHeader(http.StatusOK)
  1756. raw := `{
  1757. "resultcode": 1,
  1758. "resultmsg": "ok"
  1759. }`
  1760. if _, err := w.Write([]byte(raw)); err != nil {
  1761. t.Fatal(err)
  1762. }
  1763. }))
  1764. raw := `{
  1765. "wx_token": "xxxxxx",
  1766. "shopid": "test_shop_id",
  1767. "shop_no": "test_shop_id",
  1768. "shop_order_id": "xxxxxxxxxxx",
  1769. "waybill_id": "xxxxxxxxxxxxx",
  1770. "action_time": 12345678,
  1771. "order_status": 101,
  1772. "action_msg": "xxxxxx",
  1773. "wxa_path": "xxxxxx",
  1774. "expected_delivery_time": 123456,
  1775. "agent": {
  1776. "name": "xxxxxx",
  1777. "phone": "xxxxxx",
  1778. "is_phone_encrypted": 1
  1779. }
  1780. }`
  1781. updater := new(DeliveryOrderUpdater)
  1782. err := json.Unmarshal([]byte(raw), updater)
  1783. if err != nil {
  1784. t.Fatal(err)
  1785. }
  1786. res, err := updater.update(ts.URL+apiUpdateDeliveryOrder, "mock-access-token")
  1787. if err != nil {
  1788. t.Fatal(err)
  1789. }
  1790. if res.ResultCode == 0 {
  1791. t.Error("Response 'resultcode' can not be empty")
  1792. }
  1793. if res.ResultMsg == "" {
  1794. t.Error("Response 'resultmsg' can not be empty")
  1795. }
  1796. }
  1797. func TestOnDeliveryOrderStatusUpdate(t *testing.T) {
  1798. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  1799. aesKey := base64.StdEncoding.EncodeToString([]byte("mock-aes-key"))
  1800. srv, err := NewServer("mock-app-id", "mock-access-token", aesKey, "mock-mch-id", "mock-api-key", false)
  1801. if err != nil {
  1802. t.Fatal(err)
  1803. }
  1804. srv.OnDeliveryOrderStatusUpdate(func(mix *DeliveryOrderStatusUpdateResult) *DeliveryOrderStatusUpdateReturn {
  1805. if mix.ToUserName == "" {
  1806. t.Error("ToUserName can not be empty")
  1807. }
  1808. if mix.FromUserName == "" {
  1809. t.Error("FromUserName can not be empty")
  1810. }
  1811. if mix.CreateTime == 0 {
  1812. t.Error("CreateTime can not be zero")
  1813. }
  1814. if mix.MsgType != "event" {
  1815. t.Error("Unexpected message type")
  1816. }
  1817. if mix.Event != "update_waybill_status" {
  1818. t.Error("Unexpected message event")
  1819. }
  1820. if mix.ShopID == "" {
  1821. t.Error("Result 'shopid' can not be zero")
  1822. }
  1823. if mix.ShopOrderID == "" {
  1824. t.Error("Result 'shop_order_id' can not be zero")
  1825. }
  1826. if mix.ShopNo == "" {
  1827. t.Error("Result 'shop_no' can not be zero")
  1828. }
  1829. if mix.WaybillID == "" {
  1830. t.Error("Result 'waybill_id' can not be zero")
  1831. }
  1832. if mix.ActionTime == 0 {
  1833. t.Error("Result 'action_time' can not be zero")
  1834. }
  1835. if mix.OrderStatus == 0 {
  1836. t.Error("Result 'order_status' can not be zero")
  1837. }
  1838. if mix.ActionMsg == "" {
  1839. t.Error("Result 'action_msg' can not be zero")
  1840. }
  1841. if mix.Agent.Name == "" {
  1842. t.Error("Result 'agent.name' can not be zero")
  1843. }
  1844. if mix.Agent.Phone == "" {
  1845. t.Error("Result 'agent.phone' can not be zero")
  1846. }
  1847. return &DeliveryOrderStatusUpdateReturn{
  1848. "mock-to-user-name",
  1849. "mock-from-user-name",
  1850. 20060102150405,
  1851. "mock-message-type",
  1852. "mock-event",
  1853. 0,
  1854. "mock-result-message",
  1855. }
  1856. })
  1857. if err := srv.Serve(w, r); err != nil {
  1858. t.Fatal(err)
  1859. }
  1860. }))
  1861. defer ts.Close()
  1862. jsonData := `{
  1863. "ToUserName": "toUser",
  1864. "FromUserName": "fromUser",
  1865. "CreateTime": 1546924844,
  1866. "MsgType": "event",
  1867. "Event": "update_waybill_status",
  1868. "shopid": "123456",
  1869. "shop_order_id": "123456",
  1870. "waybill_id": "123456",
  1871. "action_time": 1546924844,
  1872. "order_status": 102,
  1873. "action_msg": "xxx",
  1874. "shop_no": "123456",
  1875. "agent": {
  1876. "name": "xxx",
  1877. "phone": "1234567"
  1878. }
  1879. }`
  1880. res, err := http.Post(ts.URL, "application/json", strings.NewReader(jsonData))
  1881. if err != nil {
  1882. t.Fatal(err)
  1883. }
  1884. defer res.Body.Close()
  1885. }