Paypal express checkout and drupal

Written by Nicholas on Wed, 03/20/2019 - 21:14

At flaircore.com, we love to interact with 3rd party api(s), in this blog we will explore how you could integrate paypal payments (express checkout) with your drupal website/application. We will create a custom drupal 8 module that will contain a form, and the logic required to integrate with the official paypa-php-sdk.

In our project root directory, under modules/ folder, create a folder named custom, and inside that folder create another folder named paypal_example. This folder will contain all our module files for this specific mission.

paypal_payment_example

 

Inside that folder, create the following files

  • paypal_example.info.yml (defines our module)
  • paypal_example.routing.yml (contains the route to our paypal checkout form as well as any other route we might need to define. )
  • create an 'src' directory and in it, another directory named 'Form', in this form directory create our payment form class, lets call it 'PaypalExample.php'.payment_example

    Below is the code paypal_example.info.yml should contain.

name: 'Paypal Example'
type: module
description: 'Contains example code on how to intergrate paypal payment with drupal 8'
core: 8.x
package: 'Example'

Below is the code paypal_example.routing.yml should contain

paypal_example.paypal_example:
  path: '/paypal_example/form'
  defaults:
    _form: '\Drupal\paypal_example\Form\PaypalExample'
    _title: 'PaypalExample'
  requirements:
    _access: 'TRUE'
 

And Finally, the code that should be in our src/Form/PaypalExample.php

<?php
 
namespace Drupal\paypal_example\Form;
 
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
 
/**
 * Class PaypalExample.
 */
class PaypalExample extends FormBase {
 
 
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'paypal_example';
  }
 
  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['amount'] = [
      '#type' => 'number',
      '#title' => $this->t('Amount'),
      '#description' => $this->t('The amount in store currency'),
      '#weight' => '0',
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Pay with paypal'),
    ];
 
    return $form;
  }
 
  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
  }
 
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    /**
     * @TODO::implement paypal checkout on submit
     * will update code from here
     */
 
 
  }
 
}

If you enable the module now, you can be able to access the example form via the route defined in the paypal_example.routing.yml i.e www.yoursitename.com/paypal_example/form.

We will be required to download and install paypal-php-sdk via composer, to do this, open your favourite cli,
cd/.. into the root directory of your project and run composer require paypal/rest-api-sdk-php.

And now time to make our api calls.

Blogs