|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CashierUtils\Checkout; |
|
4
|
|
|
|
|
5
|
|
|
class StripeCheckoutUrlBuilder |
|
6
|
|
|
{ |
|
7
|
|
|
public static string $sessionKey = 'checkout_session'; |
|
8
|
|
|
public static string $resultStatusKey = 'payment_status'; |
|
9
|
|
|
|
|
10
|
|
|
public static string $statusSuccess = 'success'; |
|
11
|
|
|
public static string $statusCanceled = 'canceled'; |
|
12
|
|
|
|
|
13
|
|
|
protected string $url; |
|
14
|
|
|
|
|
15
|
|
|
protected bool $withSessionId = false; |
|
16
|
|
|
protected ?string $resultStatus = null; |
|
17
|
|
|
|
|
18
|
5 |
|
protected function __construct(string $url) |
|
19
|
|
|
{ |
|
20
|
5 |
|
$this->url = $url; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
5 |
|
public static function make(string $url) |
|
24
|
|
|
{ |
|
25
|
5 |
|
return new static($url); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
3 |
|
public function withSessionId(bool $withSessionId = true): static |
|
29
|
|
|
{ |
|
30
|
3 |
|
$this->withSessionId = $withSessionId; |
|
31
|
|
|
|
|
32
|
3 |
|
return $this; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
5 |
|
public function useResultStatus(?string $resultStatus = null): static |
|
36
|
|
|
{ |
|
37
|
5 |
|
$this->resultStatus = $resultStatus; |
|
38
|
|
|
|
|
39
|
5 |
|
return $this; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
5 |
|
public function url(): string |
|
43
|
|
|
{ |
|
44
|
5 |
|
$query = parse_url($this->url, PHP_URL_QUERY); |
|
45
|
5 |
|
parse_str($query, $query); |
|
|
|
|
|
|
46
|
5 |
|
if ($this->withSessionId) { |
|
47
|
3 |
|
$query[static::$sessionKey] = 'CHECKOUT_SESSION_ID_TO_REPLACE'; |
|
48
|
|
|
} |
|
49
|
5 |
|
if ($this->resultStatus) { |
|
50
|
5 |
|
$query[static::$resultStatusKey] = $this->resultStatus; |
|
51
|
|
|
} |
|
52
|
5 |
|
$newUrl = strtok($this->url, '?') . '?' . http_build_query($query); |
|
53
|
|
|
|
|
54
|
5 |
|
return str_replace('CHECKOUT_SESSION_ID_TO_REPLACE', '{CHECKOUT_SESSION_ID}', $newUrl); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
public static function prepareSuccessUrl(string $url, ?string $resultStatus = null): string |
|
58
|
|
|
{ |
|
59
|
2 |
|
return static::make($url)->withSessionId() |
|
60
|
2 |
|
->useResultStatus($resultStatus ?? static::$statusSuccess) |
|
61
|
2 |
|
->url(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
public static function prepareCancelUrl(string $url, ?string $resultStatus = null): string |
|
65
|
|
|
{ |
|
66
|
2 |
|
return static::make($url) |
|
67
|
2 |
|
->useResultStatus($resultStatus ?? static::$statusCanceled) |
|
68
|
2 |
|
->url(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
public static function prepareUrls(string $successUrl, ?string $cancelUrl = null): array |
|
72
|
|
|
{ |
|
73
|
1 |
|
$cancelUrl = $cancelUrl ?? $successUrl; |
|
74
|
|
|
|
|
75
|
1 |
|
return [ |
|
76
|
1 |
|
'success_url' => static::prepareSuccessUrl($successUrl), |
|
77
|
1 |
|
'cancel_url' => static::prepareCancelUrl($cancelUrl), |
|
78
|
1 |
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|