Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
godfredtenkorang committed Oct 17, 2024
1 parent 9ca1e17 commit 978d8b4
Showing 1 changed file with 20 additions and 29 deletions.
49 changes: 20 additions & 29 deletions ussd/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down

0 comments on commit 978d8b4

Please sign in to comment.