All Projects → WilliamOtieno → python-daraja

WilliamOtieno / python-daraja

Licence: MIT license
Python Wrapper for interacting with the MPESA Daraja API. More Features to be implemented

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to python-daraja

adyen-python-api-library
Adyen API Library for Python
Stars: ✭ 41 (+105%)
Mutual labels:  payment, payments, payment-gateway, payment-integration
adyen-dotnet-api-library
Adyen API Library for .NET
Stars: ✭ 69 (+245%)
Mutual labels:  payment, payments, payment-gateway, payment-integration
cybersource-android-sdk
The CyberSource InApp SDK enables developers to simply and securely incorporate mobile payments into their Android applications.
Stars: ✭ 25 (+25%)
Mutual labels:  payment, payments, payment-gateway, payment-integration
adyen-salesforce-commerce-cloud
Salesforce Commerce Cloud (formerly Demandware)
Stars: ✭ 63 (+215%)
Mutual labels:  payment, payments, payment-gateway, payment-integration
paymentgateway
Dokumentace ČSOB platební brány a jejího eAPI pro platby platebními kartami, Apple Pay, mallpay a platebními tlačítky ČSOB.
Stars: ✭ 104 (+420%)
Mutual labels:  payment, payments, payment-gateway, payment-integration
cybersource-sdk-java
Java SDK for CyberSource Simple Order API
Stars: ✭ 44 (+120%)
Mutual labels:  payment, payments, payment-gateway, payment-integration
wc-moldovaagroindbank
WooCommerce maib Moldova Agroindbank Payment Gateway
Stars: ✭ 13 (-35%)
Mutual labels:  payment, payment-gateway, payment-integration
Sdk Php
PHP SDK for Authorize.Net API
Stars: ✭ 343 (+1615%)
Mutual labels:  payment, payments, payment-gateway
ccashcow
💰 Accept cards & crypto. Payments so easy a cow could do it.
Stars: ✭ 40 (+100%)
Mutual labels:  payment, payment-gateway, payment-integration
adyen-hybris
Adyen Payment plugin for Hybris
Stars: ✭ 23 (+15%)
Mutual labels:  payment, payment-gateway, payment-integration
Gringotts
A complete payment library for Elixir and Phoenix Framework
Stars: ✭ 396 (+1880%)
Mutual labels:  payment, payments, payment-gateway
php-pesa
Open payment integration toolkit for PHP
Stars: ✭ 26 (+30%)
Mutual labels:  mpesa, mpesa-api, mpesa-payments
awesome-payment
A curated list of payment services
Stars: ✭ 22 (+10%)
Mutual labels:  payment, payment-gateway, payment-integration
react-native-payumoney
React Native Payumoney (Android/IOS)
Stars: ✭ 18 (-10%)
Mutual labels:  payment, payment-gateway, payment-integration
adyen-node-api-library
Adyen API Library for Node.js
Stars: ✭ 82 (+310%)
Mutual labels:  payment, payment-gateway, payment-integration
Adyen Ruby Api Library
Adyen API Library for Ruby
Stars: ✭ 35 (+75%)
Mutual labels:  payment, payments, payment-gateway
Adyen Php Api Library
Adyen API Library for PHP
Stars: ✭ 93 (+365%)
Mutual labels:  payment, payments, payment-gateway
Sdk Dotnet
.Net SDK for Authorize.Net API
Stars: ✭ 124 (+520%)
Mutual labels:  payment, payments, payment-gateway
ex pesa
Payment Library For Most Public Payment API's in Kenya and hopefully Africa. Let us get this moneybag
Stars: ✭ 19 (-5%)
Mutual labels:  payment, mpesa, daraja
nagadApi
This is Bangladeshi nagad payment gateway api development library. This library can be used in any php application.
Stars: ✭ 20 (+0%)
Mutual labels:  payment, payment-gateway, payment-integration

Python Daraja

Description

Python Wrapper for handling payment requests through the Daraja MPESA API

Contribution

Usage

Installation

pip install python-daraja

Initial Setup

  • Set the following constants first before proceeding
from python_daraja import payment


payment.SHORT_CODE = "YOUR_SHORTCODE"
payment.PASSKEY = "YOUR PASSKEY"
payment.CONSUMER_SECRET = "YOUR CONSUMER SECRET"
payment.CONSUMER_KEY = "YOUR CONSUMER KEY"
payment.ACCOUNT_TYPE = "PAYBILL"  # Set to TILL to use BuyGoods instead of Pay Bill
  • The first function to call is one which would trigger an automatic STK Push on your customer's phone. There is no simpler way of knowing whether the customer has successfully paid or not (failures such as inputting the wrong PIN or just cancelling the request altogether). You are therefore advised to set up a simple server with an endpoint that will accept POST requests from Daraja API with the details of the transaction.
  • For the server, it must be secure (https instead of http), it must not be localhost nor 127.0.0.1:$PORT.
  • You can use services like ngrok to tunnel localhost to live secure server.
  • Your server's endpoint should also accept a POST request hence you may need to disable constraints such as CSRF (outside the scope of this project).
  • If at all you must have CSRF Protection in your server, then you need to allow requests originating from the following IP addresses (whitelisting):
    • 192.201.214.200
    • 196.201.214.206
    • 196.201.213.114
    • 196.201.214.207
    • 196.201.214.208
    • 196.201.213.44
    • 196.201.212.127
    • 196.201.212.128
    • 196.201.212.129
    • 196.201.212.136
    • 196.201.212.74
    • 196.201.212.69
  • You can now proceed by triggering the payment process; you should expect a Python Dict object with the results of the process. This will only tell you whether it was successful and not whether the customer has actually paid.
from python_daraja import payment


details = payment.trigger_stk_push(phone_number=2547123456, amount=1, callback_url='https://your-domain/callback/',
                                   description='Payment for services rendered',
                                   account_ref='Python Good PHP Bad and Co.')
print(details)
  • The MPESA Gateway will then send some POST data to your endpoint if the customer pays, otherwise, you will not receive any data showing that your customer hasn't paid a dime.

  • It is advisable to save the details received from the previous method call to a DB of your choice. Some of those details are used for other subsequent method calls especially in getting the transaction status of your payment requests.

  • In order to query the details of a payment request made through the automatic STK Push above:-

from python_daraja import payment


details = payment.query_stk_push(checkout_request_id='ws_CO_DMZ_123212312_2342347678234')
print(details)
  • Getting a 0 as the Response Code or Result Code generally means that the transaction was successful, any other digit signifies otherwise.

Remarks

  • Unfortunately as of the moment of release of this package, Safaricom has internal problems with other types of transactions such as C2B. Maintainers of the project will work on implementing more features when the issues are addressed.

Sponsor Python Daraja

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].