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 / Shortcode / Helpers / SquareFrontendConfigHelper.php

SquareFrontendConfigHelper.php in The Innovative Form Builder – IvyForms 1.5.1, at backend/src/Services/Shortcode/Helpers/SquareFrontendConfigHelper.php

135 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\Shortcode\Helpers;
4
5 use IvyForms\Plugin\Plugin;
6 use IvyForms\Services\Payment\SquareEnvironment;
7 use IvyForms\Services\Payment\SquareOAuthService;
8 use IvyForms\Services\Payment\SquarePaymentService;
9 use IvyForms\Services\Settings\SettingsService;
10
11 class SquareFrontendConfigHelper
12 {
13 /**
14 * @param array<string, array<string, mixed>> $formList
15 */
16 public static function localizeConfigIfNeeded(string $scriptId, array $formList): void
17 {
18 if (!self::formHasPaymentField($formList)) {
19 return;
20 }
21
22 $settingsService = new SettingsService();
23 $squareSettings = self::readSquareSettings($settingsService);
24 [$locationId, $squareSettings] = self::resolveSquareLocationId($settingsService, $squareSettings);
25 $applicationId = self::resolveSquareApplicationId();
26
27 wp_localize_script(
28 $scriptId,
29 'wpIvySquareConfig',
30 [
31 'applicationId' => $applicationId,
32 'locationId' => $locationId !== '' ? $locationId : ($squareSettings['locationId'] ?? ''),
33 'currency' => strtoupper((string) ($squareSettings['currency'] ?? '')),
34 'connected' => !empty($squareSettings['connected']),
35 'sdkUrl' => SquareEnvironment::getWebPaymentsSdkUrl(),
36 'sandbox' => SquareEnvironment::isSandbox(),
37 ]
38 );
39 }
40
41 /**
42 * @param array<string, array<string, mixed>> $formList
43 */
44 private static function formHasPaymentField(array $formList): bool
45 {
46 foreach ($formList as $formData) {
47 foreach ($formData['fields'] ?? [] as $field) {
48 if (is_array($field) && ($field['type'] ?? '') === 'payment-method') {
49 return true;
50 }
51 }
52 }
53
54 return false;
55 }
56
57 /**
58 * @return array<string, mixed>
59 */
60 private static function readSquareSettings(SettingsService $settingsService): array
61 {
62 $settings = $settingsService->getSetting('payment', 'square');
63
64 return is_array($settings) ? $settings : [];
65 }
66
67 /**
68 * @param array<string, mixed> $squareSettings
69 * @return array{0:string,1:array<string,mixed>}
70 */
71 private static function resolveSquareLocationId(SettingsService $settingsService, array $squareSettings): array
72 {
73 $locationId = (string) ($squareSettings['locationId'] ?? '');
74 if (!self::shouldFetchSquareLocationId($locationId, $squareSettings)) {
75 return [$locationId, $squareSettings];
76 }
77
78 $location = self::fetchPrimarySquareLocation();
79 $locationId = (string) ($location['id'] ?? '');
80 if ($locationId === '') {
81 return ['', $squareSettings];
82 }
83
84 $squareSettings['locationId'] = $locationId;
85 $squareSettings['locationName'] = (string) ($location['name'] ?? ($squareSettings['locationName'] ?? ''));
86 $squareSettings['currency'] = (string) ($location['currency'] ?? ($squareSettings['currency'] ?? 'USD'));
87 $settingsService->setSetting('payment', 'square', $squareSettings);
88
89 return [$locationId, $squareSettings];
90 }
91
92 /**
93 * @param array<string, mixed> $squareSettings
94 */
95 private static function shouldFetchSquareLocationId(string $locationId, array $squareSettings): bool
96 {
97 return $locationId === '' && !empty($squareSettings['connected']);
98 }
99
100 /**
101 * @return array<string, mixed>
102 */
103 private static function fetchPrimarySquareLocation(): array
104 {
105 $plugin = Plugin::getInstance();
106 if ($plugin->container === null) {
107 return [];
108 }
109
110 try {
111 $squarePaymentService = $plugin->container->get(SquarePaymentService::class);
112 $location = $squarePaymentService->getPrimaryLocationFromApi();
113
114 return is_array($location) ? $location : [];
115 } catch (\Throwable $e) {
116 return [];
117 }
118 }
119
120 private static function resolveSquareApplicationId(): string
121 {
122 $plugin = Plugin::getInstance();
123 if ($plugin->container !== null) {
124 try {
125 $squareOAuthService = $plugin->container->get(SquareOAuthService::class);
126 return $squareOAuthService->getPublicApplicationId();
127 } catch (\Throwable $e) {
128 // Fall through to constant fallback.
129 }
130 }
131
132 return defined('IVYFORMS_SQUARE_APP_ID') ? (string) IVYFORMS_SQUARE_APP_ID : '';
133 }
134 }
135