Compare commits

..
Author SHA1 Message Date
Derek Ross 47f9edacfd fix: escape HTML in receipt print template to prevent injection 2026-04-12 12:58:57 -04:00
Derek Ross c229f40da8 feat: add Print Receipt button to completed transactions
Add a 'Print Receipt' option for completed credit purchase transactions
in the AI provider config dialog. Clicking it opens a print-friendly
receipt with Soapbox Technology, LLC business details (soapbox.pub,
[email protected]) in a standard receipt layout showing payment ID,
date, method, credits, processing fee, and total charged.

Follows the existing window.open() print pattern used by gift card QR
code printing. Adds i18n translations for all 6 supported languages.
2026-04-10 12:05:50 -04:00
8 changed files with 290 additions and 44 deletions

No files matched your search

+236
View File
@@ -895,6 +895,231 @@ export function AIProviderConfigDialog({
}
};
const escapeHtml = (str: string): string =>
str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#39;');
const handlePrintReceipt = (payment: Payment) => {
const receiptDate = new Date((payment.completed_at ?? payment.created_at) * 1000);
const formattedDate = new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
}).format(receiptDate);
const formattedTime = new Intl.DateTimeFormat('en-US', {
hour: '2-digit',
minute: '2-digit',
}).format(receiptDate);
const paymentMethodDisplay = payment.method === 'stripe' ? t('creditCard') : t('lightning');
const statusDisplay = payment.status.charAt(0).toUpperCase() + payment.status.slice(1);
const printWindow = window.open('', '_blank');
if (!printWindow) {
toast({
title: t('printFailed'),
description: t('failedToOpenPrintWindow'),
variant: 'destructive',
});
return;
}
const printHTML = `
<!DOCTYPE html>
<html>
<head>
<title>${t('receipt')} - ${escapeHtml(payment.id)}</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
padding: 40px 20px;
background: white;
color: #000;
}
.receipt {
max-width: 600px;
margin: 0 auto;
border: 1px solid #e0e0e0;
padding: 48px 40px;
}
.header {
text-align: center;
padding-bottom: 28px;
border-bottom: 2px solid #000;
margin-bottom: 28px;
}
.company-name {
font-size: 20px;
font-weight: 700;
letter-spacing: 0.5px;
margin-bottom: 6px;
}
.company-detail {
font-size: 13px;
color: #555;
line-height: 1.6;
}
.company-detail a {
color: #555;
text-decoration: none;
}
.receipt-title {
text-align: center;
font-size: 14px;
font-weight: 600;
letter-spacing: 2px;
text-transform: uppercase;
color: #333;
margin-bottom: 28px;
}
.details-grid {
display: grid;
grid-template-columns: 140px 1fr;
gap: 8px 16px;
margin-bottom: 28px;
font-size: 14px;
}
.details-grid dt {
color: #666;
font-weight: 500;
}
.details-grid dd {
color: #000;
}
.divider {
border: none;
border-top: 1px solid #e0e0e0;
margin: 0 0 28px 0;
}
.line-items {
margin-bottom: 28px;
}
.line-item {
display: flex;
justify-content: space-between;
padding: 6px 0;
font-size: 14px;
}
.line-item-label {
color: #333;
}
.line-item-value {
font-variant-numeric: tabular-nums;
}
.line-item-total {
border-top: 1px solid #000;
margin-top: 8px;
padding-top: 10px;
font-weight: 700;
font-size: 15px;
}
.footer {
text-align: center;
padding-top: 28px;
border-top: 1px solid #e0e0e0;
}
.footer p {
font-size: 13px;
color: #666;
line-height: 1.6;
}
@media print {
body {
padding: 0;
}
.receipt {
border: none;
padding: 0;
max-width: 100%;
}
}
</style>
</head>
<body>
<div class="receipt">
<div class="header">
<div class="company-name">Soapbox Technology, LLC</div>
<div class="company-detail">
<a href="https://soapbox.pub">soapbox.pub</a><br />
[email protected]
</div>
</div>
<div class="receipt-title">${t('receipt')}</div>
<dl class="details-grid">
<dt>${t('receiptNumber')}</dt>
<dd>${escapeHtml(payment.id)}</dd>
<dt>${t('date')}</dt>
<dd>${formattedDate} ${formattedTime}</dd>
<dt>${t('status')}</dt>
<dd>${escapeHtml(statusDisplay)}</dd>
<dt>${t('paymentMethodLabel')}</dt>
<dd>${paymentMethodDisplay}</dd>
${payment.description ? `<dt>${t('description')}</dt><dd>${escapeHtml(payment.description)}</dd>` : ''}
</dl>
<hr class="divider" />
<div class="line-items">
<div class="line-item">
<span class="line-item-label">${t('credits')}</span>
<span class="line-item-value">${formatCurrency(payment.amount)}</span>
</div>
${payment.fee > 0 ? `
<div class="line-item">
<span class="line-item-label">${t('processingFee')}</span>
<span class="line-item-value">${formatCurrency(payment.fee)}</span>
</div>
` : ''}
<div class="line-item line-item-total">
<span class="line-item-label">${t('totalCharged')}</span>
<span class="line-item-value">${formatCurrency(payment.total)}</span>
</div>
</div>
<div class="footer">
<p>${t('thankYouForPurchase')}</p>
</div>
</div>
</body>
</html>
`;
printWindow.document.write(printHTML);
printWindow.document.close();
printWindow.onload = () => {
setTimeout(() => {
printWindow.print();
}, 250);
};
};
const handleDownloadCSV = () => {
if (!giftcards || giftcards.length === 0) return;
@@ -1307,6 +1532,17 @@ export function AIProviderConfigDialog({
)}
</div>
)}
{payment.status === 'completed' && (
<Button
variant="ghost"
size="sm"
onClick={() => handlePrintReceipt(payment)}
className="h-6 px-2 text-xs"
>
<Printer className="h-3 w-3 mr-1" />
{t('printReceipt')}
</Button>
)}
</div>
</div>
))}
-38
View File
@@ -396,41 +396,6 @@ export class SessionManager {
});
};
// Replace large tool result content with a truncated prefix for messages the
// model has already seen and responded to at least twice. Like evictSeenImages,
// this is a send-time transformation only — session.messages stays intact for
// the UI. We keep the first TOOL_OUTPUT_EVICTION_THRESHOLD bytes so the model
// retains partial context and can decide whether to re-fetch the full output.
const TOOL_OUTPUT_EVICTION_THRESHOLD = 1024; // 1 KiB
const TOOL_OUTPUT_MIN_ASSISTANT_TURNS = 2; // Only evict after 2+ assistant responses
const evictSeenToolOutputs = (msgs: OpenAI.Chat.Completions.ChatCompletionMessageParam[]): OpenAI.Chat.Completions.ChatCompletionMessageParam[] => {
// Collect indices of all assistant messages.
const assistantIndices: number[] = [];
for (let i = 0; i < msgs.length; i++) {
if (msgs[i].role === 'assistant') {
assistantIndices.push(i);
}
}
// Need at least TOOL_OUTPUT_MIN_ASSISTANT_TURNS + 1 assistant messages:
// the ones that have "seen" the tool output, plus the current pending turn.
if (assistantIndices.length < TOOL_OUTPUT_MIN_ASSISTANT_TURNS + 1) return msgs;
// The eviction cutoff is the assistant message N turns back from the end.
// Tool results before this index have been processed by at least N assistant
// turns, so truncating them is safe.
const cutoffIdx = assistantIndices[assistantIndices.length - TOOL_OUTPUT_MIN_ASSISTANT_TURNS];
return msgs.map((msg, idx) => {
if (idx >= cutoffIdx) return msg;
if (msg.role !== 'tool') return msg;
if (typeof msg.content !== 'string') return msg;
if (msg.content.length <= TOOL_OUTPUT_EVICTION_THRESHOLD) return msg;
return { ...msg, content: msg.content.slice(0, TOOL_OUTPUT_EVICTION_THRESHOLD) + '\n\n[… truncated]' };
});
};
// Prepare messages for AI
// Note: User messages with image_url content parts are valid ChatCompletionMessageParam types
// The AIMessage type includes ChatCompletionMessageParam, so this cast is safe
@@ -438,9 +403,6 @@ export class SessionManager {
? [{ role: 'system', content: systemPrompt }, ...session.messages]
: session.messages) as OpenAI.Chat.Completions.ChatCompletionMessageParam[];
// Evict large tool outputs the model has already processed.
messages = evictSeenToolOutputs(messages);
// Evict images the model has already seen (all except the last user message).
// This is a send-time transformation only — session.messages stays intact for the UI.
messages = evictSeenImages(messages);
+9 -1
View File
@@ -573,5 +573,13 @@
"failedToDuplicateProject": "Failed to duplicate project",
"newProjectName": "New Project Name",
"duplicateProjectDescription": "Create a copy of this project with a new name",
"resume": "Resume"
"resume": "Resume",
"printReceipt": "Print Receipt",
"receipt": "Receipt",
"receiptNumber": "Receipt #",
"date": "Date",
"status": "Status",
"paymentMethodLabel": "Payment Method",
"totalCharged": "Total Charged",
"thankYouForPurchase": "Thank you for your purchase!"
}
+9 -1
View File
@@ -487,5 +487,13 @@
"failedToDuplicateProject": "An kasa kwafin aikin",
"newProjectName": "Sunan Sabon Aikin",
"duplicateProjectDescription": "Ƙirƙiri kwafin wannan aikin da sabon suna",
"resume": "Ci gaba"
"resume": "Ci gaba",
"printReceipt": "Buga Rasidi",
"receipt": "Rasidi",
"receiptNumber": "Rasidi #",
"date": "Kwanan wata",
"status": "Matsayi",
"paymentMethodLabel": "Hanyar Biyan Kuɗi",
"totalCharged": "Jimlar da aka Caji",
"thankYouForPurchase": "Mun gode da sayan ku!"
}
+9 -1
View File
@@ -484,5 +484,13 @@
"failedToDuplicateProject": "Ọ dara ada imepụta ọrụ",
"newProjectName": "Aha Ọrụ Ọhụrụ",
"duplicateProjectDescription": "Mepụta oyiri nke ọrụ a na aha ọhụrụ",
"resume": "Gaa n'ihu"
"resume": "Gaa n'ihu",
"printReceipt": "Bipụta Risịịtị",
"receipt": "Risịịtị",
"receiptNumber": "Risịịtị #",
"date": "Ụbọchị",
"status": "Ọnọdụ",
"paymentMethodLabel": "Ụzọ Ịkwụ Ụgwọ",
"totalCharged": "Nchịkọta Ego a Gbara",
"thankYouForPurchase": "Daalụ maka ịzụta gị!"
}
+9 -1
View File
@@ -496,5 +496,13 @@
"failedToDuplicateProject": "Falha ao duplicar projeto",
"newProjectName": "Nome do Novo Projeto",
"duplicateProjectDescription": "Criar uma cópia deste projeto com um novo nome",
"resume": "Retomar"
"resume": "Retomar",
"printReceipt": "Imprimir Recibo",
"receipt": "Recibo",
"receiptNumber": "Recibo Nº",
"date": "Data",
"status": "Status",
"paymentMethodLabel": "Método de Pagamento",
"totalCharged": "Total Cobrado",
"thankYouForPurchase": "Obrigado pela sua compra!"
}
+9 -1
View File
@@ -484,5 +484,13 @@
"failedToDuplicateProject": "O kuna lati ṣe ẹda iṣẹ akanṣe",
"newProjectName": "Orukọ Iṣẹ Akanṣe Tuntun",
"duplicateProjectDescription": "Ṣẹda ẹda ti iṣẹ akanṣe yii pẹlu orukọ tuntun",
"resume": "Tẹsiwaju"
"resume": "Tẹsiwaju",
"printReceipt": "Tẹ Ìwé Ẹ̀rí",
"receipt": "Ìwé Ẹ̀rí",
"receiptNumber": "Ìwé Ẹ̀rí #",
"date": "Ọjọ́",
"status": "Ipò",
"paymentMethodLabel": "Ọ̀nà Ìsanwó",
"totalCharged": "Àpapọ̀ tí a gba",
"thankYouForPurchase": "A dúpẹ́ fún rírà rẹ!"
}
+9 -1
View File
@@ -496,5 +496,13 @@
"failedToDuplicateProject": "复制项目失败",
"newProjectName": "新项目名称",
"duplicateProjectDescription": "使用新名称创建此项目的副本",
"resume": "继续"
"resume": "继续",
"printReceipt": "打印收据",
"receipt": "收据",
"receiptNumber": "收据编号",
"date": "日期",
"status": "状态",
"paymentMethodLabel": "支付方式",
"totalCharged": "总收费",
"thankYouForPurchase": "感谢您的购买!"
}