2

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
  1. global $payline_context;
  2.  
  3. $payline_context='homo';    // 'homo' or 'prod'
  4.  
  5. global $payline_merchant_id, $payline_access_key, $payline_contract_number;
  6.  
  7. if ($payline_context == 'homo') {
  8.     $payline_merchant_id = false;
  9.     $payline_access_key = false;
  10.     $payline_contract_number = '1234567';
  11. }
  12. else {
  13.     $payline_merchant_id = false;
  14.     $payline_access_key = false;
  15.     $payline_contract_number = false;
  16. }
  17.  
  18. global $payline_proxy_host, $payline_proxy_port, $payline_proxy_login, $payline_proxy_password;
  19.  
  20. $payline_proxy_host=false;
  21. $payline_proxy_port=false;
  22. $payline_proxy_login=false;
  23. $payline_proxy_password=false;
  24.  
  25. global $payline_log;
  26.  
  27. $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 testovhsms.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
  1. require_once 'payline.inc';
  2.  
  3. function donate($lang) {
  4.     global $payline_merchant_id, $payline_access_key, $payline_contract_number;
  5.  
  6.     if (empty($payline_merchant_id) or empty($payline_access_key) or empty($payline_contract_number)) {
  7.         return false;
  8.     }
donateme.php
  1.             require_once 'actions/paylinecheckout.php';
  2.  
  3.             paylinecheckout($lang, $amount, $currency);

Go to the home page of the site. If the home page doesn't display the button Donate in the banner, make sure that the action home builds the block banner passing the parameter donate at true. You can also directly enter the URL /en/donation in the address bar of the navigator.

Entrer an amount of 25 EUR and press on Donate. 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 and 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_dowebpayment 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
  1. require_once 'payline.php';
  2. require_once 'userisidentified.php';
  3. require_once 'validatecurrency.php';
  4.  
  5. function paylinecheckout($lang, $amount, $currency, $tax=0, $context=false) {
  6.     global $base_url, $payline_contract_number;
  7.  
  8.     if (!user_is_identified()) {
  9.         return run('error/unauthorized', $lang);
  10.     }
  11.  
  12.     if (!(is_numeric($amount) and $amount > 0)) {
  13.         return run('error/badrequest', $lang);
  14.     }
  15.     $amt=payline_amt($amount);
  16.  
  17.     if (!validate_currency($currency)) {
  18.         return run('error/badrequest', $lang);
  19.     }
  20.     $currencycode=payline_currency($currency);
  21.  
  22.     if (!(is_numeric($tax) and $tax >= 0)) {
  23.         return run('error/badrequest', $lang);
  24.     }
  25.     $taxamt=payline_amt($tax);
  26.  
  27.     $itemamt=payline_amt($amount-$tax);
  28.  
  29.     $params = array();
  30.  
  31.     $params['payment']['contractNumber'] = $payline_contract_number;
  32.  
  33.     $params['payment']['amount'] = $amt;
  34.     $params['payment']['currency'] = $currencycode;
  35.     $params['payment']['action'] = 101;
  36.     $params['payment']['mode'] = 'CPT';
  37.  
  38.     $params['order']['ref'] = 'P' . time();
  39.     $params['order']['amount'] = $itemamt;
  40.     $params['order']['taxes'] = $taxamt;
  41.     $params['order']['currency'] = $currencycode;
  42.     $params['order']['date'] = date('d/m/Y H:i');
  43.  
  44.     $params['returnURL'] = $base_url . url('paylinereturn', $lang);
  45.     $params['cancelURL'] = $base_url . url('paylinecancel', $lang);
  46.  
  47.     $params['languageCode'] = $lang;
  48.  
  49.     $r = payline_dowebpayment($params);
  50.  
  51.     if (!$r) {
  52.         return run('error/internalerror', $lang);
  53.     }
  54.  
  55.     $token = $r['token'];
  56.     $url = $r['redirectURL'];
  57.  
  58.     $_SESSION['payline'] = compact('token', 'amount', 'currency', 'tax', 'context');
  59.  
  60.     reload($url);
  61. }
paylinereturn.php
  1. require_once 'payline.php';
  2.  
  3. function paylinereturn($lang, $arglist=false) {
  4.     if (!isset($_SESSION['payline'])) {
  5.         return run('error/badrequest', $lang);
  6.     }
  7.  
  8.     extract($_SESSION['payline']);  // token, amount, tax, currency, context
  9.  
  10.     unset($_SESSION['payline']);
  11.  
  12.     if (!isset($arglist['token']) or $arglist['token'] != $token) {
  13.         return run('error/badrequest', $lang);
  14.     }
  15.  
  16.     $params = array();
  17.  
  18.     $params['token'] = $token;
  19.  
  20.     $r = payline_getwebpaymentdetails($params);
  21.  
  22.     if (!$r) {
  23.         return run('error/internalerror', $lang);
  24.     }
  25.  
  26.     $return_code=$r['result']['code'];
  27.  
  28.     $transaction_id=$r['transaction']['id'];
  29.     $authorization_number=$r['authorization']['number'];
  30.  
  31.     require_once 'actions/paymentaccepted.php';
  32.  
  33.     $output = paymentaccepted($lang, $amount, $currency, $context);
  34.  
  35.     return $output;
  36. }
paylinecancel.php
  1. require_once 'payline.php';
  2.  
  3. function paylinecancel($lang, $arglist=false) {
  4.     if (!isset($_SESSION['payline'])) {
  5.         return run('error/badrequest', $lang);
  6.     }
  7.  
  8.     extract($_SESSION['payline']);  // token, amount, tax, currency, context
  9.  
  10.     unset($_SESSION['payline']);
  11.  
  12.     if (!isset($arglist['token']) or $arglist['token'] != $token) {
  13.         return run('error/badrequest', $lang);
  14.     }
  15.  
  16.     $params = array();
  17.  
  18.     $params['token'] = $token;
  19.  
  20.     $r = payline_getwebpaymentdetails($params);
  21.  
  22.     if (!$r) {
  23.         return run('error/internalerror', $lang);
  24.     }
  25.  
  26.     $return_code=$r['result']['code'];
  27.  
  28.     $cancelled=($return_code == '02319');
  29.  
  30.     if ($cancelled) {
  31.         require_once 'actions/paymentcancelled.php';
  32.  
  33.         $output = paymentcancelled($lang, $amount, $currency, $context);
  34.     }
  35.     else {
  36.         require_once 'actions/paymentrejected.php';
  37.  
  38.         $output = paymentrejected($lang, $amount, $currency, $context);
  39.     }
  40.  
  41.     return $output;
  42. }

See the documentation on the functions which interface the Payline SDK defined in payline by the library.