local variable 'verify_payment' referenced before assignment

i;m having the local variable renfrenced before assign and i have tried a lot of ways that i can use to fix this. any help would be greatly appreciated

let me show some code

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...
		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':
		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')


System check identified 1 issue (0 silenced).
November 26, 2021 - 18:50:42
Django version 3.2.9, using settings 'dexxapikprj.settings'
Starting development server at
Quit the server with CTRL-BREAK.
Internal Server Error: /sub/payment/
Traceback (most recent call last):
  File "C:\Users\Destiny\Desktop\DexxaPik\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Destiny\Desktop\DexxaPik\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Destiny\Desktop\DexxaPik\dexxapikprj\subscription\views.py", line 85, in call_back_url
    initialized = verify_payment(request)
UnboundLocalError: local variable 'verify_payment' referenced before assignment
[26/Nov/2021 18:50:44] "GET /sub/payment/ HTTP/1.1" 500 66028

You’re defining your function within the else clause of that if statement. If the if condition is true, then the function - verify_payment - is never defined and will throw that error.

Is there some reason why you think you want to do all this in this manner? It looks to me like you can extract that function definition from this function entirely, and define it outside the context of call_back_url.