1<?php
2
3namespace Stripe;
4
5/**
6 * Class Customer
7 *
8 * @property string $id
9 * @property string $object
10 * @property mixed $address
11 * @property int $balance
12 * @property string $created
13 * @property string $currency
14 * @property string $default_source
15 * @property bool $delinquent
16 * @property string $description
17 * @property Discount $discount
18 * @property string $email
19 * @property string $invoice_prefix
20 * @property mixed $invoice_settings
21 * @property bool $livemode
22 * @property StripeObject $metadata
23 * @property string $name
24 * @property string $phone
25 * @property string[] preferred_locales
26 * @property mixed $shipping
27 * @property Collection $sources
28 * @property Collection $subscriptions
29 * @property string $tax_exempt
30 * @property Collection $tax_ids
31 *
32 * @package Stripe
33 */
34class Customer extends ApiResource
35{
36    const OBJECT_NAME = "customer";
37
38    use ApiOperations\All;
39    use ApiOperations\Create;
40    use ApiOperations\Delete;
41    use ApiOperations\NestedResource;
42    use ApiOperations\Retrieve;
43    use ApiOperations\Update;
44
45    /**
46     * Possible string representations of the customer's type of tax exemption.
47     * @link https://stripe.com/docs/api/customers/object#customer_object-tax_exempt
48     */
49    const TAX_EXEMPT_NONE    = 'none';
50    const TAX_EXEMPT_EXEMPT  = 'exempt';
51    const TAX_EXEMPT_REVERSE = 'reverse';
52
53    public static function getSavedNestedResources()
54    {
55        static $savedNestedResources = null;
56        if ($savedNestedResources === null) {
57            $savedNestedResources = new Util\Set([
58                'source',
59            ]);
60        }
61        return $savedNestedResources;
62    }
63
64    const PATH_BALANCE_TRANSACTIONS = '/balance_transactions';
65    const PATH_SOURCES = '/sources';
66    const PATH_TAX_IDS = '/tax_ids';
67
68    /**
69     * @param array|null $params
70     *
71     * @return InvoiceItem The resulting invoice item.
72     */
73    public function addInvoiceItem($params = null)
74    {
75        $params = $params ?: [];
76        $params['customer'] = $this->id;
77        $ii = InvoiceItem::create($params, $this->_opts);
78        return $ii;
79    }
80
81    /**
82     * @param array|null $params
83     *
84     * @return array An array of the customer's Invoices.
85     */
86    public function invoices($params = null)
87    {
88        $params = $params ?: [];
89        $params['customer'] = $this->id;
90        $invoices = Invoice::all($params, $this->_opts);
91        return $invoices;
92    }
93
94    /**
95     * @param array|null $params
96     *
97     * @return array An array of the customer's InvoiceItems.
98     */
99    public function invoiceItems($params = null)
100    {
101        $params = $params ?: [];
102        $params['customer'] = $this->id;
103        $iis = InvoiceItem::all($params, $this->_opts);
104        return $iis;
105    }
106
107    /**
108     * @param array|null $params
109     *
110     * @return array An array of the customer's Charges.
111     */
112    public function charges($params = null)
113    {
114        $params = $params ?: [];
115        $params['customer'] = $this->id;
116        $charges = Charge::all($params, $this->_opts);
117        return $charges;
118    }
119
120    /**
121     * @param array|null $params
122     *
123     * @return Subscription The updated subscription.
124     */
125    public function updateSubscription($params = null)
126    {
127        $url = $this->instanceUrl() . '/subscription';
128        list($response, $opts) = $this->_request('post', $url, $params);
129        $this->refreshFrom(['subscription' => $response], $opts, true);
130        return $this->subscription;
131    }
132
133    /**
134     * @param array|null $params
135     *
136     * @return Subscription The cancelled subscription.
137     */
138    public function cancelSubscription($params = null)
139    {
140        $url = $this->instanceUrl() . '/subscription';
141        list($response, $opts) = $this->_request('delete', $url, $params);
142        $this->refreshFrom(['subscription' => $response], $opts, true);
143        return $this->subscription;
144    }
145
146    /**
147     * @return Customer The updated customer.
148     */
149    public function deleteDiscount()
150    {
151        $url = $this->instanceUrl() . '/discount';
152        list($response, $opts) = $this->_request('delete', $url);
153        $this->refreshFrom(['discount' => null], $opts, true);
154    }
155
156    /**
157     * @param string|null $id The ID of the customer on which to create the source.
158     * @param array|null $params
159     * @param array|string|null $opts
160     *
161     * @return ApiResource
162     */
163    public static function createSource($id, $params = null, $opts = null)
164    {
165        return self::_createNestedResource($id, static::PATH_SOURCES, $params, $opts);
166    }
167
168    /**
169     * @param string|null $id The ID of the customer to which the source belongs.
170     * @param string|null $sourceId The ID of the source to retrieve.
171     * @param array|null $params
172     * @param array|string|null $opts
173     *
174     * @return ApiResource
175     */
176    public static function retrieveSource($id, $sourceId, $params = null, $opts = null)
177    {
178        return self::_retrieveNestedResource($id, static::PATH_SOURCES, $sourceId, $params, $opts);
179    }
180
181    /**
182     * @param string|null $id The ID of the customer to which the source belongs.
183     * @param string|null $sourceId The ID of the source to update.
184     * @param array|null $params
185     * @param array|string|null $opts
186     *
187     * @return ApiResource
188     */
189    public static function updateSource($id, $sourceId, $params = null, $opts = null)
190    {
191        return self::_updateNestedResource($id, static::PATH_SOURCES, $sourceId, $params, $opts);
192    }
193
194    /**
195     * @param string|null $id The ID of the customer to which the source belongs.
196     * @param string|null $sourceId The ID of the source to delete.
197     * @param array|null $params
198     * @param array|string|null $opts
199     *
200     * @return ApiResource
201     */
202    public static function deleteSource($id, $sourceId, $params = null, $opts = null)
203    {
204        return self::_deleteNestedResource($id, static::PATH_SOURCES, $sourceId, $params, $opts);
205    }
206
207    /**
208     * @param string|null $id The ID of the customer on which to retrieve the sources.
209     * @param array|null $params
210     * @param array|string|null $opts
211     *
212     * @return Collection The list of sources.
213     */
214    public static function allSources($id, $params = null, $opts = null)
215    {
216        return self::_allNestedResources($id, static::PATH_SOURCES, $params, $opts);
217    }
218
219    /**
220     * @param string|null $id The ID of the customer on which to create the tax id.
221     * @param array|null $params
222     * @param array|string|null $opts
223     *
224     * @return ApiResource
225     */
226    public static function createTaxId($id, $params = null, $opts = null)
227    {
228        return self::_createNestedResource($id, static::PATH_TAX_IDS, $params, $opts);
229    }
230
231    /**
232     * @param string|null $id The ID of the customer to which the tax id belongs.
233     * @param string|null $taxIdId The ID of the tax id to retrieve.
234     * @param array|null $params
235     * @param array|string|null $opts
236     *
237     * @return ApiResource
238     */
239    public static function retrieveTaxId($id, $taxIdId, $params = null, $opts = null)
240    {
241        return self::_retrieveNestedResource($id, static::PATH_TAX_IDS, $taxIdId, $params, $opts);
242    }
243
244    /**
245     * @param string|null $id The ID of the customer to which the tax id belongs.
246     * @param string|null $taxIdId The ID of the tax id to delete.
247     * @param array|null $params
248     * @param array|string|null $opts
249     *
250     * @return ApiResource
251     */
252    public static function deleteTaxId($id, $taxIdId, $params = null, $opts = null)
253    {
254        return self::_deleteNestedResource($id, static::PATH_TAX_IDS, $taxIdId, $params, $opts);
255    }
256
257    /**
258     * @param string|null $id The ID of the customer on which to retrieve the tax ids.
259     * @param array|null $params
260     * @param array|string|null $opts
261     *
262     * @return Collection The list of tax ids.
263     */
264    public static function allTaxIds($id, $params = null, $opts = null)
265    {
266        return self::_allNestedResources($id, static::PATH_TAX_IDS, $params, $opts);
267    }
268
269    /**
270     * @param string|null $id The ID of the customer on which to create the balance transaction.
271     * @param array|null $params
272     * @param array|string|null $opts
273     *
274     * @return ApiResource
275     */
276    public static function createBalanceTransaction($id, $params = null, $opts = null)
277    {
278        return self::_createNestedResource($id, static::PATH_BALANCE_TRANSACTIONS, $params, $opts);
279    }
280
281    /**
282     * @param string|null $id The ID of the customer to which the balance transaction belongs.
283     * @param string|null $balanceTransactionId The ID of the balance transaction to retrieve.
284     * @param array|null $params
285     * @param array|string|null $opts
286     *
287     * @return ApiResource
288     */
289    public static function retrieveBalanceTransaction($id, $balanceTransactionId, $params = null, $opts = null)
290    {
291        return self::_retrieveNestedResource($id, static::PATH_BALANCE_TRANSACTIONS, $balanceTransactionId, $params, $opts);
292    }
293
294    /**
295     * @param string|null $id The ID of the customer on which to update the balance transaction.
296     * @param string|null $balanceTransactionId The ID of the balance transaction to update.
297     * @param array|null $params
298     * @param array|string|null $opts
299     *
300     *
301     * @return ApiResource
302     */
303    public static function updateBalanceTransaction($id, $balanceTransactionId, $params = null, $opts = null)
304    {
305        return self::_updateNestedResource($id, static::PATH_BALANCE_TRANSACTIONS, $balanceTransactionId, $params, $opts);
306    }
307
308    /**
309     * @param string|null $id The ID of the customer on which to retrieve the customer balance transactions.
310     * @param array|null $params
311     * @param array|string|null $opts
312     *
313     * @return Collection The list of customer balance transactions.
314     */
315    public static function allBalanceTransactions($id, $params = null, $opts = null)
316    {
317        return self::_allNestedResources($id, static::PATH_BALANCE_TRANSACTIONS, $params, $opts);
318    }
319}
320