Saturday, July 6, 2019

The fastest way for developers to integrate the {daraja} MPESA APIs using Bees (www.bees.software)

Over the course of a year, during the development of Nectar (https://www.nectar.software), one of the most frustrating components was the integration of MPESA as a payment option. And it wasn't because of the technical aspects; the MPESA APIs have a fairly well documented developer portal with details on its REST API endpoints. Rather, it was the arduous manual set up procedure that required a developer to engage with the MPESA Business support team for manual configuration as well as the verbose and unnecessary aspects of the API itself.

In case, you are wondering what Nectar is, Nectar is the most performant STS (IEC 62055-41) compliant prepaid water, electricity and gas cloud based SaaS available. It allows service providers offering water, electricity and gas services to get payment for these services from their customers *before* usage at zero cost. This way, the service providers do not have to read meters or disconnect services while customers pay for exactly what they need and cannot afford. More details are available at https://www.nectar.software. Clearly, in Kenya, allowing payment for services using MPESA using the MPESA APIs was absolutely necessary.

In total, it took me 3 months of back-and-forth with the MPESA Business team to get MPESA integration working. During this time, I had to incorporate a company, open a bank account with a major bank and modify the MPESA short code from a Till No to a Paybill No, the latter being that a reference (really a short additional note) can be added per transaction. Various APIs also required mundane information for example the Lipa Na MPESA API that requires a 'Timestamp' to record when the transaction was initiated. For most instances, I imagine an code/auto-generated 'Timestamp' would be sufficient.

While I understand MPESA's need to prevent money laundering via their API, it seemed that for a casual developer hoping to integrate the {daraja} MPESA APIs for a minimal number of payments, the task has been made particularly and unnecessarily complex.

And hence, Bees© was created. Bees© is a platform that provides REST endpoints allowing developers to integrate MPESA APIs into their applications in mere minutes.  Bees© has already obtained Safaricom MPESA G2 API integration. Using this integration, Bees© provides a succinct, intuitive and easy to use wrapper API to allow developers to use the Safaricom MPESA G2 APIs in minutes. Using this Bees© wrapper API, developers create the required Bees© credentials and withdrawal accounts before making REST API calls to trigger STK pushes on their target customers' MPESA-enabled devices or query C2B transaction statuses.

Using Bees© (https://www.bees.software) is simple and requires the following steps that can be done using CuRL requests or the Bees© SDKs.

Create a Bees© account

To create a Bees© account, a developer can either use the registration form available at https://www.bees.software or make a REST request to the Bees© MPESA API. This Bees© REST returns a user_ref to be used in calls to create credential pairs.

curl --request POST \
    --url https://bees.software/v1/user \
    --header 'content-type: application/json' \
    --data '{
             "first_name":"John",
             "last_name":"Doe",
             "username":"username",
             "password":"password",
             "phone_no":"0700100100",
             "email":"info@email.com",
             "image_url":"https://s3.aws.com/avatar.jpeg"
         }'

Or using Bees© Python SDK

bees.get_user_factory().create_user(first_name='John',
                                    last_name='Doe',
                                    username='username',
                                    password='password',
                                    phone_no='0700100100',
                                    email='info@email.com',
                                    image_url='https://s3.aws.com/avatar.jpeg')

Create a Bees© credentials key pair

A credentials key pair is used to authenticate REST API calls and must be kept confidential. To create or delete Bees© credentials (key & secret pairs), a developer must use the profile page in this management portal. Each set of credentials consists of a consumer key and consumer secret with selected permissions.

Create a Bees© withdrawal account

A withdrawal account is an MPESA enabled mobile number or bank account with a Safaricom Paybill No that receives a transaction payment initiated using a Bees© REST API call. To create a Bees© withdrawal account, a developer can either use the profile page or make a REST request to the Bees© MPESA API. The list of supported banks and bank_refs can be obtained using the Bees© MPESA APIs. All transactions (STK Push & C2B) will have their value minus a 3% transaction charge remitted to the specified withdrawal account within 24 hours. No other charges are paid to use the Bees© MPESA APIs.

curl --request POST \
    --url https://bees.software/v1/account/ \
    --header 'authorization: BEES deccf75f6e941e95df6...VhODM1YjRlNDYyMGY1NzY3YjllYWU5OA==' \
    --header 'content-md5: 3A416975F48B8C5E1C2157484091441B' \
    --header 'content-type: application/json' \
    --header 'date: Thu, 17 Nov 2019 18:48:58 GMT' \
    --header 'nonce: 5efdf6947f14325d490bd1d67a93940d' \
    --data '{
             "bank_ref":"3ec0192b733cd6d65bd9f5543dd41e5f",
             "account_no":"123456789"
       }'
             

Or using Bees© Python SDK

bees.get_account_factory().create_account(bank_ref='3ec0192b733cd6d65bd9f5543dd41e5f',
                                          account_no='123456789')
                            

Initiate Bees© REST API payment call

To initiate an STK payment push to a mobile device, use a Bees© credentials (key & secret pair) pair and withdrawals account to prompt payment. The customer whose phone number is included in this request will observe the MPESA STK Push applet initiated on their mobile device with the payment details configured in the API call. Once completed, the transaction status and result of this STK push will be submitted by Bees© to the callback_url specified in this request. The account_ref in this API call must be associated to a valid withdrawal account.

curl --request POST \
    --url https://bees.software/v1/promptSTKPushPayment \
    --header 'authorization: BEES deccf75f6e941e95df6073497214c266:OTZkZm...2YxNDNmZTRiZDAwMQ==' \
    --header 'content-md5: F3216235710FA290C6508C0BFA8B4041' \
    --header 'content-type: application/json' \
    --header 'date: Thu, 17 Nov 2019 18:48:58 GMT' \
    --header 'nonce: 2659c837e161e039ecf23fe47e6db42f' \
    --data '{
             "payment_type":"mpesa_stk_push",
             "payer":"254703133896",
             "callback_url":"https://nectar.software/log.php",
             "displayed_desc":"Sweater",
             "full_desc":"Payment for Sweater",
             "amount":"100",
             "account_ref":"5774a10f60414b67f12abb105235479e",
             "env":"live"
        }'
           

Or using Bees© Python SDK

bees.get_stkpushtransactions_factory().prompt_stkpush_transaction(payment_type=PaymentType.mpesa_stk_push,
                                                                  payer='254703133896',
                                                                  callback_url='https://nectar.software/log.php',
                                                                  displayed_desc='Sweater',
                                                                  full_desc='Payment for Sweater',
                                                                  amount=100,
                                                                  account_ref='5774a10f60414b67f12abb105235479e',
                                                                  env=Env.live)

Alternatively, the Bees© API can be used to poll payments made directly to the short code 826588 for C2B transactions. This creates asynchronous call whose results will be sent to the callback_url specified once the status of the C2B transaction is obtained.

curl --request POST \
  --url https://bees.software/v1/transactions/poll \
  --header 'authorization: BEES deccf75f6e941e95df6073497214c266:NzExOWFlYzEwOW...jY4YWZhNTcwMDg1NjRkZmE3MTlkMA==' \
  --header 'content-md5: D41D8CD98F00B204E9800998ECF8427E' \
  --header 'content-type: application/json' \
  --header 'date: Thu, 17 Nov 2005 18:48:58 GMT'
  --header 'nonce: 815c7150b00b64a934a8162b711650de' \
  --data '{
           "payment_status_type":"mpesa_poll",
           "transaction_id":"NEN14BDQZH",
           "callback_url":"https://nectar.software/log.php",
           "env":"live"
  }'

Or using Bees© Python SDK

bees.get_c2btransactions_factory().poll_transaction(payment_status_type=PaymentStatusType.mpesa_poll,
                                                    transaction_id='NEN14BDQZH',
                                                    callback_url='https://nectar.software/log.php',
                                                    env=Env.live)

Details on Bees© can be obtained at www.bees.software and the SDKs found at https://bitbucket.org/account/user/nectar_co_ke/projects/BS

Bees© dashboards

Bees© has a number of dashboards that allow developers to keep track of the number of STKPush and C2B transactions initiated or referenced to their credentials. Dashboard charts and elements include
  1. Generated transaction revenue
  2. Number of transactions
  3. Devices used when using the Bees© APIs
  4. SDK/REST API usage
  5. Location from which requests are initiated
Log in to the Bees© dashboard to experience a better way to manage your {daraja} M-PESA API transactions.


feature