@hintergrund/worker 0.1.5 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/cms/handlers/admin-users.handler.d.ts +8 -0
- package/dist/api/cms/handlers/admin-users.handler.d.ts.map +1 -0
- package/dist/api/cms/handlers/admin-users.handler.js +121 -0
- package/dist/api/cms/handlers/agent-conversations.handler.d.ts +7 -0
- package/dist/api/cms/handlers/agent-conversations.handler.d.ts.map +1 -0
- package/dist/api/cms/handlers/agent-conversations.handler.js +94 -0
- package/dist/api/cms/handlers/auth.handler.d.ts +0 -3
- package/dist/api/cms/handlers/auth.handler.d.ts.map +1 -1
- package/dist/api/cms/handlers/auth.handler.js +74 -153
- package/dist/api/cms/handlers/website-repo.handler.d.ts +8 -0
- package/dist/api/cms/handlers/website-repo.handler.d.ts.map +1 -0
- package/dist/api/cms/handlers/website-repo.handler.js +148 -0
- package/dist/api/cms/routes/index.d.ts.map +1 -1
- package/dist/api/cms/routes/index.js +16 -4
- package/dist/api/emails/handlers/campaigns.handler.d.ts +10 -23
- package/dist/api/emails/handlers/campaigns.handler.d.ts.map +1 -1
- package/dist/api/emails/handlers/campaigns.handler.js +164 -92
- package/dist/api/emails/handlers/invoice.handler.d.ts +5 -19
- package/dist/api/emails/handlers/invoice.handler.d.ts.map +1 -1
- package/dist/api/emails/handlers/invoice.handler.js +58 -340
- package/dist/api/emails/handlers/public.handler.d.ts +6 -29
- package/dist/api/emails/handlers/public.handler.d.ts.map +1 -1
- package/dist/api/emails/handlers/public.handler.js +150 -312
- package/dist/api/emails/handlers/send.handler.d.ts +5 -17
- package/dist/api/emails/handlers/send.handler.d.ts.map +1 -1
- package/dist/api/emails/handlers/send.handler.js +107 -92
- package/dist/api/emails/handlers/settings.handler.d.ts +28 -33
- package/dist/api/emails/handlers/settings.handler.d.ts.map +1 -1
- package/dist/api/emails/handlers/settings.handler.js +96 -287
- package/dist/api/emails/handlers/subscribers.handler.d.ts +13 -0
- package/dist/api/emails/handlers/subscribers.handler.d.ts.map +1 -0
- package/dist/api/emails/handlers/subscribers.handler.js +125 -0
- package/dist/api/emails/handlers/webhook.handler.d.ts +1 -2
- package/dist/api/emails/handlers/webhook.handler.d.ts.map +1 -1
- package/dist/api/emails/handlers/webhook.handler.js +66 -166
- package/dist/api/emails/routes/admin.routes.d.ts +1 -3
- package/dist/api/emails/routes/admin.routes.d.ts.map +1 -1
- package/dist/api/emails/routes/admin.routes.js +19 -49
- package/dist/api/emails/routes/public.routes.d.ts +1 -3
- package/dist/api/emails/routes/public.routes.d.ts.map +1 -1
- package/dist/api/emails/routes/public.routes.js +2 -9
- package/dist/api/shop/handlers/prices.handler.d.ts +2 -15
- package/dist/api/shop/handlers/prices.handler.d.ts.map +1 -1
- package/dist/api/shop/handlers/prices.handler.js +2 -83
- package/dist/api/shop/handlers/products.handler.d.ts +29 -20
- package/dist/api/shop/handlers/products.handler.d.ts.map +1 -1
- package/dist/api/shop/handlers/products.handler.js +195 -162
- package/dist/api/shop/handlers/public.handler.d.ts +24 -8
- package/dist/api/shop/handlers/public.handler.d.ts.map +1 -1
- package/dist/api/shop/handlers/public.handler.js +525 -78
- package/dist/api/shop/routes.d.ts.map +1 -1
- package/dist/api/shop/routes.js +14 -9
- package/dist/hintergrund-assets.d.ts +3 -2
- package/dist/hintergrund-assets.d.ts.map +1 -1
- package/dist/hintergrund-assets.js +4 -2
- package/dist/index.d.ts +14 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +44 -4
- package/dist/lib/auth/crypto.d.ts +2 -15
- package/dist/lib/auth/crypto.d.ts.map +1 -1
- package/dist/lib/auth/crypto.js +8 -95
- package/dist/lib/auth/jwt.d.ts +0 -4
- package/dist/lib/auth/jwt.d.ts.map +1 -1
- package/dist/lib/auth/jwt.js +0 -6
- package/dist/lib/config.d.ts +1 -1
- package/dist/lib/config.d.ts.map +1 -1
- package/dist/lib/config.js +42 -86
- package/dist/lib/email-template.d.ts +15 -0
- package/dist/lib/email-template.d.ts.map +1 -0
- package/dist/lib/email-template.js +58 -0
- package/dist/lib/email.d.ts +18 -8
- package/dist/lib/email.d.ts.map +1 -1
- package/dist/lib/email.js +59 -70
- package/dist/lib/github-config.d.ts +19 -0
- package/dist/lib/github-config.d.ts.map +1 -0
- package/dist/lib/github-config.js +23 -0
- package/dist/lib/github.d.ts +10 -0
- package/dist/lib/github.d.ts.map +1 -1
- package/dist/lib/github.js +20 -2
- package/dist/lib/project-db.d.ts +24 -0
- package/dist/lib/project-db.d.ts.map +1 -0
- package/dist/lib/project-db.js +172 -0
- package/dist/lib/template.js +4 -4
- package/dist/middleware/auth.d.ts +0 -2
- package/dist/middleware/auth.d.ts.map +1 -1
- package/dist/middleware/auth.js +21 -50
- package/package.json +2 -1
- package/dist/api/builder/handlers/generate.handler.d.ts +0 -12
- package/dist/api/builder/handlers/generate.handler.d.ts.map +0 -1
- package/dist/api/builder/handlers/generate.handler.js +0 -203
- package/dist/api/builder/handlers/template.handler.d.ts +0 -12
- package/dist/api/builder/handlers/template.handler.d.ts.map +0 -1
- package/dist/api/builder/handlers/template.handler.js +0 -208
- package/dist/api/builder/routes.d.ts +0 -7
- package/dist/api/builder/routes.d.ts.map +0 -1
- package/dist/api/builder/routes.js +0 -15
- package/dist/api/cms/handlers/assets.handler.d.ts +0 -31
- package/dist/api/cms/handlers/assets.handler.d.ts.map +0 -1
- package/dist/api/cms/handlers/assets.handler.js +0 -200
- package/dist/api/cms/handlers/config.handler.d.ts +0 -7
- package/dist/api/cms/handlers/config.handler.d.ts.map +0 -1
- package/dist/api/cms/handlers/config.handler.js +0 -37
- package/dist/api/cms/handlers/content.handler.d.ts +0 -21
- package/dist/api/cms/handlers/content.handler.d.ts.map +0 -1
- package/dist/api/cms/handlers/content.handler.js +0 -117
- package/dist/api/cms/routes/assets.routes.d.ts +0 -4
- package/dist/api/cms/routes/assets.routes.d.ts.map +0 -1
- package/dist/api/cms/routes/assets.routes.js +0 -13
- package/dist/api/cms/routes/content.routes.d.ts +0 -4
- package/dist/api/cms/routes/content.routes.d.ts.map +0 -1
- package/dist/api/cms/routes/content.routes.js +0 -11
- package/dist/api/emails/handlers/branding.handler.d.ts +0 -17
- package/dist/api/emails/handlers/branding.handler.d.ts.map +0 -1
- package/dist/api/emails/handlers/branding.handler.js +0 -120
- package/dist/api/emails/handlers/contacts.handler.d.ts +0 -37
- package/dist/api/emails/handlers/contacts.handler.d.ts.map +0 -1
- package/dist/api/emails/handlers/contacts.handler.js +0 -161
- package/dist/api/emails/handlers/templates.handler.d.ts +0 -32
- package/dist/api/emails/handlers/templates.handler.d.ts.map +0 -1
- package/dist/api/emails/handlers/templates.handler.js +0 -132
- package/dist/api/newsletter/handlers/email.handler.d.ts +0 -7
- package/dist/api/newsletter/handlers/email.handler.d.ts.map +0 -1
- package/dist/api/newsletter/handlers/email.handler.js +0 -102
- package/dist/api/newsletter/handlers/emails.handler.d.ts +0 -31
- package/dist/api/newsletter/handlers/emails.handler.d.ts.map +0 -1
- package/dist/api/newsletter/handlers/emails.handler.js +0 -262
- package/dist/api/newsletter/handlers/invoice.handler.d.ts +0 -22
- package/dist/api/newsletter/handlers/invoice.handler.d.ts.map +0 -1
- package/dist/api/newsletter/handlers/invoice.handler.js +0 -455
- package/dist/api/newsletter/handlers/public.handler.d.ts +0 -22
- package/dist/api/newsletter/handlers/public.handler.d.ts.map +0 -1
- package/dist/api/newsletter/handlers/public.handler.js +0 -300
- package/dist/api/newsletter/handlers/send.handler.d.ts +0 -12
- package/dist/api/newsletter/handlers/send.handler.d.ts.map +0 -1
- package/dist/api/newsletter/handlers/send.handler.js +0 -153
- package/dist/api/newsletter/handlers/subscribers.handler.d.ts +0 -22
- package/dist/api/newsletter/handlers/subscribers.handler.d.ts.map +0 -1
- package/dist/api/newsletter/handlers/subscribers.handler.js +0 -195
- package/dist/api/newsletter/routes/admin.routes.d.ts +0 -4
- package/dist/api/newsletter/routes/admin.routes.d.ts.map +0 -1
- package/dist/api/newsletter/routes/admin.routes.js +0 -28
- package/dist/api/newsletter/routes/index.d.ts +0 -4
- package/dist/api/newsletter/routes/index.d.ts.map +0 -1
- package/dist/api/newsletter/routes/index.js +0 -9
- package/dist/api/newsletter/routes/public.routes.d.ts +0 -4
- package/dist/api/newsletter/routes/public.routes.d.ts.map +0 -1
- package/dist/api/newsletter/routes/public.routes.js +0 -9
- package/dist/api/shop/handlers/assets.handler.d.ts +0 -3
- package/dist/api/shop/handlers/assets.handler.d.ts.map +0 -1
- package/dist/api/shop/handlers/assets.handler.js +0 -62
- package/dist/api/shop/handlers/checkout.handler.d.ts +0 -10
- package/dist/api/shop/handlers/checkout.handler.d.ts.map +0 -1
- package/dist/api/shop/handlers/checkout.handler.js +0 -50
- package/dist/api/shop/handlers/customers.handler.d.ts +0 -17
- package/dist/api/shop/handlers/customers.handler.d.ts.map +0 -1
- package/dist/api/shop/handlers/customers.handler.js +0 -89
- package/dist/api/shop/handlers/orders.handler.d.ts +0 -17
- package/dist/api/shop/handlers/orders.handler.d.ts.map +0 -1
- package/dist/api/shop/handlers/orders.handler.js +0 -84
- package/dist/api/shop/handlers/stats.handler.d.ts +0 -10
- package/dist/api/shop/handlers/stats.handler.d.ts.map +0 -1
- package/dist/api/shop/handlers/stats.handler.js +0 -71
- package/dist/api/shop/handlers/webhook.handler.d.ts +0 -10
- package/dist/api/shop/handlers/webhook.handler.d.ts.map +0 -1
- package/dist/api/shop/handlers/webhook.handler.js +0 -233
- package/dist/cms-assets.d.ts +0 -4
- package/dist/cms-assets.d.ts.map +0 -1
- package/dist/cms-assets.js +0 -5
- package/dist/cms-bundle.d.ts +0 -3
- package/dist/cms-bundle.d.ts.map +0 -1
- package/dist/cms-bundle.js +0 -4
- package/dist/config-loader.d.ts +0 -26
- package/dist/config-loader.d.ts.map +0 -1
- package/dist/config-loader.js +0 -98
- package/dist/jwt.d.ts +0 -26
- package/dist/jwt.d.ts.map +0 -1
- package/dist/jwt.js +0 -102
- package/dist/lib/plunk.d.ts +0 -204
- package/dist/lib/plunk.d.ts.map +0 -1
- package/dist/lib/plunk.js +0 -259
- package/dist/template.d.ts +0 -2
- package/dist/template.d.ts.map +0 -1
- package/dist/template.js +0 -61
- package/dist/utils.d.ts +0 -23
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js +0 -110
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { getShopConfig } from '../../../lib/config';
|
|
2
|
-
import Stripe from 'stripe';
|
|
3
|
-
function getStripe(env) {
|
|
4
|
-
const config = getShopConfig(env);
|
|
5
|
-
return new Stripe(config.stripeSecretKey, {
|
|
6
|
-
apiVersion: '2025-02-24.acacia'
|
|
7
|
-
});
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* GET /api/shop/admin/stats
|
|
11
|
-
* Get shop statistics
|
|
12
|
-
*/
|
|
13
|
-
export async function getStats(c) {
|
|
14
|
-
try {
|
|
15
|
-
const stripe = getStripe(c.env);
|
|
16
|
-
// Fetch data in parallel
|
|
17
|
-
const [products, activeProducts, paymentIntents] = await Promise.all([
|
|
18
|
-
stripe.products.list({ limit: 1 }),
|
|
19
|
-
stripe.products.list({ active: true, limit: 1 }),
|
|
20
|
-
stripe.paymentIntents.list({
|
|
21
|
-
limit: 100,
|
|
22
|
-
expand: ['data.customer']
|
|
23
|
-
})
|
|
24
|
-
]);
|
|
25
|
-
// Get total counts (using total_count if available)
|
|
26
|
-
const totalProducts = products.data.length > 0 ? (await stripe.products.list({ limit: 100 })).data.length : 0;
|
|
27
|
-
const activeProductCount = activeProducts.data.length > 0 ? (await stripe.products.list({ active: true, limit: 100 })).data.length : 0;
|
|
28
|
-
// Calculate order stats
|
|
29
|
-
const succeededPayments = paymentIntents.data.filter((pi) => pi.status === 'succeeded');
|
|
30
|
-
const totalRevenue = succeededPayments.reduce((sum, pi) => sum + pi.amount_received, 0);
|
|
31
|
-
// Get default currency from first payment or default to USD
|
|
32
|
-
const currency = succeededPayments[0]?.currency || 'usd';
|
|
33
|
-
// Map recent orders
|
|
34
|
-
const recentOrders = paymentIntents.data.slice(0, 10).map((pi) => {
|
|
35
|
-
let customerEmail = pi.receipt_email || null;
|
|
36
|
-
let customerName = null;
|
|
37
|
-
if (typeof pi.customer !== 'string' && pi.customer && !pi.customer.deleted) {
|
|
38
|
-
const customer = pi.customer;
|
|
39
|
-
customerEmail = customerEmail || customer.email || null;
|
|
40
|
-
customerName = customer.name || null;
|
|
41
|
-
}
|
|
42
|
-
return {
|
|
43
|
-
id: pi.id,
|
|
44
|
-
customerId: typeof pi.customer === 'string' ? pi.customer : pi.customer?.id || null,
|
|
45
|
-
customerEmail,
|
|
46
|
-
customerName,
|
|
47
|
-
amount: pi.amount,
|
|
48
|
-
amountReceived: pi.amount_received,
|
|
49
|
-
currency: pi.currency,
|
|
50
|
-
status: pi.status,
|
|
51
|
-
paymentMethod: typeof pi.payment_method === 'string' ? pi.payment_method : pi.payment_method?.id || null,
|
|
52
|
-
lineItems: [],
|
|
53
|
-
metadata: pi.metadata || {},
|
|
54
|
-
createdAt: pi.created
|
|
55
|
-
};
|
|
56
|
-
});
|
|
57
|
-
const response = {
|
|
58
|
-
totalProducts,
|
|
59
|
-
activeProducts: activeProductCount,
|
|
60
|
-
totalOrders: succeededPayments.length,
|
|
61
|
-
totalRevenue,
|
|
62
|
-
currency,
|
|
63
|
-
recentOrders
|
|
64
|
-
};
|
|
65
|
-
return c.json(response);
|
|
66
|
-
}
|
|
67
|
-
catch (error) {
|
|
68
|
-
console.error('Failed to fetch stats:', error);
|
|
69
|
-
return c.json({ success: false, error: 'Failed to fetch stats' }, 500);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { Context } from 'hono';
|
|
2
|
-
import type { Bindings } from '../../../index';
|
|
3
|
-
/**
|
|
4
|
-
* POST /api/shop/webhook
|
|
5
|
-
* Handle Stripe webhook events
|
|
6
|
-
*/
|
|
7
|
-
export declare function handleStripeWebhook(c: Context<{
|
|
8
|
-
Bindings: Bindings;
|
|
9
|
-
}>): Promise<Response>;
|
|
10
|
-
//# sourceMappingURL=webhook.handler.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"webhook.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/shop/handlers/webhook.handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAkC/C;;;GAGG;AACH,wBAAsB,mBAAmB,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAsC/F"}
|
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
import { getShopConfig, getEmailsConfig, getGitHubConfig } from '../../../lib/config';
|
|
2
|
-
import { createGitHubClient } from '../../../lib/github';
|
|
3
|
-
import Stripe from 'stripe';
|
|
4
|
-
import { generateInvoicePdf } from '../../emails/handlers/invoice.handler';
|
|
5
|
-
function getStripe(env) {
|
|
6
|
-
const config = getShopConfig(env);
|
|
7
|
-
return new Stripe(config.stripeSecretKey, {
|
|
8
|
-
apiVersion: '2025-02-24.acacia'
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
// Default invoice configuration
|
|
12
|
-
const DEFAULT_INVOICE_CONFIG = {
|
|
13
|
-
showLogo: true,
|
|
14
|
-
primaryColor: '#1f2937',
|
|
15
|
-
accentColor: '#3b82f6',
|
|
16
|
-
logoPosition: 'left',
|
|
17
|
-
fontFamily: 'helvetica',
|
|
18
|
-
companyName: 'Your Company',
|
|
19
|
-
companyAddress: '123 Main Street, City, Country',
|
|
20
|
-
companyEmail: 'contact@company.com',
|
|
21
|
-
companyPhone: '+1 234 567 890',
|
|
22
|
-
currencySymbol: '€',
|
|
23
|
-
currencyPosition: 'before',
|
|
24
|
-
showTaxBreakdown: true,
|
|
25
|
-
taxLabel: 'VAT',
|
|
26
|
-
taxRate: 19,
|
|
27
|
-
dueDays: 14,
|
|
28
|
-
footerText: 'Thank you for your business!'
|
|
29
|
-
};
|
|
30
|
-
/**
|
|
31
|
-
* POST /api/shop/webhook
|
|
32
|
-
* Handle Stripe webhook events
|
|
33
|
-
*/
|
|
34
|
-
export async function handleStripeWebhook(c) {
|
|
35
|
-
const stripe = getStripe(c.env);
|
|
36
|
-
const webhookSecret = c.env.HG_SHOP_STRIPE_WEBHOOK_SECRET;
|
|
37
|
-
if (!webhookSecret) {
|
|
38
|
-
console.error('Stripe webhook secret not configured');
|
|
39
|
-
return c.json({ error: 'Webhook not configured' }, 500);
|
|
40
|
-
}
|
|
41
|
-
// Get the raw body and signature
|
|
42
|
-
const signature = c.req.header('stripe-signature');
|
|
43
|
-
if (!signature) {
|
|
44
|
-
return c.json({ error: 'Missing stripe-signature header' }, 400);
|
|
45
|
-
}
|
|
46
|
-
const rawBody = await c.req.text();
|
|
47
|
-
let event;
|
|
48
|
-
try {
|
|
49
|
-
event = await stripe.webhooks.constructEventAsync(rawBody, signature, webhookSecret);
|
|
50
|
-
}
|
|
51
|
-
catch (err) {
|
|
52
|
-
console.error('Webhook signature verification failed:', err.message);
|
|
53
|
-
return c.json({ error: 'Webhook signature verification failed' }, 400);
|
|
54
|
-
}
|
|
55
|
-
// Handle the event
|
|
56
|
-
switch (event.type) {
|
|
57
|
-
case 'checkout.session.completed': {
|
|
58
|
-
const session = event.data.object;
|
|
59
|
-
await handleCheckoutSessionCompleted(c, stripe, session);
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
// Add more event handlers as needed
|
|
63
|
-
default:
|
|
64
|
-
console.log(`Unhandled event type: ${event.type}`);
|
|
65
|
-
}
|
|
66
|
-
return c.json({ received: true });
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Handle checkout.session.completed event
|
|
70
|
-
* Generate and send invoice PDF to customer
|
|
71
|
-
*/
|
|
72
|
-
async function handleCheckoutSessionCompleted(c, stripe, session) {
|
|
73
|
-
try {
|
|
74
|
-
// Get customer email
|
|
75
|
-
const customerEmail = session.customer_details?.email;
|
|
76
|
-
if (!customerEmail) {
|
|
77
|
-
console.error('No customer email in checkout session');
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
// Get line items
|
|
81
|
-
const lineItems = await stripe.checkout.sessions.listLineItems(session.id, {
|
|
82
|
-
expand: ['data.price.product']
|
|
83
|
-
});
|
|
84
|
-
// Get invoice template and branding from GitHub
|
|
85
|
-
let template;
|
|
86
|
-
let branding;
|
|
87
|
-
try {
|
|
88
|
-
const gitConfig = getGitHubConfig(c.env);
|
|
89
|
-
const github = createGitHubClient(gitConfig);
|
|
90
|
-
const settingsPath = `emails/settings.json`;
|
|
91
|
-
const result = await github.readJsonFile(settingsPath);
|
|
92
|
-
if (result?.data.invoice) {
|
|
93
|
-
template = result.data.invoice;
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
template = {
|
|
97
|
-
id: 'invoice',
|
|
98
|
-
config: DEFAULT_INVOICE_CONFIG,
|
|
99
|
-
createdAt: Date.now(),
|
|
100
|
-
updatedAt: Date.now()
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
if (result?.data.branding) {
|
|
104
|
-
branding = result.data.branding;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
catch (error) {
|
|
108
|
-
console.error('Failed to fetch settings from GitHub:', error);
|
|
109
|
-
template = {
|
|
110
|
-
id: 'invoice',
|
|
111
|
-
config: DEFAULT_INVOICE_CONFIG,
|
|
112
|
-
createdAt: Date.now(),
|
|
113
|
-
updatedAt: Date.now()
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
// Build invoice data
|
|
117
|
-
const invoiceItems = lineItems.data.map((item) => {
|
|
118
|
-
const product = item.price?.product;
|
|
119
|
-
return {
|
|
120
|
-
description: product?.name || item.description || 'Item',
|
|
121
|
-
quantity: item.quantity || 1,
|
|
122
|
-
unitPrice: (item.price?.unit_amount || 0) / 100,
|
|
123
|
-
total: (item.amount_total || 0) / 100
|
|
124
|
-
};
|
|
125
|
-
});
|
|
126
|
-
const subtotal = (session.amount_subtotal || 0) / 100;
|
|
127
|
-
const tax = session.total_details?.amount_tax ? session.total_details.amount_tax / 100 : undefined;
|
|
128
|
-
const total = (session.amount_total || 0) / 100;
|
|
129
|
-
const invoiceData = {
|
|
130
|
-
invoiceNumber: `INV-${session.id.slice(-8).toUpperCase()}`,
|
|
131
|
-
invoiceDate: new Date().toLocaleDateString('en-US', {
|
|
132
|
-
year: 'numeric',
|
|
133
|
-
month: 'long',
|
|
134
|
-
day: 'numeric'
|
|
135
|
-
}),
|
|
136
|
-
customerName: session.customer_details?.name || 'Customer',
|
|
137
|
-
customerEmail,
|
|
138
|
-
customerAddress: session.customer_details?.address
|
|
139
|
-
? formatAddress(session.customer_details.address)
|
|
140
|
-
: undefined,
|
|
141
|
-
items: invoiceItems,
|
|
142
|
-
subtotal,
|
|
143
|
-
tax,
|
|
144
|
-
total,
|
|
145
|
-
currency: session.currency?.toUpperCase() || 'EUR',
|
|
146
|
-
paymentMethod: session.payment_method_types?.[0] || 'card'
|
|
147
|
-
};
|
|
148
|
-
// Generate PDF with branding
|
|
149
|
-
const pdfBytes = await generateInvoicePdf(template, invoiceData, branding ? {
|
|
150
|
-
logo: branding.logo,
|
|
151
|
-
companyName: branding.config.companyName,
|
|
152
|
-
primaryColor: branding.config.primaryColor,
|
|
153
|
-
} : undefined);
|
|
154
|
-
// Send email with PDF attachment
|
|
155
|
-
await sendInvoiceEmail(c, customerEmail, invoiceData, pdfBytes);
|
|
156
|
-
console.log(`Invoice sent to ${customerEmail} for session ${session.id}`);
|
|
157
|
-
}
|
|
158
|
-
catch (error) {
|
|
159
|
-
console.error('Failed to handle checkout session completed:', error);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Format Stripe address object to string
|
|
164
|
-
*/
|
|
165
|
-
function formatAddress(address) {
|
|
166
|
-
const parts = [];
|
|
167
|
-
if (address.line1)
|
|
168
|
-
parts.push(address.line1);
|
|
169
|
-
if (address.line2)
|
|
170
|
-
parts.push(address.line2);
|
|
171
|
-
if (address.city)
|
|
172
|
-
parts.push(address.city);
|
|
173
|
-
if (address.postal_code)
|
|
174
|
-
parts.push(address.postal_code);
|
|
175
|
-
if (address.country)
|
|
176
|
-
parts.push(address.country);
|
|
177
|
-
return parts.join(', ');
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* Send invoice email with PDF attachment
|
|
181
|
-
*/
|
|
182
|
-
async function sendInvoiceEmail(c, to, invoiceData, pdfBytes) {
|
|
183
|
-
const emailsConfig = getEmailsConfig(c.env);
|
|
184
|
-
const apiKey = emailsConfig.plunkSecretKey;
|
|
185
|
-
const fromEmail = emailsConfig.fromEmail;
|
|
186
|
-
const fromName = emailsConfig.fromName || 'Shop';
|
|
187
|
-
if (!apiKey || !fromEmail) {
|
|
188
|
-
console.error('Email not configured');
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
// Convert PDF to base64
|
|
192
|
-
const pdfBase64 = btoa(String.fromCharCode(...pdfBytes));
|
|
193
|
-
// Build email HTML
|
|
194
|
-
const html = `
|
|
195
|
-
<div style="font-family: sans-serif; max-width: 600px; margin: 0 auto; padding: 20px;">
|
|
196
|
-
<h1 style="color: #1f2937;">Thank you for your purchase!</h1>
|
|
197
|
-
<p style="color: #4b5563;">Dear ${invoiceData.customerName},</p>
|
|
198
|
-
<p style="color: #4b5563;">Thank you for your order. Please find your invoice attached to this email.</p>
|
|
199
|
-
|
|
200
|
-
<div style="background-color: #f3f4f6; padding: 20px; border-radius: 8px; margin: 20px 0;">
|
|
201
|
-
<p style="margin: 0; color: #1f2937;"><strong>Invoice Number:</strong> ${invoiceData.invoiceNumber}</p>
|
|
202
|
-
<p style="margin: 8px 0; color: #1f2937;"><strong>Date:</strong> ${invoiceData.invoiceDate}</p>
|
|
203
|
-
<p style="margin: 8px 0; color: #1f2937;"><strong>Total:</strong> ${invoiceData.currency} ${invoiceData.total.toFixed(2)}</p>
|
|
204
|
-
</div>
|
|
205
|
-
|
|
206
|
-
<p style="color: #4b5563;">If you have any questions about your order, please don't hesitate to contact us.</p>
|
|
207
|
-
|
|
208
|
-
<p style="color: #9ca3af; font-size: 12px; margin-top: 40px;">
|
|
209
|
-
This is an automated email. Please do not reply directly to this message.
|
|
210
|
-
</p>
|
|
211
|
-
</div>
|
|
212
|
-
`;
|
|
213
|
-
// Send via Plunk API
|
|
214
|
-
const payload = {
|
|
215
|
-
to,
|
|
216
|
-
subject: `Your Invoice ${invoiceData.invoiceNumber}`,
|
|
217
|
-
body: html,
|
|
218
|
-
from: fromEmail,
|
|
219
|
-
name: fromName,
|
|
220
|
-
};
|
|
221
|
-
const response = await fetch('https://api.useplunk.com/v1/send', {
|
|
222
|
-
method: 'POST',
|
|
223
|
-
headers: {
|
|
224
|
-
Authorization: `Bearer ${apiKey}`,
|
|
225
|
-
'Content-Type': 'application/json'
|
|
226
|
-
},
|
|
227
|
-
body: JSON.stringify(payload)
|
|
228
|
-
});
|
|
229
|
-
if (!response.ok) {
|
|
230
|
-
const error = await response.text();
|
|
231
|
-
throw new Error(`Failed to send invoice email: ${error}`);
|
|
232
|
-
}
|
|
233
|
-
}
|