4
443

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 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
  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.     }

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
  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 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 Pay is not a problem.

Enter 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. 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
  1. require_once 'payline.php';
  2. require_once 'userisidentified.php';
  3. 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.

  1. 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.

  1.     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.

  1.     if (!user_is_identified()) {
  2.         return run('error/unauthorized', $lang);
  3.     }

Checks if the user is identified.

  1.     if (!(is_numeric($amount) and $amount > 0)) {
  2.         return run('error/badrequest', $lang);
  3.     }
  4.     $amt=payline_amt($amount);

Checks the parameter $amount and formats it for the Payline interface.

  1.     if (!validate_currency($currency)) {
  2.         return run('error/badrequest', $lang);
  3.     }
  4.     $currencycode=payline_currency($currency);

Checks the parameter $currency and formats it for the Payline interface.

  1.     if (!(is_numeric($tax) and $tax >= 0)) {
  2.         return run('error/badrequest', $lang);
  3.     }
  4.     $taxamt=payline_amt($tax);

Checks the parameter $tax and formats it for the Payline interface.

  1.     $itemamt=payline_amt($amount-$tax);

Calculates and formats the total price free of tax.

  1.     $params = array();
  2.  
  3.     $params['payment']['contractNumber'] = $payline_contract_number;
  4.  
  5.     $params['payment']['amount'] = $amt;
  6.     $params['payment']['currency'] = $currencycode;
  7.     $params['payment']['action'] = 101;
  8.     $params['payment']['mode'] = 'CPT';
  9.  
  10.     $params['order']['ref'] = 'P' . time();
  11.     $params['order']['amount'] = $itemamt;
  12.     $params['order']['taxes'] = $taxamt;
  13.     $params['order']['currency'] = $currencycode;
  14.     $params['order']['date'] = date('d/m/Y H:i');
  15.  
  16.     $params['returnURL'] = $base_url . url('paylinereturn', $lang);
  17.     $params['cancelURL'] = $base_url . url('paylinecancel', $lang);
  18.  
  19.     $params['languageCode'] = $lang;

Prepares the parameters required by the Payline interface for a request for an immediate payment.

  1.     $r = payline_dowebpayment($params);
  2.  
  3.     if (!$r) {
  4.         return run('error/internalerror', $lang);
  5.     }

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.

  1.     $token = $r['token'];
  2.     $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.

  1.     $_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.

  1.     reload($url);
  2. }

Loads the payment page of the Payline site in the navigator.

paylinereturn.php
  1. require_once 'payline.php';

Loads the code of the function payline_getwebpaymentdetails and the configuration file payline.inc.

  1. function paylinereturn($lang, $arglist=false) {

paylinereturn is an action associated to a URL in aliases.inc.

  1.     if (!isset($_SESSION['payline'])) {
  2.         return run('error/badrequest', $lang);
  3.     }
  4.  
  5.     extract($_SESSION['payline']);  // token, amount, tax, currency, context
  6.  
  7.     unset($_SESSION['payline']);

Checks if a payment request is saved in the session. Extracts the parameters of the payment. Cleans up the session.

  1.     if (!isset($arglist['token']) or $arglist['token'] != $token) {
  2.         return run('error/badrequest', $lang);
  3.     }

Checks if the token passed as a parameter in the request by Payline is identical to the one saved in the session.

  1.     $params = array();
  2.  
  3.     $params['token'] = $token;
  4.  
  5.     $r = payline_getwebpaymentdetails($params);
  6.  
  7.     if (!$r) {
  8.         return run('error/internalerror', $lang);
  9.     }
  10.  
  11.     $return_code=$r['result']['code'];
  12.  
  13.     $transaction_id=$r['transaction']['id'];
  14.     $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.

  1.     require_once 'actions/paymentaccepted.php';
  2.  
  3.     $output = paymentaccepted($lang, $amount, $currency, $context);
  4.  
  5.     return $output;
  6. }

Calls the code which generates the page for a payment which is accepted and returns it.

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'];

The code of the actions paylinereturn and paylinecancel is almost identical.

  1.     $cancelled=($return_code == '02319');
  2.  
  3.     if ($cancelled) {
  4.         require_once 'actions/paymentcancelled.php';
  5.  
  6.         $output = paymentcancelled($lang, $amount, $currency, $context);
  7.     }
  8.     else {
  9.         require_once 'actions/paymentrejected.php';
  10.  
  11.         $output = paymentrejected($lang, $amount, $currency, $context);
  12.     }
  13.  
  14.     return $output;
  15. }

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

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].