<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Leonard on Medium]]></title>
        <description><![CDATA[Stories by Leonard on Medium]]></description>
        <link>https://medium.com/@leyo?source=rss-d5b48fb78b19------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*q50W5CXKQpGo04LC.jpg</url>
            <title>Stories by Leonard on Medium</title>
            <link>https://medium.com/@leyo?source=rss-d5b48fb78b19------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 09:55:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@leyo/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[MySQL: 1062 Duplicate entry ‘’ for key ‘group_key’]]></title>
            <link>https://medium.com/@leyo/mysql-1062-duplicate-entry-for-key-group-key-e9deed8d596b?source=rss-d5b48fb78b19------2</link>
            <guid isPermaLink="false">https://medium.com/p/e9deed8d596b</guid>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[mysql]]></category>
            <dc:creator><![CDATA[Leonard]]></dc:creator>
            <pubDate>Thu, 14 Nov 2019 17:07:57 GMT</pubDate>
            <atom:updated>2019-11-14T17:07:57.660Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-NfgBteJ0tzuzx3WSSqW3Q.jpeg" /><figcaption>Source: <a href="https://unsplash.com/photos/fPkvU7RDmCo">https://unsplash.com/photos/fPkvU7RDmCo</a></figcaption></figure><p>This is just another <strong>note to self</strong> for the future.</p><p>Today one of our data intensive apps faced a very weird error on one of the joins in production. Those type of “It was working, now it doesn’t”. No one had touched the system and no fresh deployments had been made.</p><p>It gets to a point in your life where something that was working just decides not to anymore and for no reason. Today was that day and it this 1062 Duplicate entry for ‘group_key’.</p><p>It seems to be a well known MySQL bug that you only face when you hit some threshold in data storage and manipulation. Here is the bug documentation:</p><p><a href="https://bugs.mysql.com/bug.php?id=58081">https://bugs.mysql.com/bug.php?id=58081</a></p><p>Solution?</p><ol><li>Increase your max_heap_table_size</li></ol><p>SET SESSION max_heap_table_size=536870912;</p><p>2. Increase your tmp_table_size</p><p>SET SESSION tmp_table_size=536870912;</p><p>For us, that still didn’t solve the problem and we had to:</p><p>Increase /tmp size.</p><p>How? mount -o remount,size=5G /tmp/</p><p>Back to &lt;code&gt;</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e9deed8d596b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Strongswan apparmor=”DENIED” operation=”open” Fix]]></title>
            <link>https://medium.com/@leyo/strongswan-apparmor-denied-operation-open-fix-4d07a11a7153?source=rss-d5b48fb78b19------2</link>
            <guid isPermaLink="false">https://medium.com/p/4d07a11a7153</guid>
            <category><![CDATA[strongswan]]></category>
            <category><![CDATA[vpn]]></category>
            <dc:creator><![CDATA[Leonard]]></dc:creator>
            <pubDate>Wed, 30 Oct 2019 12:20:12 GMT</pubDate>
            <atom:updated>2019-10-30T12:20:12.101Z</atom:updated>
            <content:encoded><![CDATA[<p>This is more a note to my future self on Strongswan VPN config error:</p><p>The following is the fix:</p><p>apt-get install -y apparmor-utils<br>aa-complain /usr/lib/ipsec/charon<br>aa-complain /usr/lib/ipsec/stroke</p><p>Ubuntu 18</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4d07a11a7153" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sniff HTTP Traffic to a specific port]]></title>
            <link>https://medium.com/@leyo/sniff-http-traffic-to-a-specific-port-e1c2bba7bb48?source=rss-d5b48fb78b19------2</link>
            <guid isPermaLink="false">https://medium.com/p/e1c2bba7bb48</guid>
            <category><![CDATA[tcpdump]]></category>
            <category><![CDATA[sms]]></category>
            <category><![CDATA[https]]></category>
            <category><![CDATA[vpn]]></category>
            <category><![CDATA[ussd]]></category>
            <dc:creator><![CDATA[Leonard]]></dc:creator>
            <pubDate>Thu, 13 Jul 2017 12:25:39 GMT</pubDate>
            <atom:updated>2017-07-27T19:21:30.062Z</atom:updated>
            <content:encoded><![CDATA[<p>This is more of a note for me to reference in future.</p><p>tcpdump -s 0 -A ‘tcp dst port 80 and (tcp[((tcp[12:1] &amp; 0xf0) &gt;&gt; 2):4] = 0x504f5354)’</p><p>This simpler one also works:</p><p>tcpdump -n dst port 80</p><p>Other useful commands when troubleshooting network:<br><a href="https://rationallyparanoid.com/articles/tcpdump.html">https://rationallyparanoid.com/articles/tcpdump.html</a></p><p>I just completed integration to one of the PRSPs (Premium rate service providers) in Kenya for Mobile VAS services i.e. USSD and SMS services. Mostly PRSPs don’t usually have weird connectivity requirement and you would just make a HTTP/SMPP request to them for SMS and USSD.</p><p>But this particular was special and they required a VPN between our server and theirs and some other weird requirements that I will rant about in future.</p><p>So we configured the VPNs and completed the network requirements and connectivity and went straight to consuming and sending requests and that is where things became tough.</p><p>For HTTP traffic I use <a href="https://www.getpostman.com/">POSTman</a> to simulate external traffic and it has never disappointed. But here was a situation where a request from POSTMan had a <a href="https://httpstatuses.com/200">200 response</a> while a request from our partner was resulting in a<a href="https://httpstatuses.com/400"> 400</a>.</p><p>Of course debugging begins:</p><p>Application level Debugging:</p><ul><li>Check routes/Endpoints — Okay</li><li>Check access and error logs — Nothing weird</li></ul><p>Host Level:</p><p>We are running apache 2.4.7 on an ubuntu server and so:</p><ul><li>Check virtualhosts (Default and custom) — All okay</li><li>Check apache logs (Access and Error) — (400 Bad request noted)</li></ul><p>Network level:</p><p>In order to check the requests at the network level, the following tcpdump command helped</p><p>tcpdump -s 0 -A ‘tcp dst port 80 and (tcp[((tcp[12:1] &amp; 0xf0) &gt;&gt; 2):4] = 0x504f5354)’</p><ul><li>-s 0: Don’t limit the amount of payload data that is printed out. Print it all.</li><li>-A: Print packets in ASCII</li><li>and tcp port http: Only capture HTTP packets.</li><li>This is where I noted difference in HTTP headers being forwarded.</li></ul><p>As per RFC Standards:</p><pre>A client MUST include a Host header field in all HTTP/1.1 request messages. An HTTP/1.1 proxy MUST ensure that any request message it forwards does contain an appropriate Host header field that identifies the service being requested by the proxy. All Internet-based HTTP/1.1 servers MUST respond with a 400 (Bad Request) status code to any HTTP/1.1 request message which lacks a Host header field</pre><p><a href="https://tools.ietf.org/html/rfc2616#section-14.23">RFC 2616 - Hypertext Transfer Protocol -- HTTP/1.1</a></p><p>Difference between POSTMan and external traffic were mainly the Host Header hence the 400 request.</p><p>After fixing the Host header we were back in business!</p><p>Back to &lt;code&gt;</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e1c2bba7bb48" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tokenization Part 2 — Access Tokens at work]]></title>
            <link>https://medium.com/@leyo/tokenization-part-2-access-tokens-at-work-79ccaed3aa96?source=rss-d5b48fb78b19------2</link>
            <guid isPermaLink="false">https://medium.com/p/79ccaed3aa96</guid>
            <category><![CDATA[fintech]]></category>
            <dc:creator><![CDATA[Leonard]]></dc:creator>
            <pubDate>Wed, 24 May 2017 10:34:58 GMT</pubDate>
            <atom:updated>2017-06-18T06:31:00.995Z</atom:updated>
            <content:encoded><![CDATA[<p>This is a follow up on the <a href="https://medium.com/@leyo/tokenization-how-to-get-a-token-on-equity-banks-eazzy-api-and-safaricom-s-m-pesa-api-apigee-641827315e1c">part 1 on tokenization</a>. We’ll mainly focus on access tokens at work and we’ll imagine a few other opportunities that will come up due to this.</p><p>So lets jump right in and see some examples of access tokens at work.</p><p><strong>Facebook:</strong></p><p>Ever had an application just post to facebook on your behalf? If so, at some point in your life, you must have allowed it to do so, most probably via one of those click baits like ‘Find-out-when-you-will-get-married’ or ‘How-you-will-look-when-old’ links</p><p><strong>KCB M- PESA:</strong></p><p>I don’t have full details on this but one way that KCB is able to debit money from MPESA directly without requiring you to enter PINs (USSD Push Service PINs or again<strong> soon to be released</strong> STK Push) should be through some form of tokenization. You give KCB access and they have ability to move money without requiring another PIN.</p><h3>Imagining future products due to Tokenization of M-PESA/Eazzy API:</h3><p>(Feel free to steal these ideas or give me a call (0728355429) or email (leo[at]devs[dot]mobi) we build them up.</p><p><strong>Mobile money (M-PESA) for School children.</strong></p><p>If someone asked me/you why M-PESA isn’t being used by every child in Kenya, it would probably boil down to:</p><ol><li>They are not of legal age and hence don’t have IDs, hence cannot register for mobile money services</li><li>They don’t have or aren’t allowed to own a phone</li></ol><p>However:</p><ol><li>Children are allowed to have access to pocket money whether self-stored or with the school bursar.</li><li>Children are allowed to transact in the school canteens</li><li>Children are allowed and encouraged to save or develop a saving habit when they are still young.</li></ol><p><strong>a) Tokenization can help extend mobile money services further down to schools by:</strong></p><ul><li>Enabling a parent give some <strong>limited</strong> <strong>access </strong>to their mobile money wallet to be drawn/used by selected pre-authorized POS for a limited(capped) transaction value. Main point here is to ensure the transaction is PINless as the parent and child will be detached and away from the parent.</li><li>Enabling a child use the access to transact at selected points of sales with capped amounts. The usual form of access could be in form of a card like MPESA 1 tap (without the PIN bit) etc.</li></ul><p><strong>b) Full Tokenization can help automate recurrying bill payments</strong></p><ul><li>Currently mobile money bill payments require that you go through the several steps of paying via Paybill say (MPESA, Paybill, Business number, Account number, amount, PIN, confirm)</li><li>The above process can be used the first time to establish a ‘soft contract’ the first time a recurrying bill is being settled. An access token can then be generated to be used on subsequent payments to the same service provider.</li><li>This will help some services like automated HELB Payments, Water, Internet, Electricity, Insurance Premiums, Loan repayments, rent etc to be possible using Mobile money. It is already happening with bank payments and online payment providers like Paypal so it shouldn’t have that much friction to implement.</li></ul><p><strong>c) On-demand services, subscription services and Pay-on-the-go services.</strong></p><p>September 2014, I had the priviledge of attending Israel’s <a href="http://dldtelaviv.com/">DLD Innovation Week</a> in Tel Aviv. One think that struck me was the <a href="https://www.youtube.com/watch?v=55QQJ97ZZ1Y">Tel-o-fun Bike Sharing system</a>. At the core of the bike sharing system is an automated payment processing system and the following is my attempt at describing how it works.</p><p>The Bike sharing system consists of a network of roughly 2,000 heavy-duty aluminum bikes with 180 docking stations distributed across Tel aviv.</p><ul><li>A potential rider, inserts their debit/credit card at the docking station for validation.</li><li>Once validated, the card is charged a daily fee (17 Shekels then).</li><li>Rider selects available bikes from a user interface and unlocks it</li><li>Bike unlocks and rider has up to 30 minutes of continuos ride to move from one point of the city to the other</li><li>Rider docks the bike on the nearest docking station next to his/her destination.</li><li>Time counter stops once the bike is successfully docked.</li></ul><p>The bike sharing system is designed to encourage sharing and discourage securing resources(bikes) when they are not in use. This is mainly achieved as there is an automatic extra charge additional minutes beyond the 30 minutes allocated.</p><p>Services like this bike sharing, will be possible in Nairobi if we have an automated way of billing a client. The key problem of course of potential delay in return of bicycles or even rogue riders can be addressed by a combination of cators and an automated deduction of funds from mobile money is one key one.</p><p>Other services like M-Kopa Solar will have a way easier time collecting payments from clients and we should even see way more Pay-As-You-Go products in the market.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=79ccaed3aa96" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tokenization]]></title>
            <link>https://medium.com/@leyo/tokenization-how-to-get-a-token-on-equity-banks-eazzy-api-and-safaricom-s-m-pesa-api-apigee-641827315e1c?source=rss-d5b48fb78b19------2</link>
            <guid isPermaLink="false">https://medium.com/p/641827315e1c</guid>
            <category><![CDATA[oauth]]></category>
            <dc:creator><![CDATA[Leonard]]></dc:creator>
            <pubDate>Tue, 23 May 2017 20:44:08 GMT</pubDate>
            <atom:updated>2017-06-18T06:23:40.664Z</atom:updated>
            <content:encoded><![CDATA[<p>Today, I got a query from somebody on how to go about getting an <strong>access token</strong> to use with Equity Bank’s Eazzy <a href="http://developers.equitybankgroup.com/">API</a>. It got me thinking hard about the entire tokenization concept.</p><p><a href="https://www.youtube.com/watch?v=BXUZ8MfvOIY&amp;feature=youtu.be">Here</a> is a step by step guide on how to go about it so if you’re here for that, you can skip all the nonsense below.</p><p>Right?</p><p>I guess I’m left with 70–80% of the readers who have ~9 minutes.</p><p><strong>Apigee and access tokens</strong>.</p><p>For those not aware, Equity bank <a href="http://cio.co.ke/news/top-stories/equity-bank%27s-apis-are-now-open-to-developers">released/exposed</a> Eazzy APIs to developers sometime last year. The API sandbox and documentation is available <a href="https://developers.equitybankgroup.com/apis">here</a>.</p><p>Safaricom simlarly exposed APIs as part of MPESA G2 platform a couple of years ago. But Devs and businesses <a href="https://medium.com/@kmarima/safaricom-integration-nightmare-e99a1f6bf893">complained</a> about the complexities involved in setting this up and getting it to run or at least give you an error. It still takes a first time integrator close to <strong>3 months</strong> to get connected.</p><p>This is changing and the new platform is currently on closed alpha/beta. <a href="https://medium.com/@leyo/mpesa-g2-api-integration-c2b-b2c-b2b-online-checkout-step-by-step-guide-part-1-58f3a90d4daa">All this</a>, or most of it, will be reduced to an <strong>access token.</strong></p><p>But what’s is this animal called an <strong>access token</strong>?</p><h3><strong>Tokens</strong></h3><p>Have you ever given someone a ‘token’ of appreciation? Have you ever thought about why you use the word <strong>token</strong> in that phrase? What would you have done if you did not have the token? Hold that thought right there.</p><p><strong>Human beings and being protective.</strong></p><p>I’ve always told myself that if I were to go back in time, I would have studied psychology before Maths/Statistics/probability and eventually software development. So much work goes into understanding and putting human natural way of life into software development. Do you ask yourself why we’re constantly trying to create robots that look like humans?</p><p>Human beings have always <strong>protected with all their might, that which is dear or valuable to them</strong>.At the same time, we get in crazy situations that require we give access (sometimes<strong> full rights</strong>) to that which we consider very dear to us in order to benefit in some way or the other. However, we have progressively found ways to minimize the risk of giving out <strong>full access</strong> to what we hold dear.</p><p>Okay forget that Psychology 101 stuff.</p><h3><strong>Consider a car, your car:</strong></h3><p>Say you have worked hard/smart, saved enough money and finally bought a good Japanese/German or a zero mileage Kenyan <a href="http://mobiusmotors.com/mobius-II/">Mobius II</a> machine. The natural next step is to <strong>protect it.</strong> Most of the time you’ll do all sorts of crazy stuff. First the number of gadgets to protect it: Trackers, alarms etc then as if that is not enough, you go ahead and engrave the number plate on all side mirrors, windows, windscreen, engine, lights… you name it (But why?)!</p><p>You drive it in these dusty Kidero roads and soon you realise it needs a wash and you don’t have enough time to do thorough cleaning and it comes a time to take it to the <strong>car wash</strong>. You are requested to leave the keys behind and hesitantly you hand over, but you decide to buy a drink close by so that you have a full watch over it as it gets cleaned.</p><p><strong>Access:</strong></p><p>The car wash guys need the keys so that they have <strong>full access</strong> to the vehicle for cleaning. They also need to move the car from time to time to different location for number or reasons including to dry or to access different areas.</p><p>Your first and second wash you watch keenly so that they don’t steal or mess up those ‘ninis’ on the ‘hood’ or steal the Gym shoes kwa trunk!</p><p>By the 3rd wash, you drop the car and leave the <strong>keys, </strong>they call you they are done.</p><p><strong>Progressively, </strong>manufacturers have found ways to allow some form of access to the car without the keys. Keys got wireless technology embeded and cars can be unlocked from a distance and give the car wash guys <strong>temporary access</strong> .</p><p>This <strong>temporary access</strong> is very close to what it means to have an<strong> access token</strong>.</p><p>Okay so far? So where were we?</p><p><strong>Oh yeah, Tokens</strong></p><p>The recorded <a href="https://en.wikipedia.org/wiki/Tokenization_(data_security)#Concepts_and_origins">history of <strong>tokens</strong></a><strong> </strong>dates back to the first forms currencies.</p><p>“<em>The concept of tokenization, as adopted by the industry today, has existed since the first </em><a href="https://en.wikipedia.org/wiki/Currency"><em>currency</em></a><em> systems emerged centuries ago as a means to reduce risk in handling high value </em><a href="https://en.wikipedia.org/wiki/Financial_instrument"><em>financial instruments</em></a><em> by replacing them with surrogate equivalents</em>”</p><p><em>“In more recent history, subway tokens and casino chips found adoption for their respective ecosystems to replace physical currency and cash handling risks such as theft”</em></p><p><strong>Access tokens on Eazzy Banking API and M-PESA API.</strong></p><p><a href="https://medium.com/@leyo/mpesa-g2-api-integration-c2b-b2c-b2b-online-checkout-step-by-step-guide-part-1-58f3a90d4daa">All this</a>, or most of it, may eventually be reduced to an <strong>access token.</strong></p><p>Unbelievable right? All those hours spent on setting up VPNs, sending emails to several internal teams to just get something as simple as a c2b payment trigger is about to disappear like that.</p><p>Since the new M-PESA API is not out, lets focus more on Eazzy banking today and how to get the access token. It is good to start with understanding the RFC 6749 <a href="https://tools.ietf.org/html/rfc6749#section-1.2"><strong>OAuth</strong></a><strong> </strong>and<strong> Protocol flow.</strong></p><p><strong>OAuth:</strong></p><p>“<em>The OAuth 2.0 authorization framework enables a third-party application to obtain limited access to an HTTP service, either on behalf of a resource owner by orchestrating an approval interaction between the resource owner and the HTTP service, or by allowing the third-party application to obtain access on its own behalf</em>”</p><p>OAuth 2.0 replaced <a href="https://tools.ietf.org/html/rfc5849">oAuth 1.0</a> RFC 5849 in October 2012 as the standard for enabling a 3rd party (car wash guy) obtaining <strong>limited/temporary access(entry to car) </strong>from and act on behalf of a <strong>resource owner (car owner).</strong></p><p><a href="https://apigee.com/api-management/#/homepage"><strong>Apigee:</strong></a></p><p><a href="https://apigee.com/api-management/#/homepage">Apigee</a>, now part of Google, is an API management platform that powers the the Eazzy Banking API and soon to be realised Safaricom M-PESA API.</p><p><strong>oAuth Protocol Interaction flow</strong></p><p>Protocol flow is as follows(<em>source IETF documentation</em>)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/992/1*fi1w6GQKK2YdCPJa6-UCVQ.png" /></figure><ul><li>A client request for authorization from the resource owner and gets granted.</li><li>With the permission of the resource owner, the client request for an access token from the authorization server and receives an access token</li><li>Client accesses protected resource</li></ul><p>With our analogy, it would be <strong>something close to:</strong></p><ul><li>Car wash guy asks for your key and you give it to him</li><li>Car wash guy presses the unlock button and opens the car before returning the key</li><li>Car wash guy gains temporary access to previously protected areas</li></ul><p><strong>Eazzy APIs</strong><br>- Click</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gqbkKu048BNFhcZytYY-Gw.png" /></figure><p>Equity Bank exposes the following APIs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0ZVz9EVNm9B-8QdyciiWrg.png" /></figure><p>Use the Identity Api to gain access to the view to request the access token.</p><p>Click on Get Merchant access token:</p><p>Then follow the process:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FBXUZ8MfvOIY%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DBXUZ8MfvOIY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FBXUZ8MfvOIY%2Fhqdefault.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb850bdee17ab3f0b8165be8b1c48609/href">https://medium.com/media/eb850bdee17ab3f0b8165be8b1c48609/href</a></iframe><p>Then use the access token to handle the Transactional APIs</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_zufJhZP3iCgcTlD8zUf5A.png" /></figure><p>Okay enough for today.</p><p>UPDATE : <a href="https://medium.com/@leyo/tokenization-part-2-access-tokens-at-work-79ccaed3aa96">Part 2 on impact of tokenization</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=641827315e1c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Laravel:: Getting average time between created_at and updated_at for a number of records in a table]]></title>
            <link>https://medium.com/@leyo/laravel-getting-average-time-between-created-at-and-updated-at-for-a-number-of-records-in-a-table-41a1403808aa?source=rss-d5b48fb78b19------2</link>
            <guid isPermaLink="false">https://medium.com/p/41a1403808aa</guid>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[mysql]]></category>
            <dc:creator><![CDATA[Leonard]]></dc:creator>
            <pubDate>Sat, 21 Jan 2017 00:04:13 GMT</pubDate>
            <atom:updated>2017-01-21T00:07:08.406Z</atom:updated>
            <content:encoded><![CDATA[<p>So this is primarily intended to be documentation for personal consumption and reference in future but may be appropriate for anyone else who faces a similar data issue.</p><p>Today I spent quite some time on a problem that I thought was quite simple but actually took a number of hours to solve.</p><p><strong>Problem description:</strong><br>At <a href="http://devs.mobi/"><strong>Mobidev</strong></a><strong>,</strong> as our name suggests, our day to day work involves building mobile applications for clients, mostly enterprise market. For the past 5 years we have built robust SMS, USSD, Android, iOS and Windows application together with their respective administrative dashboards.</p><p>For the past year, I have been part of the team maintaining a mobile health system for a client that helps patients adhere to medication. Maybe one day I will get time to get into the nitty gritties of how the system works and how we’ve experimented pushed the limit of how USSD can be used specifically for stuff like chat, mailboxes, surveys, product tours etc.</p><p>One of the requests that came through from our client was for a report on the average time taken for a patient to verify that they have taken medicine. To understand this better, basically when it is a patient’s time to take medication, say 7pm, we have a <a href="https://laravel.com/docs/5.3/scheduling">task scheduled</a> to send an SMS notification and log it in a ‘patient_compliance’ table with status 0. They respond by dialing a USSD code and selecting 1 for ‘Yes’ or 2 for ‘No’ to indicate whether or not they have taken the medication. If they respond ‘Yes’, the log status is updated to 1 and of course the updated_at field that keeps track of the time this update was made is also is updated automatically.</p><p>The specific challenge was the need to generate the average time taken between notification and treatment verification which technically translates to time difference betweent created_at and updated_at timestamps.</p><p><strong>Sample db data:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*No6lhNG1cruRis3Bx4LN0A.png" /></figure><p>Researching online led me to <a href="http://qiita.com/ryo511/items/ebcd1c1b2ad5addc5c9d">chunk and cursor</a> that seemed a good way to achieve this for a fairly large datasets. Execution times and memory requirement were key things that I was looking at and chunk and cursor promised the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*Wuac40FIJ4xN6-SOx0gzWg.png" /></figure><p><strong>Solution:</strong></p><p>So the following implementation seemed okay:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XJEApciOqJxM1aUcsKFzyA.png" /></figure><p>That would have worked well but of course you still have the overhead of iterating over a number records at runtime and this would still choke the system at some point in time.</p><p>A much faster and efficient way was to move the calculation to database and I settled on the following solution which solved the problem at hand</p><pre>$time_difference = DB::<em>select</em>(DB::<em>raw</em>(&quot;SELECT <em>AVG</em>(TIME_TO_SEC(updated_at) - TIME_TO_SEC(created_at)) as time<br>                              FROM `patient_compliance`<br>                                WHERE `status`=1 AND `created_at` &gt;= &#39;$real_from&#39; AND `created_at` &lt;= &#39;$to&#39;&quot;));</pre><p><strong>Conclusion:</strong></p><p>When faced with a data issues when using relationtional databases, try as much as possible to calculate at the database level unless your have no option of doing that.</p><p>Till next time!<br>Back to &lt;code&gt;</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41a1403808aa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MPESA G2 API Integration (C2B | B2C | B2B | Online Checkout ) STEP BY STEP GUIDE- Part 1]]></title>
            <link>https://medium.com/@leyo/mpesa-g2-api-integration-c2b-b2c-b2b-online-checkout-step-by-step-guide-part-1-58f3a90d4daa?source=rss-d5b48fb78b19------2</link>
            <guid isPermaLink="false">https://medium.com/p/58f3a90d4daa</guid>
            <category><![CDATA[safaricom]]></category>
            <category><![CDATA[integration]]></category>
            <category><![CDATA[mpesa-api]]></category>
            <category><![CDATA[mpesa]]></category>
            <dc:creator><![CDATA[Leonard]]></dc:creator>
            <pubDate>Mon, 01 Aug 2016 21:12:59 GMT</pubDate>
            <atom:updated>2016-08-01T21:20:37.773Z</atom:updated>
            <content:encoded><![CDATA[<p>Well, if you are here, you must be one of the people who have faced <a href="https://medium.com/@kmarima/safaricom-integration-nightmare-e99a1f6bf893">the pain of integrating</a> to M-PESA via the <a href="http://www.safaricom.co.ke/business/corporate/m-pesa-payments-services/m-pesa-api">G2 API</a>. Trust me, for over 5 years I have lead the team at <a href="http://devs.mobi/">Mobidev</a> where we have integrated to Telcos (Vodacom, MTN Uganda, MTN Nigeria, Etisalat Nigeria, Airtel and Safaricom) and several banks too including Barclays Cybersource API and I have not faced a more painful process than integrating to M-PESA.</p><p>This is an attempt at a step by step guide on how to go about it right from applying for a paybill to deploying your payment solution to production. This is drawn from my experience integrating C2B, B2C, B2B and Online Checkout APIs for a number of clients.</p><p>I will assume ubuntu 14.04 or *nix server (LAMP Stack) and SSH access to server.</p><p>This may not work for everyone (especially Windows people) and you may not need to read everything so take whatever suits you and move on!</p><h3><strong>So what should we expect?</strong></h3><p>The following is some sort of ‘course outline’ of what we’ll cover:</p><p><strong>Pre-Integration:</strong></p><ul><li>Applying for a Paybill</li><li>Completing the customer readiness questionaire</li><li>Completing the VPN form</li><li>Safaricom CRQ process and expectation</li></ul><p><strong>Establishing Connectivity over VPN:</strong></p><ul><li>VPN Setup on your server (We’ll use Ubuntu and OpenSwan)</li><li>Connecting your VPN to Safaricom using the Pre-Shared Key</li><li>Understanding Subnets and Routing table</li></ul><p><strong>SSL manenoz:</strong></p><ul><li>Generating a 2048kb Self-signed certificate</li><li>Request for signing from Safaricom Security team</li><li>Extracting M-Pesa API Certificate and using it in your server</li></ul><p><strong>C2b:</strong></p><ul><li>What to do with that crazy UAT excel sheet</li><li>User acceptance tests over http</li><li>Registering validation and confirmation URLs to M-PESA Test Server</li><li>User acceptance tests over https</li><li>Moving to production</li></ul><p><strong>B2C:</strong></p><ul><li>Again, what to do with that crazy B2C UAT excel sheet</li><li>User acceptance test over http</li><li>UATs over https</li><li>Moving to production</li></ul><p><strong>B2B</strong></p><ul><li>Same as <strong>B2C </strong>with minor changes</li></ul><p><strong>Online Checkout</strong></p><ul><li>There are lots of resources online already for online checkout so we’ll briefly just touch on this or point to some of the resources.</li></ul><p>As you can see, this will be a lot of work and I can’t promise to write all the documentation as fast as the community would wish due to other ongoing projects and this may take a while to complete.</p><p>I am however available for consultancy in case of any urgent need for integration. Just email leo[at]devs.mobi or mpesa[at]billing.co.ke</p><p>See you in part 2 but before then you can prepare by:</p><ol><li>Setting up an ubuntu server (<a href="http://aws.amazon.com/">AWS</a> or <a href="http://digitalocean.com/">DigitalOcean</a> should sort you out).</li><li><a href="http://www.safaricom.co.ke/images/Downloads/Resources_Downloads/M-PESA_API_Guide_download.zip">Download M-PESA Documentation</a></li><li>Read on <a href="https://en.wikipedia.org/wiki/IPsec">IPsec</a></li></ol><p>&lt;/Happy Coding&gt;</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=58f3a90d4daa" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>