PayPal basic library and Nette component

1.0.3 2016-04-28 05:51 UTC

This package is not auto-updated.

Last update: 2024-02-17 12:20:37 UTC



Simplest installation is using composer:

$ composer require seberm/paypal-component

or manually you can edit composer.json:

"require": {
        "seberm/paypal-component": "1.0.*"


A) Using DI Extension (Nette 2.1+)

Add following lines into your config.neon file.

parameters: ...

       username: ''
       password: '1332081363'
       signature: 'AWiH1IO0zFZrEQbbn0JwDZHbWukIAebmYjpOylRCqBGGgztea2bku.N4'
    sandbox: true # default is false

    paypal: Seberm\DI\PayPalExtension

    date.timezone: Europe/Prague

More about DI container extensions you can find here:

B) Manually throught factories

Alternatively you can configure component via factories.

            username: ''
            password: '1332081363'
            signature: 'AWiH1IO0zFZrEQbbn0JwDZHbWukIAebmYjpOylRCqBGGgztea2bku.N4'
        sandbox: true

        implement: Seberm\Components\PayPal\Buttons\IOrderFactory
            - setCredentials(%paypal.api%)
            - setSandBox(%paypal.sandbox%)

Example of a Presenter

Firstly, you have to get IOrderFactory object.

Getting IOrderFactory using DI extensions (method A)

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

Getting IOrderFactory using nette factories (method B)

/** @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;

Following code will be the same for both methods.

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

public function startup()

    $this->orderButton = $this->factory->create();
    $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->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;
Don't forget to define callback methods

This method is called after successful confirmation. It has one argument $data.

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);

Following method is called if some error occures (for example error in communication). It receives an array of errors.

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.

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

Adding PayPal button to a template

Add following control macro where you want to have your PayPal button.

{control paypalButton}