PHP code example of seberm / paypal-component

1. Go to this page and download the library: Download seberm/paypal-component library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

seberm / paypal-component example snippets


/** @var \Seberm\Components\PayPal\Buttons\IOrderFactory @inject */
public $factory;


/** @var \Seberm\Components\PayPal\Buttons\IOrderFactory $factory */
public $factory;

/**
 * @param \Seberm\Components\PayPal\Buttons\IOrderFactory $factory
 */
public function injectFactory(\Seberm\Components\PayPal\Buttons\IOrderFactory $factory)
{
    $this->factory = $factory;
}

/** @var \Seberm\Components\PayPal\Buttons\Order */
private $orderButton;

public function startup()
{
    parent::startup();

    $this->orderButton = $this->factory->create();
    $this->orderButton->setSessionSection($this->session->getSection('paypal'));
    $this->orderButton->onSuccessPayment[] = array($this, 'successPayment');
}

/**
 * Creates new button control. After that you can load this control in template
 * via {control paypalButton}.
 * @return Seberm\Components\PayPal\Buttons\Order
 */
protected function createComponentPaypalButton()
{
    $control = $this->orderButton;
    $control->setCurrencyCode(\Seberm\PayPal\API\API::CURRENCY_EURO);
    $control->onConfirmation[] = array($this, 'confirmOrder');
    $control->onError[] = array($this, 'errorOccurred');
    $control->onCancel[] = array($this, 'canceled');

    // It is possible to set shipping
    $button->shipping = 4.3;

    // or set a tax
    $button->tax = 3.1;

    $price = 56; // In Euro in this example

    $control->addItemToCart('Product A', 'A - Product description', $price);
    $control->addItemToCart('Product B', 'B - Product description', 123);

    return $control;
}

public function successPayment($data) {
    /**
     * Here you can proccess information about user. For example save him to the
     * database...
     */

     $payerID = $data->payerID;
     $firstName = $data->firstName;
     $lastName = $data->lastName;
     $email = $data->email;

     // See dump($data);
}

public function errorOccurred($errors)  { ... }

// It is called if payment inicialization succeeds
public function confirmOrder($data) {
    /**
     * Here you can do some checks of the order data. If everything is ok,
     * you can confirm the order with confirmExpressCheckout() method.
     */
    ...
    $this->orderButton->confirmExpressCheckout();
}

public function canceled($data)       { ... } // Called if user cancels his order
yml
parameters: ...

---------------------------
paypal:
    api:
       username: 'seberm_1332081338_biz_api1.gmail.com'
       password: '1332081363'
       signature: 'AWiH1IO0zFZrEQbbn0JwDZHbWukIAebmYjpOylRCqBGGgztea2bku.N4'
    sandbox: true # default is false

extensions:
    paypal: Seberm\DI\PayPalExtension
---------------------------

php:
    date.timezone: Europe/Prague
    ...