-
Notifications
You must be signed in to change notification settings - Fork 8
/
views.py
165 lines (123 loc) · 5.39 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from django.utils import timezone
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from rest_framework import permissions
import json
import datetime
from urllib.parse import quote_plus
from .utils import order_data, remove_order_data
from .utils import sign as sign_func
from .utils import urlencode_data as urlencode_data_func
from .utils import check_sign as check_sign_func
from .utils import check_ali_sign as check_ali_sign_func
from .config import NOTIFY_URL, APP_ID, RSA_PRIVATE, ALIPAY_PUBLIC_KEY, PAY_URL
from .models import Order, Refund
from .serializers import OrderSerializer, RefundSerializer
# 获取签名
class SignView(GenericAPIView):
serializer_class = OrderSerializer
permission_classes = (permissions.IsAuthenticated,)
def post(self, request):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = self.request.user
instance = Order.objects.create(**request.data)
instance.out_trade_no = timezone.now().strftime('%Y%m%d') +'{}'.format(instance.id)
instance.save()
subject = serializer.validated_data.get('subject')
total_amount = serializer.validated_data.get('total_amount')
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
notify_url = NOTIFY_URL
app_id = APP_ID
biz_content = {
'subject': subject,
'total_amount': '%.2f' % float(total_amount),
'product_code': 'QUICK_MSECURITY_PAY',
'out_trade_no': instance.out_trade_no,
}
payload = {
'app_id': app_id,
'method': 'alipay.trade.app.pay',
'charset': 'UTF-8',
'format': 'json',
'sign_type': 'RSA2',
'timestamp': timestamp,
'version': '1.0',
'notify_url': notify_url,
'biz_content': json.dumps(biz_content, separators=(',', ':')),
}
private_key = RSA_PRIVATE
# 对payload转化成key+value&对,并且排序
order_payload = order_data(payload)
# print('order', order_payload)
# 根据排序好的数据获取签名
sign = sign_func(order_payload, private_key)
# print('sign', sign)
# 获取urlencode签名数据
urlencode_data = urlencode_data_func(payload, sign)
return Response({'sign': urlencode_data})
# 支付宝通知
class PayNotifyView(GenericAPIView):
def post(self, request, *args, **kwargs):
sign = request.data.get('sign')
payload = remove_order_data(request.data)
alipay_public_key = ALIPAY_PUBLIC_KEY
is_checked = check_ali_sign_func(payload, sign, alipay_public_key)
if not is_checked:
return Response('fail')
total_amount = float(request.data.get('total_amount'))
out_trade_no = request.data.get('out_trade_no')
trade_no = request.data.get('trade_no')
order = Order.objects.filter(out_trade_no=out_trade_no).first()
if order and order.status == 1:
return Response('success')
if order and order.status == 0:
if "%s" % order.total_amount == "%s" % total_amount:
order.trade_no = trade_no
order.status = True
order.save()
return Response('success') # 有效数据需要返回 'success' 给 alipay
class RefundView(GenericAPIView):
serializer_class = RefundSerializer
permission_classes = (permissions.AllowAny,)
def post(self, request):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
# user = self.request.user
instance = Refund.objects.create(**request.data)
instance.out_refund_no = 'alipay-refund' + timezone.now().strftime('%Y%m%d') + \
'%s' % instance.id
instance.save()
out_trade_no = serializer.validated_data['out_trade_no']
refund_amount = serializer.validated_data['refund_amount']
refund_reason = serializer.validated_data.get('refund_reason')
biz_content = {
'refund_reason': refund_reason,
'refund_amount': '%.2f' % float(refund_amount),
'out_trade_no': out_trade_no,
}
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
app_id = settings.ALIPAY['APP_ID']
payload = {
'app_id': app_id,
'method': 'alipay.trade.refund',
'charset': 'utf-8',
'sign_type': 'RSA2',
'timestamp': timestamp,
'version': '1.0',
'biz_content': json.dumps(biz_content, separators=(',', ':'))
}
private_key = settings.RSA_PRIVATE
# 对payload转化成key+value&对,并且排序
order_payload = order_data(payload)
# 根据排序好的数据获取签名
sign = sign_func(order_payload, private_key)
urlencode_data = urlencode_data_func(payload, sign)
url = PAY_URL.replace('payload', urlencode_data)
req_obj = requests.get(url).json()
if req_obj['alipay_trade_refund_response']['code'] == '10000':
instance.status = "1"
instance.trade_no = req_obj['alipay_trade_refund_response']['trade_no']
instance.save()
# print('req_obj: {}'.format(req_obj))
return Response({'message': 'ok'})