PHP code example of pasisltd / php-sdk

1. Go to this page and download the library: Download pasisltd/php-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pasisltd / php-sdk example snippets




asis\SDK\Client;

// Create a new client with your app credentials
$client = new Client('your-app-key', 'your-secret-key');

// Get wallet details
$wallet = $client->wallet();
echo "Wallet ID: {$wallet->id}\n";

// Get merchant profile
$profile = $client->profile();
echo "Merchant: {$profile->firstName} {$profile->lastName}\n";

$client = new Client(
    'app-key',
    'secret-key',
    [
        'baseURL' => 'https://api.pasis.com/api',
    ]
);

use GuzzleHttp\Client as HttpClient;

$httpClient = new HttpClient([
    'timeout' => 60.0,
    'verify' => true,
]);

$client = new Client(
    'app-key',
    'secret-key',
    [
        'httpClient' => $httpClient,
    ]
);

// Disable retries
$client = new Client(
    'app-key',
    'secret-key',
    [
        'retryCount' => 0,
    ]
);

// Set custom retry count (e.g., 5 retries)
$client = new Client(
    'app-key',
    'secret-key',
    [
        'retryCount' => 5,
    ]
);

use Pasis\SDK\Cache\TokenCacheInterface;
use DateTime;

// Implement the TokenCacheInterface
class RedisTokenCache implements TokenCacheInterface
{
    private $redis;

    public function __construct($redis)
    {
        $this->redis = $redis;
    }

    public function get(): array
    {
        $token = $this->redis->get('pasis:access_token');
        $refreshToken = $this->redis->get('pasis:refresh_token');
        $expiresAt = $this->redis->get('pasis:expires_at');
        
        return [
            $token ?: '',
            $refreshToken ?: '',
            $expiresAt ? new DateTime($expiresAt) : null,
        ];
    }

    public function set(string $token, string $refreshToken, DateTime $expiresAt): void
    {
        $this->redis->set('pasis:access_token', $token);
        $this->redis->set('pasis:refresh_token', $refreshToken);
        $this->redis->set('pasis:expires_at', $expiresAt->format('c'));
    }

    public function clear(): void
    {
        $this->redis->del('pasis:access_token', 'pasis:refresh_token', 'pasis:expires_at');
    }
}

// Use the custom cache
$redisCache = new RedisTokenCache($redisClient);
$client = new Client(
    'app-key',
    'secret-key',
    [
        'tokenCache' => $redisCache,
    ]
);

$wallet = $client->wallet();
echo "Balance: {$wallet->balance} {$wallet->currency}\n";

use Pasis\SDK\DepositRequest;

$depositReq = new DepositRequest(
    '100.00',
    'USD',
    'mobile_money',
    'US',
    '+1234567890',
    ['reference' => 'order-123']
);

$transaction = $client->deposit($depositReq);
echo "Transaction ID: {$transaction->id}\n";
echo "Status: {$transaction->status}\n";

use Pasis\SDK\WithdrawRequest;

$withdrawReq = new WithdrawRequest(
    '50.00',
    'USD',
    'bank_transfer',
    'US',
    '+1234567890',
    ['reference' => 'payout-456']
);

$transaction = $client->withdraw($withdrawReq);
echo "Transaction ID: {$transaction->id}\n";

// List first page with 20 items
$transactions = $client->transactions(1, 20);

echo "Total transactions: {$transactions->pagination->total}\n";
foreach ($transactions->data as $tx) {
    echo "Transaction {$tx->id}: {$tx->amount} {$tx->currency} ({$tx->status})\n";
}

$transaction = $client->transaction('txn-123');

echo "Amount: {$transaction->amount}\n";
echo "Type: {$transaction->type}\n";
echo "Status: {$transaction->status}\n";
if ($transaction->fees !== null) {
    echo "Fees: {$transaction->fees->total}\n";
}

$profile = $client->profile();

echo "Name: {$profile->firstName} {$profile->lastName}\n";
echo "Email: {$profile->email}\n";
echo "Phone: {$profile->phoneNumber}\n";

use Pasis\SDK\APIError;
use Pasis\SDK\AuthError;
use Pasis\SDK\ValidationError;

try {
    $transaction = $client->deposit($depositReq);
} catch (AuthError $e) {
    echo "Authentication failed: {$e->getMessage()}\n";
    // Handle authentication failure
} catch (ValidationError $e) {
    echo "Validation failed: {$e->getMessage()}\n";
    if ($e->field !== null) {
        echo "Field: {$e->field}\n";
    }
    // Handle validation failure
} catch (APIError $e) {
    echo "API error (status {$e->statusCode}): {$e->message}\n";
    if (!empty($e->errors)) {
        foreach ($e->errors as $error) {
            echo "  - {$error}\n";
        }
    }
    // Handle API error
} catch (\Exception $e) {
    echo "Unexpected error: {$e->getMessage()}\n";
}

// Good: Create once, reuse everywhere
class MyService
{
    private Client $client;

    public function __construct()
    {
        $this->client = new Client('app-key', 'secret-key');
    }

    public function handleRequest()
    {
        $wallet = $this->client->wallet();
        // ...
    }
}

use GuzzleHttp\Client as HttpClient;

$httpClient = new HttpClient([
    'timeout' => 30.0,
    'connect_timeout' => 10.0,
]);

$client = new Client(
    'app-key',
    'secret-key',
    [
        'httpClient' => $httpClient,
    ]
);

try {
    $result = $client->wallet();
} catch (APIError $e) {
    // Handle API errors
    if ($e->statusCode >= 500) {
        // Server error - might want to retry
    } elseif ($e->statusCode >= 400) {
        // Client error - fix the request
    }
} catch (\Exception $e) {
    // Handle other errors
}

class PaymentService
{
    public function __construct(private Client $client)
    {
    }

    public function processPayment(float $amount): void
    {
        $request = new DepositRequest(
            (string)$amount,
            'USD',
            'mobile_money',
            'US'
        );
        
        $transaction = $this->client->deposit($request);
        // Process transaction...
    }
}
bash
composer