mlocati/nexi-xpay-web

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

2.0.0 2024-05-24 08:05 UTC

This package is auto-updated.

Last update: 2024-10-27 16:19:04 UTC


README

Tests

MLocati's unofficial Nexi XPay Web client library for PHP

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

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

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-xpay-web

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\XPayWeb\Configuration::DEFAULT_APIKEY_TEST constant.

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

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

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

use MLocati\Nexi\XPayWeb\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\XPayWeb\Configuration interface.

The Nexi Client

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

You can create an instance of it simply with:

use MLocati\Nexi\XPayWeb\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\XPayWeb\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\XPayWeb\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\XPayWeb\CorrelationProvider interface. Then, when you create the Nexi client, you can write some code like this:

use MLocati\Nexi\XPayWeb\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\XPayWeb\Dictionary\Currency;
    use MLocati\Nexi\XPayWeb\Dictionary\Language;
    use MLocati\Nexi\XPayWeb\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
            }
        }
    }