mlocati/nexi-xpay-web 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mlocati/nexi-xpay-web

Composer 安装命令:

composer require mlocati/nexi-xpay-web

包简介

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

README 文档

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

mlocati/nexi-xpay-web 适用场景与选型建议

mlocati/nexi-xpay-web 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 652 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 05 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「payment」 「gateway」 「nexi」 「xpay」 「x-pay」 「intesa」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 mlocati/nexi-xpay-web 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mlocati/nexi-xpay-web 我们能提供哪些服务?
定制开发 / 二次开发

基于 mlocati/nexi-xpay-web 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 652
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 24
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-15