Skip to main content

PHP Client

Code example for PHP

<?php

class PaymentEngineClient {
private string $apiKey;
private string $secretKey;
private string $baseUrl;

public function __construct(string $apiKey, string $secretKey, string $baseUrl = 'https://api.2settle.io') {
$this->apiKey = $apiKey;
$this->secretKey = $secretKey;
$this->baseUrl = $baseUrl;
}

private function sign(string $method, string $path, array $body = []): array {
$timestamp = (string) round(microtime(true) * 1000);
$bodyStr = json_encode($body ?: new stdClass(), JSON_UNESCAPED_SLASHES);
$bodyHash = hash('sha256', $bodyStr);
$payload = "{$timestamp}|{$method}|{$path}|{$bodyHash}";
$hmacKey = hash('sha256', $this->secretKey);
$signature = hash_hmac('sha256', $payload, $hmacKey);
return [$timestamp, $signature, $bodyStr];
}

private function request(string $method, string $path, array $body = null): array {
[$timestamp, $signature, $bodyStr] = $this->sign($method, $path, $body ?? []);

$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $this->baseUrl . $path,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'X-API-Key: ' . $this->apiKey,
'X-Timestamp: ' . $timestamp,
'X-Signature: ' . $signature,
],
]);

if ($method !== 'GET' && $body !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyStr);
}

$response = curl_exec($ch);
curl_close($ch);

return json_decode($response, true);
}

public function createPayment(array $options): array {
return $this->request('POST', '/v1/payments', $options);
}

public function getPayment(string $reference): array {
return $this->request('GET', "/v1/payments/{$reference}", []);
}

public function claimGift(string $reference, array $receiver): array {
return $this->request('POST', "/v1/payments/gifts/{$reference}/claim/confirm", ['receiver' => $receiver]);
}

public function fulfillRequest(string $reference, array $payer, string $crypto, string $network): array {
return $this->request('POST', "/v1/payments/requests/{$reference}/fulfill", [
'payer' => $payer,
'crypto' => $crypto,
'network' => $network,
]);
}
}

// Usage
$client = new PaymentEngineClient('pk_xxx', 'sk_xxx');

$result = $client->createPayment([
'type' => 'transfer',
'fiatAmount' => 10000,
'fiatCurrency' => 'NGN',
'crypto' => 'USDT',
'network' => 'trc20',
'chargeFrom' => 'fiat',
'payer' => ['chatId' => '7389201648'],
'receiver' => [
'bankCode' => '090405',
'accountNumber' => '8012345678',
],
]);

echo "Deposit {$result['payment']['cryptoAmount']} USDT to:\n";
echo $result['payment']['depositAddress'] . "\n";