1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elimuswift\Core; |
4
|
|
|
|
5
|
|
|
class EnvatoApi |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Envanto API bearer token. |
9
|
|
|
* |
10
|
|
|
* @var string |
11
|
|
|
**/ |
12
|
|
|
private $bearer; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Purchase verification endpoint. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
**/ |
19
|
|
|
protected $url = 'https://api.envato.com/v3/market/author/sale?code='; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Request headers. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
**/ |
26
|
|
|
private $headers; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Create EnvantoApi Instance. |
30
|
|
|
**/ |
31
|
|
|
public function __construct($bearer) |
32
|
|
|
{ |
33
|
|
|
$this->bearer = $bearer; |
34
|
|
|
$this->buildHeaders(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
//end __construct() |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Make a call to the Envato API to verify purchase. |
41
|
|
|
* |
42
|
|
|
* @return mixed Guzzle\Response::getBody() |
43
|
|
|
* |
44
|
|
|
* @param string $code |
45
|
|
|
**/ |
46
|
|
|
public function getPurchaseData($code) |
47
|
|
|
{ |
48
|
|
|
$ch_verify = curl_init($this->url . $code); |
49
|
|
|
curl_setopt($ch_verify, CURLOPT_HTTPHEADER, $this->headers); |
50
|
|
|
curl_setopt($ch_verify, CURLOPT_SSL_VERIFYPEER, false); |
51
|
|
|
curl_setopt($ch_verify, CURLOPT_RETURNTRANSFER, 1); |
52
|
|
|
curl_setopt($ch_verify, CURLOPT_CONNECTTIMEOUT, 5); |
53
|
|
|
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'); |
54
|
|
|
|
55
|
|
|
$cinit_verify_data = curl_exec($ch_verify); |
56
|
|
|
curl_close($ch_verify); |
57
|
|
|
|
58
|
|
|
if ($cinit_verify_data != '') { |
59
|
|
|
return json_decode($cinit_verify_data, true); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return [ |
63
|
|
|
'error' => 'exception', |
64
|
|
|
'description' => 'A server error was encountered please notify us if you see this', |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
//end getPurchaseData() |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Verify purchase. |
72
|
|
|
* |
73
|
|
|
* @return string Array |
74
|
|
|
* |
75
|
|
|
* @param string $code Purchase Code |
76
|
|
|
**/ |
77
|
|
|
public function verifyPurchase(string $code) |
78
|
|
|
{ |
79
|
|
|
$purchase = []; |
80
|
|
|
$purchase['response'] = (object) $this->getPurchaseData($code); |
81
|
|
|
if ($purchase->error) { |
82
|
|
|
return $purchase['status'] = 'error'; |
83
|
|
|
} else { |
84
|
|
|
return $purchase['status'] = 'success'; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
//end verifyPurchase() |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* setting the header for the rest of the api. |
92
|
|
|
**/ |
93
|
|
|
protected function buildHeaders() |
94
|
|
|
{ |
95
|
|
|
$headers = [ |
96
|
|
|
'Content-type' => 'application/json', |
97
|
|
|
'Authorization' => 'Bearer ' . $this->bearer, |
98
|
|
|
]; |
99
|
|
|
$h = []; |
100
|
|
|
foreach ($headers as $key => $value) { |
101
|
|
|
$h[] = $key . ':' . $value; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$this->headers = $h; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
//end buildHeaders() |
108
|
|
|
}//end class |
109
|
|
|
|