如何在PHP中验证Envato购买代码

时间:2018-10-02 16:29:52

标签: php json wordpress api

我正在使用Envato API验证购买代码,我使用了来自GitHub https://github.com/codehaiku/Envato-Purchase-Code-Verifier的插件示例代码,在这里,当我尝试尝试获取时,还添加了令牌和AUTHOR_SALES_ENDPOINT_URI ='https://api.envato.com/v2/market/author/sale?code='错误无效的购买代码何时输入有效的购买代码。我在这里错过了什么吗?谁能建议我。还有其他替代方法吗?

 <!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;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

请尝试遵循,它已经过测试并且可以正常工作

<?php 
$code = "86781236-23d0-4b3c-7dfa-c1c147e0dece";

// If you took $code from user input it's a good idea to trim it:

$code = trim($code);

// Make sure the code is valid before sending it to Envato:

if (!preg_match("/^(\w{8})-((\w{4})-){3}(\w{12})$/", $code))
    throw new Exception("Invalid code");

// Query using CURL:

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => "https://api.envato.com/v3/market/author/sale?code={$code}",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 20,

    CURLOPT_HTTPHEADER => array(
        "Authorization: Bearer KEY_HERE",
        "User-Agent: Enter a description of your app here for the API team"
    )
));

答案 1 :(得分:-1)

检查是否已更新作曲家,并确保您拥有文件EnvatoPurchaseCodeVerifier.php。因为看到您的代码片段我了解的内容,所以您尝试在缺少该类之前将EnvatoPurchaseCodeVerifier类添加到同一文件中,并添加一些php开头标记。正确的方法是将其放在其他文件中,并检查作曲家的自动加载是否正常。