<?php
$base = 'http://127.0.0.1:8001';
function req($method, $url, $headers = [], $body = null) {
    $ch = curl_init($url);
    curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => $method, CURLOPT_HTTPHEADER => $headers]);
    if ($body !== null) curl_setopt($ch, CURLOPT_POSTFIELDS, is_string($body) ? $body : json_encode($body, JSON_UNESCAPED_SLASHES));
    $raw = curl_exec($ch);
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($raw === false) $raw = curl_error($ch);
    curl_close($ch);
    return [$status, json_decode($raw, true), $raw];
}
function hmacReq($method, $base, $path, $key, $secret, $body = null, $extra = []) {
    $raw = $body === null ? '' : json_encode($body, JSON_UNESCAPED_SLASHES);
    $ts = (string) time();
    $nonce = bin2hex(random_bytes(12));
    $canonical = strtoupper($method)."\n".$path."\n".$ts."\n".$nonce."\n".hash('sha256', $raw);
    $sig = hash_hmac('sha256', $canonical, $secret);
    $headers = array_merge([
        'Accept: application/json',
        'Content-Type: application/json',
        'x-api-key: '.$key,
        'x-api-timestamp: '.$ts,
        'x-api-nonce: '.$nonce,
        'x-api-signature: v1='.$sig,
    ], $extra);
    return req($method, $base.$path, $headers, $raw);
}
[$s, $login] = req('POST', $base.'/api/v1/auth/login', ['Accept: application/json','Content-Type: application/json'], ['email'=>'ahmadshahzaib2490+3@gmail.com','password'=>'waqas123waqas']);
$token = $login['data']['token'] ?? '';
if (!$token) { echo "LOGIN_FAIL $s\n"; var_export($login); exit(1); }
[$s, $issued] = req('POST', $base.'/api/v1/api-keys', ['Accept: application/json','Content-Type: application/json','Authorization: Bearer '.$token], ['environment'=>'sandbox','scopes'=>['payments:read','payments:write','balances:read','payouts:read','payouts:write','webhooks:write','merchants:read','merchants:write','wallets:read','wallets:write']]);
$key = $issued['data']['public_key'] ?? '';
$secret = $issued['data']['secret'] ?? '';
if (!$key || !$secret) { echo "KEY_FAIL $s\n"; var_export($issued); exit(1); }
$ref = 'cust_smoke_'.time();
[$s1, $customer] = hmacReq('POST', $base, '/api/v1/customers', $key, $secret, ['reference_id'=>$ref,'name'=>'Smoke Customer','email'=>$ref.'@example.com','metadata'=>['source'=>'codex']]);
$cid = $customer['data']['id'] ?? '';
[$s2, $list] = hmacReq('GET', $base, '/api/v1/customers?per_page=5', $key, $secret);
[$s3, $invoice] = hmacReq('POST', $base, '/api/v1/invoices', $key, $secret, ['price_amount'=>'10.00','price_currency'=>'USD','pay_currency'=>'USDTTRC20','order_id'=>'customer_invoice_'.time(),'customer_id'=>$cid,'items'=>[['name'=>'Test item','quantity'=>1,'unit_amount'=>'10.00']]], ['Idempotency-Key: customer-invoice-'.time()]);
echo json_encode(['create_customer'=>$s1,'customer_id'=>$cid,'list_customers'=>$s2,'create_invoice_with_customer'=>$s3,'invoice_customer_id'=>$invoice['data']['customer_id'] ?? null], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)."\n";