1 | <?php |
||
2 | /** |
||
3 | * ApiClient is a simple class for sending requests to FastSpring API. |
||
4 | * |
||
5 | * This class aims to handle some APIs of Fastspring if you think to develop and |
||
6 | * add some other features please consider not doing it with one class. |
||
7 | * |
||
8 | * Note: This class does not cover whole FastSpring API, it covers just a couple |
||
9 | * of things in FastSpring API like accounts and sessions. |
||
10 | * |
||
11 | * Example usage: |
||
12 | * ```php |
||
13 | * $fastspring = new ApiClient(); |
||
14 | * $accounts = $fastspring->getAccounts(); |
||
15 | * ``` |
||
16 | * |
||
17 | * @author Bilal Gultekin <[email protected]> |
||
18 | * @version v0.1: |
||
19 | * @copyright 2019 22 Digital |
||
20 | * @license MIT |
||
21 | * @see https://docs.fastspring.com/integrating-with-fastspring/fastspring-api |
||
22 | */ |
||
23 | |||
24 | namespace TwentyTwoDigital\CashierFastspring\Fastspring; |
||
25 | |||
26 | use GuzzleHttp\Client; |
||
27 | |||
28 | /** |
||
29 | * This class describes an api client. |
||
30 | */ |
||
31 | class ApiClient |
||
32 | { |
||
33 | /** |
||
34 | * The Fastspring API Username. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | public $username; |
||
39 | |||
40 | /** |
||
41 | * The Fastspring API password. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | public $password; |
||
46 | |||
47 | /** |
||
48 | * The Fastspring API Base Url. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | public $apiBase = 'https://api.fastspring.com'; |
||
53 | |||
54 | /** |
||
55 | * Global queries to apply every requests. |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | public $globalQuery = []; |
||
60 | |||
61 | /** |
||
62 | * Guzzle client options. |
||
63 | * Can be used to test class or process. |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | public $clientOptions = []; |
||
68 | |||
69 | /** |
||
70 | * Create a new Fastspring API interface instance. |
||
71 | * |
||
72 | * @param string $username Fastspring API username |
||
73 | * @param string $password Fastspring API password |
||
74 | * |
||
75 | * @return void |
||
76 | */ |
||
77 | 17 | public function __construct($username = null, $password = null) |
|
78 | { |
||
79 | 17 | $this->username = $username |
|
80 | 3 | ? $username |
|
81 | 16 | : (getenv('FASTSPRING_USERNAME') ?: config('services.fastspring.username')); |
|
82 | 17 | $this->password = $password |
|
83 | 3 | ? $password |
|
84 | 16 | : (getenv('FASTSPRING_PASSWORD') ?: config('services.fastspring.password')); |
|
85 | 17 | } |
|
86 | |||
87 | /** |
||
88 | * Send a request to Fastspring API with given parameters. |
||
89 | * |
||
90 | * @param string $method Method of HTTP request like PUT, GET, POST |
||
91 | * @param string $path Path of API |
||
92 | * @param array $query Query parameters array |
||
93 | * @param array $formParameters Form parameters |
||
94 | * @param array $jsonPayload Json payload |
||
95 | * |
||
96 | * @return \GuzzleHttp\Psr7\Response |
||
97 | */ |
||
98 | 31 | public function apiRequest($method, $path, $query = [], $formParameters = [], $jsonPayload = []) |
|
99 | { |
||
100 | // prepare guzzle options |
||
101 | 31 | $clientOptions = $this->clientOptions ?: ['base_uri' => $this->apiBase]; |
|
102 | |||
103 | // create guzzle instance |
||
104 | 31 | $client = new Client($clientOptions); |
|
105 | |||
106 | // delete first slash character |
||
107 | 31 | $path = ltrim($path, '/'); |
|
108 | |||
109 | // prepare options |
||
110 | $options = [ |
||
111 | 31 | 'auth' => [$this->username, $this->password], |
|
112 | 31 | 'query' => $this->globalQuery, |
|
113 | ]; |
||
114 | |||
115 | // set parameters |
||
116 | 31 | $options['query'] = array_merge($options['query'], $query); |
|
117 | |||
118 | 31 | if ($formParameters) { |
|
0 ignored issues
–
show
|
|||
119 | 1 | $options['form_params'] = $formParameters; |
|
120 | } |
||
121 | |||
122 | 31 | if ($jsonPayload) { |
|
0 ignored issues
–
show
The expression
$jsonPayload of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using ![]() |
|||
123 | 14 | $options['json'] = $jsonPayload; |
|
124 | } |
||
125 | |||
126 | // send request and get response |
||
127 | 31 | $response = $client->request($method, $path, $options); |
|
128 | |||
129 | // convert it to object |
||
130 | 31 | return $this->handleResponse($response); |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Set guzzle client options. |
||
135 | * |
||
136 | * @param array $options Guzzle client options |
||
137 | * |
||
138 | * @see http://docs.guzzlephp.org/en/latest/quickstart.html Quickstart |
||
139 | * @see http://docs.guzzlephp.org/en/latest/testing.html Testing |
||
140 | * |
||
141 | * @return void |
||
142 | */ |
||
143 | 36 | public function setClientOptions($options) |
|
144 | { |
||
145 | 36 | return $this->clientOptions = $options; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Set global query items. |
||
150 | * |
||
151 | * @param array $query Queries like ['mode' => 'test'] |
||
152 | * |
||
153 | * @return void |
||
154 | */ |
||
155 | 1 | public function setGlobalQuery($query) |
|
156 | { |
||
157 | 1 | return $this->globalQuery = $query; |
|
158 | } |
||
159 | |||
160 | /** |
||
161 | * Handle JSON response and convert it to array. |
||
162 | * |
||
163 | * @param \GuzzleHttp\Psr7\Response $response Guzzle response |
||
164 | * |
||
165 | * @return object |
||
166 | */ |
||
167 | 31 | protected function handleResponse($response) |
|
168 | { |
||
169 | 31 | $message = $response->getBody()->getContents(); |
|
170 | |||
171 | // json decode |
||
172 | // we assume fastspring sends always json |
||
173 | 31 | return json_decode($message); |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * Create account. |
||
178 | * |
||
179 | * @param array $account Account details |
||
180 | * |
||
181 | * @see https://docs.fastspring.com/integrating-with-fastspring/fastspring-api/accounts Account details |
||
182 | * |
||
183 | * @return object Response of fastspring |
||
184 | */ |
||
185 | 4 | public function createAccount($account) |
|
186 | { |
||
187 | 4 | return $this->apiRequest('POST', 'accounts', [], [], $account); |
|
188 | } |
||
189 | |||
190 | /** |
||
191 | * Update account. |
||
192 | * |
||
193 | * @param string $fastspringId Fastspring ID of related account |
||
194 | * @param array $account Account details |
||
195 | * |
||
196 | * @see https://docs.fastspring.com/integrating-with-fastspring/fastspring-api/accounts Account details |
||
197 | * |
||
198 | * @return object Response of fastspring |
||
199 | */ |
||
200 | 2 | public function updateAccount($fastspringId, $account) |
|
201 | { |
||
202 | 2 | return $this->apiRequest('POST', implode('/', ['accounts', $fastspringId]), [], [], $account); |
|
203 | } |
||
204 | |||
205 | /** |
||
206 | * Get account list. |
||
207 | * |
||
208 | * @param array $parameters Query parameters |
||
209 | * |
||
210 | * @return object Response of fastspring |
||
211 | */ |
||
212 | 3 | public function getAccounts($parameters = []) |
|
213 | { |
||
214 | 3 | return $this->apiRequest('GET', 'accounts', $parameters, [], []); |
|
215 | } |
||
216 | |||
217 | /** |
||
218 | * Get the account with the given id. |
||
219 | * |
||
220 | * @param string|int $accountId ID of the account |
||
221 | * @param array $parameters Query Parameters |
||
222 | * |
||
223 | * @return object Response of fastspring |
||
224 | */ |
||
225 | 2 | public function getAccount($accountId, $parameters = []) |
|
226 | { |
||
227 | 2 | return $this->apiRequest('GET', implode('/', ['accounts', $accountId]), $parameters, [], []); |
|
228 | } |
||
229 | |||
230 | /** |
||
231 | * Create session. |
||
232 | * |
||
233 | * @param array $session Sessions details |
||
234 | * |
||
235 | * @see https://docs.fastspring.com/integrating-with-fastspring/fastspring-api/sessions Session details |
||
236 | * |
||
237 | * @return object Response of fastspring |
||
238 | */ |
||
239 | 5 | public function createSession($session) |
|
240 | { |
||
241 | 5 | return $this->apiRequest('POST', 'sessions', [], [], $session); |
|
242 | } |
||
243 | |||
244 | /** |
||
245 | * Get orders. |
||
246 | * |
||
247 | * @param array $parameters Query parameters |
||
248 | * |
||
249 | * @return object Response of fastspring |
||
250 | */ |
||
251 | 1 | public function getOrders($parameters = []) |
|
252 | { |
||
253 | 1 | return $this->apiRequest('GET', 'accounts', $parameters, [], []); |
|
254 | } |
||
255 | |||
256 | /** |
||
257 | * Get subscriptions. |
||
258 | * |
||
259 | * @param array $subscriptionIds Fastspring ids of subscriptions |
||
260 | * |
||
261 | * @see https://docs.fastspring.com/integrating-with-fastspring/fastspring-api/subscriptions#id-/subscriptions-Getoneormoresubscriptioninstances |
||
262 | * |
||
263 | * @return object Response of fastspring |
||
264 | */ |
||
265 | 1 | public function getSubscriptions($subscriptionIds) |
|
266 | { |
||
267 | 1 | return $this->apiRequest('GET', implode( |
|
268 | 1 | '/', |
|
269 | 1 | ['subscriptions', implode(',', $subscriptionIds)] |
|
270 | 1 | ), [], [], []); |
|
271 | } |
||
272 | |||
273 | /** |
||
274 | * Get subscription, returns one instance. |
||
275 | * |
||
276 | * @param array $subscriptionId Fastspring id of subscriptions |
||
277 | * |
||
278 | * @see https://docs.fastspring.com/integrating-with-fastspring/fastspring-api/subscriptions#id-/subscriptions-Getoneormoresubscriptioninstances |
||
279 | * |
||
280 | * @return object Response of fastspring |
||
281 | */ |
||
282 | 4 | public function getSubscriptionsEntries($subscriptionIds) |
|
283 | { |
||
284 | 4 | return $this->apiRequest('GET', implode( |
|
285 | 4 | '/', |
|
286 | 4 | ['subscriptions', implode(',', $subscriptionIds), 'entries'] |
|
287 | 4 | ), [], [], []); |
|
288 | } |
||
289 | |||
290 | /** |
||
291 | * Update subscriptions. |
||
292 | * |
||
293 | * @param array $subscriptions Data of all subscriptions wanted to be |
||
294 | * updated (should include subscription => $id) |
||
295 | * |
||
296 | * @see https://docs.fastspring.com/integrating-with-fastspring/fastspring-api/subscriptions#id-/subscriptions-Updateexistingsubscriptioninstances |
||
297 | * |
||
298 | * @return object Response of fastspring |
||
299 | */ |
||
300 | 7 | public function updateSubscriptions($subscriptions) |
|
301 | { |
||
302 | 7 | return $this->apiRequest('POST', 'subscriptions', [], [], [ |
|
303 | 7 | 'subscriptions' => $subscriptions, |
|
304 | ]); |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Cancel subscription. |
||
309 | * |
||
310 | * @param string|int $subscriptionId ID of the subscription |
||
311 | * @param array $parameters Query Parameters for example to delete |
||
312 | * immediately pass ['billingPeriod' => 0] |
||
313 | * |
||
314 | * @return object Response of fastspring |
||
315 | */ |
||
316 | 5 | public function cancelSubscription($subscriptionId, $parameters = []) |
|
317 | { |
||
318 | 5 | return $this->apiRequest('DELETE', implode('/', ['subscriptions', $subscriptionId]), $parameters, [], []); |
|
319 | } |
||
320 | |||
321 | /** |
||
322 | * Uncancel subscription. |
||
323 | * |
||
324 | * @param string|int $subscriptionId ID of the subscription |
||
325 | * |
||
326 | * @return object Response of fastspring |
||
327 | */ |
||
328 | 3 | public function uncancelSubscription($subscriptionId) |
|
329 | { |
||
330 | 3 | return $this->updateSubscriptions([ |
|
331 | [ |
||
332 | 3 | 'subscription' => $subscriptionId, |
|
333 | 'deactivation' => null, |
||
334 | ], |
||
335 | ]); |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Get authenticated url of fastspring account management panel. |
||
340 | * |
||
341 | * @param string|int $accountId ID of the account |
||
342 | * |
||
343 | * @return object Response of fastspring |
||
344 | */ |
||
345 | 2 | public function getAccountManagementURI($accountId) |
|
346 | { |
||
347 | 2 | return $this->apiRequest('GET', implode('/', ['accounts', $accountId, 'authenticate']), [], [], []); |
|
348 | } |
||
349 | |||
350 | /** |
||
351 | * Swap subscription to another plan. |
||
352 | * |
||
353 | * @param string|int $subscriptionId ID of the subscription |
||
354 | * @param string $newPlan Name of the new plan |
||
355 | * @param bool $prorate Prorate parameter |
||
356 | * @param int $quantity Quantity of the product |
||
357 | * @param array $coupons Coupons wanted to be applied |
||
358 | * |
||
359 | * @return object Returns JSON object from the updateSubscriptions method. |
||
360 | */ |
||
361 | 3 | public function swapSubscription($subscriptionId, $newPlan, $prorate, $quantity = 1, $coupons = []) |
|
362 | { |
||
363 | 3 | return $this->updateSubscriptions([ |
|
364 | [ |
||
365 | 3 | 'subscription' => $subscriptionId, |
|
366 | 3 | 'product' => $newPlan, |
|
367 | 3 | 'quantity' => $quantity, |
|
368 | 3 | 'coupons' => $coupons, |
|
369 | 3 | 'prorate' => $prorate, |
|
370 | ], |
||
371 | ]); |
||
372 | } |
||
373 | } |
||
374 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.