Customer::invoices()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php namespace Arcanedev\Stripe\Resources;
2
3
use Arcanedev\Stripe\Contracts\Resources\Customer as CustomerContract;
4
use Arcanedev\Stripe\StripeResource;
5
6
/**
7
 * Class     Customer
8
 *
9
 * @package  Arcanedev\Stripe\Resources
10
 * @author   ARCANEDEV <[email protected]>
11
 * @link     https://stripe.com/docs/api/php#customer_object
12
 *
13
 * @property  string                                         id
14
 * @property  string                                         object           // 'customer'
15
 * @property  int                                            account_balance
16
 * @property  int                                            created          // timestamp
17
 * @property  string                                         currency
18
 * @property  string                                         default_source
19
 * @property  bool                                           delinquent
20
 * @property  string                                         description
21
 * @property  \Arcanedev\Stripe\Resources\Discount|null      discount
22
 * @property  string                                         email
23
 * @property  bool                                           livemode
24
 * @property  \Arcanedev\Stripe\AttachedObject               metadata
25
 * @property  string                                         shipping
26
 * @property  \Arcanedev\Stripe\Collection                   sources
27
 * @property  \Arcanedev\Stripe\Collection                   subscriptions
28
 * @property  \Arcanedev\Stripe\Resources\Subscription|null  subscription    // It's for updateSubscription and cancelSubscription
29
 */
30
class Customer extends StripeResource implements CustomerContract
31
{
32
    /* -----------------------------------------------------------------
33
     |  Constants
34
     | -----------------------------------------------------------------
35
     */
36
37
    const PATH_SOURCES = '/sources';
38
39
    /* -----------------------------------------------------------------
40
     |  Properties
41
     | -----------------------------------------------------------------
42
     */
43
44
    /**
45
     * Allow to check attributes while setting.
46
     *
47
     * @var bool
48
     */
49
    protected $checkUnsavedAttributes = true;
50
51
    /* -----------------------------------------------------------------
52
     |  Getters & Setters
53
     | -----------------------------------------------------------------
54
     */
55
56
    /**
57
     * Get Subscription URL.
58
     *
59
     * @throws \Arcanedev\Stripe\Exceptions\InvalidRequestException
60
     *
61
     * @return string
62
     */
63 4
    private function getSubscriptionUrl()
64
    {
65 4
        return $this->instanceUrl().'/subscription';
66
    }
67
68
    /**
69
     * Get Discount URL.
70
     *
71
     * @throws \Arcanedev\Stripe\Exceptions\InvalidRequestException
72
     *
73
     * @return string
74
     */
75 6
    private function getDiscountUrl()
76
    {
77 6
        return $this->instanceUrl().'/discount';
78
    }
79
80
    /* -----------------------------------------------------------------
81
     |  Main Methods
82
     | -----------------------------------------------------------------
83
     */
84
85
    /**
86
     * List all Customers.
87
     * @link   https://stripe.com/docs/api/php#list_customers
88
     *
89
     * @param  array|null         $params
90
     * @param  array|string|null  $options
91
     *
92
     * @return \Arcanedev\Stripe\Collection|array
93
     */
94 2
    public static function all($params = [], $options = null)
95
    {
96 2
        return self::scopedAll($params, $options);
97
    }
98
99
    /**
100
     * Retrieve a Customer.
101
     * @link   https://stripe.com/docs/api/php#retrieve_customer
102
     *
103
     * @param  string             $id
104
     * @param  array|string|null  $options
105
     *
106
     * @return self
107
     */
108 32
    public static function retrieve($id, $options = null)
109
    {
110 32
        return self::scopedRetrieve($id, $options);
111
    }
112
113
    /**
114
     * Create Customer.
115
     * @link   https://stripe.com/docs/api/php#create_customer
116
     *
117
     * @param  array|null         $params
118
     * @param  array|string|null  $options
119
     *
120
     * @return self|array
121
     */
122 96
    public static function create($params = [], $options = null)
123
    {
124 96
        return self::scopedCreate($params, $options);
125
    }
126
127
    /**
128
     * Update a Customer.
129
     * @link   https://stripe.com/docs/api/php#create_customer
130
     *
131
     * @param  string             $id
132
     * @param  array|null         $params
133
     * @param  array|string|null  $options
134
     *
135
     * @return self
136
     */
137 2
    public static function update($id, $params = [], $options = null)
138
    {
139 2
        return self::scopedUpdate($id, $params, $options);
140
    }
141
142
    /**
143
     * Update/Save a Customer.
144
     * @link   https://stripe.com/docs/api/php#create_customer
145
     *
146
     * @param  array|string|null  $options
147
     *
148
     * @return self
149
     */
150 24
    public function save($options = null)
151
    {
152 24
        return self::scopedSave($options);
153
    }
154
155
    /**
156
     * Delete Customer.
157
     * @link   https://stripe.com/docs/api/php#delete_customer
158
     *
159
     * @param  array|null         $params
160
     * @param  array|string|null  $options
161
     *
162
     * @return self
163
     */
164 2
    public function delete($params = [], $options = null)
165
    {
166 2
        return self::scopedDelete($params, $options);
167
    }
168
169
    /* -----------------------------------------------------------------
170
     |  Relationships Methods
171
     | -----------------------------------------------------------------
172
     */
173
174
    /**
175
     * Add an invoice item.
176
     *
177
     * @param  array  $params
178
     *
179
     * @return \Arcanedev\Stripe\Resources\InvoiceItem|array
180
     */
181 4
    public function addInvoiceItem($params = [])
182
    {
183 4
        $this->appCustomerParam($params);
184
185 4
        return InvoiceItem::create($params, $this->opts);
186
    }
187
188
    /**
189
     * Get all invoices.
190
     *
191
     * @param  array  $params
192
     *
193
     * @return \Arcanedev\Stripe\Collection|array
194
     */
195 2
    public function invoices($params = [])
196
    {
197 2
        $this->appCustomerParam($params);
198
199 2
        return Invoice::all($params, $this->opts);
200
    }
201
202
    /**
203
     * Get all invoice items.
204
     *
205
     * @param  array  $params
206
     *
207
     * @return \Arcanedev\Stripe\Collection|array
208
     */
209 2
    public function invoiceItems($params = [])
210
    {
211 2
        $this->appCustomerParam($params);
212
213 2
        return InvoiceItem::all($params, $this->opts);
214
    }
215
216
    /**
217
     * Get all charges.
218
     *
219
     * @param  array  $params
220
     *
221
     * @return \Arcanedev\Stripe\Collection|array
222
     */
223 2
    public function charges($params = [])
224
    {
225 2
        $this->appCustomerParam($params);
226
227 2
        return Charge::all($params, $this->opts);
228
    }
229
230
    /**
231
     * Update a Subscription.
232
     *
233
     * @param  array|null  $params
234
     *
235
     * @return \Arcanedev\Stripe\Resources\Subscription
236
     */
237 2
    public function updateSubscription($params = [])
238
    {
239 2
        list($response, $opts) = $this->request('post', $this->getSubscriptionUrl(), $params);
240 2
        $this->refreshFrom(['subscription' => $response], $opts, true);
241
242 2
        return $this->subscription;
243
    }
244
245
    /**
246
     * Cancel Subscription.
247
     *
248
     * @param  array|null  $params
249
     *
250
     * @return \Arcanedev\Stripe\Resources\Subscription
251
     */
252 2
    public function cancelSubscription($params = [])
253
    {
254 2
        list($response, $opts) = $this->request('delete', $this->getSubscriptionUrl(), $params);
255 2
        $this->refreshFrom(['subscription' => $response], $opts, true);
256
257 2
        return $this->subscription;
258
    }
259
260
    /**
261
     * Delete Discount.
262
     *
263
     * @return \Arcanedev\Stripe\StripeObject
264
     */
265 6
    public function deleteDiscount()
266
    {
267 6
        list($response, $opts) = $this->request('delete', $this->getDiscountUrl());
268 6
        $this->refreshFrom(['discount' => null], $opts, true);
269
270 6
        unset($response);
271
272 6
        return $this;
273
    }
274
275
    /**
276
     * Create a source.
277
     *
278
     * @param  string             $id
279
     * @param  array|null         $params
280
     * @param  array|string|null  $options
281
     *
282
     * @return \Arcanedev\Stripe\Bases\ExternalAccount
283
     */
284 2
    public static function createSource($id, $params = null, $options = null)
285
    {
286 2
        return self::createNestedResource($id, static::PATH_SOURCES, $params, $options);
287
    }
288
289
    /**
290
     * Retrieve a source.
291
     *
292
     * @param  string             $id
293
     * @param  string             $sourceId
294
     * @param  array|null         $params
295
     * @param  array|string|null  $options
296
     *
297
     * @return \Arcanedev\Stripe\Bases\ExternalAccount
298
     */
299 2
    public static function retrieveSource($id, $sourceId, $params = null, $options = null)
300
    {
301 2
        return self::retrieveNestedResource($id, static::PATH_SOURCES, $sourceId, $params, $options);
302
    }
303
304
    /**
305
     * Update a source.
306
     *
307
     * @param  string             $id
308
     * @param  string             $sourceId
309
     * @param  array|null         $params
310
     * @param  array|string|null  $options
311
     *
312
     * @return \Arcanedev\Stripe\Bases\ExternalAccount
313
     */
314 2
    public static function updateSource($id, $sourceId, $params = null, $options = null)
315
    {
316 2
        return self::updateNestedResource($id, static::PATH_SOURCES, $sourceId, $params, $options);
317
    }
318
319
    /**
320
     * Delete a source.
321
     *
322
     * @param  string             $id
323
     * @param  string             $sourceId
324
     * @param  array|null         $params
325
     * @param  array|string|null  $options
326
     *
327
     * @return \Arcanedev\Stripe\Bases\ExternalAccount
328
     */
329 2
    public static function deleteSource($id, $sourceId, $params = null, $options = null)
330
    {
331 2
        return self::deleteNestedResource($id, static::PATH_SOURCES, $sourceId, $params, $options);
332
    }
333
334
    /**
335
     * List all the sources.
336
     *
337
     * @param  string             $id
338
     * @param  array|null         $params
339
     * @param  array|string|null  $options
340
     *
341
     * @return \Arcanedev\Stripe\Collection
342
     */
343 2
    public static function allSources($id, $params = null, $options = null)
344
    {
345 2
        return self::allNestedResources($id, static::PATH_SOURCES, $params, $options);
346
    }
347
348
    /* -----------------------------------------------------------------
349
     |  Other Methods
350
     | -----------------------------------------------------------------
351
     */
352
353
    /**
354
     * Add Customer ID to parameters.
355
     *
356
     * @param  array  $params
357
     */
358 8
    private function appCustomerParam(&$params)
359
    {
360 8
        if (empty($params)) $params = [];
361
362 8
        $params['customer'] = $this->id;
363 8
    }
364
}
365