|
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\Billing\Obol; |
|
23
|
|
|
|
|
24
|
|
|
use Obol\Model\BillingDetails; |
|
25
|
|
|
use Obol\Model\Subscription; |
|
26
|
|
|
use Parthenon\Billing\Entity\Price; |
|
27
|
|
|
use Parthenon\Billing\Plan\PlanPrice; |
|
28
|
|
|
|
|
29
|
|
|
class SubscriptionFactory implements SubscriptionFactoryInterface |
|
30
|
|
|
{ |
|
31
|
|
|
public function __construct( |
|
32
|
|
|
private BillingDetailsFactoryInterface $billingDetailsFactory, |
|
33
|
|
|
) { |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function createSubscription( |
|
37
|
|
|
BillingDetails $billingDetails, |
|
38
|
|
|
PlanPrice|Price $planPrice, |
|
39
|
|
|
int $seatNumbers, |
|
40
|
|
|
bool $hasTrial = false, |
|
41
|
|
|
int $trialLengthDays = 0, |
|
42
|
|
|
): Subscription { |
|
43
|
|
|
$obolSubscription = new Subscription(); |
|
44
|
|
|
$obolSubscription->setBillingDetails($billingDetails); |
|
45
|
|
|
$obolSubscription->setSeats($seatNumbers); |
|
46
|
|
|
$obolSubscription->setCostPerSeat($planPrice->getAsMoney()); |
|
47
|
|
|
|
|
48
|
|
|
$obolSubscription->setHasTrial($hasTrial); |
|
49
|
|
|
$obolSubscription->setTrialLengthDays($trialLengthDays); |
|
50
|
|
|
|
|
51
|
|
|
if ($planPrice instanceof Price) { |
|
52
|
|
|
$obolSubscription->setPriceId($planPrice->getExternalReference()); |
|
|
|
|
|
|
53
|
|
|
} elseif ($planPrice->hasPriceId()) { |
|
54
|
|
|
$obolSubscription->setPriceId($planPrice->getPriceId()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $obolSubscription; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function createSubscriptionFromEntity(\Parthenon\Billing\Entity\Subscription $subscription, bool $fullyBuilt = true): Subscription |
|
61
|
|
|
{ |
|
62
|
|
|
$obolSubscription = new Subscription(); |
|
63
|
|
|
|
|
64
|
|
|
if ($fullyBuilt) { |
|
65
|
|
|
$paymentDetails = $subscription->getPaymentDetails(); |
|
66
|
|
|
$billingDetails = $this->billingDetailsFactory->createFromCustomerAndPaymentDetails($subscription->getCustomer(), $paymentDetails); |
|
|
|
|
|
|
67
|
|
|
$obolSubscription->setBillingDetails($billingDetails); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$obolSubscription->setId($subscription->getMainExternalReference()); |
|
|
|
|
|
|
71
|
|
|
$obolSubscription->setLineId($subscription->getChildExternalReference()); |
|
72
|
|
|
$obolSubscription->setCostPerSeat($subscription->getMoneyAmount()); |
|
73
|
|
|
$obolSubscription->setSeats($subscription->getSeats()); |
|
|
|
|
|
|
74
|
|
|
$obolSubscription->setHasTrial($subscription->getSubscriptionPlan()?->getHasTrial() ?? false); |
|
75
|
|
|
$obolSubscription->setTrialLengthDays($subscription->getSubscriptionPlan()?->getTrialLengthDays() ?? 0); |
|
76
|
|
|
|
|
77
|
|
|
return $obolSubscription; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|