<?php
require_once __DIR__ . '/config.php';

$pay_settings = $pdo->query("SELECT setting_key, setting_value FROM admin_settings WHERE setting_key IN ('gopay_payout_api_key','gopay_payout_api_secret')")->fetchAll(PDO::FETCH_KEY_PAIR);

$gp_api_key    = $pay_settings['gopay_payout_api_key'] ?? '';
$gp_api_secret = $pay_settings['gopay_payout_api_secret'] ?? '';

$payout_order_id = "PO_" . time() . "_TEST_1234";
$payout_amount = 500;
$gp_method_code = 'easypaisa';
$acc = ['account_number' => '03001234567', 'account_name' => 'Ali Khan'];
$notify_url = "https://pakclub24.club/webhook/";

$body = [
    'merchant_payout_id' => $payout_order_id,
    'amount'             => intval($payout_amount),
    'currency'           => 'PKR',
    'method'             => $gp_method_code,
    'account_number'     => $acc['account_number'],
    'account_name'       => $acc['account_name'],
    'notify_url'         => $notify_url
];

$body_json = json_encode($body);
$signature = hash_hmac('sha256', $body_json, $gp_api_secret);

$ch = curl_init('https://gopayx.live/api/public/v1/payouts');
curl_setopt_array($ch, [
    CURLOPT_POST           => true,
    CURLOPT_POSTFIELDS     => $body_json,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_TIMEOUT        => 30,
    CURLOPT_HTTPHEADER     => [
        'Content-Type: application/json',
        'x-api-key: ' . $gp_api_key,
        'x-signature: ' . $signature
    ]
]);
$resp = curl_exec($ch);
$err  = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo "HTTP Code: $httpCode\n";
echo "Error: $err\n";
echo "Response: $resp\n";
