1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LauLamanApps\eCurring\Resource; |
6
|
|
|
|
7
|
|
|
use DateTimeImmutable; |
8
|
|
|
use LauLamanApps\eCurring\Http\Resource\Creatable; |
9
|
|
|
use LauLamanApps\eCurring\Http\Resource\Updatable; |
10
|
|
|
use LauLamanApps\eCurring\Resource\Exception\MandateNotAcceptedException; |
11
|
|
|
use LauLamanApps\eCurring\Resource\Subscription\Mandate; |
12
|
|
|
use LauLamanApps\eCurring\Resource\Subscription\Status; |
13
|
|
|
|
14
|
|
|
final class Subscription implements SubscriptionInterface, Creatable, Updatable |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
private $id; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Mandate |
23
|
|
|
* |
24
|
|
|
* The unique mandate code which is generated when creating a subscription. |
25
|
|
|
*/ |
26
|
|
|
private $mandate; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var DateTimeImmutable |
30
|
|
|
* |
31
|
|
|
* The start date of the subscription. |
32
|
|
|
* Transactions will be planned relative from this date. |
33
|
|
|
* If the start date is in the future, eCurring won't charge any amounts before the specific date. |
34
|
|
|
*/ |
35
|
|
|
private $startDate; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Status |
39
|
|
|
* |
40
|
|
|
* The current status of the subscription. |
41
|
|
|
*/ |
42
|
|
|
private $status; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var DateTimeImmutable|null |
46
|
|
|
* |
47
|
|
|
* If a subscription has the status cancelled, |
48
|
|
|
* this indicates the moment at which the subscription has been cancelled. |
49
|
|
|
* When the date is in the future this indicates when the subscription will automatically be cancelled. |
50
|
|
|
*/ |
51
|
|
|
private $cancelDate; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var DateTimeImmutable|null |
55
|
|
|
* |
56
|
|
|
* If a subscription has the status paused, |
57
|
|
|
* this indicates on which date the subscription will be activated again. |
58
|
|
|
*/ |
59
|
|
|
private $resumeDate; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var string |
63
|
|
|
* |
64
|
|
|
* The URL which allows customers to accept their mandate and activate their subscription. |
65
|
|
|
*/ |
66
|
|
|
private $confirmationPage; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var bool |
70
|
|
|
* |
71
|
|
|
* Indicates whether the above confirmation page was sent to the customer, |
72
|
|
|
* which is the default when a subscription is created without an accepted mandate. |
73
|
|
|
*/ |
74
|
|
|
private $confirmationSent; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var string|null |
78
|
|
|
* |
79
|
|
|
* The webhook URL we will call when the status of the subscription changes. |
80
|
|
|
*/ |
81
|
|
|
private $subscriptionWebhookUrl; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var string|null |
85
|
|
|
* |
86
|
|
|
* The webhook URL we will call when the status of a transaction, scheduled by this subscription, changes. |
87
|
|
|
*/ |
88
|
|
|
private $transactionWebhookUrl; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var string|null |
92
|
|
|
* |
93
|
|
|
* The URL we will redirect the client to after a successful activation of the subscription |
94
|
|
|
* (after the customer approved the mandate). |
95
|
|
|
*/ |
96
|
|
|
private $successRedirectUrl; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @var ProductInterface |
100
|
|
|
* |
101
|
|
|
* The subscription plan which is attached to the subscription |
102
|
|
|
*/ |
103
|
|
|
private $subscriptionPlan; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @var CustomerInterface |
107
|
|
|
* |
108
|
|
|
* The customer which the subscription belongs to |
109
|
|
|
*/ |
110
|
|
|
private $customer; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @var TransactionInterface[] |
114
|
|
|
*/ |
115
|
|
|
private $transactions; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @var DateTimeImmutable |
119
|
|
|
* |
120
|
|
|
* The date on which the subscription was created |
121
|
|
|
*/ |
122
|
|
|
private $createdAt; |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @var DateTimeImmutable |
126
|
|
|
* |
127
|
|
|
* The date on which the subscription was last updated |
128
|
|
|
*/ |
129
|
|
|
private $updatedAt; |
130
|
|
|
|
131
|
1 |
|
private function __construct() |
132
|
|
|
{ |
133
|
1 |
|
} |
134
|
|
|
|
135
|
1 |
|
public static function fromData( |
136
|
|
|
int $id, |
137
|
|
|
Mandate $mandate, |
138
|
|
|
DateTimeImmutable $startDate, |
139
|
|
|
Status $status, |
140
|
|
|
string $confirmationPage, |
141
|
|
|
bool $confirmationSent, |
142
|
|
|
CustomerInterface $customer, |
143
|
|
|
ProductInterface $subscriptionPlan, |
144
|
|
|
DateTimeImmutable $createdAt, |
145
|
|
|
DateTimeImmutable $updatedAt, |
146
|
|
|
?string $subscriptionWebhookUrl, |
147
|
|
|
?string $transactionWebhookUrl, |
148
|
|
|
?string $successRedirectUrl, |
149
|
|
|
?DateTimeImmutable $cancelDate, |
150
|
|
|
?DateTimeImmutable $resumeDate, |
151
|
|
|
?TransactionInterface ...$transactions |
152
|
|
|
): self { |
153
|
1 |
|
$self = new self(); |
154
|
1 |
|
$self->id = $id; |
155
|
1 |
|
$self->mandate = $mandate; |
156
|
1 |
|
$self->startDate = $startDate; |
157
|
1 |
|
$self->status = $status; |
158
|
1 |
|
$self->confirmationPage = $confirmationPage; |
159
|
1 |
|
$self->confirmationSent = $confirmationSent; |
160
|
1 |
|
$self->subscriptionWebhookUrl = $subscriptionWebhookUrl; |
161
|
1 |
|
$self->transactionWebhookUrl = $transactionWebhookUrl; |
162
|
1 |
|
$self->successRedirectUrl = $successRedirectUrl; |
163
|
1 |
|
$self->subscriptionPlan = $subscriptionPlan; |
164
|
1 |
|
$self->customer = $customer; |
165
|
1 |
|
$self->createdAt = $createdAt; |
166
|
1 |
|
$self->updatedAt = $updatedAt; |
167
|
1 |
|
$self->cancelDate = $cancelDate; |
168
|
1 |
|
$self->resumeDate = $resumeDate; |
169
|
1 |
|
$self->transactions = $transactions; |
170
|
|
|
|
171
|
1 |
|
return $self; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public static function new( |
175
|
|
|
CustomerInterface $customer, |
176
|
|
|
ProductInterface $subscriptionPlan, |
177
|
|
|
?Mandate $mandate = null, |
178
|
|
|
?DateTimeImmutable $startDate = null, |
179
|
|
|
?DateTimeImmutable $cancelDate = null, |
180
|
|
|
?DateTimeImmutable $resumeDate = null, |
181
|
|
|
?bool $confirmationSent = false, |
182
|
|
|
?Status $status = null, |
183
|
|
|
?string $subscriptionWebhookUrl = null, |
184
|
|
|
?string $transactionWebhookUrl = null |
185
|
|
|
|
186
|
|
|
): self { |
187
|
|
|
$self = new self(); |
188
|
|
|
$self->customer = $customer; |
189
|
|
|
$self->subscriptionPlan = $subscriptionPlan; |
190
|
|
|
$self->mandate = $mandate; |
191
|
|
|
$self->startDate = $startDate; |
192
|
|
|
$self->cancelDate = $cancelDate; |
193
|
|
|
$self->resumeDate = $resumeDate; |
194
|
|
|
$self->confirmationSent = $confirmationSent; |
195
|
|
|
$self->status = $status; |
196
|
|
|
$self->subscriptionWebhookUrl = $subscriptionWebhookUrl; |
197
|
|
|
$self->transactionWebhookUrl = $transactionWebhookUrl; |
198
|
|
|
|
199
|
|
|
return $self; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @throws MandateNotAcceptedException |
204
|
|
|
*/ |
205
|
|
|
public function activate(): void |
206
|
|
|
{ |
207
|
|
|
if ($this->mandate->isAccepted() !== true) { |
208
|
|
|
throw new MandateNotAcceptedException(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$this->status = Status::active(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function pause(): void |
215
|
|
|
{ |
216
|
|
|
$this->status = Status::paused(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function cancel(): void |
220
|
|
|
{ |
221
|
|
|
$this->status = Status::cancelled(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function setStartDate(DateTimeImmutable $startDate): void |
225
|
|
|
{ |
226
|
|
|
$this->startDate = $startDate; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function setCancelDate(?DateTimeImmutable $cancelDate): void |
230
|
|
|
{ |
231
|
|
|
$this->cancelDate = $cancelDate; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function setResumeDate(?DateTimeImmutable $resumeDate): void |
235
|
|
|
{ |
236
|
|
|
$this->resumeDate = $resumeDate; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function setConfirmationSent(bool $confirmationSent): void |
240
|
|
|
{ |
241
|
|
|
$this->confirmationSent = $confirmationSent; |
242
|
|
|
} |
243
|
|
|
|
244
|
1 |
|
public function getId(): ?int |
245
|
|
|
{ |
246
|
1 |
|
return $this->id; |
247
|
|
|
} |
248
|
|
|
|
249
|
1 |
|
public function getMandate(): ?Mandate |
250
|
|
|
{ |
251
|
1 |
|
return $this->mandate; |
252
|
|
|
} |
253
|
|
|
|
254
|
1 |
|
public function getStartDate(): ?DateTimeImmutable |
255
|
|
|
{ |
256
|
1 |
|
return $this->startDate; |
257
|
|
|
} |
258
|
|
|
|
259
|
1 |
|
public function getStatus(): ?Status |
260
|
|
|
{ |
261
|
1 |
|
return $this->status; |
262
|
|
|
} |
263
|
|
|
|
264
|
1 |
|
public function getCancelDate(): ?DateTimeImmutable |
265
|
|
|
{ |
266
|
1 |
|
return $this->cancelDate; |
267
|
|
|
} |
268
|
|
|
|
269
|
1 |
|
public function getResumeDate(): ?DateTimeImmutable |
270
|
|
|
{ |
271
|
1 |
|
return $this->resumeDate; |
272
|
|
|
} |
273
|
|
|
|
274
|
1 |
|
public function getConfirmationPage(): string |
275
|
|
|
{ |
276
|
1 |
|
return $this->confirmationPage; |
277
|
|
|
} |
278
|
|
|
|
279
|
1 |
|
public function isConfirmationSent(): ?bool |
280
|
|
|
{ |
281
|
1 |
|
return $this->confirmationSent; |
282
|
|
|
} |
283
|
|
|
|
284
|
1 |
|
public function getSubscriptionWebhookUrl(): ?string |
285
|
|
|
{ |
286
|
1 |
|
return $this->subscriptionWebhookUrl; |
287
|
|
|
} |
288
|
|
|
|
289
|
1 |
|
public function getTransactionWebhookUrl(): ?string |
290
|
|
|
{ |
291
|
1 |
|
return $this->transactionWebhookUrl; |
292
|
|
|
} |
293
|
|
|
|
294
|
1 |
|
public function getSuccessRedirectUrl(): ?string |
295
|
|
|
{ |
296
|
1 |
|
return $this->successRedirectUrl; |
297
|
|
|
} |
298
|
|
|
|
299
|
1 |
|
public function getSubscriptionPlan(): ProductInterface |
300
|
|
|
{ |
301
|
1 |
|
return $this->subscriptionPlan; |
302
|
|
|
} |
303
|
|
|
|
304
|
1 |
|
public function getCustomer(): CustomerInterface |
305
|
|
|
{ |
306
|
1 |
|
return $this->customer; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @return TransactionInterface[]|null |
311
|
|
|
*/ |
312
|
1 |
|
public function getTransactions(): array |
313
|
|
|
{ |
314
|
1 |
|
return $this->transactions; |
315
|
|
|
} |
316
|
|
|
|
317
|
1 |
|
public function getCreatedAt(): DateTimeImmutable |
318
|
|
|
{ |
319
|
1 |
|
return $this->createdAt; |
320
|
|
|
} |
321
|
|
|
|
322
|
1 |
|
public function getUpdatedAt(): DateTimeImmutable |
323
|
|
|
{ |
324
|
1 |
|
return $this->updatedAt; |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|