nps/php-sdk

Ingenico ePayments 的 Php SDK - NPS LatAm 服务

1.3.0 2019-01-04 18:53 UTC

README

可用性

支持 PHP 5.3 及以上版本

如何安装

先决条件

您需要为您的 PHP 安装以下软件包

· SimpleXML

· Curl

· Soap

Composer 安装

SDK 可以通过更新您的 composer.json 文件来使用 Composer 进行安装

{
    "require": 
        {
            "nps/php-sdk": "1.3.0"
        }
}

或通过执行以下命令

$ composer require nps/php-sdk

手动安装

您可以从我们的 Github 页面 下载或克隆 SDK,然后包含 init.php 文件。

require_once __DIR__ . '/vendor/autoload.php';

配置

这是 SDK 的基本配置

require_once __DIR__ . '/vendor/autoload.php';
use NpsSDK\Configuration;
use NpsSDK\Constants;
Configuration::environment(Constants::STAGING_ENV);
Configuration::secretKey(“yourSecretKeyHere”);

以下是一个简单的示例请求

require_once __DIR__ . '/vendor/autoload.php';

use NpsSDK\Sdk;
use NpsSDK\ApiException;
use NpsSDK\Configuration;
use NpsSDK\Constants;

Configuration::environment(Constants::SANDBOX_ENV);
Configuration::secretKey("YourKeyhere");

$sdk = new Sdk();

$params = array(
    'psp_Version'          => '2.2',
    'psp_MerchantId'       => 'psp_test',
    'psp_TxSource'         => 'WEB',
    'psp_MerchTxRef'       => 'ORDER56666-3',
    'psp_MerchOrderId'     => 'ORDER56666',
    'psp_Amount'           => '1000',
    'psp_NumPayments'      => '1',
    'psp_Currency'         => '032', 
    'psp_Country'          => 'ARG', 
    'psp_Product'          => '14',
    'psp_CustomerMail'     => 'john.doe@example.com',
    'psp_CardNumber'       => '4507990000000010', 
    'psp_CardExpDate'      => '1903', 
    'psp_CardSecurityCode' => '306',
    'psp_SoftDescriptor'   => 'Sol Tropical E',
    'psp_PosDateTime'      => '2016-12-01 12:00:00'
    
);
try{
    $resp = $sdk->payOnline2p($params);
}catch(ApiException $e){
    echo 'Code to handle error';
}

环境

require_once __DIR__ . '/vendor/autoload.php';

use NpsSDK\Configuration;
use NpsSDK\Constants;
Configuration::environment(Constants::STAGING_ENV);
Configuration::environment(Constants::SANDBOX_ENV);
Configuration::environment(Constants::PRODUCTION_ENV);

错误处理

ApiException:当发生 ReadTimeout 或 ConnectTimeout 时,会抛出此异常。

注意:NPS 提供的响应中可以详细说明可能发生的其他异常,或者由 php SoapClient 类提供。

require_once __DIR__ . '/vendor/autoload.php';

use NpsSDK\ApiException;

//Code
try{
    //code or sdk call
}catch(ApiException $e){
    //Code to handle error
}

高级配置

日志记录

Nps SDK 允许您在 SDK 内部记录您的请求,默认情况下记录到 stout。SDK 使用您为项目使用的自定义记录器。

monolog 记录器的示例。

use Monolog\Logger;
$logger = new Logger(“NpsSdk”);

use NpsSDK\Configuration;

Configuration::secretKey(“your key here”);
Configuration::logger($logger);

日志级别

注意:记录器需要遵守 PSR-3 规范才能在 SDK 内部正常工作,以下是一些示例(Monolog, Analog)。

"INFO" 级别将写入请求的简要信息并隐藏请求的敏感数据。 "DEBUG" 级别将写入有关请求的信息,以便开发人员以更详细的方式对其进行调试。

use NpsSDK\Configuration;

Configuration::secretKey(“your key here”);
Configuration::loglevel(“DEBUG”);

清理

清理允许 SDK 截断可能导致请求失败的某些字段,例如极长的名称。

use NpsSDK\Configuration;

Configuration::secretKey(“your key here”);
Configuration::sanitize(true);

超时

您可以更改请求的超时时间。

执行超时(默认=60秒):您可以更改请求的执行超时。

连接超时(默认=10秒):您可以更改请求的连接超时。

use NpsSDK\Configuration;

Configuration::secretKey(“your key here”);
Configuration::executionTimeout(60);
Configuration::connectionTimeout(10);

代理配置

use NpsSDK\Configuration;

Configuration::secretKey(“your key here”);
Configuration::proxyUrl("http://yourproxy");
Configuration::proxyPort(6854);
Configuration::proxyUser("proxyUsername");
Configuration::proxyPass("proxyPassword");

缓存

use NpsSDK\Configuration;

Configuration::secretKey(“your key here”);
Configuration::useCache(True);
Configuration::cacheTTL(86400);
Configuration::cacheLocation("/tmp");