How to setup PayPal Pro Hosted Edition on Magento

Magento is a great e-commerce platform, and the built-in integration with PayPal cuts down on your development time and saves you having to trawl through the PayPal developer docs or use a third party module.

If you are on Website Payments Standard, you can setup Magento really quickly and use the built-in options to redirect people to the PayPal payments page upon order confirmation. Of course, as people seasoned with PayPal know though, the focus of this payment page is to login into or create a new PayPal account. Customers are usually confused as they have to fish around on the page to see the ‘Don’t have a PayPal account? Use your credit card or bank account (where available)’ link.

The alternative to the standard edition is Website Payments Pro, whereby users aren’t required to have a PayPal account and card details can be taken on your website. Magento integrates straight up with this version of PayPal, and after you plug in your login details and API key, you’ll find a new debit / credit card form on the order checkout. What many using Magento won’t be aware of though is that taking card details on your website, even if you don’t store them, will incur the wrath of PCI compliance and security issues. In a nutshell, you will have to pay for and pass quarterly compliance checks on your site and take responsibility for the security of Magento and your server, both of which are normally outwith your control.

The solution – Payments Pro Hosted Edition…

According to PayPal:

Buyers pay with a debit or credit card, or their PayPal account and you do not have to capture or store credit card  information on your website, thereby helping towards achieving PCI compliance.

Hosted Solution is the choice for merchants who prefer a solution where all financial details  are handled by PayPal.

The problem – Magento doesn’t come with the option for Payments Pro Hosted, and I couldn’t find any reference on how to get it working on the PayPal or Magento docs. After working on it for a few hours, here is how I got it working:

Step 1: Create a new Module

Magento is a huge, complex system. Like many other developers, I strongly recommend that you do not edit core code files in app/code/core. Instead, you can create a module to extend the core classes and models and then tell Magento to use your code ontop of its own.

So, create your own folder inside app/code/local with the name of your client or your (the developer) name. For example, I created the folder ‘Papertank’ and ended up with

  • App
    • Code
      • Local
        • Papertank

Inside this folder, you need to add a directory for your module – each change you make to different parts of the system should be in their own module. There are various tutorials on forums and the Magento wiki that detail the process of creating modules and what each folder and file does, but for now just go ahead and add the structure below (using the folder you created above)

  • Papertank
    • PayPalHosted
      • Block
        • PayPal
          • Standard
      • etc

Now, inside the Standard subdirectory, add a file called Redirect.php with the following code (making sure to rename Papertank to the folder you created) :

<?phpclass Papertank_Paypalhosted_Block_Paypal_Standard_Redirect extendsMage_Core_Block_Abstract{protected function _toHtml(){$standard = Mage::getModel(‘paypal/standard’);

$form = new Varien_Data_Form();

$actionUrl = $standard->getConfig() ? $standard->getConfig()->getPaypalUrl() : $standard->getPaypalUrl();

$form->setAction($actionUrl)

->setId(‘paypal_standard_checkout’)

->setName(‘paypal_standard_checkout’)

->setMethod(‘POST’)

->setUseContainer(true);

$fields = $standard->getStandardCheckoutFormFields();

//This is the line that gets us hosted payments!

$fields['cmd'] = ’_hosted-payment’;

foreach ($fields as $field=>$value) {

$form->addField($field, ’hidden’, array(‘name’=>$field, ’value’=>$value));

}

$html = ’<html><body>’;

$html.= $this->__(‘You will be redirected to the payment page in a few seconds.’);

$html.= $form->toHtml();

$html.= ’<script type=”text/javascript”>document.getElementById(“paypal_standard_checkout”).submit();</script>’;

$html.= ’</body></html>’;

return $html;

}

}

 

Now, in the etc folder, add a file called config.xml with the following content (again replacing Papertank with your named folder):

 

<?xml version=”1.0″?><!–/*** @category   Papertank* @package    Papertank_Paypalhosted* @author     David Rushton (david@papertank.co.uk)

*/

–>

<config>

<modules>

<Papertank_Paypalhosted>

<version>0.1</version>

</Papertank_Paypalhosted>

</modules>

<global>

<blocks>

<paypal>

<rewrite>

<standard_redirect>PayPal_Paypalhosted_Block_Paypal_Standard_Redirect</standard_redirect>

</rewrite>

</paypal>

</blocks>

</global>

</config>

 

One last thing to do is to add a file called Papertank_PaypalHosted.xml into app/etc/modules with the following content (once more changing Papertank with your own):

<?xml version=”1.0″?><config><modules><Papertank_Bestsellers><active>true</active><codePool>local</codePool>

</Papertank_Bestsellers>

</modules>

</config>

 

Now, refresh your cache inside the Magento admin and inside the configuration pages, select PayPal Payments Standard as the payment method.

Instead of instructing PayPal to use the standard payment page, Magento should now force the payment to go through on the Payments Pro Hosted template.

Of course, you have to actually have PayPal Payments Pro enabled in your account as this won’t work if you’re still using the basic version.

? comments