PHP code example of mlocati / nexi-xpay-web

1. Go to this page and download the library: Download mlocati/nexi-xpay-web library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mlocati / nexi-xpay-web example snippets




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']);

use MLocati\Nexi\XPayWeb\Client;

$client = new Client($configuration);

use MLocati\Nexi\XPayWeb\Client;

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

use MLocati\Nexi\XPayWeb\Client;

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

   
   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('[email protected]')
   ;

   $response = $client->createOrderForHostedPayment($request);

   // Store somewhere your $internalOrderID, for example with $_SESSION['order-id'] = $internalOrderID
   

   // 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
           }
       }
   }