-
Notifications
You must be signed in to change notification settings - Fork 687
/
Copy pathManagesInvoices.php
353 lines (303 loc) · 10.8 KB
/
ManagesInvoices.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
namespace Laravel\Cashier\Concerns;
use Illuminate\Pagination\Cursor;
use Illuminate\Pagination\CursorPaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Laravel\Cashier\Exceptions\InvalidInvoice;
use Laravel\Cashier\Invoice;
use Laravel\Cashier\Payment;
use LogicException;
use Stripe\Exception\CardException as StripeCardException;
use Stripe\Exception\InvalidRequestException as StripeInvalidRequestException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
trait ManagesInvoices
{
/**
* Add an invoice item to the customer's upcoming invoice.
*
* @param string $description
* @param int $amount
* @param array $options
* @return \Stripe\InvoiceItem
*/
public function tab($description, $amount, array $options = [])
{
if ($this->isAutomaticTaxEnabled() && ! array_key_exists('price_data', $options)) {
throw new LogicException(
'When using automatic tax calculation, you must include "price_data" in the provided options array.'
);
}
$this->assertCustomerExists();
$options = array_merge([
'customer' => $this->stripe_id,
'currency' => $this->preferredCurrency(),
'description' => $description,
], $options);
if (array_key_exists('price_data', $options)) {
$options['price_data'] = array_merge([
'unit_amount' => $amount,
'currency' => $this->preferredCurrency(),
], $options['price_data']);
} elseif (array_key_exists('quantity', $options)) {
$options['unit_amount'] = $options['unit_amount'] ?? $amount;
} else {
$options['amount'] = $amount;
}
return static::stripe()->invoiceItems->create($options);
}
/**
* Invoice the customer for the given amount and generate an invoice immediately.
*
* @param string $description
* @param int $amount
* @param array $tabOptions
* @param array $invoiceOptions
* @return \Laravel\Cashier\Invoice
*
* @throws \Laravel\Cashier\Exceptions\IncompletePayment
*/
public function invoiceFor($description, $amount, array $tabOptions = [], array $invoiceOptions = [])
{
$this->tab($description, $amount, $tabOptions);
return $this->invoice($invoiceOptions);
}
/**
* Add an invoice item for a specific Price ID to the customer's upcoming invoice.
*
* @param string $price
* @param int $quantity
* @param array $options
* @return \Stripe\InvoiceItem
*/
public function tabPrice($price, $quantity = 1, array $options = [])
{
$this->assertCustomerExists();
$options = array_merge([
'customer' => $this->stripe_id,
'price' => $price,
'quantity' => $quantity,
], $options);
return static::stripe()->invoiceItems->create($options);
}
/**
* Invoice the customer for the given Price ID and generate an invoice immediately.
*
* @param string $price
* @param int $quantity
* @param array $tabOptions
* @param array $invoiceOptions
* @return \Laravel\Cashier\Invoice
*
* @throws \Laravel\Cashier\Exceptions\IncompletePayment
*/
public function invoicePrice($price, $quantity = 1, array $tabOptions = [], array $invoiceOptions = [])
{
$this->tabPrice($price, $quantity, $tabOptions);
return $this->invoice($invoiceOptions);
}
/**
* Invoice the customer outside of the regular billing cycle.
*
* @param array $options
* @return \Laravel\Cashier\Invoice
*
* @throws \Laravel\Cashier\Exceptions\IncompletePayment
*/
public function invoice(array $options = [])
{
try {
$payOptions = Arr::only($options, $payOptionKeys = [
'forgive',
'mandate',
'off_session',
'paid_out_of_band',
'payment_method',
'source',
]);
Arr::forget($options, $payOptionKeys);
$invoice = $this->createInvoice(array_merge([
'pending_invoice_items_behavior' => 'include',
], $options));
return $invoice->chargesAutomatically() ? $invoice->pay($payOptions) : $invoice->send();
} catch (StripeCardException) {
$payment = new Payment(
static::stripe()->paymentIntents->retrieve(
$invoice->asStripeInvoice()->refresh()->payment_intent,
['expand' => ['invoice.subscription']]
)
);
$payment->validate();
}
}
/**
* Create an invoice within Stripe.
*
* @param array $options
* @return \Laravel\Cashier\Invoice
*/
public function createInvoice(array $options = [])
{
$this->assertCustomerExists();
$stripeCustomer = $this->asStripeCustomer();
$parameters = array_merge([
'automatic_tax' => $this->automaticTaxPayload(),
'customer' => $this->stripe_id,
'currency' => $stripeCustomer->currency ?? config('cashier.currency'),
], $options);
if (isset($parameters['subscription'])) {
unset($parameters['currency']);
}
if (array_key_exists('subscription', $parameters)) {
unset($parameters['pending_invoice_items_behavior']);
}
$stripeInvoice = static::stripe()->invoices->create($parameters);
return new Invoice($this, $stripeInvoice);
}
/**
* Get the customer's upcoming invoice.
*
* @param array $options
* @return \Laravel\Cashier\Invoice|null
*/
public function upcomingInvoice(array $options = [])
{
if (! $this->hasStripeId()) {
return;
}
$parameters = array_merge([
'automatic_tax' => $this->automaticTaxPayload(),
'customer' => $this->stripe_id,
], $options);
try {
$stripeInvoice = static::stripe()->invoices->upcoming($parameters);
return new Invoice($this, $stripeInvoice, $parameters);
} catch (StripeInvalidRequestException $exception) {
//
}
}
/**
* Find an invoice by ID.
*
* @param string $id
* @return \Laravel\Cashier\Invoice|null
*/
public function findInvoice($id)
{
$stripeInvoice = null;
try {
$stripeInvoice = static::stripe()->invoices->retrieve($id);
} catch (StripeInvalidRequestException $exception) {
//
}
return $stripeInvoice ? new Invoice($this, $stripeInvoice) : null;
}
/**
* Find an invoice or throw a 404 or 403 error.
*
* @param string $id
* @return \Laravel\Cashier\Invoice
*
* @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function findInvoiceOrFail($id)
{
try {
$invoice = $this->findInvoice($id);
} catch (InvalidInvoice $exception) {
throw new AccessDeniedHttpException;
}
if (is_null($invoice)) {
throw new NotFoundHttpException;
}
return $invoice;
}
/**
* Create an invoice download Response.
*
* @param string $id
* @param array $data
* @param string $filename
* @return \Symfony\Component\HttpFoundation\Response
*/
public function downloadInvoice($id, array $data = [], $filename = null)
{
$invoice = $this->findInvoiceOrFail($id);
return $filename ? $invoice->downloadAs($filename, $data) : $invoice->download($data);
}
/**
* Get a collection of the customer's invoices.
*
* @param bool $includePending
* @param array $parameters
* @return \Illuminate\Support\Collection|\Laravel\Cashier\Invoice[]
*/
public function invoices($includePending = false, $parameters = [])
{
if (! $this->hasStripeId()) {
return new Collection();
}
$invoices = [];
$parameters = array_merge(['limit' => 24], $parameters);
$stripeInvoices = static::stripe()->invoices->all(
['customer' => $this->stripe_id] + $parameters
);
// Here we will loop through the Stripe invoices and create our own custom Invoice
// instances that have more helper methods and are generally more convenient to
// work with than the plain Stripe objects are. Then, we'll return the array.
if (! is_null($stripeInvoices)) {
foreach ($stripeInvoices->data as $invoice) {
if ($invoice->paid || $includePending) {
$invoices[] = new Invoice($this, $invoice);
}
}
}
return new Collection($invoices);
}
/**
* Get an array of the customer's invoices, including pending invoices.
*
* @param array $parameters
* @return \Illuminate\Support\Collection|\Laravel\Cashier\Invoice[]
*/
public function invoicesIncludingPending(array $parameters = [])
{
return $this->invoices(true, $parameters);
}
/**
* Get a cursor paginator for the customer's invoices.
*
* @param int|null $perPage
* @param array $parameters
* @param string $cursorName
* @param \Illuminate\Pagination\Cursor|string|null $cursor
* @return \Illuminate\Contracts\Pagination\CursorPaginator
*/
public function cursorPaginateInvoices($perPage = 24, array $parameters = [], $cursorName = 'cursor', $cursor = null)
{
if (! $cursor instanceof Cursor) {
$cursor = is_string($cursor)
? Cursor::fromEncoded($cursor)
: CursorPaginator::resolveCurrentCursor($cursorName, $cursor);
}
if (! is_null($cursor)) {
if ($cursor->pointsToNextItems()) {
$parameters['starting_after'] = $cursor->parameter('id');
} else {
$parameters['ending_before'] = $cursor->parameter('id');
}
}
$invoices = $this->invoices(true, array_merge($parameters, ['limit' => $perPage + 1]));
if (! is_null($cursor) && $cursor->pointsToPreviousItems()) {
$invoices = $invoices->reverse();
}
return new CursorPaginator($invoices, $perPage, $cursor, array_merge([
'path' => Paginator::resolveCurrentPath(),
'cursorName' => $cursorName,
'parameters' => ['id'],
]));
}
}