PHPackages                             mlocati/nexi-xpay - 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

ActiveLibrary[Payment Processing](/categories/payments)

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

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

1.0.1(1mo ago)227[1 issues](https://github.com/mlocati/nexi-xpay/issues)1MITPHPPHP &gt;= 7.2CI passing

Since May 27Pushed 1mo ago1 watchersCompare

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

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

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

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

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

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

[](#installation)

### Install with composer

[](#install-with-composer)

Simply run the following command:

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

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

### The Nexi Client

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

[](#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\XPay\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\XPay\HttpClient\StreamWrapper` class)

You can also provide your own implementation, provided it implements [the `MLocati\Nexi\XPay\HttpClient` interface](https://github.com/mlocati/nexi-xpay/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\XPay\Client;

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

### Sample Usage

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

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