-
Notifications
You must be signed in to change notification settings - Fork 681
/
Cashier.php
270 lines (229 loc) · 6.34 KB
/
Cashier.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
<?php
namespace Laravel\Cashier;
use Illuminate\Database\Eloquent\SoftDeletes;
use Money\Currencies\ISOCurrencies;
use Money\Currency;
use Money\Formatter\IntlMoneyFormatter;
use Money\Money;
use NumberFormatter;
use Stripe\BaseStripeClient;
use Stripe\Customer as StripeCustomer;
use Stripe\StripeClient;
class Cashier
{
/**
* The Cashier library version.
*
* @var string
*/
const VERSION = '14.14.0';
/**
* The Stripe API version.
*
* @var string
*/
const STRIPE_VERSION = '2022-11-15';
/**
* The base URL for the Stripe API.
*
* @var string
*/
public static $apiBaseUrl = BaseStripeClient::DEFAULT_API_BASE;
/**
* The custom currency formatter.
*
* @var callable
*/
protected static $formatCurrencyUsing;
/**
* Indicates if Cashier migrations will be run.
*
* @var bool
*/
public static $runsMigrations = true;
/**
* Indicates if Cashier routes will be registered.
*
* @var bool
*/
public static $registersRoutes = true;
/**
* Indicates if Cashier will mark past due subscriptions as inactive.
*
* @var bool
*/
public static $deactivatePastDue = true;
/**
* Indicates if Cashier will mark incomplete subscriptions as inactive.
*
* @var bool
*/
public static $deactivateIncomplete = true;
/**
* Indicates if Cashier will automatically calculate taxes using Stripe Tax.
*
* @var bool
*/
public static $calculatesTaxes = false;
/**
* The default customer model class name.
*
* @var string
*/
public static $customerModel = 'App\\Models\\User';
/**
* The subscription model class name.
*
* @var string
*/
public static $subscriptionModel = Subscription::class;
/**
* The subscription item model class name.
*
* @var string
*/
public static $subscriptionItemModel = SubscriptionItem::class;
/**
* Get the customer instance by its Stripe ID.
*
* @param \Stripe\Customer|string|null $stripeId
* @return \Laravel\Cashier\Billable|null
*/
public static function findBillable($stripeId)
{
$stripeId = $stripeId instanceof StripeCustomer ? $stripeId->id : $stripeId;
$model = static::$customerModel;
$builder = in_array(SoftDeletes::class, class_uses_recursive($model))
? $model::withTrashed()
: new $model;
return $stripeId ? $builder->where('stripe_id', $stripeId)->first() : null;
}
/**
* Get the Stripe SDK client.
*
* @param array $options
* @return \Stripe\StripeClient
*/
public static function stripe(array $options = [])
{
$config = array_merge([
'api_key' => $options['api_key'] ?? config('cashier.secret'),
'stripe_version' => static::STRIPE_VERSION,
'api_base' => static::$apiBaseUrl,
], $options);
return app(StripeClient::class, ['config' => $config]);
}
/**
* Set the custom currency formatter.
*
* @param callable $callback
* @return void
*/
public static function formatCurrencyUsing(callable $callback)
{
static::$formatCurrencyUsing = $callback;
}
/**
* Format the given amount into a displayable currency.
*
* @param int $amount
* @param string|null $currency
* @param string|null $locale
* @param array $options
* @return string
*/
public static function formatAmount($amount, $currency = null, $locale = null, array $options = [])
{
if (static::$formatCurrencyUsing) {
return call_user_func(static::$formatCurrencyUsing, $amount, $currency, $locale, $options);
}
$money = new Money($amount, new Currency(strtoupper($currency ?? config('cashier.currency'))));
$locale = $locale ?? config('cashier.currency_locale');
$numberFormatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
if (isset($options['min_fraction_digits'])) {
$numberFormatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $options['min_fraction_digits']);
}
$moneyFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies());
return $moneyFormatter->format($money);
}
/**
* Configure Cashier to not register its migrations.
*
* @return static
*/
public static function ignoreMigrations()
{
static::$runsMigrations = false;
return new static;
}
/**
* Configure Cashier to not register its routes.
*
* @return static
*/
public static function ignoreRoutes()
{
static::$registersRoutes = false;
return new static;
}
/**
* Configure Cashier to maintain past due subscriptions as active.
*
* @return static
*/
public static function keepPastDueSubscriptionsActive()
{
static::$deactivatePastDue = false;
return new static;
}
/**
* Configure Cashier to maintain incomplete subscriptions as active.
*
* @return static
*/
public static function keepIncompleteSubscriptionsActive()
{
static::$deactivateIncomplete = false;
return new static;
}
/**
* Configure Cashier to automatically calculate taxes using Stripe Tax.
*
* @return static
*/
public static function calculateTaxes()
{
static::$calculatesTaxes = true;
return new static;
}
/**
* Set the customer model class name.
*
* @param string $customerModel
* @return void
*/
public static function useCustomerModel($customerModel)
{
static::$customerModel = $customerModel;
}
/**
* Set the subscription model class name.
*
* @param string $subscriptionModel
* @return void
*/
public static function useSubscriptionModel($subscriptionModel)
{
static::$subscriptionModel = $subscriptionModel;
}
/**
* Set the subscription item model class name.
*
* @param string $subscriptionItemModel
* @return void
*/
public static function useSubscriptionItemModel($subscriptionItemModel)
{
static::$subscriptionItemModel = $subscriptionItemModel;
}
}