|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Copyright (C) 2020-2024 Iain Cambridge |
|
7
|
|
|
* |
|
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU General Public License as published by |
|
10
|
|
|
* the Free Software Foundation, either version 3 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 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 App\Parthenon\Billing\BillaBear\Subscription; |
|
23
|
|
|
|
|
24
|
|
|
use BillaBear\ApiException; |
|
|
|
|
|
|
25
|
|
|
use BillaBear\Model\SubscriptionStartBody; |
|
|
|
|
|
|
26
|
|
|
use Obol\Model\Enum\ChargeFailureReasons; |
|
27
|
|
|
use Parthenon\Billing\BillaBear\SdkFactory; |
|
28
|
|
|
use Parthenon\Billing\Dto\StartSubscriptionDto; |
|
29
|
|
|
use Parthenon\Billing\Entity\CustomerInterface; |
|
30
|
|
|
use Parthenon\Billing\Entity\PaymentCard; |
|
31
|
|
|
use Parthenon\Billing\Entity\Price; |
|
32
|
|
|
use Parthenon\Billing\Entity\Subscription; |
|
33
|
|
|
use Parthenon\Billing\Entity\SubscriptionPlan; |
|
34
|
|
|
use Parthenon\Billing\Enum\BillingChangeTiming; |
|
35
|
|
|
use Parthenon\Billing\Event\SubscriptionCreated; |
|
36
|
|
|
use Parthenon\Billing\Exception\NoPaymentDetailsException; |
|
37
|
|
|
use Parthenon\Billing\Exception\PaymentFailureException; |
|
38
|
|
|
use Parthenon\Billing\Plan\Plan; |
|
39
|
|
|
use Parthenon\Billing\Plan\PlanManagerInterface; |
|
40
|
|
|
use Parthenon\Billing\Plan\PlanPrice; |
|
41
|
|
|
use Parthenon\Billing\Subscription\SubscriptionManagerInterface; |
|
42
|
|
|
use Parthenon\Common\Exception\GeneralException; |
|
43
|
|
|
use Parthenon\Common\LoggerAwareTrait; |
|
44
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
45
|
|
|
|
|
46
|
|
|
class SubscriptionManager implements SubscriptionManagerInterface |
|
47
|
|
|
{ |
|
48
|
|
|
use LoggerAwareTrait; |
|
49
|
|
|
|
|
50
|
|
|
public function __construct( |
|
51
|
|
|
private SdkFactory $sdkFactory, |
|
52
|
|
|
private PlanManagerInterface $planManager, |
|
53
|
|
|
private EventDispatcherInterface $dispatcher, |
|
54
|
|
|
) { |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function startSubscription( |
|
58
|
|
|
CustomerInterface $customer, |
|
59
|
|
|
SubscriptionPlan|Plan $plan, |
|
60
|
|
|
Price|PlanPrice $planPrice, |
|
61
|
|
|
?PaymentCard $paymentDetails = null, |
|
62
|
|
|
int $seatNumbers = 1, |
|
63
|
|
|
?bool $hasTrial = null, |
|
64
|
|
|
?int $trialLengthDays = 0, |
|
65
|
|
|
): Subscription { |
|
66
|
|
|
if (!$plan instanceof Plan) { |
|
67
|
|
|
throw new GeneralException('Invalid type of plan given'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$customerId = $customer->getExternalCustomerReference(); |
|
71
|
|
|
$payload = [ |
|
72
|
|
|
'subscription_plan' => $plan->getEntityId(), |
|
73
|
|
|
'price' => $planPrice->getEntityId(), |
|
|
|
|
|
|
74
|
|
|
'seat_numbers' => $seatNumbers, |
|
75
|
|
|
]; |
|
76
|
|
|
|
|
77
|
|
|
if ($paymentDetails) { |
|
78
|
|
|
$payload['payment_details'] = $paymentDetails->getId(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$subscriptionStart = new SubscriptionStartBody($payload); |
|
82
|
|
|
|
|
83
|
|
|
try { |
|
84
|
|
|
$response = $this->sdkFactory->createSubscriptionsApi()->customerStartSubscription($subscriptionStart, $customerId); |
|
85
|
|
|
} catch (ApiException $apiException) { |
|
86
|
|
|
if (402 === $apiException->getCode()) { |
|
87
|
|
|
$body = $apiException->getResponseBody(); |
|
88
|
|
|
$json = json_decode($body, true); |
|
89
|
|
|
throw new PaymentFailureException(ChargeFailureReasons::from($json['reason']), previous: $apiException); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (406 === $apiException->getCode()) { |
|
93
|
|
|
throw new NoPaymentDetailsException('No payment details', previous: $apiException); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
throw new GeneralException(previous: $apiException); |
|
97
|
|
|
} catch (\Throwable $exception) { |
|
98
|
|
|
new GeneralException($exception->getMessage(), previous: $exception); |
|
99
|
|
|
} |
|
100
|
|
|
$subscription = new Subscription(); |
|
101
|
|
|
$subscription->setCustomer($customer); |
|
102
|
|
|
$subscription->setId($response->getId()); |
|
103
|
|
|
$subscription->setValidUntil(new \DateTime($response->getValidUntil())); |
|
104
|
|
|
$subscription->setMainExternalReference($response->getMainExternalReference()); |
|
105
|
|
|
$subscription->setChildExternalReference($response->getChildExternalReference()); |
|
106
|
|
|
|
|
107
|
|
|
$this->dispatcher->dispatch(new SubscriptionCreated($subscription), SubscriptionCreated::NAME); |
|
108
|
|
|
|
|
109
|
|
|
return $subscription; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function startSubscriptionWithDto(CustomerInterface $customer, StartSubscriptionDto $startSubscriptionDto): Subscription |
|
113
|
|
|
{ |
|
114
|
|
|
$plan = $this->planManager->getPlanByName($startSubscriptionDto->getPlanName()); |
|
115
|
|
|
$planPrice = $plan->getPriceForPaymentSchedule($startSubscriptionDto->getSchedule(), $startSubscriptionDto->getCurrency()); |
|
116
|
|
|
|
|
117
|
|
|
return $this->startSubscription($customer, $plan, $planPrice, null, $startSubscriptionDto->getSeatNumbers()); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function cancelSubscriptionAtEndOfCurrentPeriod(Subscription $subscription): Subscription |
|
121
|
|
|
{ |
|
122
|
|
|
// TODO: Implement cancelSubscriptionAtEndOfCurrentPeriod() method. |
|
123
|
|
|
} |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
public function cancelSubscriptionInstantly(Subscription $subscription): Subscription |
|
126
|
|
|
{ |
|
127
|
|
|
// TODO: Implement cancelSubscriptionInstantly() method. |
|
128
|
|
|
} |
|
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
public function cancelSubscriptionOnDate(Subscription $subscription, \DateTimeInterface $dateTime): Subscription |
|
131
|
|
|
{ |
|
132
|
|
|
// TODO: Implement cancelSubscriptionOnDate() method. |
|
133
|
|
|
} |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
public function changeSubscriptionPrice(Subscription $subscription, Price $price, BillingChangeTiming $billingChangeTiming): void |
|
136
|
|
|
{ |
|
137
|
|
|
// TODO: Implement changeSubscriptionPrice() method. |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function changeSubscriptionPlan(Subscription $subscription, SubscriptionPlan $plan, Price $price, BillingChangeTiming $billingChangeTiming): void |
|
141
|
|
|
{ |
|
142
|
|
|
// TODO: Implement changeSubscriptionPlan() method. |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths