Completed
Push — master ( 8bbda0...ede634 )
by ARCANEDEV
8s
created

Customer::charges()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php namespace Arcanedev\Stripe\Resources;
2
3
use Arcanedev\Stripe\Contracts\Resources\CustomerInterface;
4
use Arcanedev\Stripe\Exceptions\InvalidRequestException;
5
use Arcanedev\Stripe\StripeResource;
6
7
/**
8
 * Class     Customer
9
 *
10
 * @package  Arcanedev\Stripe\Resources
11
 * @author   ARCANEDEV <[email protected]>
12
 *
13
 * @link https://stripe.com/docs/api/php#customers
14
 *
15
 * @property  int                               id
16
 * @property  string                            object          // "customer"
17
 * @property  bool                              livemode
18
 * @property  \Arcanedev\Stripe\Collection      sources
19
 * @property  int                               created
20
 * @property  int                               account_balance
21
 * @property  string                            currency
22
 * @property  string                            default_card
23
 * @property  bool                              delinquent
24
 * @property  string                            description
25
 * @property  Discount|null                     discount
26
 * @property  string                            email
27
 * @property  \Arcanedev\Stripe\AttachedObject  metadata
28
 * @property  \Arcanedev\Stripe\Collection      subscriptions
29
 * @property  Subscription                      subscription    // It's for updateSubscription and cancelSubscription
30
 *
31
 * @todo:     Update the properties.
32
 */
33
class Customer extends StripeResource implements CustomerInterface
34
{
35
    /* ------------------------------------------------------------------------------------------------
36
     |  Constants
37
     | ------------------------------------------------------------------------------------------------
38
     */
39
    const END_POINT_SUBSCRIPTION = 'subscription';
40
41
    const END_POINT_DISCOUNT     = 'discount';
42
43
    /* ------------------------------------------------------------------------------------------------
44
     |  Properties
45
     | ------------------------------------------------------------------------------------------------
46
     */
47
    /**
48
     * Allow to check attributes while setting.
49
     *
50
     * @var bool
51
     */
52
    protected $checkUnsavedAttributes = true;
53
54
    /* ------------------------------------------------------------------------------------------------
55
     |  Getters & Setters
56
     | ------------------------------------------------------------------------------------------------
57
     */
58
    /**
59
     * Get Subscription URL.
60
     *
61
     * @throws InvalidRequestException
62
     *
63
     * @return string
64
     */
65
    private function getSubscriptionUrl()
66
    {
67
        return $this->instanceUrl() . '/' . self::END_POINT_SUBSCRIPTION;
68
    }
69
70
    /**
71
     * Get Discount URL.
72
     *
73
     * @throws InvalidRequestException
74
     *
75
     * @return string
76
     */
77
    private function getDiscountUrl()
78
    {
79
        return $this->instanceUrl() . '/' . self::END_POINT_DISCOUNT;
80
    }
81
82
    /* ------------------------------------------------------------------------------------------------
83
     |  CRUD Functions
84
     | ------------------------------------------------------------------------------------------------
85
     */
86
    /**
87
     * Retrieve a Customer.
88
     *
89
     * @link   https://stripe.com/docs/api/php#retrieve_customer
90
     *
91
     * @param  string             $id
92
     * @param  array|string|null  $options
93
     *
94
     * @return self
95
     */
96 5
    public static function retrieve($id, $options = null)
97
    {
98 5
        return self::scopedRetrieve($id, $options);
99
    }
100
101
    /**
102
     * List all Customers.
103
     *
104
     * @link   https://stripe.com/docs/api/php#list_customers
105
     *
106
     * @param  array|null         $params
107
     * @param  array|string|null  $options
108
     *
109
     * @return \Arcanedev\Stripe\Collection|array
110
     */
111 5
    public static function all($params = [], $options = null)
112
    {
113 5
        return self::scopedAll($params, $options);
114
    }
115
116
    /**
117
     * Create Customer.
118
     *
119
     * @link   https://stripe.com/docs/api/php#create_customer
120
     *
121
     * @param  array|null         $params
122
     * @param  array|string|null  $options
123
     *
124
     * @return self|array
125
     */
126 140
    public static function create($params = [], $options = null)
127
    {
128 140
        return self::scopedCreate($params, $options);
129
    }
130
131
    /**
132
     * Update/Save Customer.
133
     *
134
     * @link   https://stripe.com/docs/api/php#create_customer
135
     *
136
     * @param  array|string|null  $options
137
     *
138
     * @return self
139
     */
140
    public function save($options = null)
141
    {
142
        return self::scopedSave($options);
143
    }
144
145
    /**
146
     * Delete Customer.
147
     *
148
     * @link   https://stripe.com/docs/api/php#delete_customer
149
     *
150
     * @param  array|null         $params
151
     * @param  array|string|null  $options
152
     *
153
     * @return self
154
     */
155
    public function delete($params = [], $options = null)
156
    {
157
        return self::scopedDelete($params, $options);
158
    }
159
160
    /* ------------------------------------------------------------------------------------------------
161
     |  Relationships Functions
162
     | ------------------------------------------------------------------------------------------------
163
     */
164
    /**
165
     * Add an invoice item.
166
     *
167
     * @param  array $params
168
     *
169
     * @return InvoiceItem|array
170
     */
171
    public function addInvoiceItem($params = [])
172
    {
173
        $this->appCustomerParam($params);
174
175
        return InvoiceItem::create($params, $this->opts);
176
    }
177
178
    /**
179
     * Get all invoices.
180
     *
181
     * @param  array $params
182
     *
183
     * @return \Arcanedev\Stripe\Collection|array
184
     */
185
    public function invoices($params = [])
186
    {
187
        $this->appCustomerParam($params);
188
189
        return Invoice::all($params, $this->opts);
190
    }
191
192
    /**
193
     * Get all invoice items.
194
     *
195
     * @param  array  $params
196
     *
197
     * @return \Arcanedev\Stripe\Collection|array
198
     */
199
    public function invoiceItems($params = [])
200
    {
201
        $this->appCustomerParam($params);
202
203
        return InvoiceItem::all($params, $this->opts);
204
    }
205
206
    /**
207
     * Get all charges.
208
     *
209
     * @param  array  $params
210
     *
211
     * @return \Arcanedev\Stripe\Collection|array
212
     */
213
    public function charges($params = [])
214
    {
215
        $this->appCustomerParam($params);
216
217
        return Charge::all($params, $this->opts);
218
    }
219
220
    /**
221
     * Update a Subscription.
222
     *
223
     * @param  array|null  $params
224
     *
225
     * @return Subscription
226
     */
227
    public function updateSubscription($params = [])
228
    {
229
        list($response, $opts) = $this->request('post', $this->getSubscriptionUrl(), $params);
230
        $this->refreshFrom(['subscription' => $response], $opts, true);
231
232
        return $this->subscription;
233
    }
234
235
    /**
236
     * Cancel Subscription.
237
     *
238
     * @param  array|null  $params
239
     *
240
     * @return Subscription
241
     */
242
    public function cancelSubscription($params = [])
243
    {
244
        list($response, $opts) = $this->request('delete', $this->getSubscriptionUrl(), $params);
245
        $this->refreshFrom(['subscription' => $response], $opts, true);
246
247
        return $this->subscription;
248
    }
249
250
    /**
251
     * Delete Discount.
252
     *
253
     * @return Object
254
     */
255
    public function deleteDiscount()
256
    {
257
        list($response, $opts) = $this->request('delete', $this->getDiscountUrl());
258
259
        $this->refreshFrom(['discount' => null], $opts, true);
260
        unset($response);
261
262
        return $this;
263
    }
264
265
    /* ------------------------------------------------------------------------------------------------
266
     |  Other Functions
267
     | ------------------------------------------------------------------------------------------------
268
     */
269
    /**
270
     * Add Customer ID to parameters.
271
     *
272
     * @param  array  $params
273
     */
274
    private function appCustomerParam(&$params)
275
    {
276
        if (empty($params)) {
277
            $params = [];
278
        }
279
        $params['customer'] = $this->id;
280
    }
281
}
282