-
-
Save johnulist/669d643d775c6ab71f80010145d6967c to your computer and use it in GitHub Desktop.
Using the verify-purchase endpoint of the new Envato API to validate a purchase code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// NOTE: verify-purchase has been deprecated and it's best to use the new /author/sale endpoint as documented on http://build.envato.com/ | |
// created by Envato user wpdreams https://forums.envato.com/t/verify-purchase-class/3526 | |
// usage example: | |
$o = EnvatoApi2::verifyPurchase( $purchase_code ); | |
if ( is_object($o) ) { | |
// valid... | |
// $o contains the purchase data | |
} | |
// class: | |
class EnvatoApi2 { | |
// Bearer, no need for OAUTH token, change this to your bearer string | |
// https://build.envato.com/api/#token | |
private static $bearer = "xxxxxxxxxxxxxxxxxxxxxxxxxx"; | |
static function getPurchaseData( $code ) { | |
//setting the header for the rest of the api | |
$bearer = 'bearer ' . self::$bearer; | |
$header = array(); | |
$header[] = 'Content-length: 0'; | |
$header[] = 'Content-type: application/json; charset=utf-8'; | |
$header[] = 'Authorization: ' . $bearer; | |
$verify_url = 'https://api.envato.com/v1/market/private/user/verify-purchase:'.$code.'.json'; | |
$ch_verify = curl_init( $verify_url . '?code=' . $code ); | |
curl_setopt( $ch_verify, CURLOPT_HTTPHEADER, $header ); | |
curl_setopt( $ch_verify, CURLOPT_SSL_VERIFYPEER, false ); | |
curl_setopt( $ch_verify, CURLOPT_RETURNTRANSFER, 1 ); | |
curl_setopt( $ch_verify, CURLOPT_CONNECTTIMEOUT, 5 ); | |
curl_setopt( $ch_verify, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); | |
$cinit_verify_data = curl_exec( $ch_verify ); | |
curl_close( $ch_verify ); | |
if ($cinit_verify_data != "") | |
return json_decode($cinit_verify_data); | |
else | |
return false; | |
} | |
static function verifyPurchase( $code ) { | |
$verify_obj = self::getPurchaseData($code); | |
// Check for correct verify code | |
if ( | |
(false === $verify_obj) || | |
!is_object($verify_obj) || | |
!isset($verify_obj->{"verify-purchase"}) || | |
!isset($verify_obj->{"verify-purchase"}->item_name) | |
) | |
return -1; | |
// If empty or date present, then it's valid | |
if ( | |
$verify_obj->{"verify-purchase"}->supported_until == "" || | |
$verify_obj->{"verify-purchase"}->supported_until != null | |
) | |
return $verify_obj->{"verify-purchase"}; | |
// Null or something non-string value, thus support period over | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment