spiritman
spiritman

Reputation: 65

Redirecting a user to checkout page in Django

I want to redirect a user to checkout page that is automatically generated by the payment gateway. Anytime a user submits a request, the payment gateway returns a response in the format below. What i want to do is redirect the user to the checkout page to make payment.

 "status": true,
  "message": "Authorization URL created",
  "data": {
    "authorization_url": "https://checkout.paystack.com/eddno1f6rf411p0",
    "access_code": "eddno1f6rf411p0",
    "reference": "bpozkels2v"

def paystack(request):
    url = 'https://api.paystack.co/transaction/initialize'
    transaction_id = random.randint(100000000000, 999999999999)
    data = {
            "key": "PUBLIC_KEY",
            "ref": transaction_id,
            "amount": "000000000100",
            "callback": f"http://127.0.0.1:8000",
            "email": "[email protected]",
        }
    headers =  {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer SECRET_KEY',
        'Cache-Control': 'no-cache'
    }
    res = requests.post(url, data=json.dumps(data), headers=headers)
    response_data = res.json()
    print(response_data["authorization_url"])
    redirect(response_data["authorization_url"])

The error i get is

 KeyError at /paystack
'authorization_url'

Upvotes: 2

Views: 374

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476719

GIven the response is as in the question text, you first should access the subdictionary for the "data" key, so:

response_data['data']['authorization_url']

Upvotes: 2

Related Questions