PHP code example of sofizpay / sofizpay-php-sdk

1. Go to this page and download the library: Download sofizpay/sofizpay-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/ */

    

sofizpay / sofizpay-php-sdk example snippets



ofizPay\SofizPaySDK;

// Initialize the SDK
$sdk = new SofizPaySDK();

// 1. Check DZT balance
$balance = $sdk->getBalance('YOUR_PUBLIC_KEY');
if ($balance['success']) {
    echo "💰 Balance: " . $balance['balance'] . " DZT\n";
}

// 2. Send a DZT payment
$result = $sdk->submit([
    'secretkey'            => 'YOUR_SECRET_KEY',      // 56-char Stellar seed starting with 'S'
    'destinationPublicKey' => 'RECIPIENT_PUBLIC_KEY', // Recipient's public key
    'amount'               => '100.0',                // Amount in DZT
    'memo'                 => 'Invoice #1234'         // Optional memo (max 28 chars)
]);

if ($result['success']) {
    echo "✅ Payment sent! Hash: " . $result['transactionHash'] . "\n";
} else {
    echo "❌ Error: " . $result['error'] . "\n";
}

// Fetch balance for a specific public key
$result = $sdk.getBalance('GCAZI...YOUR_PUBLIC_KEY');

// Response
[
  'success'      => true,
  'balance'      => '1500.0000000',
  'publicKey'    => 'GCAZI...',
  'asset_code'   => 'DZT',
  'asset_issuer' => 'GCAZI7YBLIDJWIVEL7ETNAZGPP3LC24NO6KAOBWZHUERXQ7M5BC52DLV',
  'timestamp'    => '2025-07-28T10:30:00+00:00'
]

$result = $sdk.submit([
    'secretkey'            => 'SXXX...YOUR_SECRET',      // 56-char Stellar seed starting with 'S'
    'destinationPublicKey' => 'GXXX...RECIPIENT',         // Recipient's public key
    'amount'               => '250.50',                  // Amount in DZT
    'memo'                 => 'Order #5567'              // Optional memo (max 28 chars)
]);

// Success Response
[
  'success'            => true,
  'transactionId'      => 'abc123...hash',
  'transactionHash'    => 'abc123...hash',
  'amount'             => '250.50',
  'memo'               => 'Order #5567',
  'destinationPublicKey' => 'GXXX...',
  'timestamp'          => '2025-07-28T10:30:00+00:00'
]

// Fetch up to 100 recent transactions
$history = $sdk.getTransactions('YOUR_PUBLIC_KEY', 100);

if ($history['success']) {
    foreach ($history['transactions'] as $tx) {
        // Log each transaction details
        echo "[{$tx['timestamp']}] {$tx['type']} — {$tx['amount']} {$tx['asset_code']}\n";
    }
}

// Search for transactions containing the specific memo
$results = $sdk.searchTransactionsByMemo('YOUR_PUBLIC_KEY', 'Order #1234', 10);
if ($results['success']) {
    echo "Found " . count($results['transactions']) . " matches";
}

$result = $sdk.rechargePhone([
    'encrypted_sk' => 'USER_ENCRYPTED_SECRET_KEY', // User's encrypted secret key
    'phone'        => '0661000000',               // Recipient's phone number
    'operator'     => 'mobilis',                  // 'mobilis' | 'djezzy' | 'ooredoo'
    'amount'       => '100',                      // Recharge amount
    'offer'        => 'Top'                       // Offer type (e.g., 'Top', 'Pix')
]);

if ($result['success']) {
    // Process successful recharge
    print_r($result['data']);
}

$result = $sdk.rechargeInternet([
    'encrypted_sk' => 'USER_ENCRYPTED_SECRET_KEY', // User's encrypted secret key
    'phone'        => '0661000000',               // Account phone number
    'operator'     => 'idoom',                    // Network operator
    'amount'       => '2000',                     // Recharge amount
    'offer'        => 'adsl'                      // Offer type (e.g., 'adsl', '4g')
]);

$result = $sdk.rechargeGame([
    'encrypted_sk' => 'USER_ENCRYPTED_SECRET_KEY',
    'operator'     => 'freefire',   // e.g., 'freefire', 'pubg'
    'playerId'     => '123456789',
    'amount'       => '500',        // 'amount' from getProducts()
    'offer'        => 'diamonds'    // 'name' from getProducts()
]);

$result = $sdk.payBill([
    'encrypted_sk' => 'USER_ENCRYPTED_SECRET_KEY',
    'operator'     => 'sonelgaz', // e.g., 'sonelgaz', 'ade'
    'bill_id'      => 'BILL_999',
    'amount'       => '1500'
]);

// Recent operations (paginated)
$history = $sdk.getOperationHistory('USER_ENCRYPTED_SK', 10, 0);
if ($history['success']) {
    print_r($history['data']);
}

// Details of a specific operation
$details = $sdk.getOperationDetails('OPERATION_ID', 'USER_ENCRYPTED_SK');

// Fetch list of available recharging products
$products = $sdk.getProducts('USER_ENCRYPTED_SK');
if ($products['success']) {
    // List all available services and offers
    print_r($products['data']);
}

$result = $sdk->makeCIBTransaction([
    'account'     => 'YOUR_STELLAR_PUBLIC_KEY',       // Your merchant Stellar public key
    'amount'      => 2500,                            // Amount in DZD (Algerian Dinars)
    'full_name'   => 'Ahmed Benali',                  // Customer's full name
    'phone'       => '0661234567',                    // Customer's phone number
    'email'       => '[email protected]',              // Customer's email
    'memo'        => 'Order #789',                    // Internal order reference
    'return_url'  => 'https://yoursite.com/callback', // Redirect after payment
    'webhook_url' => 'https://yoursite.com/api/cib-webhook', // Asynchronous Webhook callback
    'language'    => 'ar',                            // 'ar', 'fr', or 'en'
    'redirect'    => 'no'                             // 'yes' for auto-redirect
]);

if ($result['success']) {
    // Top-level direct properties
    $paymentUrl = $result['payment_url'];
    $orderId = $result['cib_transaction_id'];
    
    // Redirect customer to the SATIM checkout page
    header('Location: ' . $paymentUrl);
    exit;
}

// Initialize SDK in Sandbox mode (or pass 'is_sandbox' => true)
$sandboxSdk = new SofizPaySDK('https://horizon.stellar.org', true);

$result = $sandboxSdk->makeSandboxCIBTransaction([
    'account'     => 'YOUR_PUBLIC_KEY',
    'amount'      => 150.0,
    'full_name'   => 'Sandbox Tester',
    'phone'       => '0555000000',
    'email'       => '[email protected]',
    'webhook_url' => 'https://yoursite.com/api/cib-webhook',
    'return_url'  => 'https://yoursite.com/callback'
]);

if ($result['success']) {
    echo "Sandbox Payment URL: " . $result['payment_url'] . "\n";
    $cibId = $result['cib_transaction_id'];

    // Check sandbox order status
    $status = $sandboxSdk->checkSandboxCIBStatus($cibId);
    echo "Order Status: " . ($status['data']['status'] ?? 'pending') . "\n";
}


// webhook.php
ySDK;

$sdk = new SofizPaySDK();

// 1. Read input payload
$payload = json_decode(file_get_contents('php://input'), true);

// 2. Validate signature AND that payment_status / amount / order_number match the signed message
if (!is_array($payload) || !$sdk->verifyWebhookPayload($payload)) {
    http_response_code(400);
    echo "Invalid webhook";
    exit;
}

// 3. Process order upon confirmation (only the signed fields are used)
if ($payload['payment_status'] === 'success') {
    $orderNumber = $payload['order_number'];
    $amountInDZD = $payload['amount'] / 100;
    // ✅ Also check $payload['amount'] equals the amount stored for this order, and process each order once
    
    // ✅ Mark order as PAID in your database:
    // $db->updateOrder($orderNumber, ['status' => 'PAID', 'amount' => $amountInDZD]);
}

http_response_code(200);
echo "OK";

// Monitor account activity (simulated via polling)
$status = $sdk.getStreamStatus('YOUR_PUBLIC_KEY');

// ✅ Success
[
  'success'   => true,
  'data'      => [...], // method-specific data fields
  'timestamp' => '2025-07-28T10:30:00+00:00'
]

// ❌ Failure
[
  'success'   => false,
  'error'     => 'Error message describing the failure',
  'timestamp' => '2025-07-28T10:30:00+00:00'
]
bash
composer