WordPress: how to use PayPal with Eshop

WordPress: how to use PayPal with Eshop

Testing and debugging PayPal in the Eshop WordPress plugin.

Eshop is one of the most used WordPress plugins for handling an e-commerce website. Eshop supports PayPal, but not as many web developers think it should. More precisely, Eshop doesn't use the PayPal APIs but instead relies on simple POST queries to handle your transactions with the PayPal main website. To be honest, Eshop works pretty well. The only thing you need to bear in mind is to know exactly what are its capabilities before going live.

Using the PayPal sandbox: test mode

To work in test mode with PayPal you need a developer PayPal account. When Eshop is in test mode, it makes use of the PayPal sandbox. So you have to go to the PayPal Sandbox site and create an account.

PayPal provides you with three types of accounts:

  1. Business
  2. Premier
  3. Personal

In order to avoid problems with Eshop, you need a Business account. Before you can go live, you have to make sure that also your client is a Business account owner. PayPal associates your email with your account profile. For example, a developer Business account email may look like username_biz@site.ext.

If you don't have a Business account, many features of Eshop might not work properly. For example, when you select the option Send buyers email address to PayPal? in Options > Eshop > Merchant Gateways > PayPal and choose No, the PayPal target page will show the option Don't you have a PayPal account? only for Business accounts. If you provide a different kind of account, PayPal will ask you to create an account instead.

Make sure to write the correct email address in Main account Email address and, most of all, make sure this is an active and verified Business account. If you're not sure that your client effectively has a Business account, don't go live.

Why not? Simply put, nobody wants to be forced to create a PayPal account to purchase your products. The Don't you have a PayPal account? option allows your visitors to pay only with their credit cards without any unnecessary extra steps.

The code

Eshop handles the main transaction steps in the paypal.php file. This file relies on the /paypal/eshop-paypal.class.php class. The PHP code simply creates a POST request and send it to PayPal using stream sockets. Eshop makes use of the PayPal's IPN (Instant Payment Notification) system.

A PayPal request takes its parameters and values from the hidden fields of the Eshop form whose only visible element is the button which will redirect your users to PayPal. In test mode the redirect page is static, meaning that you have to manually proceed to PayPal by clicking on the form button.

In the paypal.php file Eshop performs several actions. For example, on the redirect page you have:


switch ($eshopaction) {
    case 'redirect':
      //...
      $echoit.=$p->eshop_submit_paypal_post($_POST); // The PayPal form
      break;
    // continues
}

You can debug this step by simply printing out the user's cart just to make sure that PayPal will receive the correct data:


$echoit.=$p->eshop_submit_paypal_post($_POST); // The PayPal form
print_r($_SESSION['eshopcart'.$blog_id];);  // prints out the user's cart

Another interesting array is the Eshop's $eshopoptions option array. This is an associative array containing all your settings specified in the plugin's option pages. This is useful when you're not sure if the parameters passed to PayPal are correct.

For example, the Business account's email address is added to the POST request as follows:


$p = new eshop_paypal_class;
//...

$p->add_field('business', $eshopoptions['business']);

business is a standard parameter name used by PayPal. For a complete list of the available parameters, please refer to the official documentation.