python實(shí)現(xiàn)美團(tuán)訂單推送到測(cè)試環(huán)境,提供便利操作示例
本文實(shí)例講述了python實(shí)現(xiàn)美團(tuán)訂單推送到測(cè)試環(huán)境,提供便利操作。分享給大家供大家參考,具體如下:
背景:
有時(shí)候需要在測(cè)試環(huán)境下一個(gè)美團(tuán)的訂單,每次都找一堆的東西,太繁瑣,于是寫(xiě)了接口請(qǐng)求數(shù)據(jù),然后把數(shù)據(jù)推送到測(cè)試環(huán)境。實(shí)現(xiàn)了可以在測(cè)試環(huán)境進(jìn)行:生成新訂單、取消訂單、騎手搶單、騎手送達(dá)、申請(qǐng)整單退款、申請(qǐng)部分退款流程。
# -*- 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():#獲取當(dāng)前的時(shí)間戳
t = time.time()
return int(t)
def get_format_time():#獲取現(xiàn)在的格式化標(biāo)準(zhǔn)時(shí)間:年-月-日 時(shí):分:秒
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方法請(qǐng)求函數(shù)
req_get = requests.get(api_url,api_data)
result = req_get.json()
return result
def req_post_result(api_url,api_data):#post方法請(qǐng)求函數(shù)
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):#根據(jù)三方訂單號(hào),返回訂單詳情
api_url = 'http://waimaiopen.meituan.com/api/v1/order/getOrderDetail'
timestamp = get_tamp()#當(dāng)前時(shí)間的時(shí)間戳
api_data = {
'app_id':app_id,
'timestamp':timestamp,
'order_id':outer_order_id
}
sort_str = param_sort(api_data) #對(duì)參數(shù)進(jìn)行排序,固定格式。
params_str = api_url+'?'+sort_str+secret #參加簽名的字符串
sig = get_md5(params_str)#獲得簽名后的字符串
api_data['sig'] = sig #把簽名串加進(jìn)請(qǐng)求參數(shù)
result = req_get_result(api_url,api_data)
order_detail = result['data']
return order_detail
def push_order(outer_order_id):#向測(cè)試環(huán)境推送一個(gè)美團(tuán)訂單
order_detail = get_order_detail(outer_order_id)
timestamp = get_tamp()
api_url = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#正式環(huán)境url,參加簽名用
api_url_test = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#測(cè)試環(huán)境url,接收數(shù)據(jù)
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'], #電商門(mén)店id
'wm_poi_name':order_detail['wm_poi_name'], #美團(tuán)門(mén)店名稱
'wm_poi_address':order_detail['wm_poi_address'], #美團(tuán)門(mén)店地址
'wm_poi_phone':order_detail['wm_poi_phone'], #美團(tuán)商家電話
'recipient_address':order_detail['recipient_address'], #收件人收貨地址
'shipping_fee':order_detail['shipping_fee'], #float,門(mén)店配送費(fèi)
'total':order_detail['total'], #double,總價(jià)
'original_price':order_detail['original_price'], #double,原價(jià)
'caution':order_detail['caution'], #忌口或備注
'shipper_phone':order_detail['shipper_phone'], #送餐員電話
'status':2, #int,訂單狀態(tài)
'city_id':order_detail['city_id'], #long,城市ID(目前暫時(shí)用不到此信息)
'has_invoiced':order_detail['has_invoiced'], #int,是否開(kāi)發(fā)票,0不開(kāi),1開(kāi)
'invoice_title':order_detail['invoice_title'], #發(fā)票抬頭
'ctime':order_detail['ctime'], #long,創(chuàng)建時(shí)間
'utime':order_detail['utime'], #long,更新時(shí)間
'delivery_time':order_detail['delivery_time'], #long,用戶預(yù)計(jì)送達(dá)時(shí)間,0表示“立即送達(dá)”
'is_third_shipping':order_detail['is_third_shipping'], #int,是否第三方配送平臺(tái)配送,0表否,1表是
'pay_type':order_detail['pay_type'], #int,支付類(lèi)型,1貨到付款,2在線支付
'latitude':order_detail['latitude'], #double,實(shí)際送餐地址緯度
'longitude':order_detail['longitude'], #double,實(shí)際送餐地址經(jīng)度
'detail':order_detail['detail'], #訂單商品詳情
'extras':order_detail['extras'], #優(yōu)惠信息
'avg_send_time':order_detail['avg_send_time'], #平均送餐時(shí)間,單位為秒
'day_seq':order_detail['day_seq'], #流水號(hào)
'recipient_phone':order_detail['recipient_phone'], #收件人電話
'recipient_name':order_detail['recipient_name'], #收件人姓名
'app_id':app_id, #appid,標(biāo)識(shí)哪個(gè)商家
'timestamp':timestamp, #時(shí)間戳
}
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): #向測(cè)試環(huán)境推送美團(tuán)訂單配送狀態(tài)
timestamp = get_tamp()
api_url = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#正式環(huán)境url,參加簽名用
api_url_test = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#測(cè)試環(huán)境url,接收數(shù)據(jù)
order_data = {
'order_id':outer_order_id, #訂單號(hào)
'logistics_status':logistics_status, #10訂單確認(rèn),40騎手已送達(dá),100配送單已取消
'time':timestamp, #操作的時(shí)間
'dispatcher_name':'美團(tuán)騎手', #騎手姓名
'dispatcher_mobile':'135xxxxxxxx', #騎手電話
'app_id':app_id, #appid,標(biāo)識(shí)哪個(gè)商家
'timestamp':timestamp, #時(shí)間戳
}
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):#向測(cè)試環(huán)境推送美團(tuán)訂單整單退
timestamp = get_tamp()
t_reason = get_format_time()
api_url = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#正式環(huán)境url,參加簽名用
api_url_test = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#測(cè)試環(huán)境url,接收數(shù)據(jù)
order_data = {
'order_id':outer_order_id, #訂單號(hào)
'notify_type':'apply', #apply:發(fā)起退款
'reason':'整單退款原因%s'%t_reason, #退款原因
'app_id':app_id, #appid,標(biāo)識(shí)哪個(gè)商家
'timestamp':timestamp, #時(shí)間戳
}
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):#向測(cè)試環(huán)境推送美團(tuán)部分退訂單
timestamp = get_tamp()
t_reason = get_format_time()
api_url = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#正式環(huán)境url,參加簽名用
api_url_test = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#測(cè)試環(huán)境url,接收數(shù)據(jù)
order_detail = get_order_detail(outer_order_id)
food_first = eval(order_detail['detail'])[0] #獲取第0個(gè)商品
#組裝退貨商品信息
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'], #商品價(jià)格
'count':1, #退貨數(shù)量,
'box_num':1, #打包盒數(shù)量
'box_price':food_first['box_price'], #打包盒價(jià)格
'origin_food_price':food_first['price'], #商品原價(jià)
'refund_price':food_first['price'] #退款價(jià)格
}
temp_list = []
temp_list.append(food_dict)
food_info = str(temp_list)
#組裝接口發(fā)送數(shù)據(jù)
order_data = {
'order_id':outer_order_id, #訂單號(hào)
'notify_type':'part', #part:發(fā)起部分退款
'reason':'部分退款原因%s'%t_reason, #退款原因
'app_id':app_id, #appid,標(biāo)識(shí)哪個(gè)商家
'timestamp':timestamp, #時(shí)間戳
'food':food_info, #退款商品信息
'money':food_first['price'], #退款金額
'res_type':0 #0:未處理,5、超過(guò)24小時(shí)自動(dòng)同意
}
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):#接單前,向測(cè)試環(huán)境推送用戶發(fā)起的取消訂單
timestamp = get_tamp()
t_reason = get_format_time()
api_url = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#正式環(huán)境url,參加簽名用
api_url_test = 'http://xxx.xx.xxxxxx.com/mt/xxxxx'#測(cè)試環(huán)境url,接收數(shù)據(jù)
order_data = {
'order_id':outer_order_id, #訂單號(hào)
'reason_code':1002, #訂單取消原因code
'reason':'用戶取消原因%s'%t_reason, #用戶取消原因
'app_id':app_id, #appid,標(biāo)識(shí)哪個(gè)商家
'timestamp':timestamp, #時(shí)間戳
}
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
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門(mén)與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- python連接mongodb操作數(shù)據(jù)示例(mongodb數(shù)據(jù)庫(kù)配置類(lèi))
- Python操作MongoDB數(shù)據(jù)庫(kù)PyMongo庫(kù)使用方法
- python操作mongodb根據(jù)_id查詢數(shù)據(jù)的實(shí)現(xiàn)方法
- python實(shí)現(xiàn)爬蟲(chóng)數(shù)據(jù)存到 MongoDB
- python連接MySQL、MongoDB、Redis、memcache等數(shù)據(jù)庫(kù)的方法
- Python常見(jiàn)MongoDB數(shù)據(jù)庫(kù)操作實(shí)例總結(jié)
- Python操作mongodb數(shù)據(jù)庫(kù)進(jìn)行模糊查詢操作示例
- python+mongodb數(shù)據(jù)抓取詳細(xì)介紹
- python 接口測(cè)試response返回?cái)?shù)據(jù)對(duì)比的方法
- python實(shí)現(xiàn)比對(duì)美團(tuán)接口返回?cái)?shù)據(jù)和本地mongo數(shù)據(jù)是否一致示例
相關(guān)文章
Python編程實(shí)現(xiàn)輸入某年某月某日計(jì)算出這一天是該年第幾天的方法
這篇文章主要介紹了Python編程實(shí)現(xiàn)輸入某年某月某日計(jì)算出這一天是該年第幾天的方法,涉及Python針對(duì)日期時(shí)間的轉(zhuǎn)換與運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-04-04
Python處理字節(jié)串:struct.pack和struct.unpack使用
這篇文章主要介紹了Python處理字節(jié)串:struct.pack和struct.unpack使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
Python使用Traits庫(kù)實(shí)現(xiàn)對(duì)象屬性
Python作為一種動(dòng)態(tài)編程語(yǔ)言,它的變量沒(méi)有類(lèi)型,這種靈活性給快速開(kāi)發(fā)帶來(lái)很多便利,不過(guò)它也不是沒(méi)有缺點(diǎn),Traits庫(kù)的一個(gè)很重要的目的就是為了解決這些缺點(diǎn)所帶來(lái)的問(wèn)題,trait為Python對(duì)象的屬性增加了類(lèi)型定義的功能2023-11-11
python英語(yǔ)單詞測(cè)試小程序代碼實(shí)例
這篇文章主要介紹了python英語(yǔ)單詞測(cè)試小程序代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
Python3 導(dǎo)入上級(jí)目錄中的模塊實(shí)例
今天小編就為大家分享一篇Python3 導(dǎo)入上級(jí)目錄中的模塊實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
利用pygame完成動(dòng)畫(huà)精靈和碰撞檢測(cè)
這篇文章主要介紹了利用pygame完成動(dòng)畫(huà)精靈和碰撞檢測(cè),代碼詳細(xì),內(nèi)容豐富,對(duì)于想要學(xué)習(xí)pygame的朋友來(lái)講是一個(gè)不錯(cuò)的練習(xí),需要的朋友可以參考下2021-04-04
Python使用Scapy實(shí)現(xiàn)構(gòu)造特殊數(shù)據(jù)包
Scapy是一款Python庫(kù),可用于構(gòu)建、發(fā)送、接收和解析網(wǎng)絡(luò)數(shù)據(jù)包,這篇文章主要為大家詳細(xì)介紹了python如何使用Scapy構(gòu)造特殊數(shù)據(jù)包,有需要的可以參考下2023-11-11

