-
Notifications
You must be signed in to change notification settings - Fork 0
/
afapi.php
134 lines (113 loc) · 3.64 KB
/
afapi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
class OAuthRequiredException extends Exception { }
class OAuthErrorException extends Exception { }
class AFApi {
function __construct($clientId, $secret, $state, $oauthRedirectUri, $isSandBox=False) {
$this->clientId = $clientId;
$this->secret = $secret;
$this->state = $state;
$this->isSandbox = $isSandBox;
$this->redirectUri = $oauthRedirectUri;
$this->baseUrl = $isSandBox ? 'https://api.appsfuel.com/v1/sandbox' : 'https://api.appsfuel.com/v1/live';
}
function getRequestTokenUrl() {
if ($this->isSandbox)
$baseUrl = 'https://api.appsfuel.com/v1/sandbox/choose/';
else
$baseUrl = 'http://app.appsfuel.com/content/permission';
return $baseUrl . '?client_id=' . $this->clientId .
'&redirect_uri=' . urlencode($this->redirectUri) .
'&state='.$this->state .
'&response_type=code';
}
function oauthAuthentication() {
if (!session_id()) {
session_start();
}
if (($_SESSION['appsfuel_access_token_expiring'] < time()) &&
array_key_exists('appsfuel_access_token', $_SESSION) &&
$_SESSION['appsfuel_access_token'])
{
return;
}
unset($_SESSION['appsfuel_access_token_expiring']);
unset($_SESSION['appsfuel_access_token']);
if (array_key_exists('error', $_GET)) {
throw new OAuthErrorException($_GET['error']);
}
if (!array_key_exists('code', $_GET)) {
header('Location: ' . $this->getRequestTokenUrl(), true, 302);
exit();
}
$resp = $this->requestAccessToken($_GET['code']);
$_SESSION['appsfuel_access_token'] = $resp['access_token'];
$_SESSION['appsfuel_access_token_expiring'] = time() + $resp['expire_in'];
}
function getAccessToken() {
$this->oauthAuthentication();
return $_SESSION['appsfuel_access_token'];
}
function requestAccessToken($code) {
$url = $this->baseUrl . '/oauth/token';
$data = array("code" => $code,
"grant_type" => "authorization_code",
"state" => $this->state,
"redirect_uri" => $this->redirectUri,
"client_id" => $this->clientId,
"client_secret" => $this->secret);
$resp = $this->post($url, $data);
return $resp;
}
function getUserInfo($accessToken) {
$url = $this->baseUrl . '/user';
$url .= '?access_token=' . $accessToken;
$resp = $this->get($url);
return $resp;
}
function getAppInfo($accessToken, $redirectUri) {
$url = $this->baseUrl . '/app';
$url .= '?access_token=' . $accessToken .
'&back_url=' . $redirectUri;
$resp = $this->get($url);
return $resp;
}
function getActiveProducts($iso_language) {
$url = $this->baseUrl . '/iap/getActiveProducts';
$url .= '?access_token=' . $accessToken . '&iso_language=' . $iso_language;
$resp = $this->get($url);
return $resp;
}
function restorePurchases() {
$url = $this->baseUrl . '/iap/restorePurchases';
$url .= '?access_token=' . $accessToken;
$resp = $this->get($url);
return $resp;
}
protected function get($url) {
return $this->url_request($url);
}
protected function post($url, $data) {
$fields = '';
foreach($data as $key => $value)
$fields .= $key . '=' . $value . '&';
rtrim($fields, '&');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
return $this->url_request($url,$ch);
}
protected function url_request($url, $ch=false) {
if (!$ch) $ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
$http_status = curl_getinfo($http, CURLINFO_HTTP_CODE);
if (($http_status / 100) != 2) {
// throw
throw new OAuthRequiredException($response);
}
return json_decode($response, true);
}
}