PluginProbe
The Innovative Form Builder – IvyForms / 1.5.1
The Innovative Form Builder – IvyForms v1.5.1
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Services / Payment / PaymentMethodFieldConfig.php

PaymentMethodFieldConfig.php in The Innovative Form Builder – IvyForms 1.5.1, at backend/src/Services/Payment/PaymentMethodFieldConfig.php

160 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Services\Payment;
4
5 /**
6 * Parse payment-method field htmlContent JSON (Square | Stripe | PayPal).
7 */
8 final class PaymentMethodFieldConfig
9 {
10 /**
11 * @return list<string>
12 */
13 public static function resolvePaymentMethods(object $paymentField): array
14 {
15 $raw = self::resolveRawConfig($paymentField);
16 if ($raw === '') {
17 return ['square'];
18 }
19
20 $decoded = json_decode($raw, true);
21 if (!is_array($decoded)) {
22 return ['square'];
23 }
24
25 $configured = $decoded['paymentMethods'] ?? null;
26 if (!is_array($configured)) {
27 return ['square'];
28 }
29
30 return self::normalizeMethods($configured);
31 }
32
33 private static function resolveRawConfig(object $paymentField): string
34 {
35 if (method_exists($paymentField, 'getFieldGeneralSettings')) {
36 $general = $paymentField->getFieldGeneralSettings();
37 if (is_object($general) && method_exists($general, 'getHtmlContent')) {
38 return (string) $general->getHtmlContent();
39 }
40 }
41
42 if (!method_exists($paymentField, 'toArray')) {
43 return '';
44 }
45
46 $array = $paymentField->toArray();
47
48 return is_array($array) ? (string) ($array['htmlContent'] ?? '') : '';
49 }
50
51 /**
52 * @param array<int, mixed> $configured
53 * @return list<string>
54 */
55 private static function normalizeMethods(array $configured): array
56 {
57 $methods = [];
58 foreach ($configured as $method) {
59 if (!is_string($method)) {
60 continue;
61 }
62
63 $normalized = strtolower(trim($method));
64 if (self::isSupported($normalized) && !in_array($normalized, $methods, true)) {
65 $methods[] = $normalized;
66 }
67 }
68
69 return $methods !== [] ? $methods : ['square'];
70 }
71
72 private static function isSupported(string $method): bool
73 {
74 return $method === 'square' || $method === 'stripe' || $method === 'paypal';
75 }
76
77 /**
78 * Primary gateway for the field (first configured provider).
79 */
80 public static function resolvePrimaryGatewayId(object $paymentField): string
81 {
82 $methods = self::resolvePaymentMethods($paymentField);
83
84 return $methods[0];
85 }
86
87 /**
88 * Provider implied by the submitted token, if that provider is allowed on the field.
89 *
90 * Tokens for providers not in {@see resolvePaymentMethods()} are ignored so a
91 * globally registered gateway cannot take over a field that did not offer it.
92 *
93 * @param array<string, mixed> $rawParams
94 */
95 public static function resolveGatewayIdFromToken(array $rawParams, ?object $paymentField = null): ?string
96 {
97 $tokens = [
98 'stripe' => (string) ($rawParams['paymentMethodId'] ?? '')
99 . (string) ($rawParams['paymentIntentId'] ?? ''),
100 'paypal' => (string) ($rawParams['paypalOrderId'] ?? ''),
101 'square' => (string) ($rawParams['sourceId'] ?? ''),
102 ];
103 $allowed = $paymentField !== null ? self::resolvePaymentMethods($paymentField) : null;
104
105 foreach ($tokens as $gatewayId => $token) {
106 if ($token === '') {
107 continue;
108 }
109
110 if ($allowed !== null && !in_array($gatewayId, $allowed, true)) {
111 continue;
112 }
113
114 return $gatewayId;
115 }
116
117 return null;
118 }
119
120 public static function isRequired(object $paymentField): bool
121 {
122 $fromSettings = self::requiredFromGeneralSettings($paymentField);
123 if ($fromSettings !== null) {
124 return $fromSettings;
125 }
126
127 $fromArray = self::requiredFromArray($paymentField);
128
129 return $fromArray ?? true;
130 }
131
132 private static function requiredFromGeneralSettings(object $paymentField): ?bool
133 {
134 if (!method_exists($paymentField, 'getFieldGeneralSettings')) {
135 return null;
136 }
137
138 $general = $paymentField->getFieldGeneralSettings();
139 if (!is_object($general) || !method_exists($general, 'isRequired')) {
140 return null;
141 }
142
143 return $general->isRequired();
144 }
145
146 private static function requiredFromArray(object $paymentField): ?bool
147 {
148 if (!method_exists($paymentField, 'toArray')) {
149 return null;
150 }
151
152 $array = $paymentField->toArray();
153 if (!is_array($array) || !array_key_exists('required', $array)) {
154 return null;
155 }
156
157 return (bool) $array['required'];
158 }
159 }
160