Issues (74)

src/Fastspring/ApiClient.php (36 issues)

1
<?php
2
3
namespace Bgultekin\CashierFastspring\Fastspring;
4
5
use GuzzleHttp\Client;
6
7
/**
8
 * ApiClient is a simple class for sending requests to FastSpring API.
9
 *
10
 * This class aims to handle some APIs of Fastspring if you think to develop
11
 * and add some other features please consider not doing it with one class.
12
 *
13
 * Note: This class does not cover whole FastSpring API, it covers just a couple of things
14
 * in FastSpring API like accounts and sessions.
15
 *
16
 * Example usage:
17
 * ```php
18
 * $fastspring = new ApiClient();
19
 * $accounts = $fastspring->getAccounts();
20
 * ```
21
 *
22
 * @author   Bilal Gultekin <[email protected]>
23
 *
24
 * @version  0.1
25
 *
26
 * @see      https://docs.fastspring.com/integrating-with-fastspring/fastspring-api
27
 */
28
class ApiClient
29
{
30
    /**
31
     * The Fastspring API Username.
32
     *
33
     * @var string
34
     */
35
    public $username;
36
37
    /**
38
     * The Fastspring API password.
39
     *
40
     * @var string
41
     */
42
    public $password;
43
44
    /**
45
     * The Fastspring API Base Url.
46
     *
47
     * @var string
48
     */
49
    public $apiBase = 'https://api.fastspring.com';
50
51
    /**
52
     * Global queries to apply every requests.
53
     *
54
     * @var array
55
     */
56
    public $globalQuery = [];
57
58
    /**
59
     * Guzzle client options.
60
     * Can be used to test class or process.
61
     *
62
     * @var array
63
     */
64
    public $clientOptions = [];
65
66
    /**
67
     * Create a new Fastspring API interface instance.
68
     *
69
     * @param string $username
70
     * @param string $password
71
     *
72
     * @return void
73
     */
74 17
    public function __construct($username = null, $password = null)
75
    {
76 17
        $this->username = $username
77 3
            ? $username
78 16
            : (getenv('FASTSPRING_USERNAME') ?: config('services.fastspring.username'));
79 17
        $this->password = $password
80 3
            ? $password
81 16
            : (getenv('FASTSPRING_PASSWORD') ?: config('services.fastspring.password'));
82 17
    }
83
84
    /**
85
     * Send a request to Fastspring API with given parameters.
86
     *
87
     * @param string $method         Method of HTTP request like PUT, GET, POST etc.
88
     * @param string $path           Path of API
89
     * @param string $query          Query parameters
90
     * @param string $formParameters Form parameters
91
     * @param string $jsonPayload    Json payload
92
     *
93
     * @return \GuzzleHttp\Psr7\Response
94
     */
95 28
    public function apiRequest($method, $path, $query = [], $formParameters = [], $jsonPayload = [])
96
    {
97
        // prepare guzzle options
98 28
        $clientOptions = $this->clientOptions ?: ['base_uri' => $this->apiBase];
99
100
        // create guzzle instance
101 28
        $client = new Client($clientOptions);
102
103
        // delete first slash character
104 28
        $path = ltrim($path, '/');
105
106
        // prepare options
107
        $options = [
108 28
            'auth'  => [$this->username, $this->password],
109 28
            'query' => $this->globalQuery,
110
        ];
111
112
        // set parameters
113 28
        $options['query'] = array_merge($options['query'], $query);
0 ignored issues
show
It seems like $query can also be of type string; however, parameter $array2 of array_merge() does only seem to accept null|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
        $options['query'] = array_merge($options['query'], /** @scrutinizer ignore-type */ $query);
Loading history...
114
115 28
        if ($formParameters) {
116 1
            $options['form_params'] = $formParameters;
117
        }
118
119 28
        if ($jsonPayload) {
120 13
            $options['json'] = $jsonPayload;
121
        }
122
123
        // send request and get response
124 28
        $response = $client->request($method, $path, $options);
125
126
        // convert it to object
127 28
        return $this->handleResponse($response);
128
    }
129
130
    /**
131
     * Set guzzle client options.
132
     *
133
     * @param array $options Guzzle client options
134
     *
135
     * @see http://docs.guzzlephp.org/en/latest/quickstart.html Quickstart
136
     * @see http://docs.guzzlephp.org/en/latest/testing.html Testing
137
     *
138
     * @return void
139
     */
140 33
    public function setClientOptions($options)
141
    {
142 33
        return $this->clientOptions = $options;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->clientOptions = $options returns the type array which is incompatible with the documented return type void.
Loading history...
143
    }
144
145
    /**
146
     * Set global query items.
147
     *
148
     * @param array $query Queries like ['mode' => 'test']
149
     *
150
     * @return void
151
     */
152 1
    public function setGlobalQuery($query)
153
    {
154 1
        return $this->globalQuery = $query;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->globalQuery = $query returns the type array which is incompatible with the documented return type void.
Loading history...
155
    }
156
157
    /**
158
     * Handle JSON response and convert it to array.
159
     *
160
     * @param \GuzzleHttp\Psr7\Response $response Guzzle response
161
     *
162
     * @return object
163
     */
164 28
    protected function handleResponse($response)
165
    {
166 28
        $message = $response->getBody()->getContents();
167
168
        // json decode
169
        // we assume fastspring sends always json
170 28
        return json_decode($message);
171
    }
172
173
    // API methods
174
175
    /**
176
     * Create account.
177
     *
178
     * @param array $account Account details
179
     *
180
     * @see https://docs.fastspring.com/integrating-with-fastspring/fastspring-api/accounts  Account details
181
     *
182
     * @return object Response of fastspring
183
     */
184 4
    public function createAccount($account)
185
    {
186 4
        return $this->apiRequest('POST', 'accounts', [], [], $account);
0 ignored issues
show
array() of type array is incompatible with the type string expected by parameter $query of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

186
        return $this->apiRequest('POST', 'accounts', /** @scrutinizer ignore-type */ [], [], $account);
Loading history...
$account of type array is incompatible with the type string expected by parameter $jsonPayload of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

186
        return $this->apiRequest('POST', 'accounts', [], [], /** @scrutinizer ignore-type */ $account);
Loading history...
array() of type array is incompatible with the type string expected by parameter $formParameters of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

186
        return $this->apiRequest('POST', 'accounts', [], /** @scrutinizer ignore-type */ [], $account);
Loading history...
187
    }
188
189
    /**
190
     * Update account.
191
     *
192
     * @param string $fastspringId Fastspring ID of related account
193
     * @param array  $account      Account details
194
     *
195
     * @see https://docs.fastspring.com/integrating-with-fastspring/fastspring-api/accounts  Account details
196
     *
197
     * @return object Response of fastspring
198
     */
199 2
    public function updateAccount($fastspringId, $account)
200
    {
201 2
        return $this->apiRequest('POST', implode('/', ['accounts', $fastspringId]), [], [], $account);
0 ignored issues
show
array() of type array is incompatible with the type string expected by parameter $formParameters of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

201
        return $this->apiRequest('POST', implode('/', ['accounts', $fastspringId]), [], /** @scrutinizer ignore-type */ [], $account);
Loading history...
$account of type array is incompatible with the type string expected by parameter $jsonPayload of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

201
        return $this->apiRequest('POST', implode('/', ['accounts', $fastspringId]), [], [], /** @scrutinizer ignore-type */ $account);
Loading history...
array() of type array is incompatible with the type string expected by parameter $query of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

201
        return $this->apiRequest('POST', implode('/', ['accounts', $fastspringId]), /** @scrutinizer ignore-type */ [], [], $account);
Loading history...
202
    }
203
204
    /**
205
     * Get account list.
206
     *
207
     * @param array $parameters Query parameters
208
     *
209
     * @return object Response of fastspring
210
     */
211 3
    public function getAccounts($parameters = [])
212
    {
213 3
        return $this->apiRequest('GET', 'accounts', $parameters, [], []);
0 ignored issues
show
$parameters of type array is incompatible with the type string expected by parameter $query of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

213
        return $this->apiRequest('GET', 'accounts', /** @scrutinizer ignore-type */ $parameters, [], []);
Loading history...
array() of type array is incompatible with the type string expected by parameter $formParameters of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

213
        return $this->apiRequest('GET', 'accounts', $parameters, /** @scrutinizer ignore-type */ [], []);
Loading history...
array() of type array is incompatible with the type string expected by parameter $jsonPayload of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

213
        return $this->apiRequest('GET', 'accounts', $parameters, [], /** @scrutinizer ignore-type */ []);
Loading history...
214
    }
215
216
    /**
217
     * Get the account with the given id.
218
     *
219
     * @param string|int $accountId  ID of the account
220
     * @param array      $parameters Query Parameters
221
     *
222
     * @return object Response of fastspring
223
     */
224 2
    public function getAccount($accountId, $parameters = [])
225
    {
226 2
        return $this->apiRequest('GET', implode('/', ['accounts', $accountId]), $parameters, [], []);
0 ignored issues
show
$parameters of type array is incompatible with the type string expected by parameter $query of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

226
        return $this->apiRequest('GET', implode('/', ['accounts', $accountId]), /** @scrutinizer ignore-type */ $parameters, [], []);
Loading history...
array() of type array is incompatible with the type string expected by parameter $formParameters of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

226
        return $this->apiRequest('GET', implode('/', ['accounts', $accountId]), $parameters, /** @scrutinizer ignore-type */ [], []);
Loading history...
array() of type array is incompatible with the type string expected by parameter $jsonPayload of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

226
        return $this->apiRequest('GET', implode('/', ['accounts', $accountId]), $parameters, [], /** @scrutinizer ignore-type */ []);
Loading history...
227
    }
228
229
    /**
230
     * Create session.
231
     *
232
     * @param array $session Sessions details
233
     *
234
     * @see https://docs.fastspring.com/integrating-with-fastspring/fastspring-api/sessions  Session details
235
     *
236
     * @return object Response of fastspring
237
     */
238 5
    public function createSession($session)
239
    {
240 5
        return $this->apiRequest('POST', 'sessions', [], [], $session);
0 ignored issues
show
$session of type array is incompatible with the type string expected by parameter $jsonPayload of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

240
        return $this->apiRequest('POST', 'sessions', [], [], /** @scrutinizer ignore-type */ $session);
Loading history...
array() of type array is incompatible with the type string expected by parameter $formParameters of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

240
        return $this->apiRequest('POST', 'sessions', [], /** @scrutinizer ignore-type */ [], $session);
Loading history...
array() of type array is incompatible with the type string expected by parameter $query of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

240
        return $this->apiRequest('POST', 'sessions', /** @scrutinizer ignore-type */ [], [], $session);
Loading history...
241
    }
242
243
    /**
244
     * Get orders.
245
     *
246
     * @param array $parameters Query parameters
247
     *
248
     * @return object Response of fastspring
249
     */
250 1
    public function getOrders($parameters = [])
251
    {
252 1
        return $this->apiRequest('GET', 'accounts', $parameters, [], []);
0 ignored issues
show
array() of type array is incompatible with the type string expected by parameter $jsonPayload of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

252
        return $this->apiRequest('GET', 'accounts', $parameters, [], /** @scrutinizer ignore-type */ []);
Loading history...
array() of type array is incompatible with the type string expected by parameter $formParameters of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

252
        return $this->apiRequest('GET', 'accounts', $parameters, /** @scrutinizer ignore-type */ [], []);
Loading history...
$parameters of type array is incompatible with the type string expected by parameter $query of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

252
        return $this->apiRequest('GET', 'accounts', /** @scrutinizer ignore-type */ $parameters, [], []);
Loading history...
253
    }
254
255
    /**
256
     * Get subscriptions.
257
     *
258
     * @param array $subscriptionIds Fastspring ids of subscriptions
259
     *
260
     * @see https://docs.fastspring.com/integrating-with-fastspring/fastspring-api/subscriptions#id-/subscriptions-Getoneormoresubscriptioninstances
261
     *
262
     * @return object Response of fastspring
263
     */
264 1
    public function getSubscriptions($subscriptionIds)
265
    {
266 1
        return $this->apiRequest('GET', implode(
267 1
            '/',
268 1
            ['subscriptions', implode(',', $subscriptionIds)]
269 1
        ), [], [], []);
0 ignored issues
show
array() of type array is incompatible with the type string expected by parameter $jsonPayload of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

269
        ), [], [], /** @scrutinizer ignore-type */ []);
Loading history...
array() of type array is incompatible with the type string expected by parameter $query of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

269
        ), /** @scrutinizer ignore-type */ [], [], []);
Loading history...
array() of type array is incompatible with the type string expected by parameter $formParameters of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

269
        ), [], /** @scrutinizer ignore-type */ [], []);
Loading history...
270
    }
271
272
    /**
273
     * Get subscription, returns one instance.
274
     *
275
     * @param array $subscriptionId Fastspring id of subscriptions
276
     *
277
     * @see https://docs.fastspring.com/integrating-with-fastspring/fastspring-api/subscriptions#id-/subscriptions-Getoneormoresubscriptioninstances
278
     *
279
     * @return object Response of fastspring
280
     */
281 3
    public function getSubscriptionsEntries($subscriptionIds)
282
    {
283 3
        return $this->apiRequest('GET', implode(
284 3
            '/',
285 3
            ['subscriptions', implode(',', $subscriptionIds), 'entries']
286 3
        ), [], [], []);
0 ignored issues
show
array() of type array is incompatible with the type string expected by parameter $formParameters of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

286
        ), [], /** @scrutinizer ignore-type */ [], []);
Loading history...
array() of type array is incompatible with the type string expected by parameter $query of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

286
        ), /** @scrutinizer ignore-type */ [], [], []);
Loading history...
array() of type array is incompatible with the type string expected by parameter $jsonPayload of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

286
        ), [], [], /** @scrutinizer ignore-type */ []);
Loading history...
287
    }
288
289
    /**
290
     * Update subscriptions.
291
     *
292
     * @param array $subscriptions Data of all subscriptions wanted to be updated (should include subscription => $id)
293
     *
294
     * @see https://docs.fastspring.com/integrating-with-fastspring/fastspring-api/subscriptions#id-/subscriptions-Updateexistingsubscriptioninstances
295
     *
296
     * @return object Response of fastspring
297
     */
298 6
    public function updateSubscriptions($subscriptions)
299
    {
300 6
        return $this->apiRequest('POST', 'subscriptions', [], [], [
0 ignored issues
show
array() of type array is incompatible with the type string expected by parameter $formParameters of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

300
        return $this->apiRequest('POST', 'subscriptions', [], /** @scrutinizer ignore-type */ [], [
Loading history...
array() of type array is incompatible with the type string expected by parameter $query of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

300
        return $this->apiRequest('POST', 'subscriptions', /** @scrutinizer ignore-type */ [], [], [
Loading history...
array('subscriptions' => $subscriptions) of type array<string,array> is incompatible with the type string expected by parameter $jsonPayload of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

300
        return $this->apiRequest('POST', 'subscriptions', [], [], /** @scrutinizer ignore-type */ [
Loading history...
301 6
            'subscriptions' => $subscriptions,
302
        ]);
303
    }
304
305
    /**
306
     * Cancel subscription.
307
     *
308
     * @param string|int $subscriptionId ID of the subscription
309
     * @param array      $parameters     Query Parameters for example to delete immediately pass ['billingPeriod' => 0]
310
     *
311
     * @return object Response of fastspring
312
     */
313 3
    public function cancelSubscription($subscriptionId, $parameters = [])
314
    {
315 3
        return $this->apiRequest('DELETE', implode('/', ['subscriptions', $subscriptionId]), $parameters, [], []);
0 ignored issues
show
array() of type array is incompatible with the type string expected by parameter $jsonPayload of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

315
        return $this->apiRequest('DELETE', implode('/', ['subscriptions', $subscriptionId]), $parameters, [], /** @scrutinizer ignore-type */ []);
Loading history...
$parameters of type array is incompatible with the type string expected by parameter $query of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

315
        return $this->apiRequest('DELETE', implode('/', ['subscriptions', $subscriptionId]), /** @scrutinizer ignore-type */ $parameters, [], []);
Loading history...
array() of type array is incompatible with the type string expected by parameter $formParameters of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

315
        return $this->apiRequest('DELETE', implode('/', ['subscriptions', $subscriptionId]), $parameters, /** @scrutinizer ignore-type */ [], []);
Loading history...
316
    }
317
318
    /**
319
     * Uncancel subscription.
320
     *
321
     * @param string|int $subscriptionId ID of the subscription
322
     *
323
     * @return object Response of fastspring
324
     */
325 2
    public function uncancelSubscription($subscriptionId)
326
    {
327 2
        return $this->updateSubscriptions([
328
            [
329 2
                'subscription' => $subscriptionId,
330
                'deactivation' => null,
331
            ],
332
        ]);
333
    }
334
335
    /**
336
     * Get authenticated url of fastspring account management panel.
337
     *
338
     * @param string|int $accountId ID of the account
339
     *
340
     * @return object Response of fastspring
341
     */
342 2
    public function getAccountManagementURI($accountId)
343
    {
344 2
        return $this->apiRequest('GET', implode('/', ['accounts', $accountId, 'authenticate']), [], [], []);
0 ignored issues
show
array() of type array is incompatible with the type string expected by parameter $jsonPayload of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

344
        return $this->apiRequest('GET', implode('/', ['accounts', $accountId, 'authenticate']), [], [], /** @scrutinizer ignore-type */ []);
Loading history...
array() of type array is incompatible with the type string expected by parameter $formParameters of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

344
        return $this->apiRequest('GET', implode('/', ['accounts', $accountId, 'authenticate']), [], /** @scrutinizer ignore-type */ [], []);
Loading history...
array() of type array is incompatible with the type string expected by parameter $query of Bgultekin\CashierFastspr...ApiClient::apiRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

344
        return $this->apiRequest('GET', implode('/', ['accounts', $accountId, 'authenticate']), /** @scrutinizer ignore-type */ [], [], []);
Loading history...
345
    }
346
347
    /**
348
     * Swap subscription to another plan.
349
     *
350
     * @param string|int $subscriptionId ID of the subscription
351
     * @param string     $newPlan        Name of the new plan
352
     * @param bool       $prorate        Prorate parameter
353
     * @param int        $quantity       Quantity of the product
354
     * @param array      $coupons        Coupons wanted to be applied
355
     *
356
     * @return object Response of fastspring
357
     */
358 3
    public function swapSubscription($subscriptionId, $newPlan, $prorate, $quantity = 1, $coupons = [])
359
    {
360 3
        return $this->updateSubscriptions([
361
            [
362 3
                'subscription' => $subscriptionId,
363 3
                'product'      => $newPlan,
364 3
                'quantity'     => $quantity,
365 3
                'coupons'      => $coupons,
366 3
                'prorate'      => $prorate,
367
            ],
368
        ]);
369
    }
370
}
371