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\Deletable; |
10
|
|
|
use LauLamanApps\eCurring\Resource\Transaction\Event; |
11
|
|
|
use LauLamanApps\eCurring\Resource\Transaction\PaymentMethod; |
12
|
|
|
use LauLamanApps\eCurring\Resource\Transaction\Status; |
13
|
|
|
use Money\Money; |
14
|
|
|
use Ramsey\Uuid\UuidInterface; |
15
|
|
|
|
16
|
|
|
final class Transaction implements TransactionInterface, Creatable, Deletable |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var UuidInterface |
20
|
|
|
*/ |
21
|
|
|
private $id; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Status |
25
|
|
|
* |
26
|
|
|
* The current status of the transaction. |
27
|
|
|
*/ |
28
|
|
|
private $status; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var DateTimeImmutable |
32
|
|
|
* |
33
|
|
|
* The date on which this transaction was scheduled |
34
|
|
|
* Generally this is the date on which the subscription was activated. |
35
|
|
|
*/ |
36
|
|
|
private $scheduledOn; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var DateTimeImmutable|null |
40
|
|
|
* |
41
|
|
|
* The date on which the transaction will be, or was, executed. |
42
|
|
|
* Usually a transaction will be fulfilled within 3 days of this date |
43
|
|
|
* If a transaction was rescheduled after a charge back, |
44
|
|
|
* this attribute will reflect the date of the next attempt. |
45
|
|
|
*/ |
46
|
|
|
private $dueDate; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var Money |
50
|
|
|
* |
51
|
|
|
* The amount of the transaction. |
52
|
|
|
*/ |
53
|
|
|
private $amount; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var DateTimeImmutable|null |
57
|
|
|
* |
58
|
|
|
* The date on which the transaction was cancelled. |
59
|
|
|
* Only filled if $status is 'cancelled' |
60
|
|
|
*/ |
61
|
|
|
private $canceledOn; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string|null |
65
|
|
|
* |
66
|
|
|
* The webhook URL we will call when the status of this transaction changes, |
67
|
|
|
* inherited from the subscription. |
68
|
|
|
*/ |
69
|
|
|
private $webhookUrl; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var PaymentMethod |
73
|
|
|
* |
74
|
|
|
* The payment method used for this transaction. |
75
|
|
|
* For fullfilled transactions this is the actual method used, |
76
|
|
|
* for future transactions this will be payment method used for the first attempt. |
77
|
|
|
* However, the method may change if the attempt fails and the transaction is |
78
|
|
|
* fulfilled through other means (a payment reminder, for example). |
79
|
|
|
*/ |
80
|
|
|
private $paymentMethod; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var Event[] |
84
|
|
|
* |
85
|
|
|
* Collection of events with the full life cycle history of the transaction |
86
|
|
|
*/ |
87
|
|
|
private $history; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var Subscription|null |
91
|
|
|
*/ |
92
|
|
|
private $subscription; |
93
|
|
|
|
94
|
1 |
|
private function __construct(Money $amount) |
95
|
|
|
{ |
96
|
1 |
|
$this->amount = $amount; |
97
|
1 |
|
$this->history = []; |
98
|
1 |
|
} |
99
|
|
|
|
100
|
|
|
public static function new( |
101
|
|
|
Subscription $subscription, |
102
|
|
|
Money $amount, |
103
|
|
|
?DateTimeImmutable $dueDate = null |
104
|
|
|
): self { |
105
|
|
|
$self = new self($amount); |
106
|
|
|
$self->dueDate = $dueDate; |
107
|
|
|
$self->subscription = $subscription; |
108
|
|
|
|
109
|
|
|
return $self; |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
public static function fromData( |
113
|
|
|
UuidInterface $id, |
114
|
|
|
Status $status, |
115
|
|
|
DateTimeImmutable $scheduledOn, |
116
|
|
|
Money $amount, |
117
|
|
|
PaymentMethod $paymentMethod, |
118
|
|
|
?DateTimeImmutable $dueDate = null, |
119
|
|
|
?DateTimeImmutable $canceledOn = null, |
120
|
|
|
?string $webhookUrl = null, |
121
|
|
|
?Event ...$history |
122
|
|
|
): self { |
123
|
1 |
|
$self = new self($amount); |
124
|
1 |
|
$self->id = $id; |
125
|
1 |
|
$self->status = $status; |
126
|
1 |
|
$self->scheduledOn = $scheduledOn; |
127
|
1 |
|
$self->dueDate = $dueDate; |
128
|
1 |
|
$self->amount = $amount; |
129
|
1 |
|
$self->canceledOn = $canceledOn; |
130
|
1 |
|
$self->webhookUrl = $webhookUrl; |
131
|
1 |
|
$self->paymentMethod = $paymentMethod; |
132
|
1 |
|
$self->history = $history; |
133
|
|
|
|
134
|
1 |
|
return $self; |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
public function getId(): ?UuidInterface |
138
|
|
|
{ |
139
|
1 |
|
return $this->id; |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
public function getStatus(): ?Status |
143
|
|
|
{ |
144
|
1 |
|
return $this->status; |
145
|
|
|
} |
146
|
|
|
|
147
|
1 |
|
public function getScheduledOn(): ?DateTimeImmutable |
148
|
|
|
{ |
149
|
1 |
|
return $this->scheduledOn; |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
public function getDueDate(): ?DateTimeImmutable |
153
|
|
|
{ |
154
|
1 |
|
return $this->dueDate; |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
public function getAmount(): Money |
158
|
|
|
{ |
159
|
1 |
|
return $this->amount; |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
public function getCanceledOn(): ?DateTimeImmutable |
163
|
|
|
{ |
164
|
1 |
|
return $this->canceledOn; |
165
|
|
|
} |
166
|
|
|
|
167
|
1 |
|
public function getWebhookUrl(): ?string |
168
|
|
|
{ |
169
|
1 |
|
return $this->webhookUrl; |
170
|
|
|
} |
171
|
|
|
|
172
|
1 |
|
public function getPaymentMethod(): PaymentMethod |
173
|
|
|
{ |
174
|
1 |
|
return $this->paymentMethod; |
175
|
|
|
} |
176
|
|
|
|
177
|
1 |
|
public function getHistory(): array |
178
|
|
|
{ |
179
|
1 |
|
return $this->history; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function getSubscription(): ?Subscription |
183
|
|
|
{ |
184
|
|
|
return $this->subscription; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|