1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use WHMCS\Database\Capsule; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* A WHMCS module for use by Jetpack hosting partners to provision Jetpack plans. |
8
|
|
|
* The module provides functionality for partner hosts to be able to save their |
9
|
|
|
* client id and secret to request an access token for provisioning plans. |
10
|
|
|
* |
11
|
|
|
* Plans available for provisioning include free, personal, premium and professional |
12
|
|
|
* |
13
|
|
|
* A host has options to either provision(Create == WHMCS equivalent functional term) |
14
|
|
|
* or Cancel(Terminate == WHMCS equivalent functional term) from the WHMCS client area. |
15
|
|
|
* |
16
|
|
|
* Host setup for custom fields is currently required in order to use the module. |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Jetpack Meta Data for WHMCS module. |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
if (!defined("WHMCS")) { |
26
|
|
|
die("This file cannot be accessed directly"); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
function jetpack_MetaData() |
30
|
|
|
{ |
31
|
|
|
return array( |
32
|
|
|
'DisplayName' => 'Jetpack by Automattic', |
33
|
|
|
'Description' => 'Use this module to provision Jetpack plans with your Jetpack hosting partner account', |
34
|
|
|
'APIVersion' => '1.1', |
35
|
|
|
'RequiresServer' => false, |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Basic configuration options required for a partner to get |
42
|
|
|
* a Jetpack plan provisioned. Currently a partner client id |
43
|
|
|
* and secret are the only host partner options needed to get |
44
|
|
|
* an access token to provision a Jetpack plan |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
function jetpack_ConfigOptions() |
48
|
|
|
{ |
49
|
|
|
return array( |
50
|
|
|
'Jetpack Partner Client ID' => array( |
51
|
|
|
'Type' => 'text', |
52
|
|
|
'Size' => '256', |
53
|
|
|
), |
54
|
|
|
'Jetpack Partner Client Secret' => array( |
55
|
|
|
'Type' => 'text', |
56
|
|
|
'Size' => '256', |
57
|
|
|
) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Equivalent to /provision. Create a Jetpack plan using |
64
|
|
|
* a Jetpack Hosting partner account. |
65
|
|
|
* |
66
|
|
|
* |
67
|
|
|
* @param array $params |
68
|
|
|
* @return string 'success' |
69
|
|
|
* @throws Exception |
70
|
|
|
|
71
|
|
|
*/ |
72
|
|
|
function jetpack_CreateAccount(array $params) |
73
|
|
|
{ |
74
|
|
|
|
75
|
|
|
validate_required_fields($params); |
76
|
|
|
$access_token = get_access_token($params); |
77
|
|
|
$response = provision_jetpack_plan($access_token, $params); |
78
|
|
|
|
79
|
|
|
if (isset($response->next_url)) { |
80
|
|
|
save_next_url($response->next_url, $params); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($response->success == true) { |
84
|
|
|
return 'success'; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Equivalent to partner/cancel. Cancel a Jetpack plan using |
90
|
|
|
* using a Jetpack Hosting partner account. |
91
|
|
|
* |
92
|
|
|
* @param array $params |
93
|
|
|
* @return string |
94
|
|
|
* @throws Exception |
95
|
|
|
*/ |
96
|
|
|
function jetpack_TerminateAccount(array $params) |
97
|
|
|
{ |
98
|
|
|
$access_token = get_access_token($params); |
99
|
|
|
$clean_url = str_replace('/', '::', $params['customfields']['siteurl']); |
100
|
|
|
$url = 'https://public-api.wordpress.com/rest/v1.3/jpphp/' . $clean_url .'/partner-cancel'; |
101
|
|
|
$response = make_api_request($url, $access_token); |
102
|
|
|
if ($response->success == true) { |
103
|
|
|
return 'success'; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get a Jetpack partner access token using the client_id |
109
|
|
|
* and client secret stored when the product was created in |
110
|
|
|
* the WHMCS product settings. |
111
|
|
|
* |
112
|
|
|
* |
113
|
|
|
* @param $params |
114
|
|
|
* @return mixed |
115
|
|
|
* @throws Exception |
116
|
|
|
*/ |
117
|
|
|
function get_access_token($params) |
118
|
|
|
{ |
119
|
|
|
|
120
|
|
|
$oauthURL = "https://public-api.wordpress.com/oauth2/token"; |
121
|
|
|
|
122
|
|
|
$credentials = array ( |
123
|
|
|
'client_id' => $params['configoption1'], |
124
|
|
|
'client_secret' => $params['configoption2'], |
125
|
|
|
'grant_type' => 'client_credentials', |
126
|
|
|
'scope' => 'jetpack-partner', |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
$response = make_api_request($oauthURL, null, $credentials); |
130
|
|
|
if (isset($response->access_token)) { |
131
|
|
|
return $response->access_token; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Provision a Jetpack Plan |
138
|
|
|
* |
139
|
|
|
* @param $access_token |
140
|
|
|
* @param $params |
141
|
|
|
* @return mixed |
142
|
|
|
* @throws Exception |
143
|
|
|
*/ |
144
|
|
|
function provision_jetpack_plan($access_token, $params) |
145
|
|
|
{ |
146
|
|
|
$provisioning_url = "https://public-api.wordpress.com/rest/v1.3/jpphp/provision"; |
147
|
|
|
$request_data = array ( |
148
|
|
|
'plan' => $params['customfields']['plan'], |
149
|
|
|
'siteurl' => $params['customfields']['siteurl'], |
150
|
|
|
'local_user' => $params['customfields']['local_user'], |
151
|
|
|
'force_register' => true, |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
return make_api_request($provisioning_url, $access_token, $request_data); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Make an API request for authenticating and provisioning or |
160
|
|
|
* cancelling a Jetpack plan |
161
|
|
|
* |
162
|
|
|
* @param $url |
163
|
|
|
* @param $data |
164
|
|
|
* @param string $auth |
165
|
|
|
* @return mixed |
166
|
|
|
* @throws Exception |
167
|
|
|
*/ |
168
|
|
|
function make_api_request($url, $auth='', $data=[]) |
169
|
|
|
{ |
170
|
|
|
if (isset($auth)) { |
171
|
|
|
$auth = "Authorization: Bearer " . $auth; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$curl = curl_init(); |
175
|
|
|
curl_setopt_array($curl, array( |
176
|
|
|
CURLOPT_HTTPHEADER => array($auth), |
177
|
|
|
CURLOPT_URL => $url, |
178
|
|
|
CURLOPT_RETURNTRANSFER => true, |
179
|
|
|
CURLOPT_ENCODING => "", |
180
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
181
|
|
|
CURLOPT_POSTFIELDS => $data, |
182
|
|
|
CURLOPT_CUSTOMREQUEST => "POST" |
183
|
|
|
)); |
184
|
|
|
|
185
|
|
|
$response = curl_exec($curl); |
186
|
|
|
if (curl_error($curl)) { |
187
|
|
|
throw new Exception('Unable to connect: ' . curl_errno($curl) . ' - ' . curl_error($curl)); |
188
|
|
|
} elseif (empty($response)) { |
189
|
|
|
throw new Exception('Empty response'); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
curl_close($curl); |
193
|
|
|
|
194
|
|
|
return json_decode($response); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Save the next_url for Jetpack activation/setup to the |
199
|
|
|
* order for the client |
200
|
|
|
* |
201
|
|
|
* @param $url |
202
|
|
|
* @param $orderId |
203
|
|
|
*/ |
204
|
|
|
function save_next_url($url, $params) |
205
|
|
|
{ |
206
|
|
|
$jetpack_next_url_field = Capsule::table('tblcustomfields') |
207
|
|
|
->where(array('fieldname' => 'jetpack_next_url', 'type' => 'product'))->first(); |
208
|
|
|
|
209
|
|
|
Capsule::table('tblcustomfieldsvalues')->insert(array( |
210
|
|
|
'fieldid' => $jetpack_next_url_field->id, 'relid' => $params['model']['orderId'], 'value' => $url)); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Validate that the fields required to provision a Jetpack plan are present |
215
|
|
|
* and valid for those that can be verified |
216
|
|
|
* |
217
|
|
|
* @param array $params |
218
|
|
|
* @return bool |
219
|
|
|
*/ |
220
|
|
|
function validate_required_fields(array $params) |
221
|
|
|
{ |
222
|
|
|
$allowed_plans = array('free', 'personal', 'premium', 'professional'); |
223
|
|
|
|
224
|
|
|
if (!isset($params['customfields']['plan']) || !isset($params['customfields']['siteurl']) |
225
|
|
|
|| isset($params['customfields']['local_user'])) { |
226
|
|
|
|
227
|
|
|
return false; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
if (!in_array($params['customfields']['plan'], $allowed_plans)) { |
231
|
|
|
return false; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
|