Completed
Pull Request — master (#3)
by Bernhard
02:05
created

PaymentRequest::toArray()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 20
cts 20
cp 1
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 21
nc 2
nop 0
crap 2
1
<?php
2
3
namespace GloBee\PaymentApi\Models;
4
5
/**
6
 * @property string $id
7
 * @property string $status
8
 * @property float $total
9
 * @property string $currency
10
 * @property string $customPaymentId
11
 * @property mixed $callbackData
12
 * @property string $customerName
13
 * @property string $customerEmail
14
 * @property string $redirectUrl
15
 * @property string $successUrl
16
 * @property string $cancelUrl
17
 * @property string $ipnUrl
18
 * @property string $notificationEmail
19
 * @property string $confirmationSpeed
20
 * @property string $expiresAt
21
 * @property string $createdAt
22
 */
23
class PaymentRequest extends Model
24
{
25
    use PropertyTrait;
26
27
    /**
28
     * @var float
29
     */
30
    protected $total = 0.0;
31
32
    /**
33
     * @var string
34
     */
35
    protected $currency = 'USD';
36
37
    /**
38
     * @var string
39
     */
40
    protected $customPaymentId;
41
42
    /**
43
     * @var string|array
44
     */
45
    protected $callbackData;
46
47
    /**
48
     * @var string
49
     */
50
    protected $customerName;
51
52
    /**
53
     * @var string
54
     */
55
    protected $customerEmail;
56
57
    /**
58
     * @var string
59
     */
60
    protected $successUrl;
61
62
    /**
63
     * @var string
64
     */
65
    protected $cancelUrl;
66
67
    /**
68
     * @var string
69
     */
70
    protected $ipnUrl;
71
72
    /**
73
     * @var string
74
     */
75
    protected $notificationEmail;
76
77
    /**
78
     * @var string
79
     */
80
    protected $confirmationSpeed = 'medium';
81
82
    /**
83
     * @var string
84
     */
85
    private $id;
86
87
    /**
88
     * @var string
89
     */
90
    private $status;
91
92
    /**
93
     * @var string
94
     */
95
    private $redirectUrl;
96
97
    /**
98
     * @var string
99
     */
100
    private $expiresAt;
101
102
    /**
103
     * @var string
104
     */
105
    private $createdAt;
106
107
    /**
108
     * @param array $data
109
     *
110
     * @return PaymentRequest
111
     */
112 7
    public static function fromResponse(array $data)
113
    {
114 7
        $self = new self();
115
116 7
        $callbackData = json_decode($data['callback_data'], true);
117 7
        if (json_last_error() !== JSON_ERROR_NONE) {
118 6
            $callbackData = $data['callback_data'];
119
        }
120 7
        $self->id = $data['id'];
121 7
        $self->status = $data['status'];
122 7
        $self->total = $data['total'];
123 7
        $self->currency = $data['currency'];
124 7
        $self->customPaymentId = $data['custom_payment_id'];
125 7
        $self->callbackData = $callbackData;
126 7
        $self->customerName = $data['customer']['name'];
127 7
        $self->customerEmail = $data['customer']['email'];
128 7
        $self->redirectUrl = $data['redirect_url'];
129 7
        $self->successUrl = $data['success_url'];
130 7
        $self->cancelUrl = $data['cancel_url'];
131 7
        $self->ipnUrl = $data['ipn_url'];
132 7
        $self->notificationEmail = $data['notification_email'];
133 7
        $self->confirmationSpeed = $data['confirmation_speed'];
134 7
        $self->expiresAt = $data['expires_at'];
135 7
        $self->createdAt = $data['created_at'];
136
137 7
        return $self;
138
    }
139
140
    /**
141
     * @param float $total
142
     *
143
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException
144
     * @throws \GloBee\PaymentApi\Exceptions\Validation\BelowMinimumException
145
     */
146 7
    public function setTotal($total)
147
    {
148 7
        $this->validateNumberAboveMinimum('total', $total, 0);
149 5
        $this->total = $total;
150 5
    }
151
152
    /**
153
     * @param mixed $currency
154
     *
155
     * @throws \GloBee\PaymentApi\Exceptions\Validation\ValidationException
156
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException
157
     */
158 5
    public function setCurrency($currency)
159
    {
160 5
        $this->validateStringLength('currency', $currency, 3);
161 3
        $this->currency = strtoupper($currency);
162 3
    }
163
164
    /**
165
     * @param mixed $customerEmail
166
     *
167
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException
168
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidEmailException
169
     */
170 6
    public function setCustomerEmail($customerEmail)
171
    {
172 6
        $this->validateEmail('customer.email', $customerEmail);
173 5
        $this->customerEmail = $customerEmail;
174 5
    }
175
176
    /**
177
     * @param mixed $successUrl
178
     *
179
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException
180
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidUrlException
181
     */
182 4
    public function setSuccessUrl($successUrl)
183
    {
184 4
        if ($successUrl !== null) {
185 3
            $this->validateUrl('success_url', $successUrl);
186
        }
187 3
        $this->successUrl = $successUrl;
188 3
    }
189
190
    /**
191
     * @param mixed $cancelUrl
192
     */
193 4
    public function setCancelUrl($cancelUrl)
194
    {
195 4
        if ($cancelUrl !== null) {
196 3
            $this->validateUrl('cancel_url', $cancelUrl);
197
        }
198 3
        $this->cancelUrl = $cancelUrl;
199 3
    }
200
201
    /**
202
     * @param mixed $ipnUrl
203
     */
204 4
    public function setIpnUrl($ipnUrl)
205
    {
206 4
        if ($ipnUrl !== null) {
207 3
            $this->validateUrl('ipn_url', $ipnUrl);
208
        }
209 3
        $this->ipnUrl = $ipnUrl;
210 3
    }
211
212
    /**
213
     * @param mixed $notificationEmail
214
     */
215 4
    public function setNotificationEmail($notificationEmail)
216
    {
217 4
        if ($notificationEmail !== null) {
218 3
            $this->validateEmail('notification_email', $notificationEmail);
219
        }
220 3
        $this->notificationEmail = $notificationEmail;
221 3
    }
222
223
    /**
224
     * @param mixed $confirmationSpeed
225
     *
226
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidSelectionException
227
     */
228 3
    public function setConfirmationSpeed($confirmationSpeed)
229
    {
230 3
        $this->validateOptions('confirmation_speed', $confirmationSpeed, ['low', 'medium', 'high']);
231 2
        $this->confirmationSpeed = $confirmationSpeed;
232 2
    }
233
234
    /**
235
     * @return array
236
     */
237 3
    public function toArray()
238
    {
239 3
        $callbackData = $this->callbackData;
240 3
        if (is_array($callbackData)) {
241 1
            $callbackData = json_encode($callbackData);
242
        }
243
244
        return [
245 3
            'id' => $this->id,
246 3
            'status' => $this->status,
247 3
            'total' => $this->total,
248 3
            'currency' => $this->currency,
249 3
            'custom_payment_id' => $this->customPaymentId,
250 3
            'callback_data' => $callbackData,
251
            'customer' => [
252 3
                'name' => $this->customerName,
253 3
                'email' => $this->customerEmail,
254
            ],
255 3
            'redirect_url' => $this->redirectUrl,
256 3
            'success_url' => $this->successUrl,
257 3
            'cancel_url' => $this->cancelUrl,
258 3
            'ipn_url' => $this->ipnUrl,
259 3
            'notification_email' => $this->notificationEmail,
260 3
            'confirmation_speed' => $this->confirmationSpeed,
261 3
            'expires_at' => $this->expiresAt,
262 3
            'created_at' => $this->createdAt,
263
        ];
264
    }
265
266
    /**
267
     * @return bool
268
     */
269 3
    public function isValid()
270
    {
271 3
        return $this->total > 0 && null !== $this->customerEmail;
272
    }
273
274
    /**
275
     * @return bool
276
     */
277 4
    public function exists()
278
    {
279 4
        return $this->id !== null;
280
    }
281
}
282