我有以下类用于用户的付款进度
当请求为POST
时,我在会话中设置金额,如request.session['amount'] = amount
当请求为GET
时,我应该得到会话,但会话是安全的,if语句if 'amount' in request.session:
总是假的。我该如何解决这个问题?
class PaidApiView(APIView):
MMERCHANT_ID = '??????????' # Required
ZARINPAL_WEBSERVICE = 'https://www.zarinpal.com/pg/services/WebGate/wsdl' # Required
amount = None # Required
description = None # Required
products = None
email = 'user@userurl.ir' # Optional
mobile = '09123456789' # Optional
CallbackURL = 'http://127.0.0.1:8000/store/paid/'
def post(self, request, *args, **kwargs):
amount = request.data['amount']
products = request.data['products']
productstitle = request.data['productstitle']
productsTitlestr = ''.join(productstitle)
self.amount = amount
self.products = products
self.description = productsTitlestr
request.session['amount'] = amount
client = Client(self.ZARINPAL_WEBSERVICE)
result = client.service.PaymentRequest(self.MMERCHANT_ID,
self.amount,
self.description,
self.email,
self.mobile,
self.CallbackURL)
if result.Status == 100:
return Response('https://www.zarinpal.com/pg/StartPay/' + result.Authority)
else:
return Response('Error')
def get(self, request):
if 'amount' in request.session:
self.amount = request.session['amount']
client = Client(self.ZARINPAL_WEBSERVICE)
if request.GET.get('Status') == 'OK':
result = client.service.PaymentVerification(self.MMERCHANT_ID,
request.GET['Authority'],
self.amount)
if result.Status == 100:
del request.session['amount']
return Response('Transaction success. RefID: ' + str(result.RefID))
elif result.Status == 101:
del request.session['amount']
return Response('Transaction submitted : ' + str(result.Status))
else:
del request.session['amount']
return Response('Transaction failed. Status: ' + str(result.Status))
else:
del request.session['amount']
return Response('Transaction failed or canceled by user')
else:
return Response(status=status.HTTP_400_BAD_REQUEST)
答案 0 :(得分:0)
如果您已激活SessionAuthentication类,则将拥有会话变量。但可能您使用的是TokenAuthentication,因此您最好的选择是将数据保存在表中并在每次请求时加载它。