$method, CURLOPT_HTTPHEADER => $headers, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => false, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 15, CURLOPT_HEADERFUNCTION => static function ($handle, string $line) use (&$responseHeaders): int { $header = explode(':', $line, 2); if (count($header) === 2) { $responseHeaders[strtolower(trim($header[0]))] = trim($header[1]); } return strlen($line); }, ]); if ($payload !== null) { curl_setopt($curl, CURLOPT_POSTFIELDS, $body); } $response = curl_exec($curl); $statusCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE); if ($response === false) { throw new RuntimeException('Permintaan terputus. Periksa status melalui external_id sebelum mengulang.'); } $envelope = json_decode($response, true); if ($statusCode < 200 || $statusCode >= 300 || ($envelope['status'] ?? '') !== 'SUCCESS') { throw new RuntimeException(json_encode([ 'http_status' => $statusCode, 'code' => $envelope['data']['code'] ?? null, 'request_id' => $envelope['data']['request_id'] ?? $responseHeaders['x-request-id'] ?? null, 'retry_after' => $responseHeaders['retry-after'] ?? null, ], JSON_THROW_ON_ERROR)); } return [ 'data' => $envelope['data'], 'request_id' => $responseHeaders['x-request-id'] ?? null, 'replayed' => ($responseHeaders['idempotent-replayed'] ?? '') === 'true', ]; } try { $balance = requestPartner('GET', '/api/v1/balance'); $catalog = requestPartner('GET', '/api/v1/products?limit=5'); echo json_encode(['balance' => $balance['data'], 'products' => $catalog['data']['items']], JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR), PHP_EOL; $product = getenv('PARTNER_PRODUCT_CODE') ?: ''; $destination = getenv('PARTNER_DESTINATION') ?: ''; $externalId = getenv('PARTNER_EXTERNAL_ID') ?: ''; if ($product === '' && $destination === '' && $externalId === '') { exit; } if ($product === '' || $destination === '' || $externalId === '') { throw new RuntimeException('Isi PARTNER_PRODUCT_CODE, PARTNER_DESTINATION, dan PARTNER_EXTERNAL_ID sekaligus.'); } $order = requestPartner('POST', '/api/v1/transactions', [ 'product_code' => $product, 'destination' => $destination, 'external_id' => $externalId, ]); $result = requestPartner('GET', '/api/v1/transactions/external/' . rawurlencode($externalId)); echo json_encode([ 'external_id' => $externalId, 'transaction_id' => $result['data']['transaction_id'], 'transaction_status' => $result['data']['transaction_status'], 'replayed' => $order['replayed'], ], JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR), PHP_EOL; } catch (Throwable $error) { fwrite(STDERR, $error->getMessage() . PHP_EOL); exit(1); }