title: How to validate Envato Purchase Code in PHP
body: <p>I'm working on Envato API to verify Purchase Code I used plugin sample code from GitHub <a href="https://github.com/codehaiku/Envato-Purchase-Code-Verifier" rel="nofollow noreferrer">https://github.com/codehaiku/Envato-Purchase-Code-Verifier</a> here I added token and AUTHOR_SALES_ENDPOINT_URI = '<a href="https://api.envato.com/v2/market/author/sale?code=" rel="nofollow noreferrer">https://api.envato.com/v2/market/author/sale?code=</a>' also when I used to try I'm getting error Invalid Purchase Code when to the valid purchase code. Did i missed anything here? Can anyone suggest me. Is there any other alternative way to do.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code> &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Sample Envato Purchase Code Verifier&lt;/title&gt;
      &lt;link rel="stylesheet" type="text/css" href="style.css"&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div class="wrap"&gt;
          &lt;!-- // Include the EnvatoPurchaseCodeVerifier.php file that holds the verifier class.--&gt; 
          &lt;?php require_once 'EnvatoPurchaseCodeVerifier.php'; ?&gt;
          &lt;!-- // Create your own access token at (https://build.envato.com/create-token)--&gt;
          &lt;?php $access_token = 'my token here'; // Change 'your_token' value with your own token ?&gt;
          &lt;!-- // Create new instance of EnvatoPurchaseCodeVerifier. --&gt;
          &lt;!-- // Pass your own access token --&gt;
          &lt;?php $purchase = new EnvatoPurchaseCodeVerifier($access_token); ?&gt;
  
          &lt;?php $buyer_purchase_code = filter_input(INPUT_POST, 'purchase_code', FILTER_DEFAULT); ?&gt;
  
          &lt;!-- check if user submits the form --&gt;
          &lt;?php if ( ! empty( $buyer_purchase_code ) ) { ?&gt;
  
              &lt;?php $verified = $purchase-&gt;verified($buyer_purchase_code); ?&gt;
              &lt;!-- User purchase code is good!--&gt;
              &lt;?php if ( $verified ) { ?&gt;
                  &lt;?php
                       $item_id = $verified-&gt;item-&gt;id; 
                       $item_name = $verified-&gt;item-&gt;name; 
                       $buyer = $verified-&gt;buyer; 
                       $license = $verified-&gt;license; 
                       $amount = $verified-&gt;amount; 
                       $sold_at = $verified-&gt;sold_at; 
                       $supported_until = $verified-&gt;supported_until;
                   ?&gt;
                   &lt;h1&gt;Valid Purchase Code&lt;/h1&gt;
                   &lt;table&gt;
                      &lt;tr&gt;&lt;td&gt;Item ID:&lt;/td&gt;&lt;td&gt;&lt;?php echo $item_id; ?&gt;&lt;/td&gt;&lt;/tr&gt;
                      &lt;tr&gt;&lt;td&gt;Item Name:&lt;/td&gt;&lt;td&gt;&lt;?php echo $item_name; ?&gt;&lt;/td&gt;&lt;/tr&gt;
                      &lt;tr&gt;&lt;td&gt;Buyer:&lt;/td&gt;&lt;td&gt;&lt;?php echo $buyer; ?&gt;&lt;/td&gt;&lt;/tr&gt;
                      &lt;tr&gt;&lt;td&gt;License:&lt;/td&gt;&lt;td&gt;&lt;?php echo $license; ?&gt;&lt;/td&gt;&lt;/tr&gt;
                      &lt;tr&gt;&lt;td&gt;Amount:&lt;/td&gt;&lt;td&gt;&lt;?php echo $amount; ?&gt;&lt;/td&gt;&lt;/tr&gt;
                      &lt;tr&gt;&lt;td&gt;Sold At:&lt;/td&gt;&lt;td&gt;&lt;?php echo $sold_at; ?&gt;&lt;/td&gt;&lt;/tr&gt;
                      &lt;tr&gt;&lt;td&gt;Supported Until:&lt;/td&gt;&lt;td&gt;&lt;?php echo $supported_until; ?&gt;&lt;/td&gt;&lt;/tr&gt;
                  &lt;/table&gt;
              &lt;?php } else { ?&gt;
                  &lt;!-- Invalid purchase code --&gt;
                  &lt;h1&gt;Invalid Purchase Code&lt;/h1&gt;
              &lt;?php } ?&gt;
  
          &lt;?php } else  { ?&gt;
              &lt;form action="index.php" method="post"&gt;
                  &lt;label&gt;Purchase Code: &lt;/label&gt;
                  &lt;input type="text" name="purchase_code" placeholder="Type or paste the buyer's purchase code here"&gt;&lt;br&gt;
                  &lt;input type="submit"&gt;
              &lt;/form&gt;
          &lt;?php } ?&gt;
  
  
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  
  
  require_once __DIR__ . '/vendor/autoload.php';
  
  use Curl\Curl;
  
  final class EnvatoPurchaseCodeVerifier
  {
      protected $accessToken = '';
  
      const AUTHOR_SALES_ENDPOINT_URI = 'https://api.envato.com/v2/market/author/sale?code=';
  
      /**
       * Class constructor, pass the access token.
       * @param mixed The access token &lt;https://build.envato.com/create-token/&gt;
       */
      public function __construct($accessToken)
      {
          $this-&gt;accessToken = $accessToken;
      }
  
      /**
       * Supply the purchase code of the buyer as argument.
       * @param  mixed $purchaseCode The purchase code of he buyer.
       * @return mixed (Boolean) False on fail, the api (Object) $curl-&gt;response info on success.
       */
      public function verified($purchaseCode)
      {
          if (empty($purchaseCode)) {
              return false;
          }
  
          $curl = new Curl();
  
          $curl-&gt;setHeader('Authorization', 'Bearer ' . $this-&gt;accessToken);
  
          $curl-&gt;get(self::AUTHOR_SALES_ENDPOINT_URI, array(
              'code' =&gt; $purchaseCode
          ));
  
          $curl-&gt;close();
  
          if ($curl-&gt;error) {
              return false;
          } else {
              return $curl-&gt;response;
          }
      }
  }</code></pre>
  </div>
  </div>
  </p>