Learn how to integrate M-Pesa payments into Django using Safaricom’s Daraja API and STK Push. In this step-by-step tutorial, we build a simple Django payment application called PayFlow and connect it to the M-Pesa Daraja sandbox. You’ll learn how to: * Set up a Django payment application * Connect Django to the Daraja API * Authenticate with Daraja using OAuth * Send an M-Pesa STK Push * Receive M-Pesa callbacks * Use `CheckoutRequestID` to track payments * Handle successful and failed payments * Store the M-Pesa receipt in the database * Use ngrok for local callback testing M-Pesa Payment Flow User enters phone number and amount - Django creates a pending payment - Daraja authenticates the request - Django sends an STK Push - Customer enters their M-Pesa PIN - M-Pesa processes the payment - Daraja sends a callback - Django updates the payment - Payment becomes successful or failed What We Build A simple Django + M-Pesa payment dashboard where you can make payments and track their status. Technologies * Python * Django * M-Pesa Daraja API * STK Push * SQLite * Requests * python-dotenv * ngrok Chapters 00:00 - Introduction & Finished Application 00:00 - M-Pesa Payment Flow 00:00 - Django Project Setup 00:00 - Payment Model & Form 00:00 - Payment Dashboard 00:00 - Safaricom Daraja Setup 00:00 - OAuth Access Token 00:00 - Phone Number Formatting 00:00 - STK Push 00:00 - M-Pesa Callback 00:00 - ngrok Setup 00:00 - Handling Successful & Failed Payments 00:00 - Storing M-Pesa Receipt 00:00 - Testing the Payment Flow 00:00 - Common Errors 00:00 - Final Walkthrough Important An STK Push being accepted does not mean the payment was successful. Daraja first accepts the STK Push request and gives us a `CheckoutRequestID`. The final payment result is sent later through the M-Pesa callback. This tutorial shows how Django uses that callback to update the payment as successful or failed. Resources Safaricom Daraja: https://developer.safaricom.co.ke/ GitHub repository: https://github.com/ceciliawambui/payflow Notes: https://getdevnook.com/topics/python-django-framework-integrating-m-pesa-pyments-in-django-apps If you're learning JS, React, Python, Django, APIs, and real-world software development, subscribe for more practical tutorials. #Django #MPesa #DarajaAPI #Python #STKPush #MPesaAPI #DjangoTutorial #PythonDjango #Safaricom #KenyaDevelopers
The information provided is not trading advice. kdj.com does not assume any responsibility for any investments made based on the information provided in this article. Cryptocurrencies are highly volatile and it is highly recommended that you invest with caution after thorough research!
If you believe that the content used on this website infringes your copyright, please contact us immediately (info@kdj.com) and we will delete it promptly.