|
@@ -1,156 +1,608 @@
|
|
|
-#!/usr/bin/env python3
|
|
|
-# -*- coding: utf-8 -*-
|
|
|
+#!/usr/bin/env python3
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
"""
|
|
|
@Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
|
|
|
@AUTHOR: ASJRD018
|
|
|
@NAME: AnsjerFormal
|
|
|
@software: PyCharm
|
|
|
-@DATE: 2020/4/7 10:29
|
|
|
+@DATE: 2018/12/5 9:30
|
|
|
@Version: python3.6
|
|
|
@MODIFY DECORD:ansjer dev
|
|
|
-@file: CloudStroage.py
|
|
|
+@file: CloudVod.py
|
|
|
@Contact: chanjunkai@163.com
|
|
|
"""
|
|
|
+import json
|
|
|
+import math
|
|
|
+import time
|
|
|
+import urllib
|
|
|
+from Object.AliPayObject import AliPayObject
|
|
|
+import oss2
|
|
|
+import paypalrestsdk
|
|
|
+import boto3
|
|
|
+from aliyunsdkcore import client
|
|
|
+from aliyunsdksts.request.v20150401 import AssumeRoleRequest
|
|
|
+from django.http import JsonResponse, HttpResponseRedirect, HttpResponse
|
|
|
+from django.utils.decorators import method_decorator
|
|
|
+from django.views.decorators.csrf import csrf_exempt
|
|
|
+from django.views.generic.base import View
|
|
|
+
|
|
|
+from Ansjer.config import OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET, OSS_ROLE_ARN, SERVER_DOMAIN, PAYPAL_CRD, \
|
|
|
+ SERVER_DOMAIN_SSL
|
|
|
+from Model.models import Device_Info, Order_Model, Store_Meal, VodHlsModel, OssCrdModel, UID_Bucket, StsCrdModel
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.TokenObject import TokenObject
|
|
|
-from Model.models import Store_Meal, Device_Info,UID_Bucket
|
|
|
+from Object.UidTokenObject import UidTokenObject
|
|
|
from Service.CommonService import CommonService
|
|
|
-import time
|
|
|
+from Service.ModelService import ModelService
|
|
|
+from Object.ETkObject import ETkObject
|
|
|
+
|
|
|
+
|
|
|
+# 设备信息添加
|
|
|
+class CloudStorageView(View):
|
|
|
+
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ operation = kwargs.get('operation')
|
|
|
+ return self.validation(request.GET, request, operation)
|
|
|
+
|
|
|
+ def post(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ operation = kwargs.get('operation')
|
|
|
+ return self.validation(request.POST, request, operation)
|
|
|
+
|
|
|
+ def validation(self, request_dict, request, operation):
|
|
|
+ response = ResponseObject()
|
|
|
+ if operation is None:
|
|
|
+ return response.json(444, 'error path')
|
|
|
+ elif operation == 'dopaypalcallback':
|
|
|
+ return self.do_pay_by_paypal_callback(request_dict, response)
|
|
|
+ elif operation == 'doalicallback':
|
|
|
+ return self.do_pay_by_ali_callback(request)
|
|
|
+ elif operation == 'getsignsts':
|
|
|
+ ip = CommonService.get_ip_address(request)
|
|
|
+ return self.do_get_sign_sts(request_dict, ip, response)
|
|
|
+ elif operation == 'storeplaylist':
|
|
|
+ return self.do_store_playlist(request_dict, response)
|
|
|
+ else:
|
|
|
+ token = request_dict.get('token', None)
|
|
|
+ # 设备主键uid
|
|
|
+ tko = TokenObject(token)
|
|
|
+ response.lang = tko.lang
|
|
|
+ if tko.code != 0:
|
|
|
+ return response.json(tko.code)
|
|
|
+ userID = tko.userID
|
|
|
+ if operation == 'createPayOrder':
|
|
|
+ return self.do_create_pay_order(request_dict, userID, response)
|
|
|
+ elif operation == 'changevodstatus':
|
|
|
+ return self.do_change_vod_status(request_dict, userID, response)
|
|
|
+ else:
|
|
|
+ return response.json(414)
|
|
|
+
|
|
|
+ def do_get_sign_sts(self, request_dict, ip, response):
|
|
|
+ uidToken = request_dict.get('uidToken', None)
|
|
|
+ utko = UidTokenObject(uidToken)
|
|
|
+ if utko.flag is False:
|
|
|
+ return response.json(444, 'uidToken')
|
|
|
+ uid = utko.UID
|
|
|
+ channel = utko.channel
|
|
|
+ print(channel)
|
|
|
+ print(uid)
|
|
|
+ now_time = int(time.time())
|
|
|
+ ubqs = UID_Bucket.objects.filter(uid=uid, channel=channel, endTime__gte=now_time). \
|
|
|
+ values("bucket__mold", "bucket__bucket", "bucket__endpoint", "bucket__region")
|
|
|
+ if ubqs.exists():
|
|
|
+ if ubqs[0]["bucket__mold"] == 0:
|
|
|
+ # 阿里云 oss sts
|
|
|
+ oc_qs = OssCrdModel.objects.filter(uid=uid, channel=channel). \
|
|
|
+ values("addTime", "data")
|
|
|
+ if oc_qs.exists():
|
|
|
+ endTime = int(oc_qs[0]["addTime"]) + 3500
|
|
|
+ if endTime > now_time:
|
|
|
+ print(endTime)
|
|
|
+ print(now_time)
|
|
|
+ res = json.loads(oc_qs[0]["data"])
|
|
|
+ return JsonResponse(status=200, data=res)
|
|
|
+ # 套餐id
|
|
|
+ storage = '{uid}/vod{channel}/'.format(uid=uid, channel=channel)
|
|
|
+ bucket_name = ubqs[0]['bucket__bucket']
|
|
|
+ endpoint = ubqs[0]['bucket__endpoint']
|
|
|
+ access_key_id = OSS_STS_ACCESS_KEY
|
|
|
+ access_key_secret = OSS_STS_ACCESS_SECRET
|
|
|
+ region_id = ubqs[0]['bucket__region']
|
|
|
+ role_arn = OSS_ROLE_ARN
|
|
|
+ clt = client.AcsClient(access_key_id, access_key_secret, region_id)
|
|
|
+ req = AssumeRoleRequest.AssumeRoleRequest()
|
|
|
+ # 设置返回值格式为JSON。
|
|
|
+ req.set_accept_format('json')
|
|
|
+ req.set_RoleArn(role_arn)
|
|
|
+ req.set_RoleSessionName(uid + '_' + channel)
|
|
|
+ req.set_DurationSeconds(3600)
|
|
|
+ Resource_access = "acs:oss:*:*:{bucket_name}/{uid_channel}*". \
|
|
|
+ format(bucket_name=bucket_name, uid_channel=storage)
|
|
|
+ print(Resource_access)
|
|
|
+ policys = {
|
|
|
+ "Version": "1",
|
|
|
+ "Statement": [
|
|
|
+ {
|
|
|
+ "Action": ["oss:PutObject", "oss:DeleteObject", ],
|
|
|
+ "Resource": [Resource_access],
|
|
|
+ "Effect": "Allow",
|
|
|
+ "Condition": {
|
|
|
+ "IpAddress": {"acs:SourceIp": ip}
|
|
|
+ # "IpAddress": {"acs:SourceIp": "120.237.157.184"}
|
|
|
+ # "IpAddress": {"acs:SourceIp": "*"}
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ req.set_Policy(Policy=json.dumps(policys))
|
|
|
+ body = clt.do_action(req)
|
|
|
+ # 使用RAM账号的AccessKeyId和AccessKeySecret向STS申请临时token。
|
|
|
+ token = json.loads(body.decode('utf-8'))
|
|
|
+ print(token)
|
|
|
+ res = {
|
|
|
+ 'AccessKeyId': token['Credentials']['AccessKeyId'],
|
|
|
+ 'AccessKeySecret': token['Credentials']['AccessKeySecret'],
|
|
|
+ 'SecurityToken': token['Credentials']['SecurityToken'],
|
|
|
+ 'Expiration': token['Credentials']['Expiration'],
|
|
|
+ 'expire': '3600',
|
|
|
+ 'endpoint': endpoint,
|
|
|
+ 'bucket_name': bucket_name,
|
|
|
+ 'arn': token['AssumedRoleUser']['Arn'],
|
|
|
+ 'code': 0,
|
|
|
+ 'storage': storage,
|
|
|
+ 'endTime': ubqs[0]['endTime'],
|
|
|
+ 'ip': ip}
|
|
|
+ if oc_qs.exists():
|
|
|
+ oc_qs.update(data=json.dumps(res), addTime=now_time)
|
|
|
+ else:
|
|
|
+ OssCrdModel.objects.create \
|
|
|
+ (uid=uid, channel=channel, data=json.dumps(res), addTime=now_time)
|
|
|
+ return JsonResponse(status=200, data=res)
|
|
|
+ elif ubqs[0]["bucket__mold"] == 1:
|
|
|
+ # 亚马逊 s3 sts
|
|
|
+ sts_qs = StsCrdModel.objects.filter(uid=uid, channel=channel). \
|
|
|
+ values("addTime", "data")
|
|
|
+ if sts_qs.exists():
|
|
|
+ endTime = int(sts_qs[0]["addTime"]) + 3500
|
|
|
+ if endTime > now_time:
|
|
|
+ print(endTime)
|
|
|
+ print(now_time)
|
|
|
+ res = json.loads(sts_qs[0]["data"])
|
|
|
+ return JsonResponse(status=200, data=res)
|
|
|
+ # 套餐id
|
|
|
+ storage = '{uid}/vod{channel}/'.format(uid=uid, channel=channel)
|
|
|
+ bucket_name = ubqs[0]['bucket__bucket']
|
|
|
+ endpoint = ubqs[0]['bucket__endpoint']
|
|
|
+ region_id = ubqs[0]['bucket__region']
|
|
|
+
|
|
|
+ ###############
|
|
|
+ REGION_NAME = region_id
|
|
|
+ boto3_sts = boto3.client(
|
|
|
+ 'sts',
|
|
|
+ aws_access_key_id='AKIA2E67UIMD45Y3HL53',
|
|
|
+ aws_secret_access_key='ckYLg4Lo9ZXJIcJEAKkzf2rWvs8Xth1FCjqiAqUw',
|
|
|
+ region_name=REGION_NAME
|
|
|
+ )
|
|
|
+ Policy = {
|
|
|
+ "Version": "2012-10-17",
|
|
|
+ "Statement": [
|
|
|
+ {
|
|
|
+ "Effect": "Allow",
|
|
|
+ "Action": "s3:*",
|
|
|
+ "Resource": ["arn:aws:s3:::azvod1/{uid_channel}/*".
|
|
|
+ format(uid_channel=storage)]
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ response = boto3_sts.get_federation_token(
|
|
|
+ Name='{role_name}'.format(role_name=uid + '_' + channel),
|
|
|
+ Policy=json.dumps(Policy),
|
|
|
+ DurationSeconds=7200
|
|
|
+ )
|
|
|
+ ###############
|
|
|
+ res = {
|
|
|
+ 'AccessKeyId': response['Credentials']['AccessKeyId'],
|
|
|
+ 'AccessKeySecret': response['Credentials']['SecretAccessKey'],
|
|
|
+ 'SessionToken': response['Credentials']['SessionToken'],
|
|
|
+ 'Expiration': response['Credentials']['Expiration'],
|
|
|
+ 'expire': '3600',
|
|
|
+ 'endpoint': endpoint,
|
|
|
+ 'bucket_name': bucket_name,
|
|
|
+ 'arn': response['FederatedUser']['Arn'],
|
|
|
+ 'code': 0,
|
|
|
+ 'storage': storage,
|
|
|
+ 'endTime': ubqs[0]['endTime'],
|
|
|
+ 'ip': ip,
|
|
|
+ }
|
|
|
+ if sts_qs.exists():
|
|
|
+ sts_qs.update(data=json.dumps(res), addTime=now_time)
|
|
|
+ else:
|
|
|
+ StsCrdModel.objects.create(uid=uid, channel=channel, data=json.dumps(res), addTime=now_time)
|
|
|
+ return JsonResponse(status=200, data=res)
|
|
|
+ else:
|
|
|
+ res = {'code': 404, 'msg': 'data not exists!'}
|
|
|
+ return HttpResponse(json.dumps(res, ensure_ascii=False),
|
|
|
+ content_type="application/json,charset=utf-8")
|
|
|
+
|
|
|
+ def do_store_playlist(self, request_dict, response):
|
|
|
+ return
|
|
|
+
|
|
|
+ def do_change_vod_status(self, request_dict, userID, response):
|
|
|
+ uid = request_dict.get('uid', None)
|
|
|
+ status = request_dict.get('status', None)
|
|
|
+ channel = request_dict.get('channel', None)
|
|
|
+ if not all([uid, status, channel]):
|
|
|
+ dv_qs = Device_Info.objects.filter(UID=uid, userID_id=userID, isShare=False, isExist=1)
|
|
|
+ if dv_qs.exists():
|
|
|
+ now_time = int(time.time())
|
|
|
+ try:
|
|
|
+ ubqs = UID_Bucket.objects.filter(uid=uid, channel=channel, endTime__gte=now_time).first()
|
|
|
+ except Exception as e:
|
|
|
+ print(repr(e))
|
|
|
+ return response.json(10, '未购买')
|
|
|
+ else:
|
|
|
+ ubqs.update(status=status)
|
|
|
+ if status == 0:
|
|
|
+ return response.json(0)
|
|
|
+ else:
|
|
|
+ utko = UidTokenObject()
|
|
|
+ utko.generate(data={'uid': uid, 'channel': channel})
|
|
|
+ uidTK = utko.toke
|
|
|
+ uidTkUrl = "{SERVER_DOMAIN}cloudstorage/getsignsts?uidToken={uidToken}". \
|
|
|
+ format(uidToken=uidTK, SERVER_DOMAIN=SERVER_DOMAIN)
|
|
|
+ storeHlsUrl = "{SERVER_DOMAIN}cloudstorage/storeplaylist?uidToken={uidToken}". \
|
|
|
+ format(uidToken=uidTK, SERVER_DOMAIN=SERVER_DOMAIN)
|
|
|
+ return response.json(0, {'uidTkUrl': uidTkUrl, 'storeHlsUrl': storeHlsUrl})
|
|
|
+ else:
|
|
|
+ return response.json(12)
|
|
|
+
|
|
|
+ else:
|
|
|
+ return response.json(444, 'uid,status,channel')
|
|
|
+
|
|
|
+ def do_pay_error(self):
|
|
|
+ response = HttpResponse()
|
|
|
+ response.content = '''
|
|
|
+<!DOCTYPE html>
|
|
|
+<html>
|
|
|
+<head>
|
|
|
+ <!--浏览器不缓存-->
|
|
|
+ <meta http-equiv="Pragma" content="no-cache">
|
|
|
+ <meta http-equiv="Cache-Control" content="no-cache">
|
|
|
+ <meta http-equiv="Expires" content="0">
|
|
|
+ <!--utf-8-->
|
|
|
+ <meta http-equiv="content-type" content="text/html;charset=utf-8">
|
|
|
+ <!-- viewport的<meta>标签,这个标签可以修改在大部分的移动设备上面的显示,为了确保适当的绘制和触屏缩放。-->
|
|
|
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
+ <link rel="shortcut icon" href="https://test.dvema.com/web/images/favicon.ico" type="image/x-icon" charset="utf-8"/>
|
|
|
+ <title>Trading particulars</title>
|
|
|
+ <style>
|
|
|
+ .title_head{
|
|
|
+ height: 50px;
|
|
|
+ border-radius: 5px;
|
|
|
+ background-color: #c3c6c7;
|
|
|
+ text-align: center;
|
|
|
+ line-height: 50px;
|
|
|
+ }
|
|
|
+ .content{
|
|
|
+ text-align: center;
|
|
|
+ margin-top: 50px;
|
|
|
+ font-size: 20px;
|
|
|
+ color : #ec7648
|
|
|
+ }
|
|
|
+ .content_img{
|
|
|
+ width: 60px;
|
|
|
+ height: 60px;
|
|
|
+ }
|
|
|
+ .bottom{
|
|
|
+ margin-bottom: 10px;
|
|
|
+ margin-top: 250px;
|
|
|
+ color : #ec7648
|
|
|
+ }
|
|
|
+ .bottom_div{
|
|
|
+ border: 1px solid #ec7648;
|
|
|
+ line-height: 38px;
|
|
|
+ text-align: center;
|
|
|
+ width: 100px;
|
|
|
+ height: 38px;
|
|
|
+ border-radius: 5px;
|
|
|
+ }
|
|
|
+
|
|
|
+ .bottom_div:hover{
|
|
|
+ background-color: #dde4e2;
|
|
|
+ }
|
|
|
+ </style>
|
|
|
+</head>
|
|
|
+<body>
|
|
|
+ <div class="title_head">Trading particulars</div>
|
|
|
+ <div class="content">
|
|
|
+ <p >
|
|
|
+ <img src="https://test.dvema.com/web/images/failed.jpg" class="content_img">
|
|
|
+ <br />
|
|
|
+ Payment failure
|
|
|
+ </p>
|
|
|
+ </div>
|
|
|
+ <center class="bottom">
|
|
|
+ <div class="bottom_div" onclick="payOKButton()">
|
|
|
+ Finish
|
|
|
+ </div>
|
|
|
+ </center>
|
|
|
+ <script> // 点击付款成功按钮
|
|
|
+ function payOKButton() {
|
|
|
+ // 复杂数据
|
|
|
+ console.log('success')
|
|
|
+ window.webkit.messageHandlers.jsCallOC.postMessage({"status": 0});
|
|
|
+ }
|
|
|
+ </script>
|
|
|
+</body>
|
|
|
+</html>
|
|
|
+ '''
|
|
|
+ return response
|
|
|
+
|
|
|
+ def do_pay_ok(self):
|
|
|
+ response = HttpResponse()
|
|
|
+ response.content = '''
|
|
|
+
|
|
|
+<!DOCTYPE html>
|
|
|
+<html>
|
|
|
+<head>
|
|
|
+ <!--浏览器不缓存-->
|
|
|
+ <meta http-equiv="Pragma" content="no-cache">
|
|
|
+ <meta http-equiv="Cache-Control" content="no-cache">
|
|
|
+ <meta http-equiv="Expires" content="0">
|
|
|
+ <!--utf-8-->
|
|
|
+ <meta http-equiv="content-type" content="text/html;charset=utf-8">
|
|
|
+ <!-- viewport的<meta>标签,这个标签可以修改在大部分的移动设备上面的显示,为了确保适当的绘制和触屏缩放。-->
|
|
|
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
+ <link rel="shortcut icon" href="https://test.dvema.com/web/images/favicon.ico" type="image/x-icon" charset="utf-8"/>
|
|
|
+ <title>Trading particulars</title>
|
|
|
+ <style>
|
|
|
+ .title_head{
|
|
|
+ height: 50px;
|
|
|
+ border-radius: 5px;
|
|
|
+ background-color: #c3c6c7;
|
|
|
+ text-align: center;
|
|
|
+ line-height: 50px;
|
|
|
+ }
|
|
|
+ .content{
|
|
|
+ text-align: center;
|
|
|
+ margin-top: 50px;
|
|
|
+ font-size: 20px;
|
|
|
+ color : green
|
|
|
+ }
|
|
|
+ .content_img{
|
|
|
+ width: 60px;
|
|
|
+ height: 60px;
|
|
|
+ }
|
|
|
+ .bottom{
|
|
|
+ margin-bottom: 10px;
|
|
|
+ margin-top: 250px;
|
|
|
+ color : green
|
|
|
+ }
|
|
|
+ .bottom_div{
|
|
|
+ border: 1px solid green;
|
|
|
+ line-height: 38px;
|
|
|
+ text-align: center;
|
|
|
+ width: 100px;
|
|
|
+ height: 38px;
|
|
|
+ border-radius: 5px;
|
|
|
+ }
|
|
|
+
|
|
|
+ .bottom_div:hover{
|
|
|
+ background-color: #dde4e2;
|
|
|
+ }
|
|
|
+ </style>
|
|
|
+</head>
|
|
|
+<body>
|
|
|
+ <div class="title_head">Trading particulars</div>
|
|
|
+ <div class="content">
|
|
|
+ <p >
|
|
|
+ <img src="https://test.dvema.com/web/images/timg.jpg" class="content_img">
|
|
|
+ <br />
|
|
|
+ Successful payment
|
|
|
+ </p>
|
|
|
+ </div>
|
|
|
+ <center class="bottom">
|
|
|
+ <div class="bottom_div" onclick="payOKButton()">
|
|
|
+ Finish
|
|
|
+ </div>
|
|
|
+ </center>
|
|
|
+ <script> // 点击付款成功按钮
|
|
|
+ function payOKButton() {
|
|
|
+ // 复杂数据
|
|
|
+ console.log('success')
|
|
|
+ window.webkit.messageHandlers.jsCallOC.postMessage({"status": 1});
|
|
|
+ }
|
|
|
+ </script>
|
|
|
+</body>
|
|
|
+</html>
|
|
|
+ '''
|
|
|
+ return response
|
|
|
+
|
|
|
+ def do_pay_by_ali_callback(self, request):
|
|
|
+ response = ResponseObject()
|
|
|
+ data = request.POST.dict()
|
|
|
+ signature = data["sign"]
|
|
|
+ data.pop('sign')
|
|
|
+ orderID = data['out_trade_no']
|
|
|
+
|
|
|
+ order_qs = Order_Model.objects.filter(orderID=orderID, status=0)
|
|
|
+ print(order_qs)
|
|
|
+
|
|
|
+ if not order_qs.exists():
|
|
|
+ return response.json(404)
|
|
|
+ print(json.dumps(data))
|
|
|
+ print(signature)
|
|
|
+ # verify
|
|
|
+ aliPayObj = AliPayObject()
|
|
|
+ alipay = aliPayObj.conf()
|
|
|
+ success = alipay.verify(data, signature)
|
|
|
+ if success and data["trade_status"] in ("TRADE_SUCCESS", "TRADE_FINISHED"):
|
|
|
+ print("trade succeed")
|
|
|
+ nowTime = int(time.time())
|
|
|
+ order_list = order_qs.values("UID", "commodity_code", "channel")
|
|
|
+ UID = order_list[0]['UID']
|
|
|
+ channel = order_list[0]['channel']
|
|
|
+ commodity_code = order_list[0]['commodity_code']
|
|
|
+ order_qs.update(status=1, updTime=nowTime)
|
|
|
+ smqs = Store_Meal.objects.filter(commodity_code=commodity_code). \
|
|
|
+ values("day", "bucket_id", "bucket__storeDay")
|
|
|
+ bucketId = smqs[0]['bucket_id']
|
|
|
+ if not smqs.exists():
|
|
|
+ return response.json(0, '套餐已删除')
|
|
|
+ addTime = int(smqs[0]["day"]) * 24 * 3600
|
|
|
+ ubqs = UID_Bucket.objects.filter(uid=UID, channel=channel). \
|
|
|
+ values("bucket_id", "endTime", "bucket__storeDay")
|
|
|
+ nowTime = int(time.time())
|
|
|
+ if ubqs.exists():
|
|
|
+ ubqs_count = ubqs.count()
|
|
|
+ ubq = ubqs[ubqs_count - 1, ubqs_count]
|
|
|
+ new_starTime = ubq['endTime'] + 1
|
|
|
+ ub_cqs = UID_Bucket.objects.create(uid=UID, channel=channel, bucket_id=bucketId,
|
|
|
+ endTime=new_starTime + addTime)
|
|
|
+ uid_bucket_id = ub_cqs.id
|
|
|
+ else:
|
|
|
+ ub_cqs = UID_Bucket.objects.create(uid=UID, channel=channel, bucket_id=bucketId,
|
|
|
+ endTime=nowTime + addTime)
|
|
|
+ uid_bucket_id = ub_cqs.id
|
|
|
+ order_qs.update \
|
|
|
+ (payTime=nowTime, status=1, updTime=nowTime, uid_bucket_id=uid_bucket_id)
|
|
|
+ red_url = "{SERVER_DOMAIN}cloudVod/payOK".format(SERVER_DOMAIN=SERVER_DOMAIN)
|
|
|
+ return HttpResponseRedirect(red_url)
|
|
|
+ return response.json(0, signature)
|
|
|
+
|
|
|
+ def do_pay_by_paypal_callback(self, request_dict, response):
|
|
|
+ paymentId = request_dict.get('paymentId', None)
|
|
|
+ PayerID = request_dict.get('PayerID', None)
|
|
|
+ orderID = request_dict.get('orderID', None)
|
|
|
+ if not paymentId or not PayerID or not orderID:
|
|
|
+ red_url = "{SERVER_DOMAIN}cloudVod/payError".format(SERVER_DOMAIN=SERVER_DOMAIN)
|
|
|
+ return HttpResponseRedirect(red_url)
|
|
|
+ paypalrestsdk.configure(PAYPAL_CRD)
|
|
|
+ # ID of the payment. This ID is provided when creating payment.
|
|
|
+ payment = paypalrestsdk.Payment.find(paymentId)
|
|
|
+ payres = payment.execute({"payer_id": PayerID})
|
|
|
+ print(payres)
|
|
|
+ if not payres:
|
|
|
+ red_url = "{SERVER_DOMAIN}cloudVod/payError".format(SERVER_DOMAIN=SERVER_DOMAIN)
|
|
|
+ return HttpResponseRedirect(red_url)
|
|
|
+ print("Payment execute successfully")
|
|
|
+ order_qs = Order_Model.objects.filter(orderID=orderID)
|
|
|
+ nowTime = int(time.time())
|
|
|
+ order_list = order_qs.values("UID", "channel", "commodity_code")
|
|
|
+ UID = order_list[0]['UID']
|
|
|
+ channel = order_list[0]['channel']
|
|
|
+ commodity_code = order_list[0]['commodity_code']
|
|
|
+ smqs = Store_Meal.objects.filter(commodity_code=commodity_code). \
|
|
|
+ values("day", "bucket_id", "bucket__storeDay")
|
|
|
+ bucketId = smqs[0]['bucket_id']
|
|
|
+ if not smqs.exists():
|
|
|
+ return response.json(0, '套餐已删除')
|
|
|
+ addTime = int(smqs[0]["day"]) * 24 * 3600
|
|
|
+ # ##
|
|
|
+ ubqs = UID_Bucket.objects.filter(uid=UID, channel=channel, endTime__gte=nowTime). \
|
|
|
+ values("bucket__storeDay", "bucket__region", "endTime")
|
|
|
+ if ubqs.exists():
|
|
|
+ ubqs_count = ubqs.count()
|
|
|
+ ubq = ubqs[ubqs_count - 1, ubqs_count]
|
|
|
+ new_starTime = ubq['endTime'] + 1
|
|
|
+ ub_cqs = UID_Bucket.objects.create(uid=UID, channel=channel, bucket_id=bucketId,
|
|
|
+ endTime=new_starTime + addTime)
|
|
|
+ uid_bucket_id = ub_cqs.id
|
|
|
+ else:
|
|
|
+ ub_cqs = UID_Bucket.objects.create(uid=UID, channel=channel, bucket_id=bucketId, endTime=nowTime + addTime)
|
|
|
+ uid_bucket_id = ub_cqs.id
|
|
|
+ order_qs.update(status=1, updTime=nowTime, uid_bucket_id=uid_bucket_id)
|
|
|
|
|
|
+ # return response.json(0)
|
|
|
+ red_url = "{SERVER_DOMAIN}cloudVod/payOK".format(SERVER_DOMAIN=SERVER_DOMAIN)
|
|
|
+ return HttpResponseRedirect(red_url)
|
|
|
|
|
|
-# def query_cloud_storage(request):
|
|
|
-# request.encoding = 'utf-8'
|
|
|
-# response = ResponseObject()
|
|
|
-# if request.method == 'POST':
|
|
|
-# request_dict = request.POST
|
|
|
-# elif request.method == 'GET':
|
|
|
-# request_dict = request.GET
|
|
|
-# else:
|
|
|
-# return response.json(444)
|
|
|
-# token = request_dict.get('token', None)
|
|
|
-# tko = TokenObject(token)
|
|
|
-# response.lang = tko.lang
|
|
|
-# if tko.code == 0:
|
|
|
-# qs = Store_Meal.objects.filter(bucket__mold=1)
|
|
|
-# qs = qs.values("id", "title", "price", "content", "day",
|
|
|
-# "add_time", "update_time", "currency",
|
|
|
-# "type", "bucket_id", "bucket__bucket",
|
|
|
-# "bucket__storeDay")
|
|
|
-# res = CommonService.qs_to_list(qs)
|
|
|
-# return response.json(0, res)
|
|
|
-# else:
|
|
|
-# return response.json(tko.code)
|
|
|
-#
|
|
|
-#
|
|
|
-# def cloud_storage_buy(request):
|
|
|
-# request.encoding = 'utf-8'
|
|
|
-# response = ResponseObject()
|
|
|
-# if request.method == 'POST':
|
|
|
-# request_dict = request.POST
|
|
|
-# elif request.method == 'GET':
|
|
|
-# request_dict = request.GET
|
|
|
-# else:
|
|
|
-# return response.json(444)
|
|
|
-# token = request_dict.get('token', None)
|
|
|
-# tko = TokenObject(token)
|
|
|
-# response.lang = tko.lang
|
|
|
-# if tko.code != 0:
|
|
|
-# return response.json(tko.code)
|
|
|
-# uid = request_dict.get('uid', None)
|
|
|
-# rank = request_dict.get('rank', None)
|
|
|
-# channel = request_dict.get('channel', None)
|
|
|
-# if not uid or not channel or not rank:
|
|
|
-# return response.json(444, 'channel,rank')
|
|
|
-# userID = tko.userID
|
|
|
-# qs = Device_Info.objects.filter(userID_id=userID, UID=uid, isShare=False)
|
|
|
-# if not qs.exists():
|
|
|
-# return response.json(12)
|
|
|
-# smqs = Store_Meal.objects.filter(id=rank).\
|
|
|
-# values("currency", "price", "content", "day", "bucket__storeDay", "bucket__region", "type")
|
|
|
-# if not smqs.exists():
|
|
|
-# return response.json(10, '套餐不存在')
|
|
|
-# if smqs[0]['type'] != 0:
|
|
|
-# return response.json(10, '套餐不支持paypal支付')
|
|
|
-# currency = smqs[0]['currency']
|
|
|
-# price = smqs[0]['price']
|
|
|
-# content = smqs[0]['content']
|
|
|
-# day = smqs[0]['day']
|
|
|
-# nowTime = int(time.time())
|
|
|
-# ubqs = UID_Bucket.objects.filter(uid=uid, channel=channel, endTime__gte=nowTime). \
|
|
|
-# values("bucket__storeDay", "bucket__region")
|
|
|
-# if ubqs.exists():
|
|
|
-# if ubqs[0]['bucket__region'] != smqs[0]['bucket__region']:
|
|
|
-# return response.json(712) # 区域不一致
|
|
|
-# # elif ubqs[0]['bucket__storeDay'] > smqs[0]['bucket__storeDay']:
|
|
|
-# # return response.json(711) # 不可降级
|
|
|
-# elif ubqs[0]['bucket__storeDay'] != smqs[0]['bucket__storeDay']:
|
|
|
-# return response.json(713) # 不可更改套餐
|
|
|
-# # 续费流程
|
|
|
-# nowTime = int(time.time())
|
|
|
-# # 判断是否存在未完成订单
|
|
|
-# # hasOrder = Order_Model.objects.filter(UID=uid, channel=channel, addTime__gte=nowTime - 3600, status=0,
|
|
|
-# # rank_id=rank, userID_id=userID).values('paypal')
|
|
|
-# # if hasOrder.exists():
|
|
|
-# # approval_url = hasOrder[0]['paypal']
|
|
|
-# # return response.json(0, {"redirectUrl": approval_url})
|
|
|
-# # 新增流程
|
|
|
-# orderID = CommonService.createOrderID()
|
|
|
-# call_sub_url = "{SERVER_DOMAIN}cloudVod/payExecute?orderID={orderID}". \
|
|
|
-# format(SERVER_DOMAIN=SERVER_DOMAIN, orderID=orderID)
|
|
|
-# # call_sub_url = "http://192.168.136.40:8077/cloudVod/payExecute?orderID={orderID}".format(
|
|
|
-# # SERVER_DOMAIN=SERVER_DOMAIN, orderID=orderID)
|
|
|
-# call_clc_url = "{SERVER_DOMAIN}cloudVod/payError".format(SERVER_DOMAIN=SERVER_DOMAIN)
|
|
|
-# paypalrestsdk.configure(PAYPAL_CRD)
|
|
|
-# payment = paypalrestsdk.Payment({
|
|
|
-# "intent": "sale",
|
|
|
-# "payer": {"payment_method": "paypal"},
|
|
|
-# "redirect_urls": {"return_url": call_sub_url, "cancel_url": call_clc_url},
|
|
|
-# "transactions": [{
|
|
|
-# "item_list": {
|
|
|
-# "items": [{"name": "Cloud video", "sku": "1", "price": price, "currency": "USD", "quantity": 1}]},
|
|
|
-# "amount": {"total": price, "currency": currency},
|
|
|
-# "description": content
|
|
|
-# }]})
|
|
|
-# if payment.create():
|
|
|
-# print("Payment created successfully")
|
|
|
-# else:
|
|
|
-# print(payment.error)
|
|
|
-# return response.json(10, payment.error)
|
|
|
-# print(payment)
|
|
|
-# for link in payment.links:
|
|
|
-# if link.rel == "approval_url":
|
|
|
-# approval_url = str(link.href)
|
|
|
-# print("Redirect for approval: %s" % (approval_url))
|
|
|
-# Order_Model.objects.create(orderID=orderID, UID=uid, channel=channel, userID_id=userID, desc=content,
|
|
|
-# price=price, currency=currency, addTime=nowTime, updTime=nowTime,
|
|
|
-# endTime=nowTime + int(day) * 3600 * 24,
|
|
|
-# rank_id=rank,
|
|
|
-# paypal=approval_url)
|
|
|
-# return response.json(0, {"redirectUrl": approval_url, "orderID": orderID})
|
|
|
-# return response.json(10, 'generate_order_false')
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-# /cloudstorage/commodity/buy
|
|
|
-# def query_cloud_storage5(request):
|
|
|
-# request.encoding = 'utf-8'
|
|
|
-# response = ResponseObject()
|
|
|
-# if request.method == 'POST':
|
|
|
-# request_dict = request.POST
|
|
|
-# elif request.method == 'GET':
|
|
|
-# request_dict = request.GET
|
|
|
-# else:
|
|
|
-# return response.json(444)
|
|
|
-# token = request_dict.get('token', None)
|
|
|
-# tko = TokenObject(token)
|
|
|
-# response.lang = tko.lang
|
|
|
-# if tko.code == 0:
|
|
|
-# qs = Store_Meal.objects.filter(bucket__mold=1)
|
|
|
-# qs = qs.values("id", "title", "price", "content", "day",
|
|
|
-# "add_time", "update_time", "currency",
|
|
|
-# "type", "bucket_id", "bucket__bucket",
|
|
|
-# "bucket__storeDay")
|
|
|
-# res = CommonService.qs_to_list(qs)
|
|
|
-# return response.json(0, res)
|
|
|
-# else:
|
|
|
-# return response.json(tko.code)
|
|
|
+ def do_create_pay_order(self, request_dict, userID, response):
|
|
|
+ uid = request_dict.get('uid', None)
|
|
|
+ channel = request_dict.get('channel', None)
|
|
|
+ commodity_code = request_dict.get('commodity_code', None)
|
|
|
+ pay_type = int(request_dict.get('pay_type', None))
|
|
|
+ dv_qs = Device_Info.objects.filter(userID_id=userID, UID=uid, isShare=False, isExist=1)
|
|
|
+ if not dv_qs.exists():
|
|
|
+ return response.json(12)
|
|
|
+ smqs = Store_Meal.objects.filter(commodity_code=commodity_code, type=pay_type). \
|
|
|
+ values('currency', 'price', 'content', 'day', 'commodity_type', 'title', 'content')
|
|
|
+ if not smqs.exists():
|
|
|
+ return response.json(10, '套餐不存在')
|
|
|
+ currency = smqs[0]['currency']
|
|
|
+ price = smqs[0]['price']
|
|
|
+ content = smqs[0]['content']
|
|
|
+ day = smqs[0]['day']
|
|
|
+ commodity_type = smqs[0]['commodity_type']
|
|
|
+ # ubqs = UID_Bucket.objects.filter(uid=uid, channel=channel, endTime__gte=nowTime). \
|
|
|
+ # values("bucket__storeDay", "bucket__region", "endTime")
|
|
|
+ # if ubqs.exists():
|
|
|
+ # ubqs_count = ubqs.count()
|
|
|
+ # ubq = ubqs[ubqs_count - 1, ubqs_count]
|
|
|
+ # new_starTime = ubq['endTime'] + 1
|
|
|
+ nowTime = int(time.time())
|
|
|
+ orderID = CommonService.createOrderID()
|
|
|
+ if pay_type == 0:
|
|
|
+ call_sub_url = "{SERVER_DOMAIN}cloudVod/dopaypalcallback?orderID={orderID}". \
|
|
|
+ format(SERVER_DOMAIN=SERVER_DOMAIN, orderID=orderID)
|
|
|
+ # call_sub_url = "http://192.168.136.40:8077/cloudVod/payExecute?orderID={orderID}".format(
|
|
|
+ # SERVER_DOMAIN=SERVER_DOMAIN, orderID=orderID)
|
|
|
+ call_clc_url = "{SERVER_DOMAIN}cloudVod/payError".format(SERVER_DOMAIN=SERVER_DOMAIN)
|
|
|
+ paypalrestsdk.configure(PAYPAL_CRD)
|
|
|
+ payment = paypalrestsdk.Payment({
|
|
|
+ "intent": "sale",
|
|
|
+ "payer": {"payment_method": "paypal"},
|
|
|
+ "redirect_urls": {"return_url": call_sub_url, "cancel_url": call_clc_url},
|
|
|
+ "transactions": [{
|
|
|
+ "item_list": {"items": [
|
|
|
+ {"name": "Cloud video", "sku": "1", "price": price, "currency": "USD", "quantity": 1}]},
|
|
|
+ "amount": {"total": price, "currency": currency},
|
|
|
+ "description": content}]})
|
|
|
+ if payment.create():
|
|
|
+ print("Payment created successfully")
|
|
|
+ else:
|
|
|
+ print(payment.error)
|
|
|
+ return response.json(10, payment.error)
|
|
|
+ print(payment)
|
|
|
+ for link in payment.links:
|
|
|
+ if link.rel == "approval_url":
|
|
|
+ approval_url = str(link.href)
|
|
|
+ print("Redirect for approval: %s" % (approval_url))
|
|
|
+ Order_Model.objects.create(orderID=orderID, UID=uid, channel=channel, userID_id=userID,
|
|
|
+ desc=content, payType=pay_type, payTime=nowTime,
|
|
|
+ price=price, currency=currency, addTime=nowTime, updTime=nowTime,
|
|
|
+ endTime=(nowTime + int(day) * 3600 * 24), pay_url=approval_url,
|
|
|
+ commodity_code=commodity_code, commodity_type=commodity_type)
|
|
|
+ return response.json(0, {"redirectUrl": approval_url, "orderID": orderID})
|
|
|
+ return response.json(10, 'generate_order_false')
|
|
|
+ else:
|
|
|
+ try:
|
|
|
+ aliPayObj = AliPayObject()
|
|
|
+ alipay = aliPayObj.conf()
|
|
|
+ subject = smqs[0]['title'] + smqs[0]['content']
|
|
|
+ order_string = alipay.api_alipay_trade_wap_pay(
|
|
|
+ out_trade_no=orderID,
|
|
|
+ total_amount=price,
|
|
|
+ subject=subject,
|
|
|
+ return_url="{SERVER_DOMAIN_SSL}cloudVod/payOK".format(SERVER_DOMAIN_SSL=SERVER_DOMAIN_SSL),
|
|
|
+ notify_url="{SERVER_DOMAIN_SSL}cloudVod/doalicallback".format(SERVER_DOMAIN_SSL=SERVER_DOMAIN_SSL)
|
|
|
+ # return_url="http://192.168.136.40/cloudVod/payOK",
|
|
|
+ # notify_url="http://192.168.136.40/cloudVod/aliPayCallback"
|
|
|
+ )
|
|
|
+ except Exception as e:
|
|
|
+ print(repr(e))
|
|
|
+ return response.json(10, repr(e))
|
|
|
+ else:
|
|
|
+ if order_string:
|
|
|
+ redirectUrl = aliPayObj.alipay_prefix + order_string
|
|
|
+ Order_Model.objects.create(orderID=orderID, UID=uid, channel=channel, userID_id=userID,
|
|
|
+ desc=content, payType=pay_type, payTime=nowTime,
|
|
|
+ price=price, currency=currency, addTime=nowTime, updTime=nowTime,
|
|
|
+ endTime=(nowTime + int(day) * 3600 * 24), pay_url=redirectUrl,
|
|
|
+ commodity_code=commodity_code, commodity_type=commodity_type)
|
|
|
+ return JsonResponse(status=200, data={'result_code': 0, 'reason': 'success',
|
|
|
+ 'result': {"redirectUrl": redirectUrl, "orderID": orderID},
|
|
|
+ 'error_code': 0})
|
|
|
+ else:
|
|
|
+ return response.json(10, '生成订单错误')
|