From 978d8b4ff0ec103e6f15f5bffaad88bcc4139826 Mon Sep 17 00:00:00 2001 From: Godey Tech <91687536+godfredtenkorang@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:15:13 +0000 Subject: [PATCH] changes --- ussd/views.py | 49 ++++++++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/ussd/views.py b/ussd/views.py index 58fbd84f..af4927c6 100644 --- a/ussd/views.py +++ b/ussd/views.py @@ -95,53 +95,44 @@ def send_response(msg, msgtype=True): return JsonResponse(send_response(message, True)) elif level == 'payment': - amount = session.amount * 100 + amount = session.amount telephone = msisdn - network_type = network - endpoint = "https://api.paystack.co/charge" - public_key = f"{settings.PAYSTACK_PUBLIC_KEY}" - secret_key = f"{settings.PAYSTACK_SECRET_KEY}" - email = f"{msisdn}@voteafric.com" - # Create a unique transaction reference - reference = str(uuid.uuid4()) - - amount_in_kobo = int(Decimal(amount) * 100) # Convert to kobo + # Payload for Paystack USSD payment request payload = { - 'email': email, - 'amount': amount_in_kobo, - 'currency': 'GHS', # Set appropriate currency + "email": f"{telephone}@example.com", # Use phone number as a unique identifier + "amount": int(amount * 100), # Amount in kobo/pesewas + "metadata": { + "custom_fields": [ + { + "display_name": f"{telephone}", + "variable_name": f"{telephone}", + "value": telephone + } + ] + }, "ussd": { "type": "ussd", - "bank": "057", - }, - - "metadata": { - "phone": msisdn, - "network": network_type, - "votes": session.votes - - }, - "reference": reference, - + "bank": network # The USSD code for the user's bank + } } headers = { - "Authorization": f"Bearer {secret_key}", # Replace with your actual secret key + "Authorization": f"Bearer {settings.PAYSTACK_SECRET_KEY}", # Replace with your actual secret key "Content-Type": "application/json", } # Sending payment request to Paystack - response = requests.post(endpoint, json=payload, headers=headers) + response = requests.post("https://api.paystack.co/charge", json=payload, headers=headers) if response.status_code == 200: - message = f"You are about to pay GH¢{amount:.2f}. Please follow the USSD code prompt sent to your phone to complete the payment." + res_data = response.json() + message = f"You are about to pay GH¢{amount:.2f}. Dial {res_data['data']['ussd_code']} to complete the transaction." return JsonResponse(send_response(message, False)) else: - error_data = response.json() - return JsonResponse(send_response(f"Payment request failed: {error_data.get('message', 'Please try again.')}", False)) + return JsonResponse(send_response("Payment request failed. Please try again.", False)) # elif level == 'payment': # amount = session.amount # endpoint = "https://api.nalosolutions.com/payplus/api/"