tradebp / tradebp-sdk
dev-main
2024-09-30 07:34 UTC
Requires
- guzzlehttp/guzzle: ^7.9
Requires (Dev)
- phpunit/phpunit: 10
- vlucas/phpdotenv: ^5.6
This package is not auto-updated.
Last update: 2024-10-01 05:58:48 UTC
README
简介
TradeBP SDK for PHP提供了一个简单直观的接口,用于与TradeBP CryptoHub支付系统交互。此SDK允许开发者轻松创建和管理加密货币支付请求。
安装
要在您的PHP项目中使用TradeBP SDK,您必须通过Composer包含它。运行以下命令将其添加到您的项目中。
composer require tradebp/tradebp-sdk
在您的PHP文件中,包含Composer自动加载器
require_once 'vendor/autoload.php';
API参考 - Crypto Hub支付
1. CryptoHubPaymentRequestData
此类表示创建支付请求所需的数据。
构造函数
/**
@param $amount: The payment amount.
@param $userEmail: The email of the user making the payment.
@param $callbackUrl: The URL to be called after the payment process is completed.
**/
public function __construct(float $amount, string $userEmail, string $redirectUrl, string $countryCode, string $currencyId, string $assetId)
方法
/**
* Sets the user's birthday.
*
* @param string $birthday The user's birthday in YYYY-MM-DD format.
* @return CryptoHubPaymentRequestData Returns the current instance for method chaining.
*/
public function setBirthday(string $birthday): CryptoHubPaymentRequestData
/**
* Sets the user's full name.
*
* @param string $fullName The user's full name.
* @return CryptoHubPaymentRequestData Returns the current instance for method chaining.
*/
public function setFullName(string $fullName): CryptoHubPaymentRequestData
/**
* Sets the validity period for the payment request.
*
* @param int $validityInSeconds The validity period in seconds.
* @return CryptoHubPaymentRequestData Returns the current instance for method chaining.
*/
public function setValidTill(int $validityInSeconds): CryptoHubPaymentRequestData
/**
* Sets whether reduced payment is allowed.
*
* @param bool $allowReducedPayment Flag indicating if reduced payment is allowed.
* @return CryptoHubPaymentRequestData Returns the current instance for method chaining.
*/
public function setAllowReducedPayment(bool $allowReducedPayment): CryptoHubPaymentRequestData
2. CryptoHubPayment
此类表示创建支付请求所需的数据。
/**
@param $apiKey : The API key for authentication with the TradeBP service.
*/
public function __construct(string $apiKey)
方法
/**
@param $requestData : instance of CryptoHubPaymentRequestData
@returns CryptoHubPaymentRequestResponse : The object containing the response from the API, which includes transaction uuid & payment url
*/
public function createRequest(CryptoHubPaymentRequestData $requestData): array
/**
@params $paymentId : The ID of the payment to check.
@returns CryptoHubPaymentStatusResponse : An object containing the payment status information.
*/
public function getPaymentStatus(string $paymentId): array
使用示例
1. 创建支付请求
use TradebpSdk\CryptoHub\{CryptoHubPayment, CryptoHubPaymentRequestData};
use TradebpSdk\Config\TestConfig;
// get api key from environment
$API_KEY = $_ENV['API_KEY'];
// Create payment request data
$paymentRequestData = new CryptoHubPaymentRequestData(
amount: '<transaction-amount>'
userEmail: '<user-email-id>',
redirectUrl: '<url-to-redirect-to>',
countryCode: "<two-digit-country-code>",
currencyId: "<currency-id>",
assetId: "<id-of-the-assest->"
);
/**
* Initialize payment object and create request
* @var $payment : CryptoHubPayment
* @desc : The response would be an instance of CryptoHubPayment;
*/
$payment = new CryptoHubPayment($API_KEY);
/**
* @var $response : CryptoHubPaymentRequestResponse
* @desc : The response would be an instance of CryptoHubPaymentRequestResponse;
*/
$response = $payment->createRequest($paymentRequestData);
2. 检查支付状态
use TradebpSdk\CryptoHub\CryptoHubPayment;
use TradebpSdk\Config\TestConfig;
/**
* Initialize payment object and create request
* @var $payment : CryptoHubPayment
* @desc : The response would be an instance of CryptoHubPayment;
*/
$payment = new CryptoHubPayment(TestConfig::$API_KEY);
/**
* @var $response : CryptoHubPaymentStatusResponse
* @desc : The response would be an instance of CryptoHubPaymentStatusResponse;
*/
$paymentStatus = $payment->getPaymentStatus('<payment-id>');
错误处理
为了有效地处理错误,请将API请求包装在try-catch块中,以捕获和处理异常。
try {
/**
* Initialize payment object and create request
* @var $payment : CryptoHubPayment
* @desc : The response would be an instance of CryptoHubPayment;
*/
$payment = new CryptoHubPayment(TestConfig::$API_KEY);
/**
* @var $response : CryptoHubPaymentRequestResponse
* @desc : The response would be an instance of CryptoHubPaymentRequestResponse;
*/
$response = $payment->createRequest($paymentRequestData);
} catch (Exception $e) {
// Handle the exception
echo "An error occurred: " . $e->getMessage();
}
安全考虑
- API密钥安全:始终确保您的API密钥安全。避免在源文件中硬编码它。使用环境变量或安全的配置管理系统。
- HTTPS:确保所有与API的通信都通过HTTPS进行,以确保数据传输的安全。
- 输入验证:在将输入数据发送到API之前,验证和清理所有输入数据,以避免注入攻击等漏洞。
未来改进
- 错误处理:实现全面的错误处理和日志记录。
- 测试:添加单元测试和集成测试,以确保SDK的可靠性和正确性。
- 扩展SDK:如果需要,添加对更多API端点的支持。
- 速率限制:如果API需要,实现速率限制和请求节流。