PHP WebShell

Текущая директория: /var/www/bitcardoApp/backyard/models/paystack

Просмотр файла: paystack_client.php

<?php
// models/paystack/paystack_client.php

require_once __DIR__ . '/../../config/paystack_config.php';

function paystack_api_request(string $method, string $endpoint, array $data = null): array {
    $url = 'https://api.paystack.co' . $endpoint;

    $ch = curl_init();

    $headers = [
        'Authorization: Bearer ' . PAYSTACK_SECRET_KEY,
        'Content-Type: application/json',
    ];

    $opts = [
        CURLOPT_URL            => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER     => $headers,
        CURLOPT_CUSTOMREQUEST  => strtoupper($method),
        CURLOPT_TIMEOUT        => 30,
    ];

    if (!empty($data)) {
        $opts[CURLOPT_POSTFIELDS] = json_encode($data);
    }

    curl_setopt_array($ch, $opts);

    $response = curl_exec($ch);
    $err      = curl_error($ch);
    $status   = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

    if ($err) {
        throw new Exception('Paystack cURL error: ' . $err);
    }

    $decoded = json_decode($response, true);
    if (!is_array($decoded)) {
        throw new Exception('Invalid Paystack response: ' . $response);
    }

    return [$status, $decoded];
}

/**
 * Get Paystack balances, keyed by currency.
 * Returns array like:
 * [
 *   'NGN' => ['available' => 120000.00, 'pending' => 5000.00],
 *   'USD' => [...]
 * ]
 */
function paystack_get_balances(): array {
    [$status, $body] = paystack_api_request('GET', '/balance');

    if ($status !== 200 || empty($body['status'])) {
        throw new Exception('Failed to fetch Paystack balance');
    }

    $balances = $body['data'] ?? [];
    $result   = [];

    foreach ($balances as $bal) {
        $currency  = $bal['currency'] ?? 'NGN';
        $available = ($bal['balance'] ?? 0) / 100;          // Paystack uses kobo
        $pending   = ($bal['pending_balance'] ?? 0) / 100;  // kobo

        $result[$currency] = [
            'available' => $available,
            'pending'   => $pending,
        ];
    }

    return $result;
}

/**
 * Convenience wrapper: get only NGN balance (or null if not present)
 */
function paystack_get_ngn_balance(): ?array {
    $all = paystack_get_balances();
    return $all['NGN'] ?? null;
}

Выполнить команду


Для локальной разработки. Не используйте в интернете!