Sunday, June 13, 2010

Creating a simple MPESA Payment System for your Website - Part 1

This post is meant for Kenyans or anyone familiar with the MPESA mobile payment system in kenya.

Intro
For some reason the folks at Safaricom have never thought of sharing their MPESA API with kenyan web developers. A public MPESA Api could open the door to all sorts of innovative home-made online payment solutions. Just as MPESA has made banks and ATMs irelevant, Credit cards & Paypal would fade away if a 'Pay By Mpesa' option was available at an E-Commerce site.

The MPESA advantage
Credit card and Paypal fail miserably when they are the sole payment systems. Most kenyans would rather use debit cards and have no use for credit cards While Paypal only works with a handful of banks. Actually The last time I checked only Corporative bank debit cards could work with paypal.

This only leaves MPESA as the only viable option.

The MPESA letdown
MPESA has a fabulous automated service for big corporations via the Pay Bill option in the MPESA menu. Unfortunately the requirements for being enrolled in the service simply cannot be met by small fish like you and me. The service was meant for mega-corporations that's why most kenyan sites stick to regular send money option on the mpesa menu.

The locking out of small fish from Pay Bill & the non-existance of an MPESA Api has led to manual & crude methods of accepting payments online. Take the kenyan online book store http://booksfirst.co.ke for example. Here is theer processTo make a purchase using Mpesa.
  1. Customer sends money to the provided number.
  2. An actual human Sales rep(probably holding a mobile phone) receives the money
  3. The customer calls the human sales rep to confirm the payment.
  4. The payment is linked to a partcular customer's purchase.
Wow! The process is long enough to put off any 1st time buyer to your site.

Automating the process - Importance of Identifying the sender.
Back to http://booksfirst.co.ke  let us examine the typical message the sales rep will receive. i.e :

XYZ2B17 confirmed. You have received ksh 2000 from 0723000111.

It is important to note that MPESA outputs the sender's name only when he/she is registered. Otherwise the text would have looked like this :

XYZ2B17 confirmed. You have received ksh 2000 from  Mary Wanjiku 0723000111.

In order to track and link payments, the senders will need to be identified using a unique ID.  As you can see the sender's phone number is the best candidate for a unique Identifying field. The sender's name is not a good candidate as there could be multiple people with the same name.

Also Important to note is that the Receipt or Transaction Id(XYZ2B1) is always unique and received by both parties.

Automating Payment
My solution - Send & Claim
In an online store scenario payments need to be marched to a particular order. Just identifying the sender doesn't help much. So usually a human cashier links the two by calling the sender.
 
The idea is to remove the human cashier at the other end of the transaction. The 'cashier' will be replaced by GSM modem connected to a computer with an internet connection. Whenever an MPESA payment is received, it will be recorded on the site to be claimed by the sender.

The sender will then 'claim' the payment by simply entering the MPESA receipt together with his order. This will be equivalent to entering your credit card number at the checkout page in an online store. Thus an order and a payment can be linked in only 2 simple steps.

1) SEND :
The user sends money using MPESA to the seller's number. This payment is recorded in a database table with the following columns :

MpesaReceiptAmountTimeStampSenderMobileNumberStatus
XYZ2B150014/6/2010 2:45pm?0723000111Un-Claimed

2) CLAIM :
Remember that the MPESA receipt in the confirmation text sent to the user is the same as the one sent to the seller. So now the same user 'claims' the payment entering the Receipt together with his order at the checkout i,e



My Order


Product 1

200/=

Product 2

100/=

Product 3

200/=

Total

500/=


Your Name * If you are not registered.

 


Enter MPESA Receipt





if the receipt submitted matches any un-claimed payment, the matching amount recorded  is used to make the purchase. i.e

MpesaReceiptAmountTimeStampSenderMobileNumberStatus
XYZ2B150014/6/2010 2:45pmMary Wanjiku0723000111Claimed

500/= is claimed by the user & used to make purchase

Putting it all together
As you can see our payment model works. Fully automate the recording of incoming payments and you got a super MPESA payment system.

The Automatic Cashier
Remember the whole idea is removing the human at the other end of an online transaction using MPESA. For this to work, an icoming MPESA payment needs to be automatically recorded on the site. An SMS gateway dedicated to receiving texts on our site would easily solve our problems. But our 'small fish' puts this out of the question.

We will need to come up with a system that any startup can afford. We will exploit the ever affordable GSM modem so common in the market today. Using clever software that we will write, a safaricom modem & an internet connection we will implement a simple, cheap but affordable MPESA payment gateway.


In part two we will create this gateway then later on test it on a mock online bookstore. As always I will be using the ever tasteful combination of C# & AspNet.

See you then. Cheers.

42 comments:

  1. Truth be told, Safaricom is mean, meaner than Microsoft, as a developer, I thought an API to their SMS center was in-order, nothing ever came. Then an API to Mpesa four years down the line is no where, my bet is, if they find people automating MPESA, they will quickly change the SMS... but keep up the good work.

    ReplyDelete
  2. Your audience is anxiously waiting for part two!

    ReplyDelete
  3. Hi,

    What are these requirements for PayBill?
    As far as I can tell the following are the requirements for it:

    Normal MPESA charges apply. The customer is charged Ksh 30 and the business incurs the withdrawal charges as per the MPESA tariff. There is a cost sharing option where the customer pays part of the withdrawal charges

    · No software required just a license which is provided FOC. The system is internet based

    · Must have a company registered account where cash is sent to.

    My question is, if that is the case, why aren't many smaller companies using it? What are these requirements that big fish cannot meet?
    Thanks, will really appreciate a reply and link to partII.

    ReplyDelete
  4. @coderhalisi, have you heard about the ihub?
    http://www.ihub.co.ke/

    ReplyDelete
  5. .... there's a company working to create an API to integrate with m-pesa. it wont be free, but i hear it'll be cheap enough :-)

    ReplyDelete
  6. Hey. CoderHalisi,

    Thank you for the informative article. It is true that there's a gap when it comes to E commerce business in Kenya that Safcom guys seem unwilling to fill using MPESA.

    Check out this site that seems to come up with some sort of MPESA E commerce payment gateway. www.kopokopo.com

    Also, the best example of a Kenyan site that is using the "Send & Claim" Mpesa system is www.eskul.co.ke

    ReplyDelete
  7. Thanks for the insight. This has kick started bright ideas for me and just confirmed that using GSM modems and routers for the safaricom simcards is the way to go. I am doing something similar to what you described.

    ReplyDelete
  8. I am wondering if it would be possible for a webhosting business ( as the one i am running ) to have implemented ? Any ideas will be welcomed !

    ReplyDelete
    Replies
    1. @Favour, have you got a solution? If not, and you're using WHMCS, the we have a tested solution that works for both personal accounts and Paybill business numbers. Just visit https://www.enetonlinesolutions.co.ke/portal/clients/cart.php?gid=26#.VHCsoUDiLcs

      Delete
  9. i thing you should use paybill numbers only.... read safaricoms license first ma friend....

    ReplyDelete
  10. @Coder Halisi..thanks for the info. I am a web designer, you have great content here, can you hire me to refurbish your blog to make the UI and User experience better? I also do custom websites. If so please contact me: 0713 396 827.

    @Favour: I just tried visiting your hosting website and it is showing an Apache server error, please check. Web hosters can use MPESA, what I know is that the currently available companies acting as middle men are too expensive for SMEs, though they claim to be cheap. I am looking forward to a possibility of an SME owner accepting payments to their own pay-bill numbers

    ReplyDelete
  11. @coderhalisi Just a quick one. How does your system detect whether the MPESA message is a valid message from safaricom or some crafty prisoner at KAMITI has crafted a similar message with the unique MPESA code? Since your system relies on matching the MPESA code, it means I can craft an MPESA message and send to the transaction number and it will still register in your database and at the end of it all, I will still purchase the product or service at a very affordable price of Ksh. 1 (The cost of sending the fake MPESA SMS)

    ReplyDelete
  12. Pliz, how can i get access to that souce code?

    ReplyDelete
  13. @Samuel, you can set the modem to only listen to the MPESA number from Safaricom.

    ReplyDelete
  14. As an upcoming web developer am really waiting for safaricom to release API for us if and only if we will meet terms and conditions

    ReplyDelete
  15. Really liked this blog-post - it explains the processes in much more detail than I would have the patience to do ;-)

    I know this is a fairly old article, but seeing that it is still receiving comments as of today, I think the following is valid.

    Take a note of PesaPi ( https://github.com/pluspeople/PesaPi ) it's an open source "middleware" for handling mobile payments. It is intended for exactly the sort of processes described in this blog post.
    It is intended to plug in between your own payment-process and the Telco. Essentially removing much of the trouble of writing and maintaining such code on your own.

    Some of the bennefits of this is:
    - By using that one system/package it will give you a "unified" access to multiple mobile payment systems.
    - When PesaPi is extended/improved you can just upgrade the middleware, and you get new features without any development on your own.
    - You don't have to keep track of all these SMS messages, nor keep an eye on if they change etc - in other words PesaPi is being maintained.
    - It provides you an upgrade path - you can start with your private mpesa account and then if you business takes off you can enable/switch to the paybill - which is also supported - without having to change any code in your solution..
    - etc..

    n.b. for those who asked for the "source code" ... PesaPi is open source - you can view and change all you want ;-)

    ReplyDelete
  16. how can i download that source code

    ReplyDelete
  17. Where is part II? If you don't have it you can contact me (kevosomi@gmail.com), I can help.

    ReplyDelete
  18. It is true that Sfaricom iis meaner than Microsft..

    ReplyDelete
  19. Don't we have elite hackers in kenya to get into safcom servers & databases and expose them(APIs),till saf gives up?

    ReplyDelete
  20. Nice article. I actually us PesaPI.Its a nice API to be honest. with more developers on board ,it will be even better

    ReplyDelete
  21. Good job.
    But I can't find part 2.

    ReplyDelete
  22. This comment has been removed by the author.

    ReplyDelete
  23. 2){$Amount = $Amount.$Am[$loop];}
    }
    $le = strlen($pay1Ex[5]);
    $FN = [];
    $FN = $pay1Ex[5];
    for($lup = 0; $lup < $le; $lup++)
    {
    if($lup > 3){$FirstName = $FirstName.$FN[$lup];}
    }
    $phone = [];
    $phone = $pay1Ex[7];
    for($lp = 0; $lp < 12; $lp++)
    {
    $SenderContact = $SenderContact.$phone[$lp];
    }

    echo $confNo."\n";
    echo $Amount."\n";
    echo $FirstName."\n";
    echo $SecondName."\n";
    echo $SenderContact."\n";
    }
    elseif ($opt == 2)
    {
    $confNo = $pay2Ex[0];
    $SecondName = $pay2Ex[6];
    $LastName = $pay2Ex[7];

    $l = strlen($pay2Ex[4]);
    $Am = [];
    $Am = $pay2Ex[4];
    for($loop = 0; $loop < $l; $loop++)
    {
    if($loop > 2){$Amount = $Amount.$Am[$loop];}
    }
    $le = strlen($pay2Ex[5]);
    $FN = [];
    $FN = $pay2Ex[5];
    for($lup = 0; $lup < $le; $lup++)
    {
    if($lup > 3){$FirstName = $FirstName.$FN[$lup];}
    }
    $phone = [];
    $phone = $pay2Ex[8];
    for($lp = 0; $lp < 12; $lp++)
    {
    $SenderContact = $SenderContact.$phone[$lp];
    }
    echo $confNo."\n";
    echo $Amount."\n";
    echo $FirstName."\n";
    echo $SecondName."\n";
    echo $LastName."\n";
    echo $SenderContact."\n";
    }
    ?>

    ReplyDelete
  24. Amazing, I have been thinking of writing my own system but as of now... let me see how I can utilise the Opensource Pesapi

    ReplyDelete
  25. Please contact me I want one for my website @ smwausi@gmail.com

    ReplyDelete
  26. There is an API for MPESA, or at least as I write this comment it is there now.

    ReplyDelete
  27. good job man ..its a good place for starters

    ReplyDelete
  28. our customers are pay the money using pesapi,
    1.how to check receipt number on custom payment method in magento 1.9.2
    2.If receipt no with product amount is equal we need to place the order 3.how to communicate with your pesapi payment gateway

    ReplyDelete
  29. This comment has been removed by the author.

    ReplyDelete
  30. Hello Every Buddy...
    When will the second part is come ???

    ReplyDelete
  31. you could use gammu-smsd to format the SMS and input it into the database,it also helps to limit 'receivefrom' numbers

    ReplyDelete
  32. SapamaCash.com is a MPESA Paybill and Buy Good Analytics Dashboard that enables merchants to record payments and customer details from MPESA API and analyse these information in infographic charts and graph that give them actionable insights into your customer payment patterns.

    It's something like Safaricom M-Ledger but for Lipa Na Mpesa Merchants.

    To test SapamaCash.com:
    1. Kindly go to SapamaCash.com/login
    2. Login with email: edwin@sapamatech.com
    3. Enter password:123456

    You can send a test transaction of KES 10 to Paybill number 830830 and the transaction will appear automatically on the dashboard.

    If interested, kindly contact me on 0722906835 or edwin@sapamatech.com

    ReplyDelete
  33. This post is really nice and very informative. keep updating your post.

    Ecommerce Payment Gateway

    ReplyDelete
  34. Hi Guys,
    I am a developer.I am currently working on a project and I really appreciate the help.Kudos to all the developers out there trying to create the future.

    ReplyDelete
  35. Are you having a challenge with MPESA C2B Validation and Confirmation steps? Check this out https://github.com/edwinmugendi/sapamapay#c2b-validation-and-confirmation

    ReplyDelete
  36. The best thing is that customer don't need any internet pack and if you want to grow your business fast then bulk sms is best option.Bulk SMS API

    ReplyDelete
  37. Hi Mwogi, yes you can. The MPESA code is always the first. Using a code you can extract the first word or the first ten characters etc.

    ReplyDelete
  38. Casinos Near Me - JSHub
    There are a handful of sites 제천 출장안마 that 하남 출장샵 have dedicated to gambling with mobile 양주 출장샵 app, 울산광역 출장샵 which you can play at the best casino sites in your city. 과천 출장샵 If you are wondering

    ReplyDelete