PHPackages                             mlocati/nexi-xpay-web - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Payment Processing](/categories/payments)
4. /
5. mlocati/nexi-xpay-web

ActiveLibrary[Payment Processing](/categories/payments)

mlocati/nexi-xpay-web
=====================

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

2.0.0(1y ago)140↓100%1MITPHPPHP &gt;= 7.2

Since Apr 5Pushed 1y ago1 watchersCompare

[ Source](https://github.com/mlocati/nexi-xpay-web)[ Packagist](https://packagist.org/packages/mlocati/nexi-xpay-web)[ Docs](https://github.com/mlocati/nexi-xpay-web)[ GitHub Sponsors](https://github.com/sponsors/mlocati)[ Fund](https://paypal.me/mlocati)[ RSS](/packages/mlocati-nexi-xpay-web/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (4)Used By (1)

[![Tests](https://github.com/mlocati/nexi-xpay-web/actions/workflows/test.yml/badge.svg)](https://github.com/mlocati/nexi-xpay-web/actions/workflows/test.yml)

MLocati's unofficial Nexi XPay Web client library for PHP
=========================================================

[](#mlocatis-unofficial-nexi-xpay-web-client-library-for-php)

This project contains a PHP library that makes it easy to use the [Nexi](https://www.nexi.it) 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](https://github.com/mlocati/nexi-xpay).

It has been built (almost) automatically from [the official documentation](https://developer.nexi.it/en) (see the [`/build`](https://github.com/mlocati/nexi-xpay-web/tree/main/build) directory, which is only available if you clone this repository).

Installation
------------

[](#installation)

### Install with composer

[](#install-with-composer)

Simply run the following command:

```
composer require mlocati/nexi-xpay-web
```

### Manual installation

[](#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
-----

[](#usage)

### Configuration

[](#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](https://github.com/mlocati/nexi-xpay-web/blob/main/src/Configuration.php).

### The Nexi Client

[](#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

[](#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:

- if the [cURL PHP extension](https://www.php.net/manual/en/book.curl.php) is available, it uses it (see the `MLocati\Nexi\XPayWeb\HttpClient\Curl` class)
- otherwise, if the [PHP HTTP stream wrapper](https://www.php.net/manual/en/context.http.php) is enabled, it uses it (it requires the [OpenSSL PHP extension](https://www.php.net/manual/en/book.openssl.php) - see the `MLocati\Nexi\XPayWeb\HttpClient\StreamWrapper` class)

You can also provide your own implementation, provided it implements [the `MLocati\Nexi\XPayWeb\HttpClient` interface](https://github.com/mlocati/nexi-xpay-web/blob/main/src/HttpClient.php). 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

[](#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](https://github.com/mlocati/nexi-xpay-web/blob/main/src/CorrelationProvider.php). 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

[](#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](https://developer.nexi.it/en).

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: ```
