Issues (19)

src/Models/Card.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Greenlyst\BaseCommerce\Models;
6
7
use function ArrayHelpers\array_get;
8
use function ArrayHelpers\array_has;
9
use Carbon\Carbon;
10
use Greenlyst\BaseCommerce\ClientException;
11
use Greenlyst\BaseCommerce\LogicException;
12
use Greenlyst\BaseCommerce\Traits\HasClient;
13
use Greenlyst\BaseCommerce\Traits\HasErrorMessages;
14
15
class Card
16
{
17
    use HasClient, HasErrorMessages;
18
19
    private $name;
20
    private $cardNumber;
21
    private $cardExpirationMonth;
22
    private $cardExpirationYear;
23
    private $token;
24
    private $status;
25
    private $billingAddress;
26
    private $creationDate;
27
    private $alias;
28
    private $expirationDate;
0 ignored issues
show
The private property $expirationDate is not used, and could be removed.
Loading history...
29
    private $amount;
30
    private $recurring;
31
32
    const CUSTOM_FIELD_PREFIX_BANK_CARD_TRANSACTION = 'bank_card_transaction';
33
    const CUSTOM_FIELD_PREFIX_RECURRING_TRANSACTION = 'recurring_transaction';
34
35
    const STATUS_DELETED = 'DELETED';
36
    const STATUS_FAILED = 'FAILED';
37
    const STATUS_ACTIVE = 'ACTIVE';
38
39
    const URI_ADD_BANK_CARD = '/pcms/?f=API_addBankCardV4';
40
    const URI_UPDATE_BANK_CARD = '/pcms/?f=API_updateBankCardV4';
41
    const URI_DELETE_BANK_CARD = '/pcms/?f=API_deleteBankCardV4';
42
43
    /**
44
     * @return mixed
45
     */
46
    public function getName()
47
    {
48
        return $this->name;
49
    }
50
51
    /**
52
     * @param mixed $name
53
     */
54
    public function setName($name): void
55
    {
56
        $this->name = $name;
57
    }
58
59
    /**
60
     * @return mixed
61
     */
62
    public function getCardNumber()
63
    {
64
        return $this->cardNumber;
65
    }
66
67
    /**
68
     * @param mixed $cardNumber
69
     */
70
    public function setCardNumber($cardNumber): void
71
    {
72
        $this->cardNumber = $cardNumber;
73
    }
74
75
    /**
76
     * @return mixed
77
     */
78
    public function getCardExpirationMonth()
79
    {
80
        return $this->cardExpirationMonth;
81
    }
82
83
    /**
84
     * @param mixed $cardExpirationMonth
85
     */
86
    public function setCardExpirationMonth($cardExpirationMonth): void
87
    {
88
        $this->cardExpirationMonth = $cardExpirationMonth;
89
    }
90
91
    /**
92
     * @return mixed
93
     */
94
    public function getCardExpirationYear()
95
    {
96
        return $this->cardExpirationYear;
97
    }
98
99
    /**
100
     * @param mixed $cardExpirationYear
101
     */
102
    public function setCardExpirationYear($cardExpirationYear): void
103
    {
104
        $this->cardExpirationYear = $cardExpirationYear;
105
    }
106
107
    /**
108
     * @return mixed
109
     */
110
    public function getToken()
111
    {
112
        return $this->token;
113
    }
114
115
    /**
116
     * @param mixed $token
117
     */
118
    public function setToken($token): void
119
    {
120
        $this->token = $token;
121
    }
122
123
    /**
124
     * @return mixed
125
     */
126
    public function getStatus()
127
    {
128
        return $this->status;
129
    }
130
131
    /**
132
     * @param mixed $status
133
     */
134
    public function setStatus($status): void
135
    {
136
        $this->status = $status;
137
    }
138
139
    /**
140
     * @return Address
141
     */
142
    public function getBillingAddress()
143
    {
144
        return $this->billingAddress;
145
    }
146
147
    /**
148
     * @param Address $billingAddress
149
     */
150
    public function setBillingAddress(Address $billingAddress): void
151
    {
152
        $this->billingAddress = $billingAddress;
153
    }
154
155
    /**
156
     * @return mixed
157
     */
158
    public function getCreationDate()
159
    {
160
        return $this->creationDate;
161
    }
162
163
    /**
164
     * @param mixed $creationDate
165
     */
166
    public function setCreationDate($creationDate): void
167
    {
168
        $this->creationDate = $creationDate;
169
    }
170
171
    /**
172
     * @return mixed
173
     */
174
    public function getAlias()
175
    {
176
        return $this->alias;
177
    }
178
179
    /**
180
     * @param mixed $alias
181
     */
182
    public function setAlias($alias): void
183
    {
184
        $this->alias = $alias;
185
    }
186
187
    /**
188
     * @return mixed
189
     */
190
    public function getAmount()
191
    {
192
        return $this->amount;
193
    }
194
195
    /**
196
     * @param mixed $amount
197
     */
198
    public function setAmount($amount): void
199
    {
200
        $this->amount = $amount;
201
    }
202
203
    /**
204
     * @return Recurring
205
     */
206
    public function getRecurring()
207
    {
208
        return $this->recurring;
209
    }
210
211
    /**
212
     * @param Recurring $recurring
213
     */
214
    public function setRecurring($recurring): void
215
    {
216
        $this->recurring = $recurring;
217
    }
218
219
    /**
220
     * @throws ClientException
221
     * @throws LogicException
222
     *
223
     * @return Card
224
     */
225
    public function add(): self
226
    {
227
        validate_array($this->toCreateCardArray(), [
228
                'bank_card_name', 'bank_card_number', 'bank_card_expiration_month', 'bank_card_expiration_year',
229
            ]
230
        );
231
232
        $response = $this->client->postRequest(self::URI_ADD_BANK_CARD, json_encode($this->toCreateCardArray()));
233
234
        $instance = $this->fromCardArray($response['bank_card']);
235
236
        $this->handleMessages($response);
237
238
        return $instance;
239
    }
240
241
    /**
242
     * @throws ClientException
243
     * @throws LogicException
244
     *
245
     * @return $this
246
     */
247
    public function update(): self
248
    {
249
        validate_array($this->toCreateCardArray(), [
250
                'bank_card_token', 'bank_card_expiration_month', 'bank_card_expiration_year',
251
            ]
252
        );
253
254
        $response = $this->client->postRequest(self::URI_UPDATE_BANK_CARD, json_encode($this->toUpdateCardArray()));
255
256
        $instance = $this->fromCardArray($response['bank_card']);
257
258
        $instance->handleMessages($response);
259
260
        return $instance;
261
    }
262
263
    /**
264
     * @throws ClientException
265
     * @throws LogicException
266
     *
267
     * @return Card
268
     */
269
    public function delete(): self
270
    {
271
        validate_array($this->toCreateCardArray(), ['bank_card_token']);
272
273
        $response = $this->client->postRequest(self::URI_DELETE_BANK_CARD, array_get($this->toDeleteCardArray(), 'bank_card_token'));
274
275
        $instance = $this->fromCardArray($response['bank_card']);
276
277
        $instance->handleMessages($response);
278
279
        return $instance;
280
    }
281
282
    private function fromCardArray(array $data)
283
    {
284
        $instance = new static();
285
286
        $instance->setName(array_get($data, 'bank_card_name'));
287
        $instance->setCardNumber(array_get($data, 'bank_card_number'));
288
        $instance->setCardExpirationMonth(array_get($data, 'bank_card_expiration_month'));
289
        $instance->setCardExpirationYear(array_get($data, 'bank_card_expiration_year'));
290
        $instance->setToken(array_get($data, 'bank_card_token'));
291
        $instance->setCardNumber(array_get($data, 'bank_card_number'));
292
        $instance->setBillingAddress((new Address())->fromArray(array_get($data, 'bank_card_billing_address')));
0 ignored issues
show
It seems like array_get($data, 'bank_card_billing_address') can also be of type null; however, parameter $data of Greenlyst\BaseCommerce\Models\Address::fromArray() does only seem to accept 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

292
        $instance->setBillingAddress((new Address())->fromArray(/** @scrutinizer ignore-type */ array_get($data, 'bank_card_billing_address')));
Loading history...
293
294
        $instance->setCreationDate(Carbon::parse(array_get($data, 'bank_card_creation_date_24hr'))->toDateTime());
295
296
        if (array_has($data, 'bank_card_status')) {
297
            if (gettype(array_get($data, 'bank_card_status')) === 'array') {
298
                $instance->setStatus(array_get($data, 'bank_card_status.bank_card_status_name'));
299
            } else {
300
                $instance->setStatus(array_get($data, 'bank_card_status'));
301
            }
302
        }
303
304
        if (array_has($data, 'bank_card_deleted')) {
305
            $instance->setStatus(self::STATUS_DELETED);
306
        }
307
308
        $instance->setAlias(array_get($data, 'bank_card_alias'));
309
310
        return $instance;
311
    }
312
313
    public function toCreateCardArray(): array
314
    {
315
        return clear_array([
316
            'bank_card_name'             => $this->getName(),
317
            'bank_card_number'           => $this->getCardNumber(),
318
            'bank_card_expiration_month' => $this->getCardExpirationMonth(),
319
            'bank_card_expiration_year'  => $this->getCardExpirationYear(),
320
            'bank_card_token'            => $this->getToken(),
321
            'bank_card_billing_address'  => $this->getBillingAddress() ? $this->getBillingAddress()->toArray() : null,
322
            'bank_card_alias'            => $this->getAlias(),
323
        ]);
324
    }
325
326
    private function toUpdateCardArray(): array
327
    {
328
        return clear_array([
329
            'bank_card_expiration_month' => $this->getCardExpirationMonth(),
330
            'bank_card_expiration_year'  => $this->getCardExpirationYear(),
331
            'bank_card_token'            => $this->getToken(),
332
            'bank_card_billing_address'  => $this->getBillingAddress() ? $this->getBillingAddress()->toArray() : null,
333
        ]);
334
    }
335
336
    private function toDeleteCardArray(): array
337
    {
338
        return clear_array([
339
            'bank_card_token' => $this->getToken(),
340
        ]);
341
    }
342
}
343