v1.1.0.0
StocketBase
- <?php
-
- declare(strict_types=1);
-
- require_once __DIR__ . '/orders.php';
- require_once __DIR__ . '/shop.php';
- require_once __DIR__ . '/currency.php';
- require_once __DIR__ . '/language.php';
- require_once __DIR__ . '/icons.php';
-
- final class OrderView
- {
- public static function statusLabel(string $status): string
- {
- return Language::get('order_status_' . $status, ucfirst($status));
- }
-
- public static function render(array $order, array $items, bool $viewerOwnsAccountOrder): string
- {
- $e = static fn($value): string => htmlspecialchars((string) $value, ENT_QUOTES);
- $currency = (string) $order['currency'];
- $status = (string) $order['status'];
- $isPaid = in_array($status, Orders::PAID_STATUSES, true);
- $method = (string) ($order['payment_method'] ?? '');
- if ($method === '') {
- $method = in_array((string) ($order['provider'] ?? ''), ['stripe', 'polar'], true) ? 'card' : (string) ($order['provider'] ?? '');
- }
-
- $html = '<div class="order-view">';
- $html .= '<div class="order-view-head">';
- $html .= '<div><span class="order-view-label">' . $e(Language::get('order_number_label', 'Order number')) . '</span><strong>' . $e($order['order_number']) . '</strong></div>';
- $html .= '<div><span class="order-view-label">' . $e(Language::get('order_placed_label', 'Placed on')) . '</span>' . $e(self::date((string) $order['created_at'])) . '</div>';
- $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>';
- if ($method !== '') {
- $html .= '<div><span class="order-view-label">' . $e(Language::get('order_payment_label', 'Payment')) . '</span>' . $e(Shop::paymentLabel($method)) . '</div>';
- }
- $html .= '</div>';
-
- if ($status === 'pending' && $method === 'bank_transfer') {
- $details = Shop::bankInstructions($order);
- $html .= '<div class="order-notice order-notice-info">'
- . '<h3>' . $e(Language::get('order_bank_title', 'Waiting for your bank transfer')) . '</h3>'
- . '<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>'
- . '<dl class="order-bank">'
- . '<dt>' . $e(Language::get('order_bank_amount', 'Amount')) . '</dt><dd>' . $e(Currency::format((int) $order['total_cents'], $currency)) . '</dd>'
- . '<dt>' . $e(Language::get('order_bank_reference', 'Transfer title')) . '</dt><dd><code>' . $e($order['order_number']) . '</code></dd>'
- . '</dl>'
- . ($details !== '' ? '<div class="order-bank-details">' . nl2br($e($details)) . '</div>' : '')
- . '</div>';
- } elseif ($status === 'pending' && $method === 'cod') {
- $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>';
- } elseif ($status === 'pending') {
- $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>';
- } elseif ($status === 'failed') {
- $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>';
- }
-
- if (!empty($order['tracking_number'])) {
- $tracking = !empty($order['tracking_url'])
- ? '<a href="' . $e($order['tracking_url']) . '" target="_blank" rel="noopener noreferrer">' . $e($order['tracking_number']) . '</a>'
- : $e($order['tracking_number']);
- $html .= '<p class="order-tracking">' . Icons::icon('map-pin', 'icon icon-sm') . $e(Language::get('order_tracking_label', 'Tracking')) . ': ' . $tracking . '</p>';
- }
-
- $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>';
- foreach ($items as $item) {
- $html .= '<tr><td>' . $e($item['product_title_snapshot'])
- . ($item['variant_name_snapshot'] !== null ? '<small>' . $e($item['variant_name_snapshot']) . '</small>' : '')
- . '</td><td>' . (int) $item['quantity'] . '</td><td>' . $e(Currency::format((int) $item['total_cents'], $currency)) . '</td></tr>';
- }
- $html .= '</tbody></table>';
-
- $html .= '<div class="order-totals">' . self::totals($order) . '</div>';
-
- if ($isPaid) {
- $html .= self::downloads($order, $items, $viewerOwnsAccountOrder);
- $html .= self::licenses((int) $order['id']);
- } elseif (self::orderHasDownloads($items) && $status === 'pending') {
- $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>';
- }
-
- $shipping = Shop::addressLines($order, 'shipping');
- $billing = Shop::addressLines($order, 'billing');
- if (!empty($shipping) || !empty($billing)) {
- $html .= '<div class="order-addresses">';
- if (!empty($shipping)) {
- $html .= '<div><h3>' . $e(Language::get('checkout_shipping_title', 'Shipping address')) . '</h3><p>' . implode('<br>', array_map($e, $shipping))
- . (!empty($order['shipping_method']) ? '<br><small>' . $e($order['shipping_method']) . '</small>' : '') . '</p></div>';
- }
- $showBilling = !empty($billing) && (
- (int) ($order['billing_same_as_shipping'] ?? 1) !== 1
- || empty($shipping)
- || !empty($order['billing_tax_id'])
- || !empty($order['billing_company'])
- );
- if ($showBilling) {
- $html .= '<div><h3>' . $e(Language::get('checkout_billing_title', 'Billing details')) . '</h3><p>' . implode('<br>', array_map($e, $billing)) . '</p></div>';
- }
- $html .= '</div>';
- }
-
- $history = Orders::history((int) $order['id']);
- if (count($history) > 1) {
- $html .= '<h3 class="order-section-title">' . $e(Language::get('order_history_title', 'What happened')) . '</h3><ol class="order-timeline">';
- $previous = null;
- foreach ($history as $entry) {
- if ($entry['status'] === $previous) {
- continue;
- }
- $previous = $entry['status'];
- $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>';
- }
- $html .= '</ol>';
- }
-
- if ($isPaid || $status === 'refunded') {
- $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>';
- }
-
- return $html . '</div>';
- }
-
- public static function totals(array $order): string
- {
- $e = static fn($value): string => htmlspecialchars((string) $value, ENT_QUOTES);
- $currency = (string) $order['currency'];
- $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>';
-
- $html = $row(Language::get('cart_subtotal', 'Subtotal'), Currency::format((int) $order['subtotal_cents'], $currency));
- if ((int) $order['discount_cents'] > 0) {
- $html .= $row(Language::get('cart_discount', 'Discount') . (!empty($order['discount_code']) ? ' (' . $order['discount_code'] . ')' : ''), '-' . Currency::format((int) $order['discount_cents'], $currency));
- }
- if ((int) $order['shipping_cents'] > 0 || !empty($order['shipping_method'])) {
- $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));
- }
- if ((int) ($order['cod_fee_cents'] ?? 0) > 0) {
- $html .= $row(Language::get('order_cod_fee_label', 'Cash on delivery fee'), Currency::format((int) $order['cod_fee_cents'], $currency));
- }
- if ((int) $order['tax_cents'] > 0) {
- $label = Shop::taxLabel() . ((int) ($order['tax_rate_bp'] ?? 0) > 0 ? ' ' . Shop::formatRate((int) $order['tax_rate_bp']) : '')
- . ((int) ($order['prices_include_tax'] ?? 0) === 1 ? ' (' . Language::get('cart_tax_included', 'included') . ')' : '');
- $html .= $row($label, Currency::format((int) $order['tax_cents'], $currency));
- }
-
- return $html . $row(Language::get('cart_total', 'Total'), Currency::format((int) $order['total_cents'], $currency), 'cart-summary-total');
- }
-
- private static function orderHasDownloads(array $items): bool
- {
- foreach (array_unique(array_map(static fn(array $item): int => (int) $item['product_id'], $items)) as $productId) {
- if (!empty(DigitalFiles::filesForProduct($productId))) {
- return true;
- }
- }
-
- return false;
- }
-
- private static function downloads(array $order, array $items, bool $viewerOwnsAccountOrder): string
- {
- $e = static fn($value): string => htmlspecialchars((string) $value, ENT_QUOTES);
- $grants = DigitalFiles::grantRowsForOrder((int) $order['id']);
- $groups = '';
-
- foreach (array_unique(array_map(static fn(array $item): int => (int) $item['product_id'], $items)) as $productId) {
- $files = DigitalFiles::filesForProduct($productId);
- if (empty($files)) {
- continue;
- }
- $grant = $grants[$productId] ?? null;
- $token = $viewerOwnsAccountOrder ? null : ($grant['token'] ?? null);
- if (!$viewerOwnsAccountOrder && $token === null) {
- continue;
- }
-
- $limit = '';
- if ($viewerOwnsAccountOrder) {
- $limit = Language::get('order_downloads_unlimited', 'No limit while you are logged in.');
- } elseif ($grant !== null) {
- $parts = [];
- if ($grant['max_downloads'] !== null) {
- $left = max(0, (int) $grant['max_downloads'] - (int) $grant['download_count']);
- $parts[] = str_replace('{count}', (string) $left, Language::get('order_downloads_left', '{count} downloads left'));
- }
- if (!empty($grant['expires_at'])) {
- $parts[] = str_replace('{date}', self::date((string) $grant['expires_at']), Language::get('order_downloads_until', 'link works until {date}'));
- }
- $limit = implode(' · ', $parts);
- }
-
- $title = '';
- foreach ($items as $item) {
- if ((int) $item['product_id'] === $productId) {
- $title = (string) $item['product_title_snapshot'];
- break;
- }
- }
-
- $groups .= '<div class="download-group"><h4>' . $e($title) . '</h4><ul class="download-list">';
- foreach ($files as $file) {
- $groups .= '<li><a href="' . $e(DigitalFiles::downloadUrl((int) $file['id'], $token)) . '" rel="nofollow">' . Icons::icon('download', 'icon icon-sm') . $e(DigitalFiles::displayName($file)) . '</a>'
- . '<small>' . $e(DigitalFiles::formatSize((int) $file['size_bytes'])) . '</small></li>';
- }
- $groups .= '</ul>' . ($limit !== '' ? '<p class="order-hint">' . $e($limit) . '</p>' : '') . '</div>';
- }
-
- if ($groups === '') {
- return '';
- }
-
- return '<h3 class="order-section-title">' . $e(Language::get('order_downloads_title', 'Your downloads')) . '</h3>' . $groups;
- }
-
- private static function licenses(int $orderId): string
- {
- $keys = Licenses::forOrder($orderId);
- if (empty($keys)) {
- return '';
- }
- $e = static fn($value): string => htmlspecialchars((string) $value, ENT_QUOTES);
- $html = '<h3 class="order-section-title">' . $e(Language::get('order_licenses_title', 'License keys')) . '</h3><ul class="license-list">';
- foreach ($keys as $key) {
- $html .= '<li><span>' . $e($key['product_title_snapshot'] ?? '') . '</span>'
- . '<code class="license-key">' . $e($key['license_key']) . '</code>'
- . '<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>';
- }
-
- return $html . '</ul>';
- }
-
- public static function date(string $value, bool $withTime = false): string
- {
- $timestamp = strtotime($value);
- if ($timestamp === false) {
- return $value;
- }
-
- return date($withTime ? 'Y-m-d H:i' : 'Y-m-d', $timestamp);
- }
-
- public static function copyScript(): string
- {
- 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>';
- }
- }
-