'#F6F5F2', 'surface' => '#FFFFFF', 'foreground' => '#1F1F1F', 'muted' => '#6B6B6B', 'primary' => '#1F1F1F', 'primaryForeground' => '#FFFFFF', 'accent' => '#C9A227', 'border' => '#E4E1DA', ]; private const array SERIF_FONTS = ['playfair-display', 'lora', 'fraunces', 'cormorant-garamond', 'dm-serif-display', 'libre-baskerville']; /** * @param array $colors */ private function __construct( public Store $store, public string $name, public array $colors, public string $headingFont, public string $bodyFont, /** Corner rounding for cards, in pixels. */ public int $radius, /** Corner rounding for buttons, in pixels. */ public int $buttonRadius, public bool $malay, ) {} public static function for(Store $store): self { // What shoppers see: the published design, or the draft while the store is not live yet. $plan = $store->published_plan ?? $store->plan ?? []; $theme = $plan['theme'] ?? []; $colors = self::DEFAULT_COLORS; foreach (PlanSchema::COLORS as $key) { $value = $theme['colors'][$key] ?? null; if (is_string($value) && preg_match('/^#[0-9A-Fa-f]{6}$/', $value) === 1) { $colors[$key] = $value; } } $radius = match ($theme['radius'] ?? 'medium') { 'none' => 0, 'small' => 4, 'large' => 16, 'full' => 20, default => 8, }; return new self( store: $store, name: self::logoText($plan) ?? $store->name, colors: $colors, headingFont: self::fontStack($theme['fonts']['heading'] ?? null), bodyFont: self::fontStack($theme['fonts']['body'] ?? null), radius: $radius, buttonRadius: ($theme['radius'] ?? null) === 'full' ? 999 : $radius, malay: $store->language->value === 'ms', ); } /** * The uploaded logo's full web address, for the top of the email. Designed and AI-drawn logos are not * pictures that email apps can show, so those emails show the store's name instead. * * @return array{url: string, width: int, height: int}|null */ public function logo(): ?array { $logo = $this->store->logo; if (($logo['type'] ?? null) !== 'upload') { return null; } $url = Storage::disk('public')->url($logo['path']); // Shown at most 48 pixels high and 220 wide, keeping its shape. $scale = min(48 / max(1, $logo['height']), 220 / max(1, $logo['width']), 1); return [ 'url' => str_starts_with($url, 'http') ? $url : url($url), 'width' => (int) round($logo['width'] * $scale), 'height' => (int) round($logo['height'] * $scale), ]; } /** * The email address shoppers can write to, when the merchant set one. */ public function contactEmail(): ?string { return $this->store->contact_email; } public function color(string $key): string { return $this->colors[$key]; } /** * The shopper's order page. A live store's page is on the store's own address; a store that is not * published yet only has the merchant's preview, so the link opens that. */ public function orderUrl(Order $order): string { if (! $this->store->isPublished()) { return route('stores.preview.order', [$this->store->slug, $order->public_id]); } return $this->storeUrl().'/orders/'.$order->public_id; } /** * The store's own home page. */ public function storeUrl(): string { if (! $this->store->isPublished()) { return route('stores.preview', $this->store->slug); } // Built from APP_URL, because emails are also sent from the webhook, where there is no shopper's visit. $app = parse_url((string) config('app.url')); $scheme = $app['scheme'] ?? 'https'; $port = isset($app['port']) ? ':'.$app['port'] : ''; return $scheme.'://'.$this->store->slug.'.'.config('storefront.domain').$port; } /** * A WhatsApp chat with the store about this order, or null when the store has no WhatsApp number. */ public function whatsappUrl(Order $order): ?string { if ($this->store->whatsapp_number === null || $this->store->whatsapp_number === '') { return null; } $text = $this->malay ? "Hai {$this->store->name}, saya ada soalan tentang pesanan saya #{$order->number}." : "Hi {$this->store->name}, I have a question about my order #{$order->number}."; return MalaysianPhone::whatsappLink($this->store->whatsapp_number, $text); } public function money(int $sen): string { return 'RM '.number_format($sen / 100, 2); } /** * The words shown in the header, from the store's own header section when it has one. * * @param array $plan */ private static function logoText(array $plan): ?string { // Version 2 plans keep one shared header; older ones have it among the sections. $header = $plan['header'] ?? collect($plan['sections'] ?? [])->firstWhere('type', 'header'); $text = trim((string) ($header['content']['logoText'] ?? '')); return $text !== '' ? $text : null; } private static function fontStack(?string $key): string { $family = PlanSchema::FONTS[$key] ?? null; $fallback = in_array($key, self::SERIF_FONTS, true) ? "Georgia, 'Times New Roman', serif" : 'Helvetica, Arial, sans-serif'; return $family !== null ? "'{$family}', {$fallback}" : $fallback; } }