1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Phalcon\Cashier; |
4
|
|
|
|
5
|
|
|
class InvoiceItem |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* The user instance. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
protected $user; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The Stripe invoice item instance. |
15
|
|
|
* |
16
|
|
|
* @var \Stripe\InvoiceItem |
17
|
|
|
*/ |
18
|
|
|
protected $item; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Create a new invoice item instance. |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function __construct($user, $item) |
25
|
|
|
{ |
26
|
|
|
$this->user = $user; |
27
|
|
|
$this->item = $item; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get the total for the line item. |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public function total() |
36
|
|
|
{ |
37
|
|
|
return $this->formatAmount($this->amount); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get a human readable date for the start date. |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public function startDate() |
46
|
|
|
{ |
47
|
|
|
if ($this->isSubscription()) { |
48
|
|
|
return date('M j, Y', $this->item->period->start); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get a human readable date for the end date. |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function endDate() |
58
|
|
|
{ |
59
|
|
|
if ($this->isSubscription()) { |
60
|
|
|
return date('M j, Y', $this->item->period->end); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Determine if the invoice item is for a subscription. |
66
|
|
|
* |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function isSubscription() |
70
|
|
|
{ |
71
|
|
|
return $this->item->type === 'subscription'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Format the given amount into a string based on the user's preferences. |
76
|
|
|
* |
77
|
|
|
* @param int $amount |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
protected function formatAmount($amount) |
81
|
|
|
{ |
82
|
|
|
return Cashier::formatAmount($amount); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the underlying Stripe invoice item. |
87
|
|
|
* |
88
|
|
|
* @return \Stripe\StripeObject |
89
|
|
|
*/ |
90
|
|
|
public function asStripeInvoiceItem() |
91
|
|
|
{ |
92
|
|
|
return $this->item; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Dynamically access the Stripe line item instance. |
97
|
|
|
* |
98
|
|
|
* @param string $key |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
public function __get($key) |
102
|
|
|
{ |
103
|
|
|
return $this->item->{$key}; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|