Payline
Install the PHP modules php-curl
, php-http
and php-soap
:
$ sudo apt install php-curl php-http php-soap
IMPORTANT: PHP 7 must be compiled with openssl
.
For PHP 5:
$ sudo apt install php-openssl
Install composer if necessary:
$ sudo apt install composer
Install the Payline SDK at the root of the site:
$ composer require monext/payline-sdk
IMPORTANT: Create the directory vendor/monext/payline-sdk/logs writable by the user or the group www-data
of Apache.
Edit the file payline.inc in the folder includes.
payline.inc
- global $payline_context;
- $payline_context='homo'; // 'homo' or 'prod'
- global $payline_merchant_id, $payline_access_key, $payline_contract_number;
- if ($payline_context == 'homo') {
- $payline_merchant_id = false;
- $payline_access_key = false;
- $payline_contract_number = '1234567';
- }
- else {
- $payline_merchant_id = false;
- $payline_access_key = false;
- $payline_contract_number = false;
- }
- global $payline_proxy_host, $payline_proxy_port, $payline_proxy_login, $payline_proxy_password;
- $payline_proxy_host=false;
- $payline_proxy_port=false;
- $payline_proxy_login=false;
- $payline_proxy_password=false;
- global $payline_log;
- $payline_log=true;
Initialize $payline_merchant_id
, $payline_access_key
and $payline_contract_number
in the case where $payline_context
is 'homo'
with the values defined in the configuration of your homologation account.
IMPORTANT: Make sure that $payline_context
is set to 'homo'
.
Create a link to the file tests/testpayline.php in the root folder of the site:
$ ln tests/testpayline.php testpayline.php
Execute the test:
$ php -f testpayline.php
Array
(
[result] => Array
(
[code] => 00000
[shortMessage] => ACCEPTED
[longMessage] => Transaction approved
)
[token] => 2p2wuRtuJicb6YZTj6071491346010891
[redirectURL] => https://homologation-webpayment.payline.com/webpayment/step2.do?reqCode=prepareStep2&token=2p2wuRtuJicb6YZTj6071491346010891
)
The program displays the result of a WebPayment
request.
In case of error or if the request returns a different code than 00000 ACCEPTED
, check the configuration of your homologation account and the parameters in the file payline.inc.
If $payline_log
is true
, a line has been appended at the end of the file log/payline.log:
2017-04-05 00:46:44 127.0.0.1 METHOD=WebPaymentRequest;CODE=00000;TOKEN=2p2wuRtuJicb6YZTj6071491346010891;MESSAGE=ACCEPTED:Transaction approved
Git
Delete the link on the file tests/testpayline.php in the root folder of the site:
$ rm testpayline.php
Edit the file .gitignore at the root of the site and add the following line:
vendor
The folder vendor isn't included in the deposit.
Commit this version:
$ git add .gitignore composer.json composer.lock
$ git commit -m'Adds Payline SDK'
Test
To validate a complete payment by the site, activate the donation form by editing the files donate.php and donateme.php in the folder blocks:
donate.php
- require_once 'payline.inc';
- function donate($lang) {
- global $payline_merchant_id, $payline_access_key, $payline_contract_number;
- if (empty($payline_merchant_id) or empty($payline_access_key) or empty($payline_contract_number)) {
- return false;
- }
The standard code is ready for Paypal.
Just modify the test on the list of configuration parameters for Payline in the block donate
and replace the call to the function paypal_checkout
by a call to the function payline_checkout
in the block donateme
.
donateme.php
- require_once 'actions/paylinecheckout.php';
- paylinecheckout($lang, $amount, $currency);
Go to the home page of the site.
If the home page doesn't display the button in the banner, make sure that the action home
builds the block banner
with the parameter donate
at true
.
You can also directly enter the URL /en/donation in the address bar of the navigator.
In case of error, check if the action donation
is properly associated to a URL in the file aliases.inc of the folder config.
NOTE: Changing the aspect of the payment button is not a problem.
Enter an amount of 25 EUR and press on . The navigator is redirected to the payment page of Payline.
IMPORTANT: Check that Payline indicates that you are in a test environment.
Start by cancelling the payment. The site displays the page which confirms the cancellation of a payment.
Payment cancelled
Your payment of 25.00 EUR has been cancelled. If you think this is an error, please contact us.
Retry the operation this time validating the payment with the card number and the cryptogram provided by Payline for test purposes, e.g. 9250004780000046024 and 071. Payline displays the payment ticket. End the operation. The site displays the page which confirms a payment.
Payment accepted
Your payment of 25.00 EUR has been accepted. Thank you for your confidence and support.
If a payment is rejected, the site displays yet another page.
Payment rejected
Your payment of 25.00 EUR has been rejected. If you think this is an error, please contact us.
Display the log of the operations in the file of the day in the folder vendor/monext/payline-sdk/logs.
To switch the site in production mode with real payments, initialize $payline_merchant_id
, $payline_access_key
and $payline_contract_number
in the case where $payline_context
is not 'homo'
with the values defined in the configuration of your production account.
Set the parameter $payline_context
to 'prod'
.
Exchanges
A payment is initiated by calling the action paylinecheckout
.
This action prepares the parameters for a WebPayment request to the Payline SDK and loads the payment page from the Payline website.
It calls the function payline_dowebpayment
which returns a token and a URL, saves the token and the data about the payment in the session and terminates by reloading the URL.
The user is on the Payline website.
If the payment is cancelled or rejected, Payline calls the action paylinecancel
.
If the payment is accepted, the action paylinereturn
is called.
Both actions compare the token passed by Payline with the token saved in the session and call the function payline_getwebpaymentdetails
to obtain the status of the operation.
paylinecancel
runs the action paymentcancelled
if the code returned by Payline indicates that the user cancelled the operation or the action paymentrejected
in all the other cases.
paylinereturn
always runs the action paymentaccepted
.
Code
paylinecheckout.php
- require_once 'payline.php';
- require_once 'userisidentified.php';
- require_once 'validatecurrency.php';
Loads the code of the functions payline_dowebpayment
, payline_amt
and payline_currency
which interface Payline as well as the configuration file payline.inc.
Loads the code of the functions user_is_identified
and validate_currency
.
- function paylinecheckout($lang, $amount, $currency, $tax=0, $context=false) {
The function paylinereturn
has 5 parameters: the language, the total amount of the payment all taxes included, the currency of the payment, the total tax amount and an optional array which can be used to carry through the whole process details on the payment such as a product number, billing informations, etc.
- global $base_url, $payline_contract_number;
Accesses the global variable $base_url
which contains the URL of the entry point of the site and the configuration parameter $payline_contract_number
needed for the Payline interface.
- if (!user_is_identified()) {
- return run('error/unauthorized', $lang);
- }
Checks if the user is identified.
- if (!(is_numeric($amount) and $amount > 0)) {
- return run('error/badrequest', $lang);
- }
- $amt=payline_amt($amount);
Checks the parameter $amount
and formats it for the Payline interface.
- if (!validate_currency($currency)) {
- return run('error/badrequest', $lang);
- }
- $currencycode=payline_currency($currency);
Checks the parameter $currency
and formats it for the Payline interface.
- if (!(is_numeric($tax) and $tax >= 0)) {
- return run('error/badrequest', $lang);
- }
- $taxamt=payline_amt($tax);
Checks the parameter $tax
and formats it for the Payline interface.
- $itemamt=payline_amt($amount-$tax);
Calculates and formats the total price free of tax.
- $params = array();
- $params['payment']['contractNumber'] = $payline_contract_number;
- $params['payment']['amount'] = $amt;
- $params['payment']['currency'] = $currencycode;
- $params['payment']['action'] = 101;
- $params['payment']['mode'] = 'CPT';
- $params['order']['ref'] = 'P' . time();
- $params['order']['amount'] = $itemamt;
- $params['order']['taxes'] = $taxamt;
- $params['order']['currency'] = $currencycode;
- $params['order']['date'] = date('d/m/Y H:i');
- $params['returnURL'] = $base_url . url('paylinereturn', $lang);
- $params['cancelURL'] = $base_url . url('paylinecancel', $lang);
- $params['languageCode'] = $lang;
Prepares the parameters required by the Payline interface for a request for an immediate payment.
- $r = payline_dowebpayment($params);
- if (!$r) {
- return run('error/internalerror', $lang);
- }
Calls the function payline_dowebpayment
which send a request to the Payline site.
Displays the page signaling an internal error if the operation has failed.
- $token = $r['token'];
- $url = $r['redirectURL'];
Extracts the value of the token given to the operation by Payline and the address of the payment page on the Payline site.
- $_SESSION['payline'] = compact('token', 'amount', 'currency', 'tax', 'context');
Saves in the session the token for verification when Payline returns and the parameters of the payment.
- reload($url);
- }
Loads the payment page of the Payline site in the navigator.
paylinereturn.php
- require_once 'payline.php';
Loads the code of the function payline_getwebpaymentdetails
and the configuration file payline.inc.
- function paylinereturn($lang, $arglist=false) {
paylinereturn
is an action associated to a URL in aliases.inc.
- if (!isset($_SESSION['payline'])) {
- return run('error/badrequest', $lang);
- }
- extract($_SESSION['payline']); // token, amount, tax, currency, context
- unset($_SESSION['payline']);
Checks if a payment request is saved in the session. Extracts the parameters of the payment. Cleans up the session.
- if (!isset($arglist['token']) or $arglist['token'] != $token) {
- return run('error/badrequest', $lang);
- }
Checks if the token passed as a parameter in the request by Payline is identical to the one saved in the session.
- $params = array();
- $params['token'] = $token;
- $r = payline_getwebpaymentdetails($params);
- if (!$r) {
- return run('error/internalerror', $lang);
- }
- $return_code=$r['result']['code'];
- $transaction_id=$r['transaction']['id'];
- $authorization_number=$r['authorization']['number'];
Prepares the call to the function payline_getwebpaymentdetails
which returns the status of the payment by Payline.
Gets some useful parameters given by payline_getwebpaymentdetails
.
NOTE: This information and the data in $context
can be used to save the details of a payment in the DB.
- require_once 'actions/paymentaccepted.php';
- $output = paymentaccepted($lang, $amount, $currency, $context);
- return $output;
- }
Calls the code which generates the page for a payment which is accepted and returns it.
paylinecancel.php
- require_once 'payline.php';
- function paylinecancel($lang, $arglist=false) {
- if (!isset($_SESSION['payline'])) {
- return run('error/badrequest', $lang);
- }
- extract($_SESSION['payline']); // token, amount, tax, currency, context
- unset($_SESSION['payline']);
- if (!isset($arglist['token']) or $arglist['token'] != $token) {
- return run('error/badrequest', $lang);
- }
- $params = array();
- $params['token'] = $token;
- $r = payline_getwebpaymentdetails($params);
- if (!$r) {
- return run('error/internalerror', $lang);
- }
- $return_code=$r['result']['code'];
The code of the actions paylinereturn
and paylinecancel
is almost identical.
- $cancelled=($return_code == '02319');
- if ($cancelled) {
- require_once 'actions/paymentcancelled.php';
- $output = paymentcancelled($lang, $amount, $currency, $context);
- }
- else {
- require_once 'actions/paymentrejected.php';
- $output = paymentrejected($lang, $amount, $currency, $context);
- }
- return $output;
- }
Analyzes the return code of Payline.
The value 02319
signals an operation cancelled by the user.
Another code indicates that the operation was rejected and why.
Returns the page for a payment which is cancelled or a payment which is rejected depending on the case.
See the documentation on the functions which interface the Payline SDK defined in payline by the library.
Comments