Help with purchase code verification

Hello,
I would like to know if this following function is valid for purshase code checking

public function purchase_code_checking(){

    if(isset($_POST['purchase_code'])){
            $api_key='**************************************************';
            $purchase_code = htmlspecialchars($_POST['purchase_code']);
    }
    // Your Username
    $username = '********';
  
    // Open cURL channel
    $ch = curl_init();
     
    // Set cURL options
    curl_setopt($ch, CURLOPT_URL, "http://marketplace.envato.com/api/edge/". $username ."/". $api_key ."/verify-purchase:". $purchase_code .".json");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

       //Set the user agent
       $agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)';
       curl_setopt($ch, CURLOPT_USERAGENT, $agent);     
    // Decode returned JSON
    $purchase_data = json_decode(curl_exec($ch), true);

    curl_close($ch);

    if( isset($purchase_data['verify-purchase']['buyer']) ) {
       
        echo 'Valid License Key!Details';
            echo 'Item ID: ' . $purchase_data['verify-purchase']['item_id'] . ' ';
            echo 'Item Name: ' . $purchase_data['verify-purchase']['item_name'] . ' ';
            echo 'Buyer: ' . $purchase_data['verify-purchase']['buyer']. ' ';
            echo 'License: ' . $purchase_data['verify-purchase']['licence'] . ' ';
            echo 'Created At: ' . $purchase_data['verify-purchase']['created_at'] . ' ';        echo '';
    } else{
        echo 'Invalid license key.';
    }
   
 }

Hi!

That is not recommended. The marketplace.envato.com api is deprecated and will soon be removed.

Instead, you should use the new API. I’ve written a code example here:

Cheers!

Hi baileyherbert,
i just tried this code and i got a 500 error. i used a form with ajax response.

i don’t know why?

Check your error log for a PHP error, or enable error reporting.

in php_error.log, i get this “PHP Fatal error: Uncaught Exception: Failed to validate code due to an error: HTTP 403 in /Applications/MAMP/htdocs…”
the concerned code is:
if ($responseCode !== 200)
throw new Exception(“Failed to validate code due to an error: HTTP {$responseCode}”);

You need to grab a personal token from https://build.envato.com/my-apps/, and enter it into the code. Make sure to enable the permission “View your items’ sales history”

image

For example:

    CURLOPT_HTTPHEADER => array(
        "Authorization: Bearer PKF7i4DBRlYiSfMRtMwogQR409Hk6N7ZT",
        "User-Agent: Purchase code verification script"
    )

i have created a personal token with the correct rights.
But my problem is i have not yet item in themeforest. i tried your code with one item i bought.

Not possible :frowning: … You’ll have to trust that it works, unfortunately. There is no way to get a test purchase code at the moment, and you can’t use a purchase code belonging to another author.

So what you mean is, i have to upload my theme, get a item id, before to able to test purchase code?

Many thanks baileyherbert for your great support, very helpful :blush:

As you suggest I pushed my plugin trusting on this as I cannot test it, but still not working!

How can I solve a problem I cannot reproduce and how can I marked as solved without being able to test it myself. Should I upload code until buyers don’t report any problem? It’ll take ages and I’ll annoy my customers.

I have a buyer purchase code and I’ve created a personal token and I still getting an error:

{"error":404,"description":"No sale belonging to the current user found with that code"}

Any help would be appreciated.

Thank you

Hi @pdgsolutions,

In general, you can diagnose issues surrounding purchase code verification through these steps:

  • Make sure the purchase code is in the correct format (6b355577-0c68-47d8-974b-3bc0718b31a8).
  • Make sure the purchase code doesn’t have any whitespace before or after it (you can trim it).
  • Test the purchase code at https://build.envato.com/api#market_0_Author_Sale

If it still doesn’t work, please post your PHP code here or on a snippet-sharing website like GitHub Gist and I’ll take a look. Be sure to remove your personal token when doing so. :slight_smile:

Hey @pdgsolutions,

You can verifying your customer purchase code from this link also
https://build.envato.com/api#market_0_Author_Sale

Thanks!

1 Like