Integrating Your API Key and Managing Transactions
After creating your account, business profile, and app, the next steps involve integrating the API key into your system and managing transactions effectively.
Step 6: Integrate the API Key
The API key enables your application to communicate securely with Daraza’s payment system. Follow these steps to set it up:
1. Access Daraza API Documentation
- Navigate to the "Documentation" section in your Daraza dashboard to get the endpoints.
- Review the detailed guides for API integration, including examples in popular programming languages like Python, PHP, or JavaScript.
2. Add the API Key to Your Application
-
In your application’s code, set the API key as an environment variable for security.
- Example for Python:
import os
DARAZA_API_KEY = os.getenv('DARAZA_API_KEY')
- Add your API key to the
.envfile:
DARAZA_API_KEY=your-api-key-here
- Example for Python:
-
Use the API key in API requests:
- Example of sending a payment request:
import requestsheaders = {"Authorization": f"Api-Key {DARAZA_API_KEY}",
"Content-Type": "application/json"}payload = {"method": 1,
"amount": "5000",
"amount": "+2567890000000",
"note": "Payment for Order #1234"}response = requests.post("https://api.daraza.net/api/request_to_pay/", headers=headers, json=payload)if response.status_code == 200:print("Payment Successful:", response.json())else:print("Error:", response.text)
- Example of sending a payment request:
3. Test Your Integration
- Use the Daraza Sandbox Environment to simulate payments without affecting real accounts.
- Verify your API requests and responses to ensure proper integration.
Step 7: Manage Transactions
Daraza provides tools for tracking and managing payments directly from the dashboard or via API.
1. View Transactions in the Dashboard
- Log in to your Daraza account.
- Navigate to the App making the request and then "Funds" section to see a list of all payments processed.
2. Handle Transaction Notifications
Daraza supports webhooks to notify you about transaction events:
-
Set up a Webhook URL in your app settings.
-
Handle the incoming notification payload:
- Example webhook handler (Python Flask):
from flask import Flask,request, jsonify
app = Flask(__name__)
@app.route('/webhook', methods=['POST'])
def webhook():
data = request.json
if data['status'] == 'completed':
print(f"Payment received: {data['amount']} UGX")
else:
print(f"Payment failed: {data['reason']}")
return jsonify({"status": "success"}), 200
if __name__ == '__main__':
app.run(port=5000)
- Example webhook handler (Python Flask):
-
Test your webhook with the Daraza dashboard to confirm you’re receiving notifications.
3. Refund and Reversal Management
- To initiate a refund, use the Remittance API Endpoint to send money back to a customer.
Step 8: Monitor and Analyze Transactions
Daraza’s analytics tools help you understand your payment performance:
A. General Monitor
Your home view gives the general analytics for your business profiles and apps, API request summary and the most recent activity logs
B. App Analytics
Your app has diffrent analytics scetion to give your different insights.
- Visit the Details section on your app to view:
- App deatils such as client, credentials and important dates such creation
- Number of request mades i.e. GET, POST and DELETE
- Visit the Logs section on your app to view the apps activity logs
- Visit the Funds section on your app to view:
- Total revenue.
- Breakdown of of different transaction types.