How to validate Envato Purchase Code in PHP

I’m working on Envato API to verify Purchase Code I used plugin sample code from GitHub GitHub - codehaiku/envato-purchase-code-verifier: 🚂 A nifty tool for Envato Authors needing to create purchase code verifier as easy and as fast as possible. here I added token and AUTHOR_SALES_ENDPOINT_URI = ‘https://api.envato.com/v2/market/author/sale?code=’ 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.

 <!DOCTYPE html>
<html>
<head>
    <title>Sample Envato Purchase Code Verifier</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div class="wrap">
        <!-- // Include the EnvatoPurchaseCodeVerifier.php file that holds the verifier class.--> 
        <?php require_once 'EnvatoPurchaseCodeVerifier.php'; ?>
        <!-- // Create your own access token at (https://build.envato.com/create-token)-->
        <?php $access_token = 'my token here'; // Change 'your_token' value with your own token ?>
        <!-- // Create new instance of EnvatoPurchaseCodeVerifier. -->
        <!-- // Pass your own access token -->
        <?php $purchase = new EnvatoPurchaseCodeVerifier($access_token); ?>

        <?php $buyer_purchase_code = filter_input(INPUT_POST, 'purchase_code', FILTER_DEFAULT); ?>

        <!-- check if user submits the form -->
        <?php if ( ! empty( $buyer_purchase_code ) ) { ?>

            <?php $verified = $purchase->verified($buyer_purchase_code); ?>
            <!-- User purchase code is good!-->
            <?php if ( $verified ) { ?>
                <?php
                     $item_id = $verified->item->id; 
                     $item_name = $verified->item->name; 
                     $buyer = $verified->buyer; 
                     $license = $verified->license; 
                     $amount = $verified->amount; 
                     $sold_at = $verified->sold_at; 
                     $supported_until = $verified->supported_until;
                 ?>
                 <h1>Valid Purchase Code</h1>
                 <table>
                    <tr><td>Item ID:</td><td><?php echo $item_id; ?></td></tr>
                    <tr><td>Item Name:</td><td><?php echo $item_name; ?></td></tr>
                    <tr><td>Buyer:</td><td><?php echo $buyer; ?></td></tr>
                    <tr><td>License:</td><td><?php echo $license; ?></td></tr>
                    <tr><td>Amount:</td><td><?php echo $amount; ?></td></tr>
                    <tr><td>Sold At:</td><td><?php echo $sold_at; ?></td></tr>
                    <tr><td>Supported Until:</td><td><?php echo $supported_until; ?></td></tr>
                </table>
            <?php } else { ?>
                <!-- Invalid purchase code -->
                <h1>Invalid Purchase Code</h1>
            <?php } ?>

        <?php } else  { ?>
            <form action="index.php" method="post">
                <label>Purchase Code: </label>
                <input type="text" name="purchase_code" placeholder="Type or paste the buyer's purchase code here"><br>
                <input type="submit">
            </form>
        <?php } ?>


    </div>
</body>
</html>


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 <https://build.envato.com/create-token/>
     */
    public function __construct($accessToken)
    {
        $this->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->response info on success.
     */
    public function verified($purchaseCode)
    {
        if (empty($purchaseCode)) {
            return false;
        }

        $curl = new Curl();

        $curl->setHeader('Authorization', 'Bearer ' . $this->accessToken);

        $curl->get(self::AUTHOR_SALES_ENDPOINT_URI, array(
            'code' => $purchaseCode
        ));

        $curl->close();

        if ($curl->error) {
            return false;
        } else {
            return $curl->response;
        }
    }
}

Tutorial Solution:

2 Likes