'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 https://github.com/codehaiku/Envato-Purchase-Code-Verifier 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;
}
}
}
Solution 1:[1]
Please try following, Its tested and working
<?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"
)
));
Solution 2:[2]
Try this instead:
function verify_purchase_code($code) {
$code = urlencode($code);
$url = "https://envato.eduardofiorini.com/index.php?item=35215272&code=" . $code . "&domain=" . $_SERVER['HTTP_HOST'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Content-type: application/json'));
$ret = curl_exec($ch);
curl_close($ch);
if (!$ret) {
$ret = file_get_contents($url);
}
$data = json_decode($ret??"{}",true);
if($data["error"]){
echo json_encode(array("success" => false, "message" => $data["msg"]));
exit();
}else{
return true;
}
}
Solution 3:[3]
Check whether you have updated the composer and ensure that you have the file EnvatoPurchaseCodeVerifier.php. Because seeing to your code snippet what I understand you try to add the class EnvatoPurchaseCodeVerifier in the same file and some php opening tag before that class is missing. The proper way would be put that in a different file and check whether the composer autoload works fine.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rameez Iqbal |
Solution 2 | codelone |
Solution 3 | Prakash S |