1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* Copyright (C) 2020-2025 Iain Cambridge |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by |
10
|
|
|
* the Free Software Foundation, either version 2.1 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Lesser General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace Parthenon\Payments\Stripe; |
23
|
|
|
|
24
|
|
|
use Parthenon\Common\Exception\GeneralException; |
25
|
|
|
use Parthenon\Payments\Checkout; |
26
|
|
|
use Parthenon\Payments\CheckoutInterface; |
27
|
|
|
use Parthenon\Payments\CheckoutManagerInterface; |
28
|
|
|
use Parthenon\Payments\Entity\Subscription; |
29
|
|
|
use Parthenon\Payments\Exception\NoCheckoutFoundException; |
30
|
|
|
use Stripe\StripeClient; |
31
|
|
|
|
32
|
|
|
class CheckoutManager implements CheckoutManagerInterface |
33
|
|
|
{ |
34
|
|
|
public function __construct(private Config $config, private StripeClient $stripeClient) |
35
|
|
|
{ |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function createCheckoutForSubscription(Subscription $subscription, array $options = [], int $seats = 1): CheckoutInterface |
39
|
|
|
{ |
40
|
|
|
$lineItems = []; |
41
|
|
|
|
42
|
|
|
$lineItems[] = [ |
43
|
|
|
'description' => $subscription->getPlanName(), |
44
|
|
|
'price' => $subscription->getPriceId(), |
45
|
|
|
'quantity' => $seats, |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
if (!isset($options['payment_method_types'])) { |
49
|
|
|
$options['payment_method_types'] = ['card']; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$options['mode'] = Subscription::PAYMENT_SCHEDULE_LIFETIME === $subscription->getPaymentSchedule() ? 'payment' : 'subscription'; |
53
|
|
|
$options['line_items'] = $lineItems; |
54
|
|
|
$options['success_url'] = $this->config->getSuccessUrl(); |
55
|
|
|
$options['cancel_url'] = $this->config->getCancelUrl(); |
56
|
|
|
$options['allow_promotion_codes'] = true; |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
$session = $this->stripeClient->checkout->sessions->create($options); |
60
|
|
|
} catch (\Exception $e) { |
61
|
|
|
throw new GeneralException($e->getMessage(), $e->getCode(), $e); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$subscription->setCheckoutId($session->id); |
65
|
|
|
|
66
|
|
|
return new Checkout($session->id); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function handleSuccess(Subscription $subscription): void |
70
|
|
|
{ |
71
|
|
|
try { |
72
|
|
|
$stripeSession = $this->stripeClient->checkout->sessions->retrieve($subscription->getCheckoutId()); |
73
|
|
|
} catch (\Exception $e) { |
74
|
|
|
throw new GeneralException($e->getMessage(), $e->getCode(), $e); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (!$stripeSession) { /* @phpstan-ignore-line */ |
|
|
|
|
78
|
|
|
throw new NoCheckoutFoundException(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ('paid' !== $stripeSession->payment_status) { |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$subscription->setStatus(Subscription::STATUS_ACTIVE); |
86
|
|
|
$subscription->setCustomerId($stripeSession->customer->id); |
87
|
|
|
$subscription->increaseValidUntil(); |
88
|
|
|
if (Subscription::PAYMENT_SCHEDULE_LIFETIME !== $subscription->getPaymentSchedule()) { |
89
|
|
|
$subscription->setPaymentId($stripeSession->subscription); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|