mlocati/nexi

An unofficial SDK for the Nexi payment gateway

1.1.0 2024-04-08 07:37 UTC

This package is auto-updated.

Last update: 2024-04-08 07:38:36 UTC


README

Tests

MLocati's unofficial Nexi XPay client library for PHP

This project contains a PHP library that makes it easy to use the Nexi XPay APIs.

It has been built (almost) automatically from the official documentation (see the /build directory, which is only available if you clone this repository).

Installation

Install with composer

Simply run the following command:

composer require mlocati/nexi

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 an API Key (generated by your XPay back office). For test environment, you can use the value of the MLocati\Nexi\Configuration::DEFAULT_APIKEY_TEST constant.

You also need the base URL of the Nexi API. You can find them again in MLocati\Nexi\Configuration:

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

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

use MLocati\Nexi\Configuration;

// For test environment
$configuration = new Configuration\FromArray(['environment' => 'test']);
// For production environment
$configuration = new Configuration\FromArray(['apiKey' => 'your API key']);

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\Configuration interface.

The Nexi Client

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

You can create an instance of it simply with:

use MLocati\Nexi\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\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\Client;

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

The Correlation-Id Header

Every request to the Nexi servers is associated to an unique identifier, sent via an HTTP header named Correlation-Id. By default, the Next client randomly generates it and doesn't store it. If you want to generate the value of the Correlation-Id header on your own, or if you want to log the generated Correlation-Id values, you can create a custom class that implements the MLocati\Nexi\CorrelationProvider interface. Then, when you create the Nexi client, you can write some code like this:

use MLocati\Nexi\Client;

$correlationProvider = new My\Custom\CorrelationProvider();
$client = new Client($configuration, null, $correlationProvider);

Sample Usage

The methods provided by Nexi client has well documented documentation (see the PHPDoc comments). The Nexi client provided by this library allows you to use all 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 your website and clicks a "Pay" button which invokes a route on your project that executes this code:
    <?php
    use MLocati\Nexi\Dictionary\Currency;
    use MLocati\Nexi\Dictionary\Language;
    use MLocati\Nexi\Entity\CreateOrderForHostedPayment\Request;
    
    $currency = Currency::ID_EUR;
    $amount = 123.45;
    $internalOrderID = 'internal-order-id';
    
    $currencyService = new Currency();
    
    $request = new Request();
    $request->getOrCreatePaymentSession()
        ->setActionType('PAY')
        ->setAmount($currencyService->formatDecimals($amount, $currency))
        ->setLanguage(Language::ID_ITA)
        ->setResultUrl('http://your.website/callback')
        ->setCancelUrl('http://your.website/payment-canceled')
    ;
    
    $order = $request->getOrCreateOrder();
    $order
        ->setOrderId($internalOrderID)
        ->setAmount($currencyService->formatDecimals($amount, $currency))
        ->setCurrency($currency)
        ->setDescription('The description of your order')
    ;
    $order->getOrCreateCustomerInfo()
        ->setCardHolderEmail('your.customer@email.address')
    ;
    
    $response = $client->createOrderForHostedPayment($request);
    
    // Store somewhere your $internalOrderID, for example with $_SESSION['order-id'] = $internalOrderID
  2. you then redirects your customer to the URL provided by $response->getHostedPage()
  3. when the customer pays on the Nexi website, he is redirected to your website at the URL used in the setResultUrl() above. When that URL is called, you can have some code like this:
    // retrieve your internal order ID, for example with $internalOrderID = $_SESSION['order-id']
    $order = $client->findOrderById($internalOrderID);
    foreach ($order->getOperations() as $operation) {
        if ($operation->getOperationType() === 'AUTHORIZATION') {
            switch ($operation->getOperationResult()) {
                case 'AUTHORIZED':
                case 'EXECUTED':
                    // the customer has paid the order (or at least has authorized it)
                    break;
                case 'CANCELED': // Operation canceled by the cardholder
                case 'THREEDS_FAILED': // Operation canceled by the cardholder during 3DS
                    // the user refused to pay
                    break;
                default:
                    // some other error occurred
                    break
            }
        }
    }