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;
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