|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CashierUtils; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @method string subscriptionsUrl(string|null $id = null) |
|
9
|
|
|
* @method string paymentsUrl(string|null $id = null) |
|
10
|
|
|
* @method string invoicesUrl(string|null $id = null) |
|
11
|
|
|
* @method string productsUrl(string|null $id = null) |
|
12
|
|
|
* @method string pricesUrl(string|null $id = null) |
|
13
|
|
|
* @method string couponsUrl(string|null $id = null) |
|
14
|
|
|
* @method string promotionCodesUrl(string|null $id = null) |
|
15
|
|
|
*/ |
|
16
|
|
|
class DashboardRouter |
|
17
|
|
|
{ |
|
18
|
|
|
public static string $baseUrlLive = 'https://dashboard.stripe.com'; |
|
19
|
|
|
|
|
20
|
|
|
public static string $baseUrlTest = 'https://dashboard.stripe.com/test'; |
|
21
|
|
|
|
|
22
|
|
|
protected bool $isTestMode = false; |
|
23
|
|
|
|
|
24
|
|
|
public static function fromConfig(): static |
|
25
|
|
|
{ |
|
26
|
|
|
return new static(!str_starts_with(config('cashier.key'), 'pk_live')); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(bool $isTestMode = false) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->isTestMode = $isTestMode; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function setMode(bool $testMode = true): static |
|
35
|
|
|
{ |
|
36
|
|
|
$this->isTestMode = $testMode; |
|
37
|
|
|
|
|
38
|
|
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function isTestMode(): bool |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->isTestMode; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function isLiveMode(): bool |
|
47
|
|
|
{ |
|
48
|
|
|
return !$this->isTestMode(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function baseUrl(): string |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->isTestMode ? static::$baseUrlTest : static::$baseUrlLive; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function fullUrl(string $path = ''): string |
|
57
|
|
|
{ |
|
58
|
|
|
return rtrim($this->baseUrl(), '/') . '/' . ltrim($path, '/'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function __call(string $name, array $arguments) |
|
62
|
|
|
{ |
|
63
|
|
|
if (Str::endsWith($name, 'Url') |
|
64
|
|
|
&& ($entity = Str::beforeLast($name, 'Url'))) { |
|
65
|
|
|
$entity = Str::snake($entity); |
|
66
|
|
|
$id = $arguments[0] ?? null; |
|
67
|
|
|
|
|
68
|
|
|
return $this->fullUrl($entity . ($id ? "/{$id}" : '')); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
throw new \BadMethodCallException("Method {$name} not exists."); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|