forked from laravel/cashier-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvoiceLineItem.php
275 lines (246 loc) · 6.4 KB
/
InvoiceLineItem.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
namespace Laravel\Cashier;
use Illuminate\Support\Carbon;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use JsonSerializable;
use Stripe\InvoiceLineItem as StripeInvoiceLineItem;
use Stripe\TaxRate as StripeTaxRate;
class InvoiceLineItem implements Arrayable, Jsonable, JsonSerializable
{
/**
* The Cashier Invoice instance.
*
* @var \Laravel\Cashier\Invoice
*/
protected $invoice;
/**
* The Stripe invoice line item instance.
*
* @var \Stripe\InvoiceLineItem
*/
protected $item;
/**
* Create a new invoice line item instance.
*
* @param \Laravel\Cashier\Invoice $invoice
* @param \Stripe\InvoiceLineItem $item
* @return void
*/
public function __construct(Invoice $invoice, StripeInvoiceLineItem $item)
{
$this->invoice = $invoice;
$this->item = $item;
}
/**
* Get the total for the invoice line item.
*
* @return string
*/
public function total()
{
return $this->formatAmount($this->item->amount);
}
/**
* Determine if the line item has both inclusive and exclusive tax.
*
* @return bool
*/
public function hasBothInclusiveAndExclusiveTax()
{
return $this->inclusiveTaxPercentage() && $this->exclusiveTaxPercentage();
}
/**
* Get the total percentage of the default inclusive tax for the invoice line item.
*
* @return int|null
*/
public function inclusiveTaxPercentage()
{
if ($this->invoice->isNotTaxExempt()) {
return $this->calculateTaxPercentageByTaxAmount(true);
}
return $this->calculateTaxPercentageByTaxRate(true);
}
/**
* Get the total percentage of the default exclusive tax for the invoice line item.
*
* @return int
*/
public function exclusiveTaxPercentage()
{
if ($this->invoice->isNotTaxExempt()) {
return $this->calculateTaxPercentageByTaxAmount(false);
}
return $this->calculateTaxPercentageByTaxRate(false);
}
/**
* Calculate the total tax percentage for either the inclusive or exclusive tax by tax rate.
*
* @param bool $inclusive
* @return int
*/
protected function calculateTaxPercentageByTaxRate($inclusive)
{
if (!$this->item->tax_rates) {
return 0;
}
return (int) collect($this->item->tax_rates)
->filter(function (StripeTaxRate $taxRate) use ($inclusive) {
return $taxRate->inclusive === (bool) $inclusive;
})
->sum(function (StripeTaxRate $taxRate) {
return $taxRate->percentage;
});
}
/**
* Calculate the total tax percentage for either the inclusive or exclusive tax by tax amount.
*
* @param bool $inclusive
* @return int
*/
protected function calculateTaxPercentageByTaxAmount($inclusive)
{
if (!$this->item->tax_amounts) {
return 0;
}
return (int) collect($this->item->tax_amounts)
->filter(function (object $taxAmount) use ($inclusive) {
return $taxAmount->inclusive === (bool) $inclusive;
})
->sum(function (object $taxAmount) {
return $taxAmount->tax_rate->percentage;
});
}
/**
* Determine if the invoice line item has tax rates.
*
* @return bool
*/
public function hasTaxRates()
{
if ($this->invoice->isNotTaxExempt()) {
return !empty($this->item->tax_amounts);
}
return !empty($this->item->tax_rates);
}
/**
* Get a human readable date for the start date.
*
* @return string
*/
public function startDate()
{
if ($this->isSubscription()) {
return $this->startDateAsCarbon()->toFormattedDateString();
}
}
/**
* Get a human readable date for the end date.
*
* @return string
*/
public function endDate()
{
if ($this->isSubscription()) {
return $this->endDateAsCarbon()->toFormattedDateString();
}
}
/**
* Get a Carbon instance for the start date.
*
* @return \Illuminate\Support\Carbon;
*/
public function startDateAsCarbon()
{
if ($this->isSubscription()) {
return Carbon::createFromTimestampUTC($this->item->period->start);
}
}
/**
* Get a Carbon instance for the end date.
*
* @return \Illuminate\Support\Carbon;
*/
public function endDateAsCarbon()
{
if ($this->isSubscription()) {
return Carbon::createFromTimestampUTC($this->item->period->end);
}
}
/**
* Determine if the invoice line item is for a subscription.
*
* @return bool
*/
public function isSubscription()
{
return $this->item->type === 'subscription';
}
/**
* Format the given amount into a displayable currency.
*
* @param int $amount
* @return string
*/
protected function formatAmount($amount)
{
return Cashier::formatAmount($amount, $this->item->currency);
}
/**
* Get the Stripe model instance.
*
* @return \Laravel\Cashier\Invoice
*/
public function invoice()
{
return $this->invoice;
}
/**
* Get the underlying Stripe invoice line item.
*
* @return \Stripe\InvoiceLineItem
*/
public function asStripeInvoiceLineItem()
{
return $this->item;
}
/**
* Get the instance as an array.
*
* @return array
*/
public function toArray()
{
return $this->asStripeInvoiceLineItem()->toArray();
}
/**
* Convert the object to its JSON representation.
*
* @param int $options
* @return string
*/
public function toJson($options = 0)
{
return json_encode($this->jsonSerialize(), $options);
}
/**
* Convert the object into something JSON serializable.
*
* @return array
*/
public function jsonSerialize(): mixed
{
return $this->toArray();
}
/**
* Dynamically access the Stripe invoice line item instance.
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
return $this->item->{$key};
}
}