溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python實現(xiàn)美團訂單推送到測試環(huán)境,提供便利操作示例

發(fā)布時間:2020-09-20 13:09:41 來源:腳本之家 閱讀:316 作者:zhizunyu2009 欄目:開發(fā)技術(shù)

本文實例講述了python實現(xiàn)美團訂單推送到測試環(huán)境,提供便利操作。分享給大家供大家參考,具體如下:

背景:

有時候需要在測試環(huán)境下一個美團的訂單,每次都找一堆的東西,太繁瑣,于是寫了接口請求數(shù)據(jù),然后把數(shù)據(jù)推送到測試環(huán)境。實現(xiàn)了可以在測試環(huán)境進行:生成新訂單、取消訂單、騎手搶單、騎手送達、申請整單退款、申請部分退款流程。

# -*- 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():#獲取現(xiàn)在的格式化標準時間:年-月-日 時:分:秒
  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方法請求函數(shù)
  req_get = requests.get(api_url,api_data)
  result = req_get.json()
  return result
def req_post_result(api_url,api_data):#post方法請求函數(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ù)三方訂單號,返回訂單詳情
  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) #對參數(shù)進行排序,固定格式。
  params_str = api_url+'?'+sort_str+secret #參加簽名的字符串
  sig = get_md5(params_str)#獲得簽名后的字符串
  api_data['sig'] = sig  #把簽名串加進請求參數(shù)
  result = req_get_result(api_url,api_data)
  order_detail = result['data']
  return order_detail
def push_order(outer_order_id):#向測試環(huá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'#測試環(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'],        #電商門店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,訂單狀態(tài)
    'city_id':order_detail['city_id'],             #long,城市ID(目前暫時用不到此信息)
    'has_invoiced':order_detail['has_invoiced'],        #int,是否開發(fā)票,0不開,1開
    'invoice_title':order_detail['invoice_title'],       #發(fā)票抬頭
    'ctime':order_detail['ctime'],               #long,創(chuàng)建時間
    '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,實際送餐地址經(jīng)度
    'detail':order_detail['detail'],              #訂單商品詳情
    'extras':order_detail['extras'],              #優(yōu)惠信息
    '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):        #向測試環(huá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'#測試環(huán)境url,接收數(shù)據(jù)
  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):#向測試環(huá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'#測試環(huán)境url,接收數(shù)據(jù)
  order_data = {
    'order_id':outer_order_id,      #訂單號
    'notify_type':'apply',        #apply:發(fā)起退款
    '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):#向測試環(huá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'#測試環(huán)境url,接收數(shù)據(jù)
  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,                   #退貨數(shù)量,
  'box_num':1,                  #打包盒數(shù)量
  '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)
  #組裝接口發(fā)送數(shù)據(jù)
  order_data = {
    'order_id':outer_order_id,         #訂單號
    'notify_type':'part',           #part:發(fā)起部分退款
    '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):#接單前,向測試環(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'#測試環(huán)境url,接收數(shù)據(jù)
  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

更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》

希望本文所述對大家Python程序設計有所幫助。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI