mlocati/nexi-xpay

An unofficial SDK for the Nexi XPay payment gateway (not for Intesa Sanpaolo bank customers)

1.0.0 2024-05-27 15:23 UTC

This package is auto-updated.

Last update: 2024-08-27 15:46:04 UTC


README

This project contains a PHP library that makes it easy to use the Nexi XPay APIs (not for Intesa Sanpaolo bank).

This requires a merchant Alias and a MAC Key. If instead you have an API Key, you may need to use this other library.

Installation

Install with composer

Simply run the following command:

composer require mlocati/nexi-xpay

Manual installation

Download the code of this library, place it somewhere in your project, and add this PHP instruction before using anything of this library:

require '/path/to/nexi.php';

Usage

Configuration

First of all, you have need a merchant alias and a MAC Key (provided to you by Nexi).

You also need the base URL of the Nexi XPay API. You can find its default value in MLocati\Nexi\XPay\Configuration:

  • for test environments: you can use MLocati\Nexi\XPay\Configuration::DEFAULT_BASEURL_TEST
  • for production environments: you can use MLocati\Nexi\XPay\Configuration::DEFAULT_BASEURL_PRODUCTION

This library provides an easy way to represent a configuration, by using the MLocati\Nexi\XPay\Configuration\FromArray class:

use MLocati\Nexi\XPay\Configuration;

// For test environment
$configuration = new Configuration\FromArray([
    'alias' => 'YOUR ALIAS FOR TEST',
    'macKey' => 'YOUR MAC KEY FOR TEST',
    'environment' => 'test',
]);
// For production environment
$configuration = new Configuration\FromArray([
    'alias' => 'YOUR ALIAS FOR PRODUCTION',
    'macKey' => 'YOUR MAC KEY FOR PRODUCTION',
]);

Of course you can override the default base URL (use the baseUrl array key).

You can also use a custom class, provided it implements the MLocati\Nexi\XPay\Configuration interface.

The Nexi Client

The main class of this library is MLocati\Nexi\XPay\Client: it allows you invoking the Nexi APIs.

You can create an instance of it simply with:

use MLocati\Nexi\XPay\Client;

$client = new Client($configuration);

HTTP Communications

The Nexi client needs to perform HTTP requests. In order to do that, it automatically detects the best available way to do that:

You can also provide your own implementation, provided it implements the MLocati\Nexi\XPay\HttpClient interface. That way you can easily log the communication with the Nexi servers, as well as customize the HTTP client (for example because you are behind a proxy).

For example, if you want to use your custom HTTP client implementation, you can simply write:

use MLocati\Nexi\XPay\Client;

$myHttpClient = new My\Custom\HttpClient();
$client = new Client($configuration, $myHttpClient);

Sample Usage

The Nexi client provided by this library allows you to use some of the methods you can find in the Nexi documentation website.

Here's a sample code that allows you to accept payments:

  1. Your customer is on apage of your website where you want to place a "Pay with Nexi" button:
    <?php
    use MLocati\Nexi\XPay\Dictionary\Currency;
    use MLocati\Nexi\XPay\Dictionary\Language;
    use MLocati\Nexi\XPay\Entity\SimplePay\Request;
    
    $language = Language::ID_ENG;
    $currency = Currency::ID_EUR;
    $amount = 123.45;
    $internalOrderID = 'internal-order-id';
    
    $request = new Request();
    $request
        ->setLanguageId($language)
        ->setImportoAsDecimal($amount)
        ->setDivisa($currency)
        ->setCodTrans($internalOrderID)
        ->setUrl('http://your.website/callback')
        ->setUrl_back('http://your.website/payment-canceled')
    ;
    // Store somewhere your $internalOrderID, for example with $_SESSION['order-id'] = $internalOrderID
    ?>
    <form method="POST" action="<?= htmlspecialchars($client->getSimplePaySubmitUrl()) ?>">
        <?php
        foreach ($client->sign($request) as $field => $value) {
            ?>
            <input type="hidden" name="<?= htmlspecialchars($field) ?>" value="<?= htmlspecialchars($value) ?>" />
            <?php
        }
        ?>
        <button type="submit">Pay with Nexi</button>
    </form>
  2. when the users click the "Pay with Nexi" button, they will go to the Nexi servers where they can enter their credit card
  3. when the users pay on the Nexi website, they will come back to your website at the URL used in the setUrl() above. When that URL is called, you can have some code like this:
    <?php
    use MLocati\Nexi\XPay\Entity\SimplePay\Callback\Data;
    
    $data = Data::fromCustomerRequest($_GET);
    $data->checkMac($configuration);
    if ($data->getEsito() === \MLocati\Nexi\XPay\Entity\Response::ESITO_OK) {
        // The order has been paid
    } else {
        // Display an error message and let users come back to the checkout page
    }