easytransac/easytransac-sdk-php

Easytransac支付网关PHP SDK

1.3.7 2023-01-03 09:48 UTC

README

Build PSR12 Test Coverage Status Latest Stable Version

使用我们的SDK使EasyTransac API实现更加容易。

EasyTransac SDK是一个用于处理支付的工具,使用EasyTransac API

需求

您至少需要

  • PHP >=5.6
  • cURL以获取清晰的错误消息
  • 由EasyTransac提供的API密钥(在EasyTransac网站注册账户)
  • OpenSSL版本1.0.1以支持TLSv1.2加密算法

安装

通过composer

在项目文件夹的终端中执行此命令

composer require easytransac/easytransac-sdk-php

或者在您的composer.json文件中添加此内容

"require": {
  ...
  "easytransac/easytransac-sdk-php": "*",
}

手动安装

为了使用它,您只需要引入自动加载文件easytransac/easytransac-sdk-php/sdk/EasyTransac/autoload.php

单元测试

我们的测试用例是在PHPUnit 4.4.1下编写的,如果您想运行测试,请检查您的PHPUnit版本以检查方法兼容性。

示例

SDK中提供了示例。

配置设置

// Don't forget to require the composer autoload or sdk's
require_once(__DIR__.'/vendor/easytransac/easytransac-sdk-php/sdk/EasyTransac/autoload.php');

// Provide the API key
EasyTransac\Core\Services::getInstance()->provideAPIKey('YOUR_API_KEY_HERE');

// For testing, you can activate logs
EasyTransac\Core\Services::getInstance()->setDebug(true);

// Connexion timeout in second
EasyTransac\Core\Services::getInstance()->setRequestTimeout(30);

// You can change the log directory (by default the path is the running script path)
Logger::getInstance()->setFilePath('YOU_CUSTOM_PATH');

直接支付请求

$customer = (new EasyTransac\Entities\Customer())
    ->setFirstname('Demo')
    ->setLastname('Mini SDK')
    ->setCity('Strasbourg')
    ->setUid('a1b2c3d4');

$card = (new EasyTransac\Entities\CreditCard())
    ->setNumber('1111111111111111')
    ->setMonth('10')
    ->setYear('2017')
    ->setCVV('123');

$transaction = (new EasyTransac\Entities\DirectTransaction())
    ->setAmount(100)
    ->setClientIp('127.0.0.1')
    ->setCustomer($customer)
    ->setCreditCard($card);

$dp = new EasyTransac\Requests\DirectPayment();
$response = $dp->execute($transaction);

if ($response->isSuccess())
{
	$transactionItem = $response->getContent();

	// Check if the 3DSecure is forced by the server on this transaction,
	// if yes, then we must use the 3DS url to finish the transaction
	if ($transactionItem->getSecure())
	{
	    // Using of the 3DS url
	    // You have to call this url to proceed the 3DS process
	    // $transactionItem->getSecureUrl();

	}
	else
	{
	    // Shows the transaction status and id      
	    var_dump($transactionItem->getStatus());
	    var_dump($transactionItem->getTid());
	}
}
else
{
    var_dump($response->getErrorMessage());
}

推送支付通知

// EasyTransac notifies you for the payment status
// Then in your website, you have to create a script to receive the notification
$response = \EasyTransac\Core\PaymentNotification::getContent($_POST, $myApiKey);

// Response of type \EasyTransac\Entities\Notification
var_dump($response->toArray());

// Payment status
var_dump($response->getStatus());

// If you have a doubt about a value that does not exist in the response, 
// you can print the superglobal $_POST, all notification values are there:
var_dump($_POST);

获取JSON和数组格式的基本API响应

// You can get the complete response from the api with these methods bellow
$dp = new EasyTransac\Requests\DirectPayment();
$response = $dp->execute($transaction);

var_dump($response->getRealJsonResponse()); // Jsonified response (stdClass object)
var_dump($response->getRealArrayResponse()); // Array item