3007 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 200+ members
  • Has sold $250,000+ on Envato Market
  • Had an item featured in an Envato Bundle
+7 more
duotive says
Hello.
A lot of users may use this one on their support forums registrations.

I decided to make a function that checks a purchase using the Envato API , your username, your api key, and the customers “Item Purchase Code” ( they can get this from the license file ) .

I wanted to check it and make sure that it works 100%, but i got banned from the API :) (“You have been locked out of the API for 1 hour because of too many requests. Please complain to the author of your widget/application.” )

Here is the function. Please test it and tell me if it works well, and also use it as you like in your support forums.


<?php function envato_verify_purchase($purchase_code)
{
    //SETUP THE API DATA

    $username = ''; 
    $api_key = '';

    //CHECK IF THE CALL FOR THE FUNCTION WAS EMPTY
    if ( $purchase_code != '' ):

        /*
            STEPS IN THE CODE BELOW:
             - QUERY ENVATO API FOR JSON RESULT
             - DECODE THE RESULT AND TRANSFORM IT FROM OBJECTS TO AN ARRAY
             - CHECK IF THERE IS A ITEM TITLE == THE PURCHASE WAS MADE OR NOT
        */            

        $result_from_json = file_get_contents('http://marketplace.envato.com/api/edge/'.$username.'/'.$api_key.'/verify-purchase:'.$purchase_code.'.json');
        $result = json_decode($result_from_json, true);

        if ( $result['verify-purchase']['item_name'] ) :
            return 1;
        else:
            return 0;
        endif;
    endif;        
}
?>

the comments explain the code
4390 posts ThemeForest Reviewer
  • Has referred 1+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Member of the Envato Team
+12 more
Ivor Envato team says

Hey thanks man. :)

3320 posts
  • Provided great contribution to our forums
  • Nominated Community Superstar of the month
  • Helps us moderate the forums
  • Has been part of the Envato Community for over 5 years
+18 more
dtbaker Moderator says

This code may fail if they have purchased more than 1 license of the item.

It may also fail because it doesn’t check exactly which item the user has purchased from you. Only that the user has made a purchase from you.

Also file_get_contents on a url may not work on a lot of hosting accounts. Suggest using curl. Possibly try something like this:

$result = false; // have we got a valid purchase code?
$our_item_id = 149180; // check if they've bought this item id.
$username = 'dtbaker'; // authors username
$api_key = 'gphyrdsomething'; // api key from my account area
$url = "http://marketplace.envato.com/api/edge/$username/$api_key/verify-purchase:$code.json";
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$json_res = curl_exec($ch);
$data = json_decode($json_res,true);
$purchases = $data['verify-purchase'];
if(isset($purchases['buyer'])){
   // format single purchases same as multi purchases
   $purchases=array($purchases); 
}
$purchase_details = array();
foreach($purchases as $purchase){
    $purchase=(array)$purchase; // json issues
    if((int)$purchase['item_id']==(int)$our_item_id){
        // we have a winner!
        $result = true;
        $purchase_details = $purchase;
    }
}
// do something with the users purchase details, 
// eg: check which license they've bought, save their username something
if($result){
echo 'user has bought our item';
print_r($purchase_details);
}else{
echo 'invalid purchase code';
}
3320 posts
  • Provided great contribution to our forums
  • Nominated Community Superstar of the month
  • Helps us moderate the forums
  • Has been part of the Envato Community for over 5 years
+18 more
dtbaker Moderator says

FYI here’s an example reply from the API :

{"verify-purchase":{"buyer":"buyer_id_here","created_at":"Wed Dec 01 05:00:47 +1100 2010","licence":"Regular Licence","item_name":"Your Item Name Here","item_id":"116430"}}
45 posts
  • Has collected 10+ items on Envato Market
  • Pulled off some great moustache to raise money or awareness for Movember Australia
  • Has been part of the Envato Community for over 2 years
  • Located in United States
premiumwd says

how to implement this into a registration page?

4 posts
  • Has been part of the Envato Community for over 5 years
  • Has collected 1+ items on Envato Market
  • Located in Indonesia
feryardiant says

Hi! thanks to @duotive and @dtbaker for the explanation. My WP theme still in progress and I have a plan to make the Purchase Code as option to enable the updater (Product Serial Number likes).

Some of Themeforest authors give a notification about their purchased theme/plugin(s) have an update from WP dashboard than the buyer have to go to the theme/plugin themeforest page to get the new file(s). Cause of that, I got an idea to make a “setup installation” after theme/plugin activated, than the user must input their Purchase Code > click the Finish button and after that buyer can get the updated files directly on their WP dashbord or Theme option as well.

My question is. can I use the Purchase Code as Product Serial Number likes? I wonder what I can do with this function, may be integrating the supports directly from WP dashboard and much more :D

3007 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 200+ members
  • Has sold $250,000+ on Envato Market
  • Had an item featured in an Envato Bundle
+7 more
duotive says

Hi. That is kinda useless now. Study this: https://github.com/JeffreyWay/Envato-Marketplace-API-Wrapper-in-PHP – this is what all of us are using. :D

4 posts
  • Has been part of the Envato Community for over 5 years
  • Has collected 1+ items on Envato Market
  • Located in Indonesia
feryardiant says

thanks for the repo, i’m already on it :smitten:

um, can you give me your consideration about my idea?

thanks and regard :D

3007 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 200+ members
  • Has sold $250,000+ on Envato Market
  • Had an item featured in an Envato Bundle
+7 more
duotive says

You’re welcome. I tried to think about that too, but did not do that because of bad ratings that will come with it, because customers will not know how to use it.

751 posts
  • Has been part of the Envato Community for over 6 years
  • Has referred 1+ members
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
CyberShot says

The timing for something like this couldn’t be better. I was just wondering how to do this very thing. I am looking into using another tutorial on tuts plus to make a custom logon to my site

http://wp.tutsplus.com/tutorials/creative-coding/bringing-the-membership-process-to-the-front-of-your-site/

I wanted to grab the purchase code and or the persons username for themeforest and have the system check to see if they are a buyer. This would make a fantastic plugin for one of you guys smart enough to code it. I would buy that.

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by