|
@@ -8,10 +8,13 @@
|
|
# @File : CronTaskController.py
|
|
# @File : CronTaskController.py
|
|
# @Software: PyCharm
|
|
# @Software: PyCharm
|
|
import datetime
|
|
import datetime
|
|
|
|
+import io
|
|
import threading
|
|
import threading
|
|
import time
|
|
import time
|
|
|
|
+import zipfile
|
|
|
|
|
|
import requests
|
|
import requests
|
|
|
|
+import csv
|
|
from django.db import connection, connections, transaction
|
|
from django.db import connection, connections, transaction
|
|
from django.db.models import Q, Sum, Count
|
|
from django.db.models import Q, Sum, Count
|
|
from django.views import View
|
|
from django.views import View
|
|
@@ -30,6 +33,8 @@ from Object.utils.PayPalUtil import PayPalService
|
|
from Service.CommonService import CommonService
|
|
from Service.CommonService import CommonService
|
|
from Service.VodHlsService import SplitVodHlsObject
|
|
from Service.VodHlsService import SplitVodHlsObject
|
|
from Object.UnicomObject import UnicomObjeect
|
|
from Object.UnicomObject import UnicomObjeect
|
|
|
|
+from Object.WechatPayObject import WechatPayObject
|
|
|
|
+from Object.AliPayObject import AliPayObject
|
|
|
|
|
|
|
|
|
|
class CronDelDataView(View):
|
|
class CronDelDataView(View):
|
|
@@ -1397,6 +1402,10 @@ class CronComparedDataView(View):
|
|
response = ResponseObject()
|
|
response = ResponseObject()
|
|
if operation == 'PaypalOrder': # 定时对比paypal订单
|
|
if operation == 'PaypalOrder': # 定时对比paypal订单
|
|
return self.compared_paypal_order(response)
|
|
return self.compared_paypal_order(response)
|
|
|
|
+ elif operation == 'WechatOrder': # 定时对比微信订单
|
|
|
|
+ return self.compared_wechat_order(response)
|
|
|
|
+ elif operation == 'AlipayOrder': # 定时对比阿里订单
|
|
|
|
+ return self.compared_alipay_order(response)
|
|
else:
|
|
else:
|
|
return response.json(404)
|
|
return response.json(404)
|
|
|
|
|
|
@@ -1416,7 +1425,7 @@ class CronComparedDataView(View):
|
|
('transaction_status', 'S')
|
|
('transaction_status', 'S')
|
|
)
|
|
)
|
|
order_list = PayPalService(PAYPAL_CRD['client_id'], PAYPAL_CRD['client_secret']).get_transactions(data)
|
|
order_list = PayPalService(PAYPAL_CRD['client_id'], PAYPAL_CRD['client_secret']).get_transactions(data)
|
|
- thread = threading.Thread(target=CronComparedDataView.thread_compared_order,
|
|
|
|
|
|
+ thread = threading.Thread(target=CronComparedDataView.thread_compared_pyapal_order,
|
|
args=(order_list['transaction_details'],))
|
|
args=(order_list['transaction_details'],))
|
|
thread.start()
|
|
thread.start()
|
|
return response.json(0)
|
|
return response.json(0)
|
|
@@ -1426,7 +1435,7 @@ class CronComparedDataView(View):
|
|
return response.json(500)
|
|
return response.json(500)
|
|
|
|
|
|
@staticmethod
|
|
@staticmethod
|
|
- def thread_compared_order(order_list):
|
|
|
|
|
|
+ def thread_compared_pyapal_order(order_list):
|
|
now_time = int(time.time())
|
|
now_time = int(time.time())
|
|
for item in order_list:
|
|
for item in order_list:
|
|
trade_no = item['transaction_info']['transaction_id']
|
|
trade_no = item['transaction_info']['transaction_id']
|
|
@@ -1434,7 +1443,7 @@ class CronComparedDataView(View):
|
|
trade_no = item['transaction_info']['paypal_reference_id']
|
|
trade_no = item['transaction_info']['paypal_reference_id']
|
|
order_qs = Order_Model.objects.filter(trade_no=trade_no, payType=1)
|
|
order_qs = Order_Model.objects.filter(trade_no=trade_no, payType=1)
|
|
if not order_qs.exists():
|
|
if not order_qs.exists():
|
|
- order_id = item['transaction_info'].get('transaction_subject', '')
|
|
|
|
|
|
+ transaction_subject = item['transaction_info'].get('transaction_subject', '')
|
|
agreement_id = item['transaction_info'].get('paypal_reference_id', '')
|
|
agreement_id = item['transaction_info'].get('paypal_reference_id', '')
|
|
pay_time = datetime.datetime.strptime(item['transaction_info']['transaction_initiation_date'],
|
|
pay_time = datetime.datetime.strptime(item['transaction_info']['transaction_initiation_date'],
|
|
"%Y-%m-%dT%H:%M:%S%z").timestamp()
|
|
"%Y-%m-%dT%H:%M:%S%z").timestamp()
|
|
@@ -1446,11 +1455,13 @@ class CronComparedDataView(View):
|
|
'price': item['transaction_info']['transaction_amount']['value'],
|
|
'price': item['transaction_info']['transaction_amount']['value'],
|
|
'pay_type': 1,
|
|
'pay_type': 1,
|
|
'upd_time': now_time,
|
|
'upd_time': now_time,
|
|
- 'status': 0
|
|
|
|
|
|
+ 'status': 0,
|
|
|
|
+ 'meal_name': transaction_subject
|
|
}
|
|
}
|
|
if agreement_id:
|
|
if agreement_id:
|
|
order_dict['pay_type'] = 0
|
|
order_dict['pay_type'] = 0
|
|
- order_dict['order_id'] = order_id
|
|
|
|
|
|
+ order_dict['meal_name'] = 'paypal_cycle'
|
|
|
|
+ order_dict['order_id'] = transaction_subject
|
|
params = {'trade_no': trade_no}
|
|
params = {'trade_no': trade_no}
|
|
response = requests.get('https://www.zositeche.com/testApi/checkOrderExist', params=params)
|
|
response = requests.get('https://www.zositeche.com/testApi/checkOrderExist', params=params)
|
|
if response.status_code != 200:
|
|
if response.status_code != 200:
|
|
@@ -1461,3 +1472,99 @@ class CronComparedDataView(View):
|
|
if result['result_code'] != 0 or not result['result']['is_exist']:
|
|
if result['result_code'] != 0 or not result['result']['is_exist']:
|
|
# 如果响应结果为空,记录在数据库
|
|
# 如果响应结果为空,记录在数据库
|
|
AbnormalOrder.objects.create(**order_dict)
|
|
AbnormalOrder.objects.create(**order_dict)
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def compared_wechat_order(response):
|
|
|
|
+ today = datetime.datetime.today()
|
|
|
|
+ start_date = today - datetime.timedelta(days=1)
|
|
|
|
+ start_date = start_date.strftime("%Y%m%d")
|
|
|
|
+ try:
|
|
|
|
+ order_list = WechatPayObject().download_bill(start_date)
|
|
|
|
+ thread = threading.Thread(target=CronComparedDataView.thread_compared_wechat_order,
|
|
|
|
+ args=(order_list,))
|
|
|
|
+ thread.start()
|
|
|
|
+ return response.json(0)
|
|
|
|
+ except Exception as e:
|
|
|
|
+ LOGGER.info('CronComparedDataView.compared_wechat_order, errLine:{}, errMsg:{}'.format(
|
|
|
|
+ e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
+ return response.json(500)
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def thread_compared_wechat_order(order_list):
|
|
|
|
+ now_time = int(time.time())
|
|
|
|
+ for order in order_list:
|
|
|
|
+ if order['交易类型'] != '`APP':
|
|
|
|
+ continue
|
|
|
|
+ order_id = order['商户订单号'].replace('`', '')
|
|
|
|
+ order_qs = Order_Model.objects.filter(orderID=order_id)
|
|
|
|
+ if not order_qs.exists():
|
|
|
|
+ order_dict = {
|
|
|
|
+ 'trade_no': order['微信订单号'].replace('`', ''),
|
|
|
|
+ 'order_id': order_id,
|
|
|
|
+ 'pay_type': 3,
|
|
|
|
+ 'price': order['订单金额'].replace('`', ''),
|
|
|
|
+ 'pay_time': int(datetime.datetime.strptime(order['\ufeff交易时间'], "`%Y-%m-%d %H:%M:%S").timestamp()),
|
|
|
|
+ 'upd_time': now_time,
|
|
|
|
+ 'meal_name': order['商品名称'].replace('`', ''),
|
|
|
|
+ }
|
|
|
|
+ AbnormalOrder.objects.create(**order_dict)
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def compared_alipay_order(response):
|
|
|
|
+ today = datetime.datetime.today()
|
|
|
|
+ start_date = today - datetime.timedelta(days=1)
|
|
|
|
+ start_date = start_date.strftime("%Y-%m-%d")
|
|
|
|
+ try:
|
|
|
|
+ ali_pay_obj = AliPayObject()
|
|
|
|
+ alipay = ali_pay_obj.conf()
|
|
|
|
+ result = alipay.server_api(
|
|
|
|
+ api_name='alipay.data.dataservice.bill.downloadurl.query',
|
|
|
|
+ biz_content={'bill_type': 'trade',
|
|
|
|
+ 'bill_date': start_date,
|
|
|
|
+ }
|
|
|
|
+ )
|
|
|
|
+ res = requests.get(result['bill_download_url'])
|
|
|
|
+ zip_file = res.content
|
|
|
|
+ zip_data = io.BytesIO(zip_file)
|
|
|
|
+ data = []
|
|
|
|
+ with zipfile.ZipFile(zip_data, 'r') as zip_ref:
|
|
|
|
+ for file in zip_ref.namelist():
|
|
|
|
+ if '汇总' not in file.encode('cp437').decode('gbk'):
|
|
|
|
+ with zip_ref.open(file) as f:
|
|
|
|
+ reader = csv.reader(io.TextIOWrapper(f, 'gbk'))
|
|
|
|
+ for row in reader:
|
|
|
|
+ data.append(row)
|
|
|
|
+ key_list = data[4]
|
|
|
|
+ orders = data[5:-4]
|
|
|
|
+ order_list = []
|
|
|
|
+ for item in orders:
|
|
|
|
+ order_list.append(dict(zip(key_list, item)))
|
|
|
|
+ thread = threading.Thread(target=CronComparedDataView.thread_compared_alipay_order,
|
|
|
|
+ args=(order_list,))
|
|
|
|
+ thread.start()
|
|
|
|
+ return response.json(0)
|
|
|
|
+ except Exception as e:
|
|
|
|
+ LOGGER.info('CronComparedDataView.compared_alipay_order, errLine:{}, errMsg:{}'.format(
|
|
|
|
+ e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
+ return response.json(500)
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def thread_compared_alipay_order(order_list):
|
|
|
|
+ now_time = int(time.time())
|
|
|
|
+ for order in order_list:
|
|
|
|
+ order_id = order['商户订单号'].replace('\t', '')
|
|
|
|
+ if len(order_id) != 20:
|
|
|
|
+ continue
|
|
|
|
+ order_qs = Order_Model.objects.filter(orderID=order_id)
|
|
|
|
+ if not order_qs.exists():
|
|
|
|
+ order_dict = {
|
|
|
|
+ 'trade_no': order['支付宝交易号'].replace('\t', ''),
|
|
|
|
+ 'order_id': order_id,
|
|
|
|
+ 'pay_type': 2,
|
|
|
|
+ 'price': order['订单金额(元)'].replace('\t', ''),
|
|
|
|
+ 'pay_time': int(datetime.datetime.strptime(order['完成时间'], "%Y-%m-%d %H:%M:%S").timestamp()),
|
|
|
|
+ 'upd_time': now_time,
|
|
|
|
+ 'meal_name': order['商品名称'].replace('\t', ''),
|
|
|
|
+ 'username': order['对方账户'].replace('\t', ''),
|
|
|
|
+ }
|
|
|
|
+ AbnormalOrder.objects.create(**order_dict)
|