WebOrbiton
v1.1.0.0

StocketBase

246 lines · 14.1 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. require_once __DIR__ . '/orders.php';
  6. require_once __DIR__ . '/shop.php';
  7. require_once __DIR__ . '/currency.php';
  8. require_once __DIR__ . '/language.php';
  9. require_once __DIR__ . '/icons.php';
  10. ​
  11. final class OrderView
  12. {
  13. public static function statusLabel(string $status): string
  14. {
  15. return Language::get('order_status_' . $status, ucfirst($status));
  16. }
  17. ​
  18. public static function render(array $order, array $items, bool $viewerOwnsAccountOrder): string
  19. {
  20. $e = static fn($value): string => htmlspecialchars((string) $value, ENT_QUOTES);
  21. $currency = (string) $order['currency'];
  22. $status = (string) $order['status'];
  23. $isPaid = in_array($status, Orders::PAID_STATUSES, true);
  24. $method = (string) ($order['payment_method'] ?? '');
  25. if ($method === '') {
  26. $method = in_array((string) ($order['provider'] ?? ''), ['stripe', 'polar'], true) ? 'card' : (string) ($order['provider'] ?? '');
  27. }
  28. ​
  29. $html = '<div class="order-view">';
  30. $html .= '<div class="order-view-head">';
  31. $html .= '<div><span class="order-view-label">' . $e(Language::get('order_number_label', 'Order number')) . '</span><strong>' . $e($order['order_number']) . '</strong></div>';
  32. $html .= '<div><span class="order-view-label">' . $e(Language::get('order_placed_label', 'Placed on')) . '</span>' . $e(self::date((string) $order['created_at'])) . '</div>';
  33. $html .= '<div><span class="order-view-label">' . $e(Language::get('order_status_label', 'Status')) . '</span><span class="status-pill status-' . $e($status) . '">' . $e(self::statusLabel($status)) . '</span></div>';
  34. if ($method !== '') {
  35. $html .= '<div><span class="order-view-label">' . $e(Language::get('order_payment_label', 'Payment')) . '</span>' . $e(Shop::paymentLabel($method)) . '</div>';
  36. }
  37. $html .= '</div>';
  38. ​
  39. if ($status === 'pending' && $method === 'bank_transfer') {
  40. $details = Shop::bankInstructions($order);
  41. $html .= '<div class="order-notice order-notice-info">'
  42. . '<h3>' . $e(Language::get('order_bank_title', 'Waiting for your bank transfer')) . '</h3>'
  43. . '<p>' . $e(Language::get('order_bank_intro', 'Send the amount below to this account. We will process your order as soon as the money arrives.')) . '</p>'
  44. . '<dl class="order-bank">'
  45. . '<dt>' . $e(Language::get('order_bank_amount', 'Amount')) . '</dt><dd>' . $e(Currency::format((int) $order['total_cents'], $currency)) . '</dd>'
  46. . '<dt>' . $e(Language::get('order_bank_reference', 'Transfer title')) . '</dt><dd><code>' . $e($order['order_number']) . '</code></dd>'
  47. . '</dl>'
  48. . ($details !== '' ? '<div class="order-bank-details">' . nl2br($e($details)) . '</div>' : '')
  49. . '</div>';
  50. } elseif ($status === 'pending' && $method === 'cod') {
  51. $html .= '<div class="order-notice order-notice-info"><p>' . $e(Language::get('order_cod_note', 'You will pay when the parcel is delivered.')) . ' <strong>' . $e(Currency::format((int) $order['total_cents'], $currency)) . '</strong></p></div>';
  52. } elseif ($status === 'pending') {
  53. $html .= '<div class="order-notice order-notice-info"><p>' . $e(Language::get('order_confirmation_processing', 'Your payment is still being processed. This page will show your final order status once confirmed — you will also receive a confirmation email.')) . '</p></div>';
  54. } elseif ($status === 'failed') {
  55. $html .= '<div class="order-notice order-notice-error"><p>' . $e(Language::get('order_failed_note', 'The payment did not go through, so the order was not placed. You can try again from your cart.')) . '</p></div>';
  56. }
  57. ​
  58. if (!empty($order['tracking_number'])) {
  59. $tracking = !empty($order['tracking_url'])
  60. ? '<a href="' . $e($order['tracking_url']) . '" target="_blank" rel="noopener noreferrer">' . $e($order['tracking_number']) . '</a>'
  61. : $e($order['tracking_number']);
  62. $html .= '<p class="order-tracking">' . Icons::icon('map-pin', 'icon icon-sm') . $e(Language::get('order_tracking_label', 'Tracking')) . ': ' . $tracking . '</p>';
  63. }
  64. ​
  65. $html .= '<table class="order-items"><thead><tr><th>' . $e(Language::get('cart_column_product', 'Product')) . '</th><th>' . $e(Language::get('cart_column_quantity', 'Quantity')) . '</th><th>' . $e(Language::get('cart_column_total', 'Total')) . '</th></tr></thead><tbody>';
  66. foreach ($items as $item) {
  67. $html .= '<tr><td>' . $e($item['product_title_snapshot'])
  68. . ($item['variant_name_snapshot'] !== null ? '<small>' . $e($item['variant_name_snapshot']) . '</small>' : '')
  69. . '</td><td>' . (int) $item['quantity'] . '</td><td>' . $e(Currency::format((int) $item['total_cents'], $currency)) . '</td></tr>';
  70. }
  71. $html .= '</tbody></table>';
  72. ​
  73. $html .= '<div class="order-totals">' . self::totals($order) . '</div>';
  74. ​
  75. if ($isPaid) {
  76. $html .= self::downloads($order, $items, $viewerOwnsAccountOrder);
  77. $html .= self::licenses((int) $order['id']);
  78. } elseif (self::orderHasDownloads($items) && $status === 'pending') {
  79. $html .= '<p class="order-hint">' . $e(Language::get('order_downloads_pending', 'Your download links will appear here and be emailed to you as soon as the payment is confirmed. Refresh this page in a moment.')) . '</p>';
  80. }
  81. ​
  82. $shipping = Shop::addressLines($order, 'shipping');
  83. $billing = Shop::addressLines($order, 'billing');
  84. if (!empty($shipping) || !empty($billing)) {
  85. $html .= '<div class="order-addresses">';
  86. if (!empty($shipping)) {
  87. $html .= '<div><h3>' . $e(Language::get('checkout_shipping_title', 'Shipping address')) . '</h3><p>' . implode('<br>', array_map($e, $shipping))
  88. . (!empty($order['shipping_method']) ? '<br><small>' . $e($order['shipping_method']) . '</small>' : '') . '</p></div>';
  89. }
  90. $showBilling = !empty($billing) && (
  91. (int) ($order['billing_same_as_shipping'] ?? 1) !== 1
  92. || empty($shipping)
  93. || !empty($order['billing_tax_id'])
  94. || !empty($order['billing_company'])
  95. );
  96. if ($showBilling) {
  97. $html .= '<div><h3>' . $e(Language::get('checkout_billing_title', 'Billing details')) . '</h3><p>' . implode('<br>', array_map($e, $billing)) . '</p></div>';
  98. }
  99. $html .= '</div>';
  100. }
  101. ​
  102. $history = Orders::history((int) $order['id']);
  103. if (count($history) > 1) {
  104. $html .= '<h3 class="order-section-title">' . $e(Language::get('order_history_title', 'What happened')) . '</h3><ol class="order-timeline">';
  105. $previous = null;
  106. foreach ($history as $entry) {
  107. if ($entry['status'] === $previous) {
  108. continue;
  109. }
  110. $previous = $entry['status'];
  111. $html .= '<li><span class="status-pill status-' . $e($entry['status']) . '">' . $e(self::statusLabel((string) $entry['status'])) . '</span><time>' . $e(self::date((string) $entry['created_at'], true)) . '</time></li>';
  112. }
  113. $html .= '</ol>';
  114. }
  115. ​
  116. if ($isPaid || $status === 'refunded') {
  117. $html .= '<p class="order-actions"><a class="order-button" href="' . $e(Shop::receiptUrl($order)) . '" target="_blank" rel="noopener">' . Icons::icon('download', 'icon icon-sm') . $e(Language::get('order_receipt_button', 'Receipt')) . '</a></p>';
  118. }
  119. ​
  120. return $html . '</div>';
  121. }
  122. ​
  123. public static function totals(array $order): string
  124. {
  125. $e = static fn($value): string => htmlspecialchars((string) $value, ENT_QUOTES);
  126. $currency = (string) $order['currency'];
  127. $row = static fn(string $label, string $amount, string $class = ''): string => '<div class="cart-summary-row' . ($class !== '' ? ' ' . $class : '') . '"><span>' . $e($label) . '</span><span>' . $e($amount) . '</span></div>';
  128. ​
  129. $html = $row(Language::get('cart_subtotal', 'Subtotal'), Currency::format((int) $order['subtotal_cents'], $currency));
  130. if ((int) $order['discount_cents'] > 0) {
  131. $html .= $row(Language::get('cart_discount', 'Discount') . (!empty($order['discount_code']) ? ' (' . $order['discount_code'] . ')' : ''), '-' . Currency::format((int) $order['discount_cents'], $currency));
  132. }
  133. if ((int) $order['shipping_cents'] > 0 || !empty($order['shipping_method'])) {
  134. $html .= $row(Language::get('order_shipping_label', 'Shipping'), (int) $order['shipping_cents'] === 0 ? Language::get('checkout_shipping_free', 'Free') : Currency::format((int) $order['shipping_cents'], $currency));
  135. }
  136. if ((int) ($order['cod_fee_cents'] ?? 0) > 0) {
  137. $html .= $row(Language::get('order_cod_fee_label', 'Cash on delivery fee'), Currency::format((int) $order['cod_fee_cents'], $currency));
  138. }
  139. if ((int) $order['tax_cents'] > 0) {
  140. $label = Shop::taxLabel() . ((int) ($order['tax_rate_bp'] ?? 0) > 0 ? ' ' . Shop::formatRate((int) $order['tax_rate_bp']) : '')
  141. . ((int) ($order['prices_include_tax'] ?? 0) === 1 ? ' (' . Language::get('cart_tax_included', 'included') . ')' : '');
  142. $html .= $row($label, Currency::format((int) $order['tax_cents'], $currency));
  143. }
  144. ​
  145. return $html . $row(Language::get('cart_total', 'Total'), Currency::format((int) $order['total_cents'], $currency), 'cart-summary-total');
  146. }
  147. ​
  148. private static function orderHasDownloads(array $items): bool
  149. {
  150. foreach (array_unique(array_map(static fn(array $item): int => (int) $item['product_id'], $items)) as $productId) {
  151. if (!empty(DigitalFiles::filesForProduct($productId))) {
  152. return true;
  153. }
  154. }
  155. ​
  156. return false;
  157. }
  158. ​
  159. private static function downloads(array $order, array $items, bool $viewerOwnsAccountOrder): string
  160. {
  161. $e = static fn($value): string => htmlspecialchars((string) $value, ENT_QUOTES);
  162. $grants = DigitalFiles::grantRowsForOrder((int) $order['id']);
  163. $groups = '';
  164. ​
  165. foreach (array_unique(array_map(static fn(array $item): int => (int) $item['product_id'], $items)) as $productId) {
  166. $files = DigitalFiles::filesForProduct($productId);
  167. if (empty($files)) {
  168. continue;
  169. }
  170. $grant = $grants[$productId] ?? null;
  171. $token = $viewerOwnsAccountOrder ? null : ($grant['token'] ?? null);
  172. if (!$viewerOwnsAccountOrder && $token === null) {
  173. continue;
  174. }
  175. ​
  176. $limit = '';
  177. if ($viewerOwnsAccountOrder) {
  178. $limit = Language::get('order_downloads_unlimited', 'No limit while you are logged in.');
  179. } elseif ($grant !== null) {
  180. $parts = [];
  181. if ($grant['max_downloads'] !== null) {
  182. $left = max(0, (int) $grant['max_downloads'] - (int) $grant['download_count']);
  183. $parts[] = str_replace('{count}', (string) $left, Language::get('order_downloads_left', '{count} downloads left'));
  184. }
  185. if (!empty($grant['expires_at'])) {
  186. $parts[] = str_replace('{date}', self::date((string) $grant['expires_at']), Language::get('order_downloads_until', 'link works until {date}'));
  187. }
  188. $limit = implode(' · ', $parts);
  189. }
  190. ​
  191. $title = '';
  192. foreach ($items as $item) {
  193. if ((int) $item['product_id'] === $productId) {
  194. $title = (string) $item['product_title_snapshot'];
  195. break;
  196. }
  197. }
  198. ​
  199. $groups .= '<div class="download-group"><h4>' . $e($title) . '</h4><ul class="download-list">';
  200. foreach ($files as $file) {
  201. $groups .= '<li><a href="' . $e(DigitalFiles::downloadUrl((int) $file['id'], $token)) . '" rel="nofollow">' . Icons::icon('download', 'icon icon-sm') . $e(DigitalFiles::displayName($file)) . '</a>'
  202. . '<small>' . $e(DigitalFiles::formatSize((int) $file['size_bytes'])) . '</small></li>';
  203. }
  204. $groups .= '</ul>' . ($limit !== '' ? '<p class="order-hint">' . $e($limit) . '</p>' : '') . '</div>';
  205. }
  206. ​
  207. if ($groups === '') {
  208. return '';
  209. }
  210. ​
  211. return '<h3 class="order-section-title">' . $e(Language::get('order_downloads_title', 'Your downloads')) . '</h3>' . $groups;
  212. }
  213. ​
  214. private static function licenses(int $orderId): string
  215. {
  216. $keys = Licenses::forOrder($orderId);
  217. if (empty($keys)) {
  218. return '';
  219. }
  220. $e = static fn($value): string => htmlspecialchars((string) $value, ENT_QUOTES);
  221. $html = '<h3 class="order-section-title">' . $e(Language::get('order_licenses_title', 'License keys')) . '</h3><ul class="license-list">';
  222. foreach ($keys as $key) {
  223. $html .= '<li><span>' . $e($key['product_title_snapshot'] ?? '') . '</span>'
  224. . '<code class="license-key">' . $e($key['license_key']) . '</code>'
  225. . '<button type="button" class="license-copy" data-copy="' . $e($key['license_key']) . '" data-copied="' . $e(Language::get('code_copied', 'Copied')) . '">' . $e(Language::get('code_copy', 'Copy')) . '</button></li>';
  226. }
  227. ​
  228. return $html . '</ul>';
  229. }
  230. ​
  231. public static function date(string $value, bool $withTime = false): string
  232. {
  233. $timestamp = strtotime($value);
  234. if ($timestamp === false) {
  235. return $value;
  236. }
  237. ​
  238. return date($withTime ? 'Y-m-d H:i' : 'Y-m-d', $timestamp);
  239. }
  240. ​
  241. public static function copyScript(): string
  242. {
  243. return '<script>document.addEventListener("click",function(e){var b=e.target.closest(".license-copy");if(!b||!navigator.clipboard){return;}navigator.clipboard.writeText(b.dataset.copy).then(function(){var t=b.textContent;b.textContent=b.dataset.copied;setTimeout(function(){b.textContent=t;},1500);});});</script>';
  244. }
  245. }
  246. ​