Interesting Hack for PesaPal?

I recently had to integrate PesaPal in a very interesting way, needing me to think out of the box – as usual. To all those who know PesaPal  – integration wise, you are familiar with the way we simply iframe in the $iframe_src we come up with via the standard integration methods. What you may not know is that should you already be in an iframe when before calling in the PesaPal iframe – so that you have the pesapal iframe within another iframe – you break PesaPal functionality.

The functionality affected here is the callback URL. Since in this situation, the PesaPal iframe is already within an iframe, the callback URL which I have come to believe is designed to jump out of one iframe will not work seeing as you will be two iframes in. And by will not work, I mean that you will simply not be redirected to your callback URL.  You will however, get the ‘You are being redirected’ message, with a link to the URL you are supposed to be redirected to. I suppose a user can click that link and get redirected to your callback URL in a new tab or window (depending on your browser) but in my case, my client was not too happy with the message “You are being redirected…” and the actual redirection was actually not happening.

In a bid to follow my clients orders to “fix it!” here is what I came up with.

After getting your relevant $iframe_src, and noting the fact that I was already working in an iframe, instead of calling in another iframe, I simply got the contents of the generated $iframe_src and echoed it.

…
$iframe_src = OAuthRequest::from_consumer_and_token($consumer, $token, "GET", 
                                                    $iframelink, $params);
$iframe_src->set_parameter("oauth_callback", $callback_url);
$iframe_src->set_parameter("pesapal_request_data", $post_xml);
$iframe_src->sign_request($signature_method, $consumer, $token);
$res=file_get_contents("$iframe_src");
$res=str_replace('/api/postpayment','https://www.pesapal.com/api/postpayment',$res);
echo $res;

Surprisingly, it worked! I should though, explain the second to last line where I am performing a string replace. This is because the PesaPal form action is a relative URL which will be prefixed with the URL of the iframe you called in. In this case, the URL of the iframe where I am echoing the $res is not https://www.pesapal.com so I required the form’s action to be more specific. (I wonder how much more I can do by modifying the html and Javascript code I pull in from PesaPal…maybe a more mobile friendly version of the form…the possibilities are endless….)

I know this hack can be achieved with cURL and / or DOMDocument so do feel free to add those in the comments. The more hacks, the merrier!

In case you are wondering why I had an iframe inside an iframe in the first place…well such is what happens when you need to pull in resources from all over the place to make sites work like they should. And in this case, what I was building was a special PowerPage for The Great Run (greatrun.powerpage.co.ke)

After completing this PowerPage, watch this space for special event PowerPages with online ticket purchasing included via simple, hassle and code free PesaPal (or Ticketsasa) integration.

Should you have any queries, shout them out in the comments!

Share

4 comments

  1. Hey i was looking for tutorials on how to integrate pesapal to a site from scratch but I have not had any luck so far. Would you please direct me to any helpful materials?
    I am good in all web development languages, specifically php anf, jquery and HTML5/CSS3.
    I have been to the developer.pesapal.com site but I don’t think they explain well

  2. Hi John – first of all – sorry for the really late reply to this. Secondly – the developer.pesapal.com site I think explains their plugins as well as is expected and also has sample code for some platforms and any developer worth their salt should be able to make use of their simple examples and modify them to the application required. That said – I do not mind helping you with integration of PesaPal but I would need some information on what parts of the tutorial you do not understand.

  3. i have successfully integrated pesapal in my system but am having a problem saving the same data into my database.

Leave a Reply