Regarding Sandbox environment for API Testing

Hello
I am currently working on integrating Envato’s API to validate purchase codes for my project. Here is the api url https://api.envato.com/v3/market/author/sale
To ensure the integration is seamless and secure, I would like to test it thoroughly in a sandbox environment.
Is there any way to test it on sandbox environment ?

Here is the working code I created. Replace $personalToken = ""; with your Envato personal token code:

<?php
// Suppress all error reporting
error_reporting(0);

// Personal token for API authentication (needs to be replaced with a valid token)
$personalToken = "";

// Initialize response array with default values
$response = [
    'result' => false, // Indicates if the purchase code validation is successful
    'message' => '', // Contains feedback messages for the user
    'data' => [], // Holds additional data if the purchase code is valid
];

// Check if 'purchase_code' parameter exists and is not empty in the GET request
if (isset($_GET['purchase_code']) && !empty($_GET['purchase_code'])) {
    // Trim any whitespace from the purchase code
    $code = trim($_GET['purchase_code']);

    // User-Agent string for API requests
    $userAgent = "Purchase code verification for Grupo Chat";

    // Validate the purchase code format using a regular expression
    if (!preg_match("/^(\w{8})-((\w{4})-){3}(\w{12})$/", $code)) {
        $response['message'] = 'Invalid purchase code format.';
    } else {
        // Initialize cURL session
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL verification
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt_array($ch, [
            CURLOPT_URL => "https://api.envato.com/v3/market/author/sale?code={$code}", // API endpoint
            CURLOPT_RETURNTRANSFER => true, // Return response as a string
            CURLOPT_TIMEOUT => 20, // Set timeout for the request
            CURLOPT_HTTPHEADER => [// Set HTTP headers
                "Authorization: Bearer {$personalToken}", // Authorization header with token
                "User-Agent: {$userAgent}"               // User-Agent header
            ]
        ]);

        // Execute the API request and capture the response
        $responseData = @curl_exec($ch);
        $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // Get HTTP response code

        // Check for errors during the cURL execution
        if (curl_errno($ch) > 0) {
            $response['message'] = 'Error connecting to the server.';
        } elseif ($responseCode === 404) {
            $response['message'] = 'Purchase code not found.'; // 404 indicates the code doesn't exist
        } elseif ($responseCode !== 200) {
            $response['message'] = 'An error occurred. Please try again.'; // Handle non-success responses
        } else {
            // Decode JSON response from the API
            $body = @json_decode($responseData);
            if ($body === null && json_last_error() !== JSON_ERROR_NONE) {
                $response['message'] = 'Error decoding response.'; // Handle JSON decoding errors
            } elseif (isset($body->item->id)) {
                // If item ID exists, the purchase code is valid
                $response['result'] = true;
                $response['message'] = 'Purchase code is valid!';
                $response['data'] = $body; // Store response data
            } else {
                $response['message'] = 'Invalid purchase code.'; // Handle invalid code
            }
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Purchase Code Validation</title>
    <style>
        /* Add styling for the page */
        * {
            box-sizing: border-box;
        }
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            height: 100vh;
            margin: 0;
            padding: 20px;
            box-sizing: border-box;
        }
        .container {
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
        }
        input[type="text"], input[type="submit"] {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border-radius: 4px;
        }
        input[type="submit"] {
            background-color: #28a745;
            color: #fff;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: #218838;
        }
        .response {
            margin-top: 20px;
            font-size: 16px;
            padding: 10px;
            border-radius: 4px;
            display: none;
            /* Hidden by default */
        }
        .response.success {
            background-color: #d4edda;
            color: #155724;
        }
        .response.error {
            background-color: #f8d7da;
            color: #721c24;
        }
        .data-container {
            margin-top: 20px;
            padding: 10px;
            border-radius: 4px;
            background-color: #f8f9fa;
            border: 1px solid #ddd;
            overflow: auto;
            max-height: 300px;
            display: none;
            /* Hidden by default */
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- Display the response message if available -->
        <?php if (!empty($response['message'])): ?>
        <div class="response<?php echo $response['result'] ? ' success' : ' error'; ?>" style="display: block;">
            <?php echo htmlspecialchars($response['message']); ?>
        </div>
        <?php endif; ?>

        <!-- Form for user to input purchase code -->
        <form action="" method="get">
            <h2>Validate Purchase Code</h2>
            <input type="text" name="purchase_code" placeholder="Purchase Code" required />
            <input type="submit" value="Validate" />
        </form>

        <!-- Display additional data if the response contains it -->
        <?php if (!empty($response['data'])) {
            $response['data']->item = $response['data']->item->name;
            ?>
            <div class="data-container" style="display: block;">
                <h3>Response Data:</h3>
                <pre><?php print_r($response['data']); ?></pre>
            </div>
            <?php
        } ?>
    </div>
</body>
</html>
1 Like