A PHP package for integrating with Paytrail's E2 interface.

1.2.0 2020-10-09 06:52 UTC


Latest Stable Version PHP Test Suite Total Downloads License

A PHP client library for creating payments with Paytrail Form Interface (E2).


Install via composer

composer require paytrail/e2-module


Paytrail official documentation can be found here.


Payment without customer and product information

use Paytrail\E2Module\Merchant;
use Paytrail\E2Module\E2Payment;

$merchant = Merchant::create($merchantId, $merchantSecret);
$e2Payment = new E2Payment($merchant);

echo $e2Payment->addAmount($orderAmount)

Payment widget with customer, product information and custom return urls

Include customer information, discounted product and custom return urls. Payment, customer and product properties can be found from documentation

use Paytrail\E2Module\Merchant;
use Paytrail\E2Module\E2Payment;
use Paytrail\E2Module\Product;
use Paytrail\E2Module\Customer;

$merchant = Merchant::create($merchantId, $merchantSecret);

$e2Payment = new E2Payment($merchant);

$customer = Customer::create([
    'PAYER_PERSON_LASTNAME' => 'Customer',
    'PAYER_PERSON_ADDR_STREET' => 'Test street 1',
    'PAYER_PERSON_ADDR_TOWN' => 'Helsinki',
    'PAYER_PERSON_PHONE' => '040123456',

$paymentData = [
    'URL_SUCCESS' => 'https://url/to/shop/successUrl',
    'URL_CANCEL' => 'https://url/to/shop/cancelUrl',
    'URL_NOTIFY' => 'https://url/to/shop/notifyUrl',

$product = Product::create([
    'ITEM_TITLE' => 'Test Product',
    'ITEM_ID' => '1234',
    'ITEM_UNIT_PRICE' => 50,
    'ITEM_QUANTITY' => 2,
$shipping = Product::create([
    'ITEM_TITLE' => 'Shipping',
    'ITEM_ID' => '001',
    'ITEM_UNIT_PRICE' => 5,

echo $e2Payment->addCustomer($customer)
    ->createPayment($orderNumber, $paymentData)
    ->addProducts([$product, $shipping])

Rendering a custom payment form

use Paytrail\E2Module\Merchant;
use Paytrail\E2Module\E2Payment;

$merchant = Merchant::create($merchantId, $merchantSecret);
$e2Payment = new E2Payment($merchant);

$fields = $e2Payment->addAmount($orderAmount)

foreach ($fields as $name => $value) {
    // Your form logic should generate a hidden input field:
    // <input type="hidden" name="$name", value="$value">

Set form action to $e2Payment->getPaymentUrl();.

Validating completed payment

After returning from payment, whether success or cancelled, validate return authcode. Same validation applies to notify url.

$isValidPayment = $e2Payment->returnAuthcodeIsValid($_GET);

You can also send return parameters as array instead of using $_GET superglobal. If return code is not valid, you can get validation errors.

$errorReasons = $e2Payment->getErrors();

This return array of all error reasons in return authcode validation.

To get status of payment, paid or not.

$isPaid = $e2Payment->isPaid($_GET);

Validating payment from notification

If customer doesn't return back after payment, status can be verified from capturing payment data from notify url. Return authcode validation is similar than success and cancelled payment, but you also need determine payment status.

$isValidPayment = $e2Payment->returnAuthcodeIsValid($_GET);
if (!$isValidPayment) {
    // code to handle invalid validation.

$isPaid = $e2Payment->isPaid($_GET);
// Code to handle paid/cancelled status for order.

Getting payment id

You should save payment id from payment to order details. This id is used for making refunds, and is always unique. Paytrail customer care can also use payment id to track order in case of any problems. Payment id can be also used to get payment details from Merchant Panel.

$paymentId = $_GET['PAYMENT_ID'];