Passed
Pull Request — master (#2)
by Bernhard
02:00
created

PaymentRequest::setIpnUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
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
    protected $total = 0.0;
28
29
    protected $currency = 'USD';
30
31
    protected $customPaymentId;
32
33
    protected $callbackData;
34
35
    protected $customerName;
36
37
    protected $customerEmail;
38
39
    protected $successUrl;
40
41
    protected $cancelUrl;
42
43
    protected $ipnUrl;
44
45
    protected $notificationEmail;
46
47
    protected $confirmationSpeed = 'medium';
48
49
    private $id;
50
51
    private $status;
52
53
    private $redirectUrl;
54
55
    private $expiresAt;
56
57
    private $createdAt;
58
59 7
    public static function fromResponse(array $data)
60
    {
61 7
        $self = new self();
62
63 7
        $callbackData = json_decode($data['callback_data'], true);
64 7
        if (json_last_error() !== JSON_ERROR_NONE) {
65 6
            $callbackData = $data['callback_data'];
66
        }
67 7
        $self->id = $data['id'];
68 7
        $self->status = $data['status'];
69 7
        $self->total = $data['total'];
70 7
        $self->currency = $data['currency'];
71 7
        $self->customPaymentId = $data['custom_payment_id'];
72 7
        $self->callbackData = $callbackData;
73 7
        $self->customerName = $data['customer']['name'];
74 7
        $self->customerEmail = $data['customer']['email'];
75 7
        $self->redirectUrl = $data['redirect_url'];
76 7
        $self->successUrl = $data['success_url'];
77 7
        $self->cancelUrl = $data['cancel_url'];
78 7
        $self->ipnUrl = $data['ipn_url'];
79 7
        $self->notificationEmail = $data['notification_email'];
80 7
        $self->confirmationSpeed = $data['confirmation_speed'];
81 7
        $self->expiresAt = $data['expires_at'];
82 7
        $self->createdAt = $data['created_at'];
83
84 7
        return $self;
85
    }
86
87
    /**
88
     * @param float $total
89
     *
90
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException
91
     * @throws \GloBee\PaymentApi\Exceptions\Validation\BelowMinimumException
92
     */
93 7
    public function setTotal($total)
94
    {
95 7
        $this->validateNumberAboveMinimum('total', $total, 0);
96 5
        $this->total = $total;
97 5
    }
98
99
    /**
100
     * @param mixed $currency
101
     *
102
     * @throws \GloBee\PaymentApi\Exceptions\Validation\ValidationException
103
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException
104
     */
105 5
    public function setCurrency($currency)
106
    {
107 5
        $this->validateStringLength('currency', $currency, 3);
108 3
        $this->currency = strtoupper($currency);
109 3
    }
110
111
    /**
112
     * @param mixed $customerEmail
113
     *
114
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException
115
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidEmailException
116
     */
117 6
    public function setCustomerEmail($customerEmail)
118
    {
119 6
        $this->validateEmail('customer.email', $customerEmail);
120 5
        $this->customerEmail = $customerEmail;
121 5
    }
122
123
    /**
124
     * @param mixed $successUrl
125
     *
126
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidArgumentException
127
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidUrlException
128
     */
129 4
    public function setSuccessUrl($successUrl)
130
    {
131 4
        if ($successUrl !== null) {
132 3
            $this->validateUrl('success_url', $successUrl);
133
        }
134 3
        $this->successUrl = $successUrl;
135 3
    }
136
137
    /**
138
     * @param mixed $cancelUrl
139
     */
140 4
    public function setCancelUrl($cancelUrl)
141
    {
142 4
        if ($cancelUrl !== null) {
143 3
            $this->validateUrl('cancel_url', $cancelUrl);
144
        }
145 3
        $this->cancelUrl = $cancelUrl;
146 3
    }
147
148
    /**
149
     * @param mixed $ipnUrl
150
     */
151 4
    public function setIpnUrl($ipnUrl)
152
    {
153 4
        if ($ipnUrl !== null) {
154 3
            $this->validateUrl('ipn_url', $ipnUrl);
155
        }
156 3
        $this->ipnUrl = $ipnUrl;
157 3
    }
158
159
    /**
160
     * @param mixed $notificationEmail
161
     */
162 4
    public function setNotificationEmail($notificationEmail)
163
    {
164 4
        if ($notificationEmail !== null) {
165 3
            $this->validateEmail('notification_email', $notificationEmail);
166
        }
167 3
        $this->notificationEmail = $notificationEmail;
168 3
    }
169
170
    /**
171
     * @param mixed $confirmationSpeed
172
     *
173
     * @throws \GloBee\PaymentApi\Exceptions\Validation\InvalidSelectionException
174
     */
175 3
    public function setConfirmationSpeed($confirmationSpeed)
176
    {
177 3
        $this->validateOptions('confirmation_speed', $confirmationSpeed, ['low', 'medium', 'high']);
178 2
        $this->confirmationSpeed = $confirmationSpeed;
179 2
    }
180
181 3
    public function toArray()
182
    {
183 3
        $callbackData = $this->callbackData;
184 3
        if (is_array($callbackData)) {
185 1
            $callbackData = json_encode($callbackData);
186
        }
187
188
        return [
189 3
            'id' => $this->id,
190 3
            'status' => $this->status,
191 3
            'total' => $this->total,
192 3
            'currency' => $this->currency,
193 3
            'custom_payment_id' => $this->customPaymentId,
194 3
            'callback_data' => $callbackData,
195
            'customer' => [
196 3
                'name' => $this->customerName,
197 3
                'email' => $this->customerEmail,
198
            ],
199 3
            'redirect_url' => $this->redirectUrl,
200 3
            'success_url' => $this->successUrl,
201 3
            'cancel_url' => $this->cancelUrl,
202 3
            'ipn_url' => $this->ipnUrl,
203 3
            'notification_email' => $this->notificationEmail,
204 3
            'confirmation_speed' => $this->confirmationSpeed,
205 3
            'expires_at' => $this->expiresAt,
206 3
            'created_at' => $this->createdAt,
207
        ];
208
    }
209
210 3
    public function isValid()
211
    {
212 3
        return $this->total > 0 && null !== $this->customerEmail;
213
    }
214
215 4
    public function exists()
216
    {
217 4
        return $this->id !== null;
218
    }
219
}
220