diff --git a/src/Controller/PaymentController.php b/src/Controller/PaymentController.php index fe666f5d..11138a39 100644 --- a/src/Controller/PaymentController.php +++ b/src/Controller/PaymentController.php @@ -60,6 +60,7 @@ public function render() $vaultedPaymentTokens = $vaultingService->getVaultPaymentTokens($paypalCustomerId)["payment_tokens"]; if ($vaultedPaymentTokens) { $vaultedPaymentSources = []; + $uniquePaypalVaultedPaymentSources = []; foreach ($vaultedPaymentTokens as $vaultedPaymentToken) { foreach ($vaultedPaymentToken["payment_source"] as $paymentType => $paymentSource) { if ($paymentType === "card" && $moduleSettings->isVaultingAllowedForACDC()) { @@ -68,8 +69,21 @@ public function render() $string . $paymentSource["last_digits"]; } elseif ($paymentType === "paypal" && $moduleSettings->isVaultingAllowedForPayPal()) { $string = $lang->translateString("OSC_PAYPAL_CARD_PAYPAL_PAYMENT"); + + $email = $paymentSource["email_address"]; + $payer_id = $paymentSource["payer_id"]; + + if(!isset($uniquePaypalVaultedPaymentSources[$email])) { + $uniquePaypalVaultedPaymentSources[$email] = []; + } + + if( in_array($payer_id, $uniquePaypalVaultedPaymentSources[$email])) { + continue; + } + + $uniquePaypalVaultedPaymentSources[$email][] = $payer_id; $vaultedPaymentSources[$paymentType][] = - $string . " " . $paymentSource["email_address"]; + $string . " " . $email; } } } diff --git a/src/Core/Api/VaultingService.php b/src/Core/Api/VaultingService.php index ff506e75..5847ba40 100644 --- a/src/Core/Api/VaultingService.php +++ b/src/Core/Api/VaultingService.php @@ -247,6 +247,30 @@ public function getVaultPaymentTokens(string $paypalCustomerId): array ->log('error', __CLASS__ . ' ' . __FUNCTION__ . ' : ' . $e->getMessage()); $result = []; } + $moduleSettings = $this->getServiceFromContainer(ModuleSettings::class); + $vaultedPaymentTokens = $result['payment_tokens']; + $filteredVaultedPaymentTokens = []; + $uniquePaypalVaultedPaymentSources = []; + foreach ($vaultedPaymentTokens as $vaultedPaymentToken) { + foreach ($vaultedPaymentToken["payment_source"] as $paymentType => $paymentSource) { + if ($paymentType === 'paypal' && $moduleSettings->isVaultingAllowedForPayPal()) { + $email = $paymentSource["email_address"]; + $payer_id = $paymentSource["payer_id"]; + + if (!isset($uniquePaypalVaultedPaymentSources[$email])) { + $uniquePaypalVaultedPaymentSources[$email] = []; + } + + if (in_array($payer_id, $uniquePaypalVaultedPaymentSources[$email])) { + continue; + } + + $uniquePaypalVaultedPaymentSources[$email][] = $payer_id; + } + $filteredVaultedPaymentTokens[] = $vaultedPaymentToken; + } + } + $result['payment_tokens'] = $filteredVaultedPaymentTokens; return is_array($result) ? $result : []; }