-
Notifications
You must be signed in to change notification settings - Fork 25
/
InvoiceItem.php
141 lines (127 loc) · 2.69 KB
/
InvoiceItem.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
<?php
namespace yii2mod\cashier;
use Carbon\Carbon;
/**
* Class InvoiceItem
*
* @package yii2mod\cashier
*/
class InvoiceItem
{
/**
* The user instance.
*
* @var \yii\db\ActiveRecord
*/
protected $user;
/**
* The Stripe invoice item instance.
*
* @var \Stripe\InvoiceItem
*/
protected $item;
/**
* Create a new invoice item instance.
*
* @param \yii\db\ActiveRecord $user
* @param \Stripe\StripeObject $item
*/
public function __construct($user, $item)
{
$this->user = $user;
$this->item = $item;
}
/**
* Get the total for the line item.
*
* @return string
*/
public function total()
{
return $this->formatAmount($this->amount);
}
/**
* 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 \Carbon\Carbon
*/
public function startDateAsCarbon()
{
if ($this->isSubscription()) {
return Carbon::createFromTimestampUTC($this->item->period->start);
}
}
/**
* Get a Carbon instance for the end date.
*
* @return \Carbon\Carbon
*/
public function endDateAsCarbon()
{
if ($this->isSubscription()) {
return Carbon::createFromTimestampUTC($this->item->period->end);
}
}
/**
* Determine if the invoice item is for a subscription.
*
* @return bool
*/
public function isSubscription()
{
return $this->item->type === 'subscription';
}
/**
* Format the given amount into a string based on the user's preferences.
*
* @param int $amount
*
* @return string
*/
protected function formatAmount($amount)
{
return Cashier::formatAmount($amount);
}
/**
* Get the underlying Stripe invoice item.
*
* @return \Stripe\StripeObject
*/
public function asStripeInvoiceItem()
{
return $this->item;
}
/**
* Dynamically access the Stripe line item instance.
*
* @param string $key
*
* @return mixed
*/
public function __get($key)
{
return $this->item->{$key};
}
}