rasooll / zarinpal-django-py3

sample django zarinpal webgate with python3
62 stars 13 forks source link

Error in django view of sending request to zarinpal #4

Open Rahimz opened 1 year ago

Rahimz commented 1 year ago

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 '''

usgitAz commented 1 year ago

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)

yousefvafaei commented 3 months ago

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"})