Monday, February 24, 2014

Create an Effective Facebook Banner Advertisement.

Create Effective Facebook Banner Advertisement.
If you are running an online business, Facebook is offering a great advertisement platform that you can promote your products with millions of users. In this post I want to explain how to create an effective Facebook banner advertisement with button features using Power Editor. We are using this system for Wall Script and OAuthLogin, sure this will improve your product sales.

Note: Now Power Editor works with only on Google Chrome Browser.

Step 1
Login Facebook go to Settings - Ad Manager - Click Power Editor in left side menu or access URL https://www.facebook.com/ads/manage/powereditor/
Create Effective Facebook Banner Advertisement.

Step 2
Click Download to Power Editor in top menu button to get Facebook Advertisement data.
Create Effective Facebook Banner Advertisement.

Step 3
First you have to create a Campaign by clicking CampaignsTab and give Campaign title and set advertisement budget.
Create Effective Facebook Banner Advertisement.

Step 4
Now select Ads tab and click Create Ad button. Give ad details like ad name and objective type.
Create Effective Facebook Banner Advertisement.

Step 5
Select the unpublished ad and map with Page Post. If you want to create a fresh link click Create New Unpublished Post link
Create Effective Facebook Banner Advertisement.

Step 6
Here you have to give product URL, description and Call to Action type depends on your requirement.
Create Effective Facebook Banner Advertisement.

Step 7
Upload banner image.
Create Effective Facebook Banner Advertisement.

Step 8
Click Create Post button.
Create Effective Facebook Banner Advertisement.

Step 9
Select the ad and click Upload Changes on top header part. Power Editor will upload date to Facebook Ad Manager.
Create Effective Facebook Banner Advertisement.

Step 10
Now the Ad was created, go to Ad Manager and then click on Campaigns & Ads or access URL https://www.facebook.com/ads/manage/campaigns/
Create Effective Facebook Banner Advertisement.

Step 11
Banner advertisement will display like this with download button. If you want to see the advertisement live preview click View in News Feed link.
Create Effective Facebook Banner Advertisement.

Step 12
Live Preview.
Create Effective Facebook Banner Advertisement.

Thursday, February 6, 2014

Creating a WordPress Theme From Static HTML: Creating Template Files

In the first part of this series, I showed you how to prepare your HTML and CSS files for WordPress, ensuring the structure would work, the code was valid and that the correct classes were being used.
In this tutorial you'll learn how to take your index.html file and split it up into a set of template files for use by WordPress.

What You'll Need

For this tutorial all you'll need is the most basic tool for editing HTML and PHP:
  • A code editor of your choice.

What Are Template Files?

A WordPress theme consists of a number of template files. At the very least, a theme must contain two files for it to work, these are index.php and style.css.
However, in a well written theme, the contents of the index.php file will be split up into the main template file (index.php) and a set of include files. These are the files containing the code for the header, sidebar and footer.
In some themes, an additional include file is used for The Loop; I'll come to that in Part 4 of this series. The files are called include files because you add code to your index.php file to tell WordPress to include their contents.
Our index.html file will be split into four PHP files:
  • index.php, which will contain the main content plus the code to include the other files
  • header.php, which will include the section plus everything in the header and navigation
  • sidebar.php, which will contain the sidebar widget area
  • footer.php which will contain (you've guessed it!) the footer, plus the closing
tag Over the course of this series, you'll add to these files so that your theme can include widgets, menus and a loop, and you'll add hooks which will be used by plugins. You'll also add extra template files to display different types of content. If you want to get a head start on this, have a look at the Codex page on the WordPress Template Hierarchy.
But for now, all you're going to do is create a set of PHP files and split the contents of your index.php file to them.

Creating PHP Files

The first step is to create empty files. Create four blank files with the following names and open them in your code editor:
  • index.php
  • header.php
  • sidebar.php
  • footer.php
Make sure all of these are in a folder which has the name of your theme - in my case I'll name the folder 'wptutsplus-demo-theme'.
Copy your stylesheet into this folder, as well. You won't be editing it in this tutorial, but you will be doing so in the next part of the series.

Populating the Header File

Next you'll copy the top part of index.html into your header.php file.
Open index.html and select everything from the opening DOCTYPE declaration to the opening div class="main" tag:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>WordPress Theme Building - Creating a WordPress theme from static HTML</title>
    <link href="style.css" rel="stylesheet" media="all" type="text/css" />
    <header role="banner">
        <div class="site-name half left"></div>
        <div class="site-name half left">
            <h1 class="one-half-left" id="site-title"><a title="Creating a WordPress theme from static html - home" rel="home">WordPress Theme Building</a></h1>
            <h2 id="site-description">Creating a WordPress theme from static html</h2>
        </div>
         
        <aside class="header widget-area half right" role="complementary">
            <div class="widget-container">This will be a widget area in the header - address details (or anything else you like) goes here</div>
        </aside>
    </header>
 
    
    <nav class="menu main">php /*  Allow screen readers / text browsers to skip the navigation menu and get right to the good stuff */ ?>
        <div class="skip-link screen-reader-text"><a title="Skip to content" href="#content">Skip to content</a></div>
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Latest news</a></li>
            <li><a href="#">Featured articles</a></li>
        </ul>
    </nav>
    <div class="main">
Copy this code and paste it into your header.php file.
Save your new header file.

Populating the Sidebar File

Now repeat this for the sidebar.
In your index.html file, select the aside class="sidebar" element and everything inside it:
01
02
03
04
05
06
07
08
09
10
11
<aside class="sidebar widget-area one-third right" role="complementary">
    <div class="widget-container">
        <h3 class="widget-title">A sidebar widget</h3>
        <p>This is a sidebar widget, in your WordPress theme you can set these up to display across your site.</p>
    </div>
    <div class="widget-container">
        <h3 class="widget-title">Another sidebar widget</h3>
        <p>A second sidebar widget, maybe you could use a plugin to display a social media feed, or simply list your most recent posts.</p>
    </div>
</aside>
Now copy this into your sidebar.php file and save it.

Populating the Footer File

The process of populating the footer.php file is identical to the header and sidebar.
Select everything after the sidebar in your index.html file:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</div>
<footer>
    
    <aside class="fatfooter" role="complementary">
        <div class="first quarter left widget-area">
            <div class="widget-container">
                <h3 class="widget-title">First footer widget area</h3>
                <p>A widget area in the footer - use plugins and widgets to populate this.</p>
            </div>
        </div>
        <div class="second quarter widget-area">
            <div class="widget-container">
                <h3 class="widget-title">Second footer widget area</h3>
                <p>A widget area in the footer - use plugins and widgets to populate this.</p>
            </div>
        </div>
        <div class="third quarter widget-area">
            <div class="widget-container">
                <h3 class="widget-title">Third footer widget area</h3>
                <p>A widget area in the footer - use plugins and widgets to populate this.</p>
            </div>
        </div>
        <div class="fourth quarter right widget-area">
            <div class="widget-container">
                <h3 class="widget-title">Fourth footer widget area</h3>
                <p>A widget area in the footer - use plugins and widgets to populate this.</p>
            </div>
        </div>
    </aside>
</footer>
Copy it and paste it into your footer.php file.
Save your footer file.
You might be wondering why the .main div is closed in the footer file and not the sidebar. This is so that if you later set up a template file for pages which don't have a sidebar, you'll miss out the sidebar include in that template and keep the footer include, and the .main div will be closed correctly.

Populating the Index File

The final step is to set up your index.php file. This is slightly more involved, you'll have to add some PHP functions which WordPress uses to include the header, sidebar and footer.
Open your empty index.php file and add the code shown below:
1
2
3
4
 
Be sure to leave a space between the opening header include and the sidebar include, it's here where you'll add the contents of the index file which aren't in the header, sidebar or footer.
Now open your index.html file again and select all of the code between the opening div class="main" element and the sidebar:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
<div class="two-thirds" id="content">
    <article class="post" id="01">
        <h2 class="entry-title">This is the title of a post or page</h2>
        <img class="size-large" alt="" src="images/featured-image.jpg" />
        <section class="entry-meta">
            <p>Posted on 5 November by Rachel McCollin</p>
        </section>
        <section class="entry-content">
            <p>This is the content of the post. On an archive page it might be an excerpt of the post or you might include the entire content.</p>
            <h3>Images in WordPress</h3>
            <img class="alignright" alt="" src="images/another-image.jpg" />
            <p>This post has some images included - once you've converted this html to a WordPress theme you'll be able to get WordPress to handle images for you and life will be so much easier!</p>
 
            <p>It also has a featured image - again, WordPress will handle these for you and you'll never go back to static html again. You'll learn how to add support for featured images to your theme in Part 10 of this series. You can use them to automatically display images in your individual posts and pages and in archive pages, you'll learn how to set up a custom archive page in Part 11.</p>
        </section>
        <section class="entry-meta">
            <h3>Post Categories and Tags</h3>
            <p>In this section you can display information about the categories and tags associated with your post, you'll learn how to do this using WordPress template tags in Part 4 of this series.</p>
        </section>
    </article>
</div>
Copy this and paste it into your index.php file below the get_header() line.
Save your index.php file.

Creating a WordPress Theme From Static HTML: The Footer File

In this series, you've been learning how to create a WordPress theme form static HTML.
Up to this point, you have:
  • prepared your markup for WordPress
  • converted your HTML to PHP and split your file into template files
  • edited the stylesheet and uploaded your theme to WordPress
  • added a loop to your index file
  • added meta tags, the wp_head hook and the site title and description to your header file
  • added a navigation menu
  • added widget areas to the header and sidebar.

In this tutorial, you'll finish off the footer.php file by adding the following areas to it:
  • widget areas
  • a colophon
  • the wp_footer hook.
When you've done this. you will have a fully functioning theme. Then, in the remaining parts of the series, I'll show you how to make your theme even better by adding extra template files and featured images functionality.

What You'll Need

  • your code editor of choice
  • a browser for testing your work
  • a WordPress installation, either local or remote
  • If you're working locally, you'll need MAMP, WAMP or LAMP to enable WordPress to run.
  • If you're working remotely, you'll need FTP access to your site plus an administrator account in your WordPress installation.

1. Registering Widget Areas for the Footer

Registering widget areas for your footer is very similar to registering them for the header and sidebar which we did in the previous article in this series.
The different is that, in this article, we're going to register four widget area rather than just one. This means that the theme can have four widgetized areas displayed side-by-side in what's traditionally called a "fat footer."
Start by opening your functions.php file. Find the wptutsplus_widgets_init() function and add the following code inside it, below the code for the three sidebars you've already registered:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// First footer widget area, located in the footer. Empty by default.
register_sidebar(
    array(
        'name' => __( 'First Footer Widget Area', 'compass' ),
        'id' => 'first-footer-widget-area',
        'description' => __( 'The first footer widget area', 'compass' ),
        'before_widget' => '
'
,
                'after_widget' => '
'
,
        'before_title' => '

'

,
                'after_title' => '
'
    )
);
 
// Second Footer Widget Area, located in the footer. Empty by default.
register_sidebar(
    array(
        'name' => 'Second Footer Widget Area',
        'id' => 'second-footer-widget-area',
        'description' => 'The second footer widget area',
        'before_widget' => '
'
,
                'after_widget' => '
',
        'before_title' => '

'

,
                'after_title' => '
',
    )
);
 
// Third Footer Widget Area, located in the footer. Empty by default.
register_sidebar(
    array(
        'name' => 'Third Footer Widget Area',
        'id' => 'third-footer-widget-area',
        'description' => 'The third footer widget area',
        'before_widget' => '
'
,
                'after_widget' => '
',
        'before_title' => '

'

,
                'after_title' => '
',
    )
);
 
// Fourth Footer Widget Area, located in the footer. Empty by default.
register_sidebar(
    array(
        'name' => 'Fourth Footer Widget Area',
        'id' => 'fourth-footer-widget-area',
        'description' => 'The fourth footer widget area',
        'before_widget' => '
'
,
                'after_widget' => '
',
        'before_title' => '

'

,
                'after_title' => '
',
    )
);
This registers four new widget areas, each of which has a unique ID and description.
If you open the "Widgets" admin screen now, you'll see the four empty widget areas ready for you to populate:
creating-wordpress-theme-from-static-html-widgets-screen-before-footer-widgets-added
But you still need to add the widget areas to your footer.php file to make them work properly.

2. Adding Widget Areas to the Footer File

Open your theme's footer.php file and find this code:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  
class="first quarter left widget-area">
    
class="widget-container">
      

class="widget-title">First footer widget area

      A widget area in the footer - use plugins and widgets to populate this.
    
  
  
class="second quarter widget-area">
    
class="widget-container">
      

class="widget-title">Second footer widget area

      A widget area in the footer - use plugins and widgets to populate this.
      
    
  
class="third quarter widget-area">
    
class="widget-container">
      

class="widget-title">Third footer widget area

      A widget area in the footer - use plugins and widgets to populate this.
    
  
  
class="fourth quarter right widget-area">
    
class="widget-container">
      

class="widget-title">Fourth footer widget area

        A widget area in the footer - use plugins and widgets to populate this.
    
  
Replace it with the code below:
1
2
3
4
5
6
7
  
class="first quarter left widget-area">
  
class="second quarter widget-area">
  
class="third quarter widget-area">
  
class="fourth quarter right widget-area">
Now save your footer template.
You can now add widgets to your widget areas via the "Widgets" admin screen. I won't cover this here as we've already reviewed you how to do that in the previous tutorial.

3. Adding a Colophon to Your Footer

A colophon is a note at the bottom of the page with small print. It may include copyright information, or company details if your site is for a company, or other similar information. For many WordPress-based sites, many of them include a link that reads "Proudly Powered by WordPress."
In my colophon, I'm going to add a copyright notice with the date - I'll use the bloginfo() function to retrieve information about the site.
In your footer.php file, immediately after the closing
tag, add the following code:
1
2
3
4
5
6
7
8
9
class="colophon" role="contentinfo">
  class="copyright half left">
    © 2013
  
  class="credits half right">
     Proudly powered by "http://wordpress.org/">WordPress.
  
Now if you save your footer file and visit your site, you will see the colophon displayed (as well as the footer widgets):
creating-wordpress-theme-from-static-html-colophon

4. Adding the wp_footer Hook

The final step is to add the wp_footer hook. You may remember that in Part 5 of this series you added the wp_head hook to the header.php file. Both of these hooks are used by plugins and both are essential for any site using your theme to work.
In your footer.php template, before the closing
tag, add the following line:
1
Finally, save your file.

Getting Started With The PayPal API

PayPal is the most popular platform for receiving online payments today. The ease of opening a PayPal account and receiving payments compared to opening a merchant account with a traditional payment gateway is probably the number one reason for its popularity, with a close second being the comprehensive API that PayPal provides for its payment services. In this post, I will break down some of the techniques and approaches to working with the PayPal API, in order to make integration and troubleshooting simpler and easier.
Disclaimer: PayPal’s API is among the worst I’ve ever had to deal with. Inconsistencies, sometimes poor or conflicting documentation, unpredictable failures and account changes, and major differences between the live and sandbox versions all conspire to make the PayPal API quite a pain in the arse to work with. Over the years, I’ve taken my lumps from working quite a bit with the PayPal API, and I’ve published the results of my hard-learned lessons as a commercial PHP PayPal API component on the source-code marketplace Binpress.
PayPal at LeWeb, Copyright Jean-Christophe Capelli, Some Rights Reserved

The Different Payment Options

PayPal offers a variety of payment options, which might be confusing at first:
  • Express Checkout
    The premier PayPal service. Express Checkout allows you to receive payments without having a merchant account and without having to meet special requirements other than verifying your account (either via a bank account or a credit card). Previously, you could receive Express Checkout payments from PayPal users only, but PayPal has since added a credit-card option for non-PayPal users, making this service accessible to practically anyone with a major credit card. Note that the Express Checkout process occurs on PayPal’s platform and thus can never be fully integrated in your website’s experience.
  • Direct Payment
    The Direct Payment method allows you to receive credit-card payments directly through an API call. This enables you to host the payment process on your website in full, which might make for a more complete shopping experience for your customers. The Direct Payment method has several variations that enable you to authorize a payment and complete it at a later date: the appropriately named Authorization and Capture methods. These variations are a part of the Website Payments Pro API, which is available only to US, Canadian and UK accounts.
  • Recurring Payments
    This allows you to set up a recurring transaction (i.e. a subscription payment).
  • Mass Payments
    This allows you to transfer money to multiple accounts at once.
  • Adaptive Payments
    Here is another API for sending funds to multiple recipients, with some differences from the Mass Payments API. (Did I mention that the PayPal API is confusing and a bit redundant?)
This list is not comprehensive, but it covers the main payment options (see the API documentation for more).

Making API Requests

PayPal supports two main formats over HTTP: NVP and SOAP. NVP is short for Name-Value Pair, and SOAP stands for Simple Object Access Protocol. I will cover the NVP approach, which I prefer to SOAP’s relatively verbose and complex syntax.
Each of the API methods has different parameters, but they all share some basic parameters, which are used to identify the API account and sign the transaction. These include:
  • USER
    Your PayPal API user name.
  • PWD
    Your PayPal API password.
  • VERSION
    The version number of the NVP API service, such as 74.0 (the most recent as of this writing).
  • SIGNATURE
    Your PayPal API signature string. This parameter is optional if you use a certificate to authenticate.
The last required parameter is METHOD, which declares which API method we are calling.
Requests are made over HTTPS. We’ll use cURL to build our basic request, and then encapsulate the process in a class:
class Paypal {
   /**
    * Last error message(s)
    * @var array
    */
   protected $_errors = array();

   /**
    * API Credentials
    * Use the correct credentials for the environment in use (Live / Sandbox)
    * @var array
    */
   protected $_credentials = array(
      'USER' => 'seller_1297608781_biz_api1.lionite.com',
      'PWD' => '1297608792',
      'SIGNATURE' => 'A3g66.FS3NAf4mkHn3BDQdpo6JD.ACcPc4wMrInvUEqO3Uapovity47p',
   );

   /**
    * API endpoint
    * Live - https://api-3t.paypal.com/nvp
    * Sandbox - https://api-3t.sandbox.paypal.com/nvp
    * @var string
    */
   protected $_endPoint = 'https://api-3t.sandbox.paypal.com/nvp';

   /**
    * API Version
    * @var string
    */
   protected $_version = '74.0';

   /**
    * Make API request
    *
    * @param string $method string API method to request
    * @param array $params Additional request parameters
    * @return array / boolean Response array / boolean false on failure
    */
   public function request($method,$params = array()) {
      $this -> _errors = array();
      if( empty($method) ) { //Check if API method is not empty
         $this -> _errors = array('API method is missing');
         return false;
      }

      //Our request parameters
      $requestParams = array(
         'METHOD' => $method,
         'VERSION' => $this -> _version
      ) + $this -> _credentials;

      //Building our NVP string
      $request = http_build_query($requestParams + $params);

      //cURL settings
      $curlOptions = array (
         CURLOPT_URL => $this -> _endPoint,
         CURLOPT_VERBOSE => 1,
         CURLOPT_SSL_VERIFYPEER => true,
         CURLOPT_SSL_VERIFYHOST => 2,
         CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem', //CA cert file
         CURLOPT_RETURNTRANSFER => 1,
         CURLOPT_POST => 1,
         CURLOPT_POSTFIELDS => $request
      );

      $ch = curl_init();
      curl_setopt_array($ch,$curlOptions);

      //Sending our request - $response will hold the API response
      $response = curl_exec($ch);

      //Checking for cURL errors
      if (curl_errno($ch)) {
         $this -> _errors = curl_error($ch);
         curl_close($ch);
         return false;
         //Handle errors
      } else  {
         curl_close($ch);
         $responseArray = array();
         parse_str($response,$responseArray); // Break the NVP string to an array
         return $responseArray;
      }
   }
}
Note that I use a CA certificate file for SSL certificate validation. You can obtain the file from the cURL website or any trusted source. Update the path to the certificate file according to where you’ve placed it.
The response returned will be in NVP format as well, and I reformat it into an array before returning it. A parameter named ACK signifies the status of the request: Success or SuccessWithWarning when the request succeeds, and Error or Warning when the request fails.
A request could fail for many reasons, and there are different reasons for each API method, which are covered in detail in the manual. We’ll go over some further down in this article and look at ways to handle them. Keep in mind that the parameter values are case-sensitive, so code against them accordingly.

Express Checkout

One of the most popular APIs is the Express Checkout API, which enables you to receive payments without opening a Website Payments Pro account (which is available only to verified US accounts) or hosting the actual transaction yourself (which requires additional security).
The Express Checkout process works as follows:
  1. We request a checkout token from PayPal using the transaction details;
  2. If successful, we redirect the user to the PayPal endpoint using the received token;
  3. The user completes or cancels the payment on the PayPal platform and is redirected back to our website;
  4. We complete the payment either when the user is redirected back or via an Instant Payment Notification (IPN).
Express Checkout flow

1. Getting the Checkout Token: SetExpressCheckout

We initiate the Express Checkout process by passing the order details to the PayPal API, and we receive a token string that identifies it. This token would be used in the next step to redirect to PayPal.
Here are the required parameters:
  • METHOD
    This is the API method that we’re using (i.e. SetExpressCheckout).
  • RETURNURL
    The URL that the user will be redirected to after the payment process is completed.
  • CANCELURL
    The URL that the user will be redirected to after having cancelled the payment process.
  • PAYMENTREQUEST_0_AMT
    The transaction’s total amount. This must have two decimal places, with the decimal separator being a period (.). The optional thousands separator must be a comma (,).
  • PAYMENTREQUEST_0_ITEMAMT
    The total cost of the items in the order, excluding shipping, taxes and other costs. If there are no extra costs, then it should be the same value as PAYMENTREQUEST_0_AMT.
We can pass additional parameters to add more information about the order, some of which have default values:
  • PAYMENTREQUEST_0_CURRENCYCODE
    The payment’s currency, as a three-letter code. The default is USD.
  • PAYMENTREQUEST_0_SHIPPINGAMT
    The total shipping costs for this order.
  • PAYMENTREQUEST_0_TAXAMT
    The total tax amount for this order. This is required if per-item tax is specified (see below).
  • PAYMENTREQUEST_0_DESC
    The order’s description.
We can also add details about individual items in the order:
  • L_PAYMENTREQUEST_0_NAMEm
    The item’s name.
  • L_PAYMENTREQUEST_0_DESCm
    The item’s description.
  • L_PAYMENTREQUEST_0_AMTm
    The item’s cost.
  • L_PAYMENTREQUEST_0_QTYm
    The quantity of an item.
The variable index m identifies the item. (Use the same variable for all details of the same item.)
There are many other optional parameters, which can be found in the API documentation.
We’ll use the function that we wrote above to build the SetExpressCheckout request:
//Our request parameters
$requestParams = array(
   'RETURNURL' => 'http://www.yourdomain.com/payment/success',
   'CANCELURL' => 'http://www.yourdomain.com/payment/cancelled'
);

$orderParams = array(
   'PAYMENTREQUEST_0_AMT' => '500',
   'PAYMENTREQUEST_0_SHIPPINGAMT' => '4',
   'PAYMENTREQUEST_0_CURRENCYCODE' => 'GBP',
   'PAYMENTREQUEST_0_ITEMAMT' => '496'
);

$item = array(
   'L_PAYMENTREQUEST_0_NAME0' => 'iPhone',
   'L_PAYMENTREQUEST_0_DESC0' => 'White iPhone, 16GB',
   'L_PAYMENTREQUEST_0_AMT0' => '496',
   'L_PAYMENTREQUEST_0_QTY0' => '1'
);

$paypal = new Paypal();
$response = $paypal -> request('SetExpressCheckout',$requestParams + $orderParams + $item);

2. Redirecting to PayPal Using the Checkout Express Token

If the request is successful, we’ll receive a checkout token in the TOKEN parameter of the response.
if(is_array($response) && $response['ACK'] == 'Success') { //Request successful
      $token = $response['TOKEN'];
      header( 'Location: https://www.paypal.com/webscr?cmd=_express-checkout&token=' . urlencode($token) );
}
The user now goes through the purchase process on PayPal’s website. When they confirm or cancel it, they will return to one of the URLs that we’ve specified in the request.

3. Completing the Transaction

Assuming the user confirms the transaction, they will be redirected to our website by PayPal. At this point, we should use two relevant API methods: DoExpressCheckoutPayment will complete the transaction, but before that we might want to get additional information on the buyer using GetExpressCheckoutDetails.
PayPal will redirect the user back from the purchase with the checkout token, which we will use to call those methods. The token will be available in the URL query parameters via the token parameter. We will check for its existence in the confirmation URL and then send our API requests if we find it.
The GetExpressCheckoutDetails method requires only the checkout token. DoExpressCheckoutPayment requires a couple of additional parameters:
  • PAYMENTREQUEST_0_PAYMENTACTION
    This is the payment action. It should be set to Sale unless we’ve specified a different action in the SetExpressCheckout method (possible values include Authorization and Capture).
  • PAYERID
    This is the unique identification for the PayPal account. This, too, is returned in the URL query parameters (in the PayerID parameter) and can also be retrieved from the details returned by GetExpressCheckoutDetails.
if( isset($_GET['token']) && !empty($_GET['token']) ) { // Token parameter exists
   // Get checkout details, including buyer information.
   // We can save it for future reference or cross-check with the data we have
   $paypal = new Paypal();
   $checkoutDetails = $paypal -> request('GetExpressCheckoutDetails', array('TOKEN' => $_GET['token']));

   // Complete the checkout transaction
   $requestParams = array(
       'TOKEN' => $_GET['token'],
       'PAYMENTACTION' => 'Sale',
       'PAYERID' => $_GET['PayerID'],
       'PAYMENTREQUEST_0_AMT' => '500', // Same amount as in the original request
       'PAYMENTREQUEST_0_CURRENCYCODE' => 'GBP' // Same currency as the original request
   );

   $response = $paypal -> request('DoExpressCheckoutPayment',$requestParams);
   if( is_array($response) && $response['ACK'] == 'Success') { // Payment successful
       // We'll fetch the transaction ID for internal bookkeeping
       $transactionId = $response['PAYMENTINFO_0_TRANSACTIONID'];
   }
}

Direct Payment

The Direct Payment API allows you to receive payments directly on your website or application, giving you complete control over the checkout process. PayPal tends to push users to register and use a PayPal account, which is understandable, but this conflicts somewhat with our interest to make the payment process as simple and clear as possible for our customers. For this reason, full control over the checkout process is preferred and gives us more options to optimize sales and generate more sales.
Direct Payment flow
The process is a bit simpler than that of Express Checkout, because the entire interaction occurs on our website, and we need to perform just one API call to process a normal payment: DoDirectPayment.
A couple of more API requests are required if you want to perform a transaction that is billed at a later date (for example, when you ship the product or confirm availability). These would be the Authorization & Capture API methods, which I will not cover in this post, but be aware that this option exists.

DirectPayment Parameters

DirectPayment requires different parameters than Express Checkout, as to be expected. While the transaction details parameters are similar (with different key names, to make it more interesting), the method also requires credit-card and address information.
DirectPayment’s basic parameters:
  • METHOD
    This is DoDirectPayment.
  • IPADDRESS
    This is the IP address of the payer. In PHP, we can retrieve it using the superglobal $_SERVER['REMOTE_ADDR']. You’ll have to do a bit more work to get the IP when dealing with set-ups that have a proxy between the PHP process and the outside network (such as nginx).
  • PAYMENTACTION
    This is the type of action that we want to perform. A value of Sale indicates an immediate transaction. A value of Authorization indicates that this transaction will not be performed immediately, but rather will be captured later using the Authorization & Capture API mentioned earlier.
Credit-card details:
  • CREDITCARDTYPE
    The credit-card type (Visa, MasterCard, etc.). See the API documentation for the full list.
  • ACCT
    The credit-card number. (Don’t you love these abbreviated key names?) This must conform to the particular format of the card’s type.
  • EXPDATE
    The expiration date, in MMYYYY format (i.e. a two-digit month and a four-digit year, as one string).
  • CVV2
    The “card verification value,” or security code, as it’s sometimes known.
Payer information and address parameters:
  • FIRSTNAME, LASTNAME
    The payer’s first name and last name, respectively (in separate fields). You can also provide an email address in an EMAIL parameter, but it’s not required.
  • CITY, STATE, COUNTRYCODE, ZIP
    The city, state, country code (as a two-letter code) and zip code parts of the address, all required.
  • STREET, STREET2
    Two lines for the address (only the first is required).
This address will be used in the address verification system (AVS). You’ll receive a specific error code if a transaction has failed due to an address verification failure.
The payment details parameters are the same as the ones for Express Checkout, but with slightly different names (AMT, ITEMAMT, CURRENCYCODE, SHIPPINGAMT, TAXAMT and DESC) and without the PAYMENTREQUEST_0_ prefix. Refer to the previous section or the API documentation for specific details on those.
Similarly, the item details parameters are similar to those of Express Checkout. These include L_NAMEm, L_DESCm, L_AMTm and L_QTYm, giving you granular control of item details in the order summary. The m integer variable is used to account for multiple items (replace with 0, 1 and so on for numbered items in the order). See the API documentation for a comprehensive list of item details.

Performing the Transaction

Sending the request using our function is very similar to GetExpressCheckoutToken. We pass all of the parameters into the request function as before, with the method set to DoDirectPayment.
$requestParams = array(
   'IPADDRESS' => $_SERVER['REMOTE_ADDR'],
   'PAYMENTACTION' => 'Sale'
);

$creditCardDetails = array(
   'CREDITCARDTYPE' => 'Visa',
   'ACCT' => '4929802607281663',
   'EXPDATE' => '062012',
   'CVV2' => '984'
);

$payerDetails = array(
   'FIRSTNAME' => 'John',
   'LASTNAME' => 'Doe',
   'COUNTRYCODE' => 'US',
   'STATE' => 'NY',
   'CITY' => 'New York',
   'STREET' => '14 Argyle Rd.',
   'ZIP' => '10010'
);

$orderParams = array(
   'AMT' => '500',
   'ITEMAMT' => '496',
   'SHIPPINGAMT' => '4',
   'CURRENCYCODE' => 'GBP'
);

$item = array(
   'L_NAME0' => 'iPhone',
   'L_DESC0' => 'White iPhone, 16GB',
   'L_AMT0' => '496',
   'L_QTY0' => '1'
);

$paypal = new Paypal();
$response = $paypal -> request('DoDirectPayment',
   $requestParams + $creditCardDetails + $payerDetails + $orderParams + $item
);

if( is_array($response) && $response['ACK'] == 'Success') { // Payment successful
   // We'll fetch the transaction ID for internal bookkeeping
   $transactionId = $response['TRANSACTIONID'];
}
There are plenty of parameters, but all relatively simple.

Error Handling

In a perfect world, this section would not exist. In reality, you will be referring to it quite a lot. PayPal can fail a transaction for a multitude of reasons, not all of which you can control.
The $response variable we returned from our paypalApiRequest() function could contain a different value than Success for the ACK parameter. That value could be:
  • Success
    Indicates a successful operation.
  • SuccessWithWarning
    Indicates a successful operation, and that messages were returned in the response that you should examine.
  • Failure
    Indicates a failed operation, and that the response contains one or more error messages explaining the failure.
  • FailureWithWarning
    Indicates a failed operation, and that messages were returned in the response that you should examine.
This gives us two success statuses and two failure statuses. The mock code above tests for the Success value only, but we could change it to check for SuccessWithWarning as well; and keep in mind that we need to find out what the warning is. A common scenario is that a Direct Payment charge will have been performed successfully, but the credit-card company responds that the transaction has failed, for whatever reason.
Errors from PayPal are returned in four parameters in the response:
  • L_ERRORCODE0
    A numeric error code, which can referenced against PayPal’s error code list (there are quite a few).
  • L_SHORTMESSAGE0
    A short error message describing the problem.
  • L_LONGMESSAGE0
    A longer error message describing the problem.
  • L_SEVERITYCODE0
    The severity code. (I couldn’t find any useful documentation on this, and it doesn’t really matter, so let’s put it aside.)
The 0 part of these parameters is an incrementing integer for multiple error message (1, 2, etc.).
Here are some common errors you’ll run into:
  • 10002
    Authentication or authorization failed. This usually indicates invalid API credentials, or credentials that do not match the type of environment you are working in (such as a live or sandbox environment).
  • 81***
    Missing parameter. There are quite a few of these, all starting with 81. Each refers to a specific required parameter that is missing from the request.
  • 104**
    Invalid argument. This indicates that one of the supplied parameters has an invalid value. Each argument has specific errors, all starting with 104. A common one is 10413, which means that the total cost of the cart items does not match the order’s amount (i.e. the total amount parameter, AMT, does not equal the items’ total plus shipping, handling, taxes and other charges).

How Do We Handle These Errors in Practice?

PayPal error messages vary and could contain private information that you do not want your users to see (such as an invalid merchant configuration). That being the case, showing PayPal error messages directly to users is not advisable, even though some of them might be useful.
In most cases, I would do the following:
  1. Set up a white-list array of errors that can be shown safely (such as a missing credit-card number and expiration date);
  2. Check the response code against that array;
  3. If the error message is not white-listed, then display a generic message, such as “An error has occurred while processing your payment. Please try again in a few minutes, or contact us if this is a recurring issue.”
If an error falls outside of the white-listed array, I will also log it to a file on the server and send an email to the administrator, with the full details so that someone is up to speed on payment failures. In fact, logging PayPal requests and responses is good practice regardless of errors, so that you can monitor and troubleshoot payment failures (I provide this option in the commercial component that I mentioned at the beginning of this article).

Ready To Get Started With The PayPal API?

In this post, I’ve covered two of the most widely used API methods, as well as error handling with the PayPal API. This should be enough for you to get started using the most popular payment platform online.
The PayPal API has many more methods and processes, more than can be covered in any one post. Once you get up to speed on the basic methods, learning the others should be relatively straightforward (even if somewhat exhausting). I hope this guide has given you a good head start on using the API. If you have questions or comments, I would love to hear from you in the comments!
Disclaimer: PayPal’s API is among the worst I’ve ever had to deal with. Inconsistencies, sometimes poor or conflicting documentation, unpredictable failures and account changes, and major differences between the live and sandbox versions all conspire to make the PayPal API quite a pain in the arse to work with. Over the years, I’ve taken my lumps from working quite a bit with the PayPal API, and I’ve published the results of my hard-learned lessons as a commercial PHP PayPal API component on the source-code marketplace Binpress.

Followers