背景:
有時候需要在測試環境下一個美團的訂單,每次都找一堆的東西,太繁瑣,於是寫了接口請求數據,然後把數據推送到測試環境。實現了可以在測試環境進行:生成新訂單、取消訂單、騎手搶單、騎手送達、申請整單退款、申請部分退款流程。
# -*- coding: utf-8 -*-
import hashlib
import time
import requests
from order30 import conf
app_id = conf.app_id
secret = conf.secret
def get_md5(string):#返回字符串md5加密後的串
hl = hashlib.md5()
hl.update(string.encode('utf-8'))
return hl.hexdigest()
def get_tamp():#獲取當前的時間戳
t = time.time()
return int(t)
def get_format_time():#獲取現在的格式化標準時間:年-月-日 時:分:秒
time_now = int(time.time())
timestr = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time_now))
return timestr
def req_get_result(api_url,api_data):#get方法請求函數
req_get = requests.get(api_url,api_data)
result = req_get.json()
return result
def req_post_result(api_url,api_data):#post方法請求函數
req_post = requests.post(api_url,data=api_data)
result = req_post.json()
return result
def param_sort(param_dict):#傳入字典,返回排序後並且連接好的字符串
keys_list = sorted(param_dict.keys())
rb_str = ''
for k in keys_list:
key_value = k + '=' + str(param_dict[k])
rb_str = rb_str + key_value +'&'
rb_str = rb_str[0:-1] #不保留字符串末尾的&
return rb_str
def get_order_detail(outer_order_id):#根據三方訂單號,返回訂單詳情
api_url = 'http://waimaiopen.meituan.com/api/v1/order/getOrderDetail'
timestamp = get_tamp()#當前時間的時間戳
api_data = {
'app_id':app_id,
'timestamp':timestamp,
'order_id':outer_order_id
}
sort_str = param_sort(api_data) #對參數進行排序,固定格式。
params_str = api_url+'?'+sort_str+secret #參加簽名的字符串
sig = get_md5(params_str)#獲得簽名後的字符串
api_data['sig'] = sig #把簽名串加進請求參數
result = req_get_result(api_url,api_data)
order_detail = result['data']
return order_detail
def push_order(outer_order_id):#向測試環境推送一個美團訂單
order_detail = get_order_detail(outer_order_id)
timestamp = get_tamp()
api_url = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#正式環境url,參加簽名用
api_url_test = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#測試環境url,接收數據
order_data = {
'order_id':order_detail['order_id'], #int,訂單id
'wm_order_id_view':order_detail['wm_order_id_view'], #int,訂單展示id
'app_poi_code':order_detail['app_poi_code'], #電商門店id
'wm_poi_name':order_detail['wm_poi_name'], #美團門店名稱
'wm_poi_address':order_detail['wm_poi_address'], #美團門店地址
'wm_poi_phone':order_detail['wm_poi_phone'], #美團商家電話
'recipient_address':order_detail['recipient_address'], #收件人收貨地址
'shipping_fee':order_detail['shipping_fee'], #float,門店配送費
'total':order_detail['total'], #double,總價
'original_price':order_detail['original_price'], #double,原價
'caution':order_detail['caution'], #忌口或備註
'shipper_phone':order_detail['shipper_phone'], #送餐員電話
'status':2, #int,訂單狀態
'city_id':order_detail['city_id'], #long,城市ID(目前暫時用不到此信息)
'has_invoiced':order_detail['has_invoiced'], #int,是否開發票,0不開,1開
'invoice_title':order_detail['invoice_title'], #發票擡頭
'ctime':order_detail['ctime'], #long,創建時間
'utime':order_detail['utime'], #long,更新時間
'delivery_time':order_detail['delivery_time'], #long,用戶預計送達時間,0表示“立即送達”
'is_third_shipping':order_detail['is_third_shipping'], #int,是否第三方配送平臺配送,0表否,1表是
'pay_type':order_detail['pay_type'], #int,支付類型,1貨到付款,2在線支付
'latitude':order_detail['latitude'], #double,實際送餐地址緯度
'longitude':order_detail['longitude'], #double,實際送餐地址經度
'detail':order_detail['detail'], #訂單商品詳情
'extras':order_detail['extras'], #優惠信息
'avg_send_time':order_detail['avg_send_time'], #平均送餐時間,單位爲秒
'day_seq':order_detail['day_seq'], #流水號
'recipient_phone':order_detail['recipient_phone'], #收件人電話
'recipient_name':order_detail['recipient_name'], #收件人姓名
'app_id':app_id, #appid,標識哪個商家
'timestamp':timestamp, #時間戳
}
sort_str = param_sort(order_data)
params_str = api_url + '?' + sort_str + secret #參加簽名的字符串
sig = get_md5(params_str) #簽名後的字符串
order_data['sig'] = sig
result = req_post_result(api_url_test,order_data)
return result
def shipping_order(outer_order_id,logistics_status): #向測試環境推送美團訂單配送狀態
timestamp = get_tamp()
api_url = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#正式環境url,參加簽名用
api_url_test = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#測試環境url,接收數據
order_data = {
'order_id':outer_order_id, #訂單號
'logistics_status':logistics_status, #10訂單確認,40騎手已送達,100配送單已取消
'time':timestamp, #操作的時間
'dispatcher_name':'美團騎手', #騎手姓名
'dispatcher_mobile':'135xxxxxxxx', #騎手電話
'app_id':app_id, #appid,標識哪個商家
'timestamp':timestamp, #時間戳
}
sort_str = param_sort(order_data)
params_str = api_url + '?' + sort_str + secret #參加簽名的字符串
sig = get_md5(params_str) #簽名後的字符串
order_data['sig'] = sig
result = req_post_result(api_url_test,order_data)
return result
def refund_order(outer_order_id):#向測試環境推送美團訂單整單退
timestamp = get_tamp()
t_reason = get_format_time()
api_url = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#正式環境url,參加簽名用
api_url_test = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#測試環境url,接收數據
order_data = {
'order_id':outer_order_id, #訂單號
'notify_type':'apply', #apply:發起退款
'reason':'整單退款原因%s'%t_reason, #退款原因
'app_id':app_id, #appid,標識哪個商家
'timestamp':timestamp, #時間戳
}
sort_str = param_sort(order_data)
params_str = api_url + '?' + sort_str + secret #參加簽名的字符串
sig = get_md5(params_str) #簽名後的字符串
order_data['sig'] = sig
result = req_get_result(api_url_test,order_data)
return result
def refund_order_part(outer_order_id):#向測試環境推送美團部分退訂單
timestamp = get_tamp()
t_reason = get_format_time()
api_url = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#正式環境url,參加簽名用
api_url_test = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#測試環境url,接收數據
order_detail = get_order_detail(outer_order_id)
food_first = eval(order_detail['detail'])[0] #獲取第0個商品
#組裝退貨商品信息
food_dict = {
'app_food_code':food_first['app_food_code'], #商品id,即電商商品編碼
'food_name':food_first['food_name'], #商品名稱
'sku_id':food_first['sku_id'], #商品的skuid
'spec':food_first['spec'], #單位
'food_price':food_first['price'], #商品價格
'count':1, #退貨數量,
'box_num':1, #打包盒數量
'box_price':food_first['box_price'], #打包盒價格
'origin_food_price':food_first['price'], #商品原價
'refund_price':food_first['price'] #退款價格
}
temp_list = []
temp_list.append(food_dict)
food_info = str(temp_list)
#組裝接口發送數據
order_data = {
'order_id':outer_order_id, #訂單號
'notify_type':'part', #part:發起部分退款
'reason':'部分退款原因%s'%t_reason, #退款原因
'app_id':app_id, #appid,標識哪個商家
'timestamp':timestamp, #時間戳
'food':food_info, #退款商品信息
'money':food_first['price'], #退款金額
'res_type':0 #0:未處理,5、超過24小時自動同意
}
sort_str = param_sort(order_data)
params_str = api_url + '?' + sort_str + secret #參加簽名的字符串
sig = get_md5(params_str) #簽名後的字符串
order_data['sig'] = sig
result = req_get_result(api_url_test,order_data)
return result
def cancel_order(outer_order_id):#接單前,向測試環境推送用戶發起的取消訂單
timestamp = get_tamp()
t_reason = get_format_time()
api_url = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#正式環境url,參加簽名用
api_url_test = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#測試環境url,接收數據
order_data = {
'order_id':outer_order_id, #訂單號
'reason_code':1002, #訂單取消原因code
'reason':'用戶取消原因%s'%t_reason, #用戶取消原因
'app_id':app_id, #appid,標識哪個商家
'timestamp':timestamp, #時間戳
}
sort_str = param_sort(order_data)
params_str = api_url + '?' + sort_str + secret #參加簽名的字符串
sig = get_md5(params_str) #簽名後的字符串
order_data['sig'] = sig
result = req_get_result(api_url_test,order_data)
return result