-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
LemonSqueezy.php
131 lines (107 loc) · 3.48 KB
/
LemonSqueezy.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
<?php
namespace LemonSqueezy\Laravel;
use Exception;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use LemonSqueezy\Laravel\Exceptions\LemonSqueezyApiError;
use Money\Currencies\ISOCurrencies;
use Money\Currency;
use Money\Formatter\IntlMoneyFormatter;
use Money\Money;
use NumberFormatter;
class LemonSqueezy
{
const VERSION = '1.6.2';
const API = 'https://api.lemonsqueezy.com/v1';
/**
* Indicates if migrations will be run.
*/
public static bool $runsMigrations = true;
/**
* Indicates if routes will be registered.
*/
public static bool $registersRoutes = true;
/**
* The customer model class name.
*/
public static string $customerModel = Customer::class;
/**
* The subscription model class name.
*/
public static string $subscriptionModel = Subscription::class;
/**
* The order model class name.
*/
public static string $orderModel = Order::class;
/**
* Perform a Lemon Squeezy API call.
*
* @throws Exception
* @throws LemonSqueezyApiError
*/
public static function api(string $method, string $uri, array $payload = []): Response
{
if (empty($apiKey = config('lemon-squeezy.api_key'))) {
throw new Exception('Lemon Squeezy API key not set.');
}
/** @var \Illuminate\Http\Client\Response $response */
$response = Http::withToken($apiKey)
->withUserAgent('LemonSqueezy\Laravel/'.static::VERSION)
->accept('application/vnd.api+json')
->contentType('application/vnd.api+json')
->$method(static::API."/{$uri}", $payload);
if ($response->failed()) {
throw new LemonSqueezyApiError($response['errors'][0]['detail'], (int) $response['errors'][0]['status']);
}
return $response;
}
/**
* Format the given amount into a displayable currency.
*/
public static function formatAmount(int $amount, string $currency, ?string $locale = null, array $options = []): string
{
$money = new Money($amount, new Currency(strtoupper($currency)));
$locale = $locale ?? config('lemon-squeezy.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 to not register any migrations.
*/
public static function ignoreMigrations(): void
{
static::$runsMigrations = false;
}
/**
* Configure to not register its routes.
*/
public static function ignoreRoutes(): void
{
static::$registersRoutes = false;
}
/**
* Set the customer model class name.
*/
public static function useCustomerModel(string $customerModel): void
{
static::$customerModel = $customerModel;
}
/**
* Set the subscription model class name.
*/
public static function useSubscriptionModel(string $subscriptionModel): void
{
static::$subscriptionModel = $subscriptionModel;
}
/**
* Set the order model class name.
*/
public static function useOrderModel(string $orderModel): void
{
static::$orderModel = $orderModel;
}
}