Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
local variable 'verify_payment' referenced before assignment

I'm having the local variable referenced before assignment error and I have tried a lot of ways that I can use to fix this. Any help would be greatly appreciated

This is my Views.py (the error after the top of the second if statement)

def call_back_url(request):
    reference = request.GET.get('reference')
    # We need to fetch the reference from PAYMENT
    check_pay = PayHistory.objects.filter(paystack_charge_id=reference).exists()
    if check_pay == False:
        # This means payment was not made error should be thrown here...
        print("Error")
    else:
        payment = PayHistory.objects.get(paystack_charge_id=reference)
        # We need to fetch this to verify if the payment was successful.
        def verify_payment(request):
            url = 'https://api.paystack.co/transaction/verify/'+reference
            headers = {
                'Authorization': 'Bearer '+settings.PAYSTACK_SECRET_KEY,
                'Content-Type' : 'application/json',
                'Accept': 'application/json',
                }
            datum = {
                "reference": payment.paystack_charge_id
                }
            x = requests.get(url, data=json.dumps(datum), headers=headers)
            if x.status_code != 200:
                return str(x.status_code)
            
            results = x.json()
            return results
    initialized = verify_payment(request)
    if initialized['data']['status'] == 'success':
        PayHistory.objects.filter(paystack_charge_id=initialized['data']['reference']).update(paid=True)
        new_payment = PayHistory.objects.get(paystack_charge_id=initialized['data']['reference'])
        instance = Membership.objects.get(id=new_payment.payment_for.id)
        sub = UserMembership.objects.filter(reference_code=initialized['data']['reference']).update(membership=instance)
        user_membership = UserMembership.objects.get(reference_code=initialized['data']['reference'])
        Subscription.objects.create(user_membership=user_membership, expires_in=dt.now().date() + timedelta(days=user_membership.membership.duration))
        return redirect('subscribed')
    return render(request, 'payment.html')


def subscribed(request):
    return render(request, 'subscribed.html')
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

try:

def verify_payment(request):
    url = 'https://api.paystack.co/transaction/verify/'+reference
    headers = {
        'Authorization': 'Bearer '+settings.PAYSTACK_SECRET_KEY,
        'Content-Type' : 'application/json',
        'Accept': 'application/json',
        }
    datum = {
        "reference": payment.paystack_charge_id
        }
    x = requests.get(url, data=json.dumps(datum), headers=headers)
    if x.status_code != 200:
        return str(x.status_code)
    
    results = x.json()
    return results

def call_back_url(request):
    reference = request.GET.get('reference')
    # We need to fetch the reference from PAYMENT
    check_pay = PayHistory.objects.filter(paystack_charge_id=reference).exists()
    if check_pay == False:
        # This means payment was not made error should be thrown here...
        print("Error")
    else:
        payment = PayHistory.objects.get(paystack_charge_id=reference)
        # We need to fetch this to verify if the payment was successful.

    initialized = verify_payment(request)
    if initialized['data']['status'] == 'success':
        PayHistory.objects.filter(paystack_charge_id=initialized['data']['reference']).update(paid=True)
        new_payment = PayHistory.objects.get(paystack_charge_id=initialized['data']['reference'])
        instance = Membership.objects.get(id=new_payment.payment_for.id)
        sub = UserMembership.objects.filter(reference_code=initialized['data']['reference']).update(membership=instance)
        user_membership = UserMembership.objects.get(reference_code=initialized['data']['reference'])
        Subscription.objects.create(user_membership=user_membership, expires_in=dt.now().date() + timedelta(days=user_membership.membership.duration))
        return redirect('subscribed')
    return render(request, 'payment.html')

def subscribed(request):
    return render(request, 'subscribed.html')

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!