AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StoreBrand.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace App\Mail;
  3. use App\Models\Order;
  4. use App\Models\Store;
  5. use App\StorePlan\PlanSchema;
  6. use App\Support\MalaysianPhone;
  7. /**
  8. * How a store looks in the emails its shoppers get: its name, colours, fonts and corners,
  9. * taken from the design shoppers see. Plus the links the emails need.
  10. *
  11. * Emails cannot load the store's web fonts reliably, so each font comes with a safe fallback
  12. * of the same kind (serif or sans-serif).
  13. */
  14. final readonly class StoreBrand
  15. {
  16. /** Used when a store has no design yet. Calm and neutral. */
  17. private const array DEFAULT_COLORS = [
  18. 'background' => '#F6F5F2',
  19. 'surface' => '#FFFFFF',
  20. 'foreground' => '#1F1F1F',
  21. 'muted' => '#6B6B6B',
  22. 'primary' => '#1F1F1F',
  23. 'primaryForeground' => '#FFFFFF',
  24. 'accent' => '#C9A227',
  25. 'border' => '#E4E1DA',
  26. ];
  27. private const array SERIF_FONTS = ['playfair-display', 'lora', 'fraunces', 'cormorant-garamond', 'dm-serif-display', 'libre-baskerville'];
  28. /**
  29. * @param array<string, string> $colors
  30. */
  31. private function __construct(
  32. public Store $store,
  33. public string $name,
  34. public array $colors,
  35. public string $headingFont,
  36. public string $bodyFont,
  37. /** Corner rounding for cards, in pixels. */
  38. public int $radius,
  39. /** Corner rounding for buttons, in pixels. */
  40. public int $buttonRadius,
  41. public bool $malay,
  42. ) {}
  43. public static function for(Store $store): self
  44. {
  45. // What shoppers see: the published design, or the draft while the store is not live yet.
  46. $plan = $store->published_plan ?? $store->plan ?? [];
  47. $theme = $plan['theme'] ?? [];
  48. $colors = self::DEFAULT_COLORS;
  49. foreach (PlanSchema::COLORS as $key) {
  50. $value = $theme['colors'][$key] ?? null;
  51. if (is_string($value) && preg_match('/^#[0-9A-Fa-f]{6}$/', $value) === 1) {
  52. $colors[$key] = $value;
  53. }
  54. }
  55. $radius = match ($theme['radius'] ?? 'medium') {
  56. 'none' => 0,
  57. 'small' => 4,
  58. 'large' => 16,
  59. 'full' => 20,
  60. default => 8,
  61. };
  62. return new self(
  63. store: $store,
  64. name: self::logoText($plan) ?? $store->name,
  65. colors: $colors,
  66. headingFont: self::fontStack($theme['fonts']['heading'] ?? null),
  67. bodyFont: self::fontStack($theme['fonts']['body'] ?? null),
  68. radius: $radius,
  69. buttonRadius: ($theme['radius'] ?? null) === 'full' ? 999 : $radius,
  70. malay: $store->language->value === 'ms',
  71. );
  72. }
  73. /**
  74. * The email address shoppers can write to, when the merchant set one.
  75. */
  76. public function contactEmail(): ?string
  77. {
  78. return $this->store->contact_email;
  79. }
  80. public function color(string $key): string
  81. {
  82. return $this->colors[$key];
  83. }
  84. /**
  85. * The shopper's order page. A live store's page is on the store's own address; a store that is not
  86. * published yet only has the merchant's preview, so the link opens that.
  87. */
  88. public function orderUrl(Order $order): string
  89. {
  90. if (! $this->store->isPublished()) {
  91. return route('stores.preview.order', [$this->store->slug, $order->public_id]);
  92. }
  93. return $this->storeUrl().'/orders/'.$order->public_id;
  94. }
  95. /**
  96. * The store's own home page.
  97. */
  98. public function storeUrl(): string
  99. {
  100. if (! $this->store->isPublished()) {
  101. return route('stores.preview', $this->store->slug);
  102. }
  103. // Built from APP_URL, because emails are also sent from the webhook, where there is no shopper's visit.
  104. $app = parse_url((string) config('app.url'));
  105. $scheme = $app['scheme'] ?? 'https';
  106. $port = isset($app['port']) ? ':'.$app['port'] : '';
  107. return $scheme.'://'.$this->store->slug.'.'.config('storefront.domain').$port;
  108. }
  109. /**
  110. * A WhatsApp chat with the store about this order, or null when the store has no WhatsApp number.
  111. */
  112. public function whatsappUrl(Order $order): ?string
  113. {
  114. if ($this->store->whatsapp_number === null || $this->store->whatsapp_number === '') {
  115. return null;
  116. }
  117. $text = $this->malay
  118. ? "Hai {$this->store->name}, saya ada soalan tentang pesanan saya #{$order->number}."
  119. : "Hi {$this->store->name}, I have a question about my order #{$order->number}.";
  120. return MalaysianPhone::whatsappLink($this->store->whatsapp_number, $text);
  121. }
  122. public function money(int $sen): string
  123. {
  124. return 'RM '.number_format($sen / 100, 2);
  125. }
  126. /**
  127. * The words shown in the header, from the store's own header section when it has one.
  128. *
  129. * @param array<string, mixed> $plan
  130. */
  131. private static function logoText(array $plan): ?string
  132. {
  133. // Version 2 plans keep one shared header; older ones have it among the sections.
  134. $header = $plan['header'] ?? collect($plan['sections'] ?? [])->firstWhere('type', 'header');
  135. $text = trim((string) ($header['content']['logoText'] ?? ''));
  136. return $text !== '' ? $text : null;
  137. }
  138. private static function fontStack(?string $key): string
  139. {
  140. $family = PlanSchema::FONTS[$key] ?? null;
  141. $fallback = in_array($key, self::SERIF_FONTS, true)
  142. ? "Georgia, 'Times New Roman', serif"
  143. : 'Helvetica, Arial, sans-serif';
  144. return $family !== null ? "'{$family}', {$fallback}" : $fallback;
  145. }
  146. }