Integration with a Library
You can integrate Paysera into your website by downloading our open source library libwebtopay. The library handles all the security parameters of transferred and received data for you.
Available libraries​
Official libraries:
- WebToPay for PHP (from GitHub):
git clone https://github.com/paysera/lib-webtopay - WebToPay for .NET (from GitHub):
git clone https://github.com/evp/webtopay-lib-dotnet
From the archive, the only file you need is libwebtopay/WebToPay.php. The other files are tests, examples and explanations.
Paysera can also be integrated using third-party open source libraries:
- Omnipay 2.x - payment processing library for PHP (from GitHub):
git clone https://github.com/povils/omnipay-paysera.git
libwebtopay can be used for both micro- and macro-services. You will only need to call two methods: one prepares the data for transfer, the other verifies the response about the execution of a payment.
A working example follows. Locations where your Paysera access data has to be entered are marked with comments.
Prerequisites​
Before you begin, ensure you have:
- PHP installed on your server
- Your
project_idandproject_passwordfrom Obtaining Credentials
Step-by-Step Implementation​
Step 1: Create a folder​
Create a folder named libwebtopay. All payment integration files will be stored here.
mkdir libwebtopay
cd libwebtopay
Step 2: Download the libwebtopay library​
Download WebToPay.php into the libwebtopay folder you just created.
Step 3: Create a file that redirects users to the Paysera website​
In the libwebtopay folder, create redirect.php with the following contents:
<?php
require_once('WebToPay.php');
function getSelfUrl(): string
{
$url = substr(strtolower($_SERVER['SERVER_PROTOCOL']), 0, strpos($_SERVER['SERVER_PROTOCOL'], '/'));
if (isset($_SERVER['HTTPS']) === true) {
$url .= ($_SERVER['HTTPS'] === 'on') ? 's' : '';
}
$url .= '://' . $_SERVER['HTTP_HOST'];
if (isset($_SERVER['SERVER_PORT']) === true && $_SERVER['SERVER_PORT'] !== '80') {
$url .= ':' . $_SERVER['SERVER_PORT'];
}
$url .= dirname($_SERVER['SCRIPT_NAME']);
return $url;
}
try {
WebToPay::redirectToPayment([
'projectid' => {{YOUR_PROJECT_ID}},
'sign_password' => {{YOUR_PROJECT_PASSWORD}},
'orderid' => 0,
'amount' => 1000,
'currency' => 'EUR',
'country' => 'LT',
'accepturl' => getSelfUrl() . '/accept.php',
'cancelurl' => getSelfUrl() . '/cancel.php',
'callbackurl' => getSelfUrl() . '/callback.php',
'test' => 0,
]);
} catch (Exception $exception) {
echo get_class($exception) . ':' . $exception->getMessage();
}
Instead of WebToPay::redirectToPayment, you can use WebToPay::buildRequest, which accepts the same parameters and returns the data array necessary for the transfer.
You can find the full list of available parameters here.
Step 4: Create accept.php​
In the libwebtopay folder, create accept.php with the following contents:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<title></title>
</head>
<body>
Thank you for buying.
</body>
</html>
Step 5: Create cancel.php​
In the libwebtopay folder, create cancel.php with the following contents:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<title></title>
</head>
<body>
Payment canceled.
</body>
</html>
Step 6: Processing of payments​
In the libwebtopay folder, create callback.php with the following contents:
<?php
require_once('WebToPay.php');
function isPaymentValid(array $order, array $response): bool
{
if (array_key_exists('payamount', $response) === false) {
if ($order['amount'] !== $response['amount'] || $order['currency'] !== $response['currency']) {
throw new Exception('Wrong payment amount');
}
} else {
if ($order['amount'] !== $response['payamount'] || $order['currency'] !== $response['paycurrency']) {
throw new Exception('Wrong payment amount');
}
}
return true;
}
try {
$response = WebToPay::validateAndParseData(
$_REQUEST,
{{YOUR_PROJECT_ID}},
{{YOUR_PROJECT_PASSWORD}}
);
if ($response['status'] === '1' || $response['status'] === '3') {
//@ToDo: Validate payment amount and currency, example provided in isPaymentValid method.
//@ToDo: Validate order status by $response['orderid']. If it is not already approved, approve it.
echo 'OK';
} else {
throw new Exception('Payment was not successful');
}
} catch (Exception $exception) {
echo get_class($exception) . ':' . $exception->getMessage();
}
The transferred parameters are returned by WebToPay::checkResponse or WebToPay::validateAndParseData. The data itself is encrypted, so it usually cannot be taken from GET parameters.
In case of success, the callback script must return a response beginning with or equal to OK.
Next Steps​
- Check Examples for more use cases
- Review FAQ for common questions
- Learn about Processing Callbacks