Can you help w/ purchase code authentication?

When I try to authenticate it works but all of my clients get “No purchase code belonging to the sale” Not sure what I am doing wrong. Here is snippet of the code I am using. I believe the API @ build it setup all correctly and authentication and access code step is done as it should.

$request_url = "https://api.envato.com/v3/market/author/sale?code=${purchase_code}";
$curl_handle = curl_init();
$headers     = array(
	'Authorization: Bearer ' . $access_token,
	'User-Agent: Purchase code verification',
);

curl_setopt( $curl_handle, CURLOPT_URL, $request_url );
curl_setopt( $curl_handle, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $curl_handle, CURLOPT_HEADER, 0 );
curl_setopt( $curl_handle, CURLOPT_CUSTOMREQUEST, 'GET' );
curl_setopt( $curl_handle, CURLOPT_POSTFIELDS, $body );
curl_setopt( $curl_handle, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl_handle, CURLOPT_TIMEOUT, 30 );

$verify_purchase_code_obj = json_decode( curl_exec( $curl_handle ) );

if ( 404 === $verify_purchase_code_obj->error ) {
	echo json_encode(
		array(
			'status'  => 0,
			'message' => $verify_purchase_code_obj->description,
		)
	);
	exit;
}

Something I just noticed when I use my Envato account authentication works but it doesn’t work for clients!

Thank you for your help in advance.
Krasen

Are you having buyers provide their own access token for this code? If so then you need to use the /buyer/purchase endpoint for them. The one you’re using, /author/sale, only works for authors.

So I if understand correctly either of the snippets below should work?

$request_url = "https://api.envato.com/v3/market/author/sale?code=${purchase_code}";
$curl_handle = curl_init();
$headers     = array(
	'Authorization: Bearer [I create this via Your Personal Token Section]',
	'User-Agent: Purchase code verification',
);

or

$request_url = "https://api.envato.com/v3/market/buyer/purchase?code=${purchase_code}";
$curl_handle = curl_init();
$headers     = array(
	'Authorization: Bearer ' . $access_token,
	'User-Agent: Purchase code verification',
);

Yes, buyers are require to authenticate first w/ their Envato account, and then I pass the access_token for purchase code auth

Ah, so you’re using OAuth.

In that case you’ll definitely need to use your bottom code snippet (with /buyer/purchase), and it’s important to note that purchase codes will only work if they belong to the current account, so nobody except the buyer can verify the purchase (this might not be ideal for certain types of items, such as themes).

FYI since you’re using OAuth you can actually use the /buyer/purchases endpoint to list all of their purchases at once so they don’t even need to enter a purchase code. I can provide more details if interested.

1 Like