|
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\Response; |
|
23
|
|
|
|
|
24
|
|
|
use Obol\Model\Enum\ChargeFailureReasons; |
|
25
|
|
|
use Parthenon\Billing\Entity\Subscription; |
|
26
|
|
|
use Symfony\Component\Validator\ConstraintViolationInterface; |
|
27
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface; |
|
28
|
|
|
|
|
29
|
|
|
class StartSubscriptionResponse |
|
30
|
|
|
{ |
|
31
|
|
|
public const CODE_REQUEST_INVALID = '320000'; |
|
32
|
|
|
public const CODE_NO_BILLING_DETAILS = '320001'; |
|
33
|
|
|
public const CODE_UNSUPPORTED_PAYMENT_PROVIDER = '320002'; |
|
34
|
|
|
public const CODE_PLAN_NOT_FOUND = '320003'; |
|
35
|
|
|
public const CODE_PLAN_PRICE_NOT_FOUND = '320004'; |
|
36
|
|
|
public const CODE_GENERAL_ERROR = '320005'; |
|
37
|
|
|
public const CODE_PAYMENT_FAILURE_ERROR = '320006'; |
|
38
|
|
|
public const CODE_NO_PAYMENT_DETAILS_ERROR = '320007'; |
|
39
|
|
|
|
|
40
|
|
|
public static function createGeneralError(): array |
|
41
|
|
|
{ |
|
42
|
|
|
return [ |
|
43
|
|
|
'success' => false, |
|
44
|
|
|
'code' => static::CODE_GENERAL_ERROR, |
|
45
|
|
|
]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public static function createInvalidRequestResponse(ConstraintViolationListInterface $errors): array |
|
49
|
|
|
{ |
|
50
|
|
|
$errorOutput = []; |
|
51
|
|
|
|
|
52
|
|
|
/** @var ConstraintViolationInterface $error */ |
|
53
|
|
|
foreach ($errors as $error) { |
|
54
|
|
|
$errorOutput[$error->getPropertyPath()] = $error->getMessage(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return [ |
|
58
|
|
|
'success' => false, |
|
59
|
|
|
'code' => static::CODE_REQUEST_INVALID, |
|
60
|
|
|
'errors' => $errorOutput, |
|
61
|
|
|
]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public static function createNoBillingDetails(): array |
|
65
|
|
|
{ |
|
66
|
|
|
return [ |
|
67
|
|
|
'success' => false, |
|
68
|
|
|
'code' => static::CODE_NO_BILLING_DETAILS, |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public static function createUnsupportedPaymentProvider(): array |
|
73
|
|
|
{ |
|
74
|
|
|
return [ |
|
75
|
|
|
'success' => false, |
|
76
|
|
|
'code' => static::CODE_UNSUPPORTED_PAYMENT_PROVIDER, |
|
77
|
|
|
]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public static function createPlanNotFound(): array |
|
81
|
|
|
{ |
|
82
|
|
|
return [ |
|
83
|
|
|
'success' => false, |
|
84
|
|
|
'code' => static::CODE_PLAN_NOT_FOUND, |
|
85
|
|
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public static function createPlanPriceNotFound(): array |
|
89
|
|
|
{ |
|
90
|
|
|
return [ |
|
91
|
|
|
'success' => false, |
|
92
|
|
|
'code' => static::CODE_PLAN_PRICE_NOT_FOUND, |
|
93
|
|
|
]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public static function createPaymentFailed(ChargeFailureReasons $chargeFailureReason): array |
|
97
|
|
|
{ |
|
98
|
|
|
return [ |
|
99
|
|
|
'success' => false, |
|
100
|
|
|
'code' => static::CODE_PAYMENT_FAILURE_ERROR, |
|
101
|
|
|
'reason' => $chargeFailureReason->value, |
|
102
|
|
|
]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public static function createNoPaymentDetails(): array |
|
106
|
|
|
{ |
|
107
|
|
|
return [ |
|
108
|
|
|
'success' => false, |
|
109
|
|
|
'code' => static::CODE_NO_BILLING_DETAILS, |
|
110
|
|
|
]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public static function createSuccessResponse(Subscription $subscription): array |
|
114
|
|
|
{ |
|
115
|
|
|
return [ |
|
116
|
|
|
'success' => true, |
|
117
|
|
|
'subscription' => [ |
|
118
|
|
|
'id' => $subscription->getId(), |
|
119
|
|
|
], |
|
120
|
|
|
]; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|