diff --git a/app/Http/Controllers/PublicAccess/AmazonReferralController.php b/app/Http/Controllers/PublicAccess/AmazonReferralController.php new file mode 100644 index 0000000..1f3a09c --- /dev/null +++ b/app/Http/Controllers/PublicAccess/AmazonReferralController.php @@ -0,0 +1,96 @@ + $this->associateTag(), + 'defaultQuery' => trim((string) $request->query('q', '')), + 'defaultAsin' => trim((string) $request->query('asin', '')), + 'defaultUrl' => trim((string) $request->query('url', '')), + ]); + } + + public function go(Request $request): RedirectResponse + { + $asin = $this->normalizeAsin((string) $request->query('asin', '')); + if ($asin !== null) { + return redirect()->away($this->appendAssociateTag('https://www.amazon.it/dp/' . rawurlencode($asin))); + } + + $url = trim((string) $request->query('url', '')); + if ($url !== '' && filter_var($url, FILTER_VALIDATE_URL)) { + return redirect()->away($this->appendAssociateTag($this->normalizeAmazonUrl($url))); + } + + $query = trim((string) $request->query('q', '')); + if ($query !== '') { + return redirect()->away($this->appendAssociateTag('https://www.amazon.it/s?k=' . rawurlencode($query))); + } + + return redirect()->route('public.amazon.index'); + } + + private function associateTag(): string + { + $tag = trim((string) config('services.amazon.referral_tag', '')); + + if ($tag === '') { + $tag = trim((string) config('catalog.amazon.associate_tag', 'netgescon-21')); + } + + return $tag !== '' ? $tag : 'netgescon-21'; + } + + private function appendAssociateTag(string $url): string + { + $parts = parse_url($url); + if (! is_array($parts)) { + return $url; + } + + $query = []; + parse_str((string) ($parts['query'] ?? ''), $query); + $query['tag'] = $this->associateTag(); + + $base = ($parts['scheme'] ?? 'https') . '://' . ($parts['host'] ?? 'www.amazon.it') . ($parts['path'] ?? ''); + + return $base . '?' . http_build_query($query); + } + + private function normalizeAmazonUrl(string $url): string + { + $asin = $this->extractAsinFromUrl($url); + + if ($asin !== null) { + return 'https://www.amazon.it/dp/' . rawurlencode($asin); + } + + return $url; + } + + private function extractAsinFromUrl(string $url): ?string + { + if (preg_match('~/(?:dp|gp/product)/([A-Z0-9]{10})(?:[/?]|$)~i', $url, $matches)) { + return strtoupper($matches[1]); + } + + return null; + } + + private function normalizeAsin(string $value): ?string + { + $value = strtoupper(trim($value)); + $value = preg_replace('/[^A-Z0-9]/', '', $value) ?? ''; + + return preg_match('/^[A-Z0-9]{10}$/', $value) ? $value : null; + } +} \ No newline at end of file diff --git a/resources/views/public/amazon-referral.blade.php b/resources/views/public/amazon-referral.blade.php new file mode 100644 index 0000000..6a1b205 --- /dev/null +++ b/resources/views/public/amazon-referral.blade.php @@ -0,0 +1,242 @@ + + +
+ + +Questa pagina pubblica serve ad aprire Amazon con il tag corretto. Puoi cercare un prodotto, incollare un ASIN oppure ripulire un link Amazon già esistente aggiungendo automaticamente il tag.
+In qualità di Affiliato Amazon io ricevo un guadagno dagli acquisti idonei.
+Puoi già distribuire anche un link diretto, ad esempio per il prodotto che stavamo testando.
+ Apri B0DN6PD8QS con tag NetGescon +