zarinpal-django-py3 icon indicating copy to clipboard operation
zarinpal-django-py3 copied to clipboard

Error in django view of sending request to zarinpal

Open Rahimz opened this issue 1 year ago • 2 comments

It seems in this line there is an error: Send request view

because I could not send the request and it raises an attribute error: '''

AttributeError at /zarinpal/request/

'dict' object has no attribute 'status_code'
Request Method: GET
http://127.0.0.1:8000/zarinpal/request/
4.2.1
AttributeError
'dict' object has no attribute 'status_code'
/home/rahim/.local/share/virtualenvs/bookstore-NVJ35xj2/lib/python3.10/site-packages/debug_toolbar/panels/history/panel.py, line 77, in generate_stats
zarinpal.views_zarin.send_request
/home/rahim/.local/share/virtualenvs/bookstore-NVJ35xj2/bin/python
3.10.6
'''

Rahimz avatar Aug 04 '23 09:08 Rahimz

dorod eror ro fix kardi ? eror man ine : AttributeError at /request/ 'dict' object has no attribute 'headers' baraye in khat code fek konam : response = requests.post(ZP_API_REQUEST, data=data,headers=headers, timeout=10)

usgitAz avatar Oct 06 '23 18:10 usgitAz

Hi Friends,

To fix this, I made two key changes:

  1. I replaced the dictionary responses with JsonResponse to ensure the HTTP responses were properly formatted and could be easily handled on the frontend.
  2. I used HttpResponseRedirect to automatically redirect the user to the payment page after a successful request.

Updated code:

from django.http import HttpResponseRedirect, JsonResponse, HttpResponse

try:
    response = requests.post(ZP_API_REQUEST, data=data, headers=headers, timeout=10)

    if response.status_code == 200:
        response = response.json()
        if response["Status"] == 100:
            return HttpResponseRedirect(ZP_API_STARTPAY + str(response["Authority"]))
        else:
            return JsonResponse({"status": False, "code": str(response["Status"])})
    return HttpResponse(response.content, status=response.status_code)

except requests.exceptions.Timeout:
    return JsonResponse({"status": False, "code": "timeout"})
except requests.exceptions.ConnectionError:
    return JsonResponse({"status": False, "code": "connection error"})

yousefvafaei avatar Aug 11 '24 08:08 yousefvafaei