mlocati / nexi-xpay
Nexi XPay支付网关的非官方SDK(不适用于Intesa Sanpaolo银行客户)
Requires
- php: >= 7.2
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^8.5
Suggests
- ext-curl: To use cURL for HTTP requests
- ext-openssl: To use the stream wrapper for HTTP requests
README
此项目包含一个PHP库,使您能够轻松使用Nexi XPay API(不适用于Intesa Sanpaolo银行)。
这需要一个商户别名和一个MAC密钥。如果您有API密钥,您可能需要使用此其他库。
安装
使用Composer安装
只需运行以下命令
composer require mlocati/nexi-xpay
手动安装
下载此库的代码,将其放置在您的项目中某个位置,并在使用此库的任何内容之前添加此PHP指令
require '/path/to/nexi.php';
用法
配置
首先,您需要一个商户别名和一个MAC密钥(由Nexi提供)。
您还需要Nexi XPay API的基本URL。您可以在MLocati\Nexi\XPay\Configuration
中找到其默认值。
- 对于测试环境:您可以使用
MLocati\Nexi\XPay\Configuration::DEFAULT_BASEURL_TEST
- 对于生产环境:您可以使用
MLocati\Nexi\XPay\Configuration::DEFAULT_BASEURL_PRODUCTION
此库通过使用MLocati\Nexi\XPay\Configuration\FromArray
类提供了一个简单的方式来表示配置。
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', ]);
当然,您可以覆盖默认的基本URL(使用baseUrl
数组键)。
您还可以使用一个自定义类,只要它实现了MLocati\Nexi\XPay\Configuration接口。
Nexi客户端
此库的主要类是MLocati\Nexi\XPay\Client
:它允许您调用Nexi API。
您可以通过以下方式创建其实例
use MLocati\Nexi\XPay\Client; $client = new Client($configuration);
HTTP通信
Nexi客户端需要执行HTTP请求。为了做到这一点,它会自动检测执行此操作的最佳方式
- 如果cURL PHP扩展可用,则使用它(请参阅
MLocati\Nexi\XPay\HttpClient\Curl
类) - 否则,如果PHP HTTP流包装器已启用,则使用它(它需要OpenSSL PHP扩展 - 请参阅
MLocati\Nexi\XPay\HttpClient\StreamWrapper
类)
您还可以提供自己的实现,只要它实现了MLocati\Nexi\XPay\HttpClient接口。这样,您可以轻松记录与Nexi服务器的通信,以及自定义HTTP客户端(例如,因为您在代理后面)。
例如,如果您想使用您自定义的HTTP客户端实现,您可以简单地编写
use MLocati\Nexi\XPay\Client; $myHttpClient = new My\Custom\HttpClient(); $client = new Client($configuration, $myHttpClient);
示例用法
此库提供的Nexi客户端允许您使用Nexi文档网站中提供的一些方法。
以下是一个示例代码,允许您接受支付
- 您的客户在您的网站上的一页上,您想在页面上放置一个“使用Nexi支付”按钮
<?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>
- 当用户点击“使用Nexi支付”按钮时,他们会转到Nexi服务器,在那里他们可以输入他们的信用卡
- 当用户在Nexi网站上支付时,他们将会回到您使用上述
setUrl()
设置的URL所在的您的网站。当调用该URL时,您可以在代码中加入如下内容<?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 }