| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Services\Payment; |
| 4 |
|
| 5 |
use IvyForms\Repository\EntryField\EntryFieldNameEmailRepositoryInterface; |
| 6 |
|
| 7 |
/** |
| 8 |
* Fills empty Payments customer cells from the linked entry's mapped Name/Email fields. |
| 9 |
*/ |
| 10 |
class PaymentListCustomerName |
| 11 |
{ |
| 12 |
private EntryFieldNameEmailRepositoryInterface $entryFieldNameEmailRepository; |
| 13 |
|
| 14 |
public function __construct(EntryFieldNameEmailRepositoryInterface $entryFieldNameEmailRepository) |
| 15 |
{ |
| 16 |
$this->entryFieldNameEmailRepository = $entryFieldNameEmailRepository; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* @param array<int, array<string, mixed>> $rows |
| 21 |
* @return array<int, array<string, mixed>> |
| 22 |
*/ |
| 23 |
public function attach(array $rows): array |
| 24 |
{ |
| 25 |
$neededEntryIds = []; |
| 26 |
foreach ($rows as $index => $row) { |
| 27 |
if (trim((string) ($row['customerName'] ?? '')) !== '' || (int) ($row['entryId'] ?? 0) <= 0) { |
| 28 |
unset($rows[$index]['formIntegrationSettings']); |
| 29 |
continue; |
| 30 |
} |
| 31 |
$neededEntryIds[(int) $row['entryId']][] = $index; |
| 32 |
} |
| 33 |
if ($neededEntryIds === []) { |
| 34 |
return $rows; |
| 35 |
} |
| 36 |
|
| 37 |
$valuesByEntry = $this->entryFieldNameEmailRepository->findNameEmailValuesByEntryIds( |
| 38 |
array_keys($neededEntryIds) |
| 39 |
); |
| 40 |
foreach ($neededEntryIds as $entryId => $indexes) { |
| 41 |
foreach ($indexes as $index) { |
| 42 |
$picked = $this->pick( |
| 43 |
$this->gatewaySettingsFromRow($rows[$index]), |
| 44 |
$valuesByEntry[$entryId] ?? [] |
| 45 |
); |
| 46 |
$rows[$index]['customerName'] = $picked !== '' ? $picked : null; |
| 47 |
unset($rows[$index]['formIntegrationSettings']); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return array_values($rows); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @param array<string, mixed> $row |
| 56 |
* @return array<string, mixed> |
| 57 |
*/ |
| 58 |
private function gatewaySettingsFromRow(array $row): array |
| 59 |
{ |
| 60 |
$all = $this->decodeIntegrationSettings($row['formIntegrationSettings'] ?? null); |
| 61 |
$method = (string) ($row['paymentMethod'] ?? ''); |
| 62 |
if ($method !== '' && is_array($all[$method] ?? null)) { |
| 63 |
return $all[$method]; |
| 64 |
} |
| 65 |
|
| 66 |
return []; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @param array<string, mixed> $gatewaySettings |
| 71 |
* @param array<string, string> $entryValues |
| 72 |
*/ |
| 73 |
private function pick(array $gatewaySettings, array $entryValues): string |
| 74 |
{ |
| 75 |
$fromName = $this->mappedOrPrefixed( |
| 76 |
$entryValues, |
| 77 |
(string) ($gatewaySettings['billingNameField'] ?? ''), |
| 78 |
'name_' |
| 79 |
); |
| 80 |
if ($fromName !== '') { |
| 81 |
return $fromName; |
| 82 |
} |
| 83 |
|
| 84 |
return $this->mappedOrPrefixed( |
| 85 |
$entryValues, |
| 86 |
(string) ($gatewaySettings['buyerEmailField'] ?? ''), |
| 87 |
'email_' |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @param array<string, string> $values |
| 93 |
*/ |
| 94 |
private function mappedOrPrefixed(array $values, string $mappedKey, string $prefix): string |
| 95 |
{ |
| 96 |
if ($mappedKey === '' || empty($values[$mappedKey])) { |
| 97 |
return ''; |
| 98 |
} |
| 99 |
|
| 100 |
return $this->sanitizeByPrefix($prefix, $values[$mappedKey]); |
| 101 |
} |
| 102 |
|
| 103 |
private function sanitizeByPrefix(string $prefix, string $value): string |
| 104 |
{ |
| 105 |
return $prefix === 'email_' ? sanitize_email($value) : sanitize_text_field($value); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @param mixed $raw |
| 110 |
* @return array<string, mixed> |
| 111 |
*/ |
| 112 |
private function decodeIntegrationSettings($raw): array |
| 113 |
{ |
| 114 |
$decoded = []; |
| 115 |
if (is_array($raw)) { |
| 116 |
$decoded = $raw; |
| 117 |
} |
| 118 |
if (is_string($raw) && $raw !== '') { |
| 119 |
$decoded = json_decode($raw, true); |
| 120 |
} |
| 121 |
if (!is_array($decoded)) { |
| 122 |
return []; |
| 123 |
} |
| 124 |
if (isset($decoded['integrationSettings']) && is_array($decoded['integrationSettings'])) { |
| 125 |
return $decoded['integrationSettings']; |
| 126 |
} |
| 127 |
|
| 128 |
return $decoded; |
| 129 |
} |
| 130 |
} |
| 131 |
|