Press "Enter" to skip to content

Tag: php

Paypal Express Checkout and Recurring Payments

Are you, like me, using Paypal Express Checkout for integration into your shopping cart/ecommerce site?
Are you, like me, utilising the Paypal Subscriptions (Recurring Payments) options to set up future payments?
Are you, like me, getting a blank page when you are trying to setup a Recurring payment (maybe using the PHP NVP kit) after clicking “Agree and Pay” on the “Review your payment” page of the Paypal sandbox or live site)?

If so, the problem may be because you are sending an “AMT” (Amount) of 0 to Paypal: I did this because I didn’t want to actually take any money now…

It appears this is a long standing issue with the SetExpressCheckout section and RecurringPayments on Paypal and to avoid the blank white page, you’ll need to just send a nominal amount (such as 0.01) for Paypal to process “now”. However, Paypal does charge you a 20p transaction fee on the live system, so you may need to adjust your entire Paypal Express Checkout integration.

PHP: Zend Session: don’t set it up in the Initalizer

I’ve just wasted a few hours trying to get Zend_Session (part of the PHP Zend Framework) working correctly – previously, on this codebase, I had “rolled my own” cookie and session management system, but I thought I’d do it properly and utilise the Zend_Session system and store it all in a database…

It didn’t work.

Short answer as to why my sessions weren’t saving into the database (even after I switched to using the “default” Zend_Session_SaveHandler_DbTable ) was that I was setting up the session within the “routeStartup” section of Initalizer.php (which is loaded, in MVC fashion, via the bootstrap.php file). Changing bootstrap.php to:

// Prepare the front controller.
$frontController = Zend_Controller_Front::getInstance();
// Change to 'production' parameter under production environemtn
$frontController->registerPlugin(new Initializer('development'));
$maxSessionTime=60*60*24*30*6; // six months
Zend_Session::setSaveHandler(new My_Session_DbTable($maxSessionTime));
Zend_Session::rememberMe($maxSessionTime);

(i.e. take move the session savehandler out of the Initializer, but keep all the $thingy=new Zend_Session_Namespace whereever they are needed) means it started to work.

Most annoying thing: that there was no reason it didn’t work (no warnings, no errors, no documentation hints and no ‘out of place feeling’). Meh.

PHP: md5 hashes, base_convert and 32 bit limitations

I’ve had cause to convert an md5 hash from the standard hexadecimal base 16 to base 36 (mainly to make it shorter for presentation purposes). However, using base_convert in PHP proved unreliable as the string wouldn’t always convert back correctly. This is on 64 bit Linux, but I’m not certain PHP has been compiled with 64 bit support (so it is probably defaulting to 32bit).

However, thanks to this post on the PHP documentation, I’ve changed the code to use:

/*use gmp library to convert base. gmp will convert numbers > 32bit*/
if (!function_exists('gmp_strval')) {
    trigger_error('GMP does not appear to be compiled into PHP');
}
function gmp_convert($num, $base_a, $base_b)
{
        return gmp_strval ( gmp_init($num, $base_a), $base_b );
}

To test this, here’s the code I used:
function testconvert() {
    $a=Array(10,23,46,239423, PHP_INT_MAX, 323927832, 174623748237, PHP_INT_MAX.PHP_INT_MAX);
    foreach ($a as $key) {
        $converted=gmp_convert($key,10,36);
        $phpstandard=base_convert($key,10,36);
        print 'key='.$key.'. Convert: '.$phpstandard.','.$converted;
        if (!(base_convert($phpstandard,36,10)==$key)) { print '<strong>Standard PHP Failed</strong>'; }
        if (!(gmp_convert($converted,36,10)==$key)) { print '<strong>GM PHP Failed</strong>'; }
        print '<br>';
    }
    foreach ($a as $skey) {
        $key=md5($skey);
        $converted=gmp_convert($key,16,36);
        $phpstandard=base_convert($key,16,36);
        print 'key='.$key.'. Convert: '.$phpstandard.','.$converted;
        if (!(base_convert($phpstandard,36,16)==$key)) { print '  <strong>Standard PHP Failed</strong>'; }
        if (!(gmp_convert($converted,36,16)==$key)) { print '  <strong>GM PHP Failed</strong>'; }
        print '<br>';
    }
}

PHP: Zend_Dojo_Form SubmitButton not showing

In version 1.7.0 of the Zend Framework, specifically the Zend_Dojo_Form_Element_SubmitButton class (which makes the Zend_Form use the Dojo Javascript library), there is a small bug which prevents the label section of the Submit button being shown.

To work around this, just add something like:
$eElement->setDijitParam(‘label’,’Submit label’);
to work around the issue.

This is a known issue in the Zend Framework and will be fixed in the next release… If, however, like me you have spent a good hour or so trying to find out why your implementation doesn’t work and other demonstrations on the internet do, well now you should be able to find out why!