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

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

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

mlocati/nexi-xpay

Composer 安装命令:

composer require mlocati/nexi-xpay

包简介

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

README 文档

README

This project contains a PHP library that makes it easy to use the Nexi 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.

Installation

Install with composer

Simply run the following command:

composer require mlocati/nexi-xpay

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

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

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\XPay\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\XPay\Client;

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

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.

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:
    <?php
    use MLocati\Nexi\XPay\Dictionary\Currency;
    use MLocati\Nexi\XPay\Dictionary\Language;
    use MLocati\Nexi\XPay\Entity\SimplePay\Request;
    
    $language = Language::ID_ENG;
    $currency = Currency::ID_EUR;
    $amount = 123.45;
    $internalOrderID = 'internal-order-id';
    
    $request = new Request();
    $request
        ->setLanguageId($language)
        ->setImportoAsDecimal($amount)
        ->setDivisa($currency)
        ->setCodTrans($internalOrderID)
        ->setUrl('http://your.website/callback')
        ->setUrl_back('http://your.website/payment-canceled')
    ;
    // Store somewhere your $internalOrderID, for example with $_SESSION['order-id'] = $internalOrderID
    ?>
    <form method="POST" action="<?= htmlspecialchars($client->getSimplePaySubmitUrl()) ?>">
        <?php
        foreach ($client->sign($request) as $field => $value) {
            ?>
            <input type="hidden" name="<?= htmlspecialchars($field) ?>" value="<?= htmlspecialchars($value) ?>" />
            <?php
        }
        ?>
        <button type="submit">Pay with Nexi</button>
    </form>
  2. when the users click the "Pay with Nexi" button, they will go to the Nexi servers where they can enter their credit card
  3. when the users pay on the Nexi website, they will come back to your website at the URL used in the setUrl() above. When that URL is called, you can have some code like this:
    <?php
    use MLocati\Nexi\XPay\Entity\SimplePay\Callback\Data;
    
    $data = Data::fromCustomerRequest($_GET);
    $data->checkMac($configuration);
    if ($data->getEsito() === \MLocati\Nexi\XPay\Entity\Response::ESITO_OK) {
        // The order has been paid
    } else {
        // Display an error message and let users come back to the checkout page
    }
    

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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