Skip to content

Commit

Permalink
Updated documentation to include B2C Payment API's
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmogusu committed Nov 16, 2020
1 parent fece314 commit e382ba0
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 9 deletions.
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ MPESA_SHORTCODE_TYPE=paybill
# Production passkey is sent via email once you go live

MPESA_PASSKEY=mpesa_passkey

# Username for initiator (to be used in B2C, B2B, AccountBalance and TransactionStatusQuery Transactions)

MPESA_INITIATOR_USERNAME=initiator_username

# Plaintext password for initiator (to be used in B2C, B2B, AccountBalance and TransactionStatusQuery Transactions)

MPESA_INITIATOR_SECURITY_CREDENTIAL=initiator_security_credential
2 changes: 0 additions & 2 deletions django_daraja/mpesa/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ def b2c_payment(self, phone_number, amount, transaction_desc, callback_url, occa
try:
r = requests.post(url, json=data, headers=headers)
response = mpesa_response(r)
print(r)
print(r.content)
return response
except requests.exceptions.ConnectionError:
raise MpesaConnectionError('Connection failed')
Expand Down
59 changes: 59 additions & 0 deletions docs/pages/apis/b2c_payment.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
B2C Payment APIs
================

The B2C Payment APIs are used to make Business to Customer payments. Currently 3 transactions are supported in this model:
- Business Payment: This is a normal business to customer payment, supports only M-Pesa registered customers.
- Salary Payment: This supports sending money to both registere and unregistered M-Pesa customers.
- Promotion Payment: This is a promotional payment to customers. The M-Pesa notification message is a congratulatory message. Supports only M-Pesa registered customers.

Examples:

Business Payment
----------------

.. code-block:: python
from django_daraja.mpesa.core import MpesaClient
phone_number = '07xxxxxxxx'
amount = 1
transaction_desc = 'Description'
occassion = 'Occassion'
callback_url = request.build_absolute_uri(reverse('mpesa_business_payment_callback'))
response = self.cl.business_payment(phone_number, amount, transaction_desc, self.callback_url, occassion)
Salary Payment
----------------

.. code-block:: python
from django_daraja.mpesa.core import MpesaClient
phone_number = '07xxxxxxxx'
amount = 1
transaction_desc = 'Description'
occassion = 'Occassion'
callback_url = request.build_absolute_uri(reverse('mpesa_salary_payment_callback'))
response = self.cl.business_payment(phone_number, amount, transaction_desc, self.callback_url, occassion)
Promotion Payment
----------------

.. code-block:: python
from django_daraja.mpesa.core import MpesaClient
phone_number = '07xxxxxxxx'
amount = 1
transaction_desc = 'Description'
occassion = 'Occassion'
callback_url = request.build_absolute_uri(reverse('mpesa_promotion_payment_callback'))
response = self.cl.promotion_payment(phone_number, amount, transaction_desc, self.callback_url, occassion)
This will assign the ``response`` variable with an ``MpesaResponse`` object containing the response returned from the Business Payment B2C API Call

.. note::
- Test credentials to use for this scenario can be found at the developer portal (https://developer.safaricom.co.ke/test_credentials)
- Use `shortcode 1` as the shortcode, and the test MSISDN as the B2C phone number
- This example will work if your site is already hosted, since the callback URL needs to be accessible via internet. For local testing purposes, you can use an endpoint hosted outside your site to check the notification received on the callback URL. There is a test listener hosted at https://darajambili.herokuapp.com, which you can use to view logs of notifications received. You can head over there to pick a callback URL to use for B2C Payments.
1 change: 1 addition & 0 deletions docs/pages/apis/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ The django-daraja library supports the different MPESA APIs exposed by the Daraj

oauth
stk_push
b2c_payment
28 changes: 22 additions & 6 deletions docs/pages/quick_start.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Create a ``.env`` file in the root folder (``my_site``) and in the file load the
# The Mpesa environment to use
# Possible values: sandbox, production
MPESA_ENVIRONMENT=sandbox
# Credentials for the daraja app
Expand All @@ -55,9 +55,9 @@ Create a ``.env`` file in the root folder (``my_site``) and in the file load the
MPESA_SHORTCODE=mpesa_shortcode
# Shortcode to use for Lipa na MPESA Online (MPESA Express) transactions
# This only has a different value on sandbox, you do not need to set it on production
# This is only used on sandbox, do not set this variable in production
# For sandbox use the Lipa na MPESA Online Shorcode provided on test credentials page
MPESA_EXPRESS_SHORTCODE=mpesa_express_shortcode
#Type of shortcode
Expand All @@ -73,6 +73,14 @@ Create a ``.env`` file in the root folder (``my_site``) and in the file load the
MPESA_PASSKEY=mpesa_passkey
# Username for initiator (to be used in B2C, B2B, AccountBalance and TransactionStatusQuery Transactions)
MPESA_INITIATOR_USERNAME=initiator_username
# Plaintext password for initiator (to be used in B2C, B2B, AccountBalance and TransactionStatusQuery Transactions)
MPESA_INITIATOR_SECURITY_CREDENTIAL=initiator_security_credential
Alternatively, in ``settings.py`` you can add the environment configuration as settings variables.

.. warning::
Expand All @@ -96,9 +104,9 @@ Alternatively, in ``settings.py`` you can add the environment configuration as s
MPESA_SHORTCODE = 'mpesa_shortcode'
# Shortcode to use for Lipa na MPESA Online (MPESA Express) transactions
# This only has a different value on sandbox, you do not need to set it on production
# For sandbox use the Lipa na MPESA Online Shorcode provided on test credentials page
# Shortcode to use for Lipa na MPESA Online (MPESA Express) transactions
# This is only used on sandbox, do not set this variable in production
# For sandbox use the Lipa na MPESA Online Shorcode provided on test credentials page

MPESA_EXPRESS_SHORTCODE = 'mpesa_express_shortcode'

Expand All @@ -115,6 +123,14 @@ Alternatively, in ``settings.py`` you can add the environment configuration as s

MPESA_PASSKEY = 'mpesa_passkey'

# Username for initiator (to be used in B2C, B2B, AccountBalance and TransactionStatusQuery Transactions)

MPESA_INITIATOR_USERNAME = 'initiator_username'

# Plaintext password for initiator (to be used in B2C, B2B, AccountBalance and TransactionStatusQuery Transactions)

MPESA_INITIATOR_SECURITY_CREDENTIAL = 'initiator_security_credential'

You could also store some configuration in ``settings.py`` and other variables in a ``.env`` file. The library will first attempt to get the configuration variable from ``settings.py``, and if not found it will revert to the os environment configuration (``os.environ``) and if not found it will look for the configuratin in a ``.env`` file.

.. hint::
Expand Down
2 changes: 1 addition & 1 deletion tests/test_b2c_payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_business_payment_success(self):
'''
Test successful business payment
'''

# Wait for a short while (to avoid SpikeArrest)
sleep(20, 'Test business payment success')

Expand Down

0 comments on commit e382ba0

Please sign in to comment.