tradebp / tradebp-sdk
SDK for integrating with tradebp payment services
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: 2025-04-29 08:50:05 UTC
README
Introduction
The TradeBP SDK for PHP provides a simple and intuitive interface to interact with the TradeBP CryptoHub payment system. This SDK allows developers to create and manage cryptocurrency payment requests with ease.
Installation
To use the TradeBP SDK in your PHP project, you must include it via Composer. Run the following command to add it to your project.
composer require tradebp/tradebp-sdk
In your PHP files, include the Composer autoloader:
require_once 'vendor/autoload.php';
API Reference - Crypto Hub Payment
1. CryptoHubPaymentRequestData
This class represents the data required to create a payment request.
Constructor
/**
@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)
Methods
/**
* 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
This class represents the data required to create a payment request.
/**
@param $apiKey : The API key for authentication with the TradeBP service.
*/
public function __construct(string $apiKey)
Methods
/**
@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
Usage Examples
1. Creating a Payment Request
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. Checking Payment Status
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>');
Error Handling
To handle errors effectively, wrap API requests in try-catch blocks to capture and process exceptions.
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();
}
Security Considerations
- API Key Security: Always keep your API key secure. Avoid hardcoding it in source files. Use environment variables or a secure configuration management system.
- HTTPS: Ensure all communication with the API is done over HTTPS for secure data transmission.
- Input Validation: Validate and sanitize all input data before sending it to the API to avoid vulnerabilities like injection attacks.
Future Improvements
- Error Handling: Implement comprehensive error handling and logging.
- Testing: Add unit and integration tests to ensure the SDK's reliability and correctness.
- Expand SDK: Add support for more API endpoints if needed.
- Rate Limiting: Implement rate limiting and request throttling if required by the API.