@hintergrund/worker 0.1.0 → 0.1.2
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/newsletter/handlers/emails.handler.d.ts +13 -3
- package/dist/api/newsletter/handlers/emails.handler.d.ts.map +1 -1
- package/dist/api/newsletter/handlers/emails.handler.js +140 -12
- package/dist/api/newsletter/handlers/invoice.handler.d.ts +22 -0
- package/dist/api/newsletter/handlers/invoice.handler.d.ts.map +1 -0
- package/dist/api/newsletter/handlers/invoice.handler.js +455 -0
- package/dist/api/newsletter/handlers/public.handler.d.ts +4 -4
- package/dist/api/newsletter/handlers/public.handler.d.ts.map +1 -1
- package/dist/api/newsletter/handlers/public.handler.js +78 -20
- package/dist/api/newsletter/handlers/send.handler.d.ts +2 -2
- package/dist/api/newsletter/handlers/send.handler.js +3 -3
- package/dist/api/newsletter/handlers/subscribers.handler.d.ts +4 -4
- package/dist/api/newsletter/handlers/subscribers.handler.d.ts.map +1 -1
- package/dist/api/newsletter/handlers/subscribers.handler.js +8 -4
- package/dist/api/newsletter/routes/admin.routes.d.ts.map +1 -1
- package/dist/api/newsletter/routes/admin.routes.js +8 -1
- package/dist/api/shop/handlers/assets.handler.d.ts +3 -0
- package/dist/api/shop/handlers/assets.handler.d.ts.map +1 -0
- package/dist/api/shop/handlers/assets.handler.js +62 -0
- package/dist/api/shop/handlers/checkout.handler.d.ts +10 -0
- package/dist/api/shop/handlers/checkout.handler.d.ts.map +1 -0
- package/dist/api/shop/handlers/checkout.handler.js +50 -0
- package/dist/api/shop/handlers/products.handler.d.ts +0 -7
- package/dist/api/shop/handlers/products.handler.d.ts.map +1 -1
- package/dist/api/shop/handlers/products.handler.js +0 -23
- package/dist/api/shop/handlers/public.handler.d.ts +17 -0
- package/dist/api/shop/handlers/public.handler.d.ts.map +1 -0
- package/dist/api/shop/handlers/public.handler.js +108 -0
- package/dist/api/shop/handlers/webhook.handler.d.ts +10 -0
- package/dist/api/shop/handlers/webhook.handler.d.ts.map +1 -0
- package/dist/api/shop/handlers/webhook.handler.js +227 -0
- package/dist/api/shop/routes.d.ts.map +1 -1
- package/dist/api/shop/routes.js +9 -12
- package/dist/index.js +4 -4
- package/dist/lib/config.js +3 -3
- package/dist/middleware/auth.js +2 -2
- package/package.json +3 -2
|
@@ -40,7 +40,7 @@ function toResendAttachments(images) {
|
|
|
40
40
|
}));
|
|
41
41
|
}
|
|
42
42
|
/**
|
|
43
|
-
* POST /api/
|
|
43
|
+
* POST /api/email/admin/send
|
|
44
44
|
* Send bulk email to all subscribed users (admin only)
|
|
45
45
|
*/
|
|
46
46
|
export async function sendBulkEmail(c) {
|
|
@@ -72,7 +72,7 @@ export async function sendBulkEmail(c) {
|
|
|
72
72
|
for (const subscriber of subscribers) {
|
|
73
73
|
try {
|
|
74
74
|
// Append unsubscribe link to HTML
|
|
75
|
-
const unsubscribeUrl = `${c.req.url.split('/api')[0]}/api/
|
|
75
|
+
const unsubscribeUrl = `${c.req.url.split('/api')[0]}/api/email/unsubscribe?token=${subscriber.unsubscribe_token}`;
|
|
76
76
|
const htmlWithUnsubscribe = `
|
|
77
77
|
${html}
|
|
78
78
|
<hr style="margin-top: 2rem; margin-bottom: 1rem; border: none; border-top: 1px solid #eee;">
|
|
@@ -112,7 +112,7 @@ export async function sendBulkEmail(c) {
|
|
|
112
112
|
}
|
|
113
113
|
}
|
|
114
114
|
/**
|
|
115
|
-
* POST /api/
|
|
115
|
+
* POST /api/email/admin/emails/test
|
|
116
116
|
* Send test email to a single email address
|
|
117
117
|
*/
|
|
118
118
|
export async function sendTestEmail(c) {
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
import { Context } from 'hono';
|
|
2
2
|
/**
|
|
3
|
-
* GET /api/
|
|
3
|
+
* GET /api/email/admin/subscribers
|
|
4
4
|
* Get all subscribers (admin only)
|
|
5
5
|
*/
|
|
6
6
|
export declare function getSubscribers(c: Context): Promise<Response>;
|
|
7
7
|
/**
|
|
8
|
-
* POST /api/
|
|
8
|
+
* POST /api/email/admin/subscribers
|
|
9
9
|
* Manually add subscriber (admin only, bypasses confirmation)
|
|
10
10
|
*/
|
|
11
11
|
export declare function addSubscriber(c: Context): Promise<Response>;
|
|
12
12
|
/**
|
|
13
|
-
* DELETE /api/
|
|
13
|
+
* DELETE /api/email/admin/subscribers/:id
|
|
14
14
|
* Delete subscriber by ID (admin only)
|
|
15
15
|
*/
|
|
16
16
|
export declare function deleteSubscriber(c: Context): Promise<Response>;
|
|
17
17
|
/**
|
|
18
|
-
* PATCH /api/
|
|
18
|
+
* PATCH /api/email/admin/subscribers/:id
|
|
19
19
|
* Update subscriber (admin only)
|
|
20
20
|
*/
|
|
21
21
|
export declare function updateSubscriber(c: Context): Promise<Response>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscribers.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/newsletter/handlers/subscribers.handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAwC/B;;;GAGG;AACH,wBAAsB,cAAc,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAmBlE;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CA6DjE;AAED;;;GAGG;AACH,wBAAsB,gBAAgB,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CA+BpE;AAED;;;GAGG;AACH,wBAAsB,gBAAgB,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,
|
|
1
|
+
{"version":3,"file":"subscribers.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/newsletter/handlers/subscribers.handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAwC/B;;;GAGG;AACH,wBAAsB,cAAc,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAmBlE;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CA6DjE;AAED;;;GAGG;AACH,wBAAsB,gBAAgB,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CA+BpE;AAED;;;GAGG;AACH,wBAAsB,gBAAgB,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CA+EpE"}
|
|
@@ -32,7 +32,7 @@ function parseSubscriber(row) {
|
|
|
32
32
|
};
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
|
-
* GET /api/
|
|
35
|
+
* GET /api/email/admin/subscribers
|
|
36
36
|
* Get all subscribers (admin only)
|
|
37
37
|
*/
|
|
38
38
|
export async function getSubscribers(c) {
|
|
@@ -51,7 +51,7 @@ export async function getSubscribers(c) {
|
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
/**
|
|
54
|
-
* POST /api/
|
|
54
|
+
* POST /api/email/admin/subscribers
|
|
55
55
|
* Manually add subscriber (admin only, bypasses confirmation)
|
|
56
56
|
*/
|
|
57
57
|
export async function addSubscriber(c) {
|
|
@@ -103,7 +103,7 @@ export async function addSubscriber(c) {
|
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
105
|
/**
|
|
106
|
-
* DELETE /api/
|
|
106
|
+
* DELETE /api/email/admin/subscribers/:id
|
|
107
107
|
* Delete subscriber by ID (admin only)
|
|
108
108
|
*/
|
|
109
109
|
export async function deleteSubscriber(c) {
|
|
@@ -131,7 +131,7 @@ export async function deleteSubscriber(c) {
|
|
|
131
131
|
}
|
|
132
132
|
}
|
|
133
133
|
/**
|
|
134
|
-
* PATCH /api/
|
|
134
|
+
* PATCH /api/email/admin/subscribers/:id
|
|
135
135
|
* Update subscriber (admin only)
|
|
136
136
|
*/
|
|
137
137
|
export async function updateSubscriber(c) {
|
|
@@ -162,6 +162,10 @@ export async function updateSubscriber(c) {
|
|
|
162
162
|
updates.push('subscribed_at = ?');
|
|
163
163
|
params.push(Date.now());
|
|
164
164
|
}
|
|
165
|
+
else if (body.status === 'unsubscribed') {
|
|
166
|
+
updates.push('unsubscribed_at = ?');
|
|
167
|
+
params.push(Date.now());
|
|
168
|
+
}
|
|
165
169
|
}
|
|
166
170
|
// Always update updated_at
|
|
167
171
|
updates.push('updated_at = ?');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"admin.routes.d.ts","sourceRoot":"","sources":["../../../../src/api/newsletter/routes/admin.routes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"admin.routes.d.ts","sourceRoot":"","sources":["../../../../src/api/newsletter/routes/admin.routes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAO5B,QAAA,MAAM,WAAW,4EAAa,CAAC;AA4B/B,eAAe,WAAW,CAAC"}
|
|
@@ -2,7 +2,8 @@ import { Hono } from 'hono';
|
|
|
2
2
|
import { authMiddleware } from '../../../middleware/auth';
|
|
3
3
|
import { getSubscribers, addSubscriber, deleteSubscriber, updateSubscriber } from '../handlers/subscribers.handler';
|
|
4
4
|
import { sendBulkEmail, sendTestEmail } from '../handlers/send.handler';
|
|
5
|
-
import { getEmails, saveDraft, deleteEmail } from '../handlers/emails.handler';
|
|
5
|
+
import { getEmails, saveDraft, deleteEmail, getTemplate, saveTemplate } from '../handlers/emails.handler';
|
|
6
|
+
import { getInvoiceTemplate, saveInvoiceTemplate } from '../handlers/invoice.handler';
|
|
6
7
|
const adminRoutes = new Hono();
|
|
7
8
|
// Protect all admin routes with authentication
|
|
8
9
|
adminRoutes.use('*', authMiddleware);
|
|
@@ -18,4 +19,10 @@ adminRoutes.get('/emails', getEmails);
|
|
|
18
19
|
adminRoutes.post('/emails/draft', saveDraft);
|
|
19
20
|
adminRoutes.post('/emails/test', sendTestEmail);
|
|
20
21
|
adminRoutes.delete('/emails/:id', deleteEmail);
|
|
22
|
+
// Admin template settings endpoints
|
|
23
|
+
adminRoutes.get('/template', getTemplate);
|
|
24
|
+
adminRoutes.put('/template', saveTemplate);
|
|
25
|
+
// Admin invoice template endpoints
|
|
26
|
+
adminRoutes.get('/invoice-template', getInvoiceTemplate);
|
|
27
|
+
adminRoutes.put('/invoice-template', saveInvoiceTemplate);
|
|
21
28
|
export default adminRoutes;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assets.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/shop/handlers/assets.handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAI/B,wBAAsB,eAAe,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAwEnE"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import Stripe from 'stripe';
|
|
2
|
+
import { getShopConfig } from '../../../lib/config';
|
|
3
|
+
export async function uploadShopAsset(c) {
|
|
4
|
+
try {
|
|
5
|
+
const config = getShopConfig(c.env);
|
|
6
|
+
const stripe = new Stripe(config.stripeSecretKey, {
|
|
7
|
+
apiVersion: '2025-02-24.acacia'
|
|
8
|
+
});
|
|
9
|
+
const { file, content, mimeType } = await c.req.json();
|
|
10
|
+
const filename = file || (await c.req.json()).name;
|
|
11
|
+
if (!filename || !content) {
|
|
12
|
+
return new Response('Missing filename or content', { status: 400 });
|
|
13
|
+
}
|
|
14
|
+
// Convert base64 to Uint8Array/Blob for Stripe
|
|
15
|
+
const binaryString = atob(content);
|
|
16
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
17
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
18
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
19
|
+
}
|
|
20
|
+
const fileObject = new File([bytes], filename, { type: mimeType });
|
|
21
|
+
// Upload to Stripe Files API
|
|
22
|
+
const stripeFile = await stripe.files.create({
|
|
23
|
+
file: {
|
|
24
|
+
data: bytes,
|
|
25
|
+
name: filename,
|
|
26
|
+
type: mimeType
|
|
27
|
+
},
|
|
28
|
+
purpose: 'product_image'
|
|
29
|
+
});
|
|
30
|
+
// Create a public file link so the image is accessible
|
|
31
|
+
const fileLink = await stripe.fileLinks.create({
|
|
32
|
+
file: stripeFile.id
|
|
33
|
+
});
|
|
34
|
+
const url = fileLink.url;
|
|
35
|
+
console.log('[Upload] Stripe File ID:', stripeFile.id);
|
|
36
|
+
console.log('[Upload] Generated URL:', url);
|
|
37
|
+
console.log('[Upload] File Type:', mimeType);
|
|
38
|
+
console.log('[Upload] Original Name:', filename);
|
|
39
|
+
if (!url) {
|
|
40
|
+
throw new Error('Failed to get URL from Stripe File upload');
|
|
41
|
+
}
|
|
42
|
+
return new Response(JSON.stringify({
|
|
43
|
+
success: true,
|
|
44
|
+
url,
|
|
45
|
+
message: `Uploaded ${filename} to Stripe`
|
|
46
|
+
}), {
|
|
47
|
+
status: 200,
|
|
48
|
+
headers: {
|
|
49
|
+
'Content-Type': 'application/json'
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
console.error('Shop asset upload error:', error);
|
|
55
|
+
return new Response(JSON.stringify({ error: error instanceof Error ? error.message : String(error) }), {
|
|
56
|
+
status: 500,
|
|
57
|
+
headers: {
|
|
58
|
+
'Content-Type': 'application/json'
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Context } from 'hono';
|
|
2
|
+
import type { Bindings } from '../../../index';
|
|
3
|
+
/**
|
|
4
|
+
* POST /api/shop/checkout
|
|
5
|
+
* Create a Stripe Checkout Session
|
|
6
|
+
*/
|
|
7
|
+
export declare function createCheckoutSession(c: Context<{
|
|
8
|
+
Bindings: Bindings;
|
|
9
|
+
}>): Promise<Response>;
|
|
10
|
+
//# sourceMappingURL=checkout.handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checkout.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/shop/handlers/checkout.handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAW/C;;;GAGG;AACH,wBAAsB,qBAAqB,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CA8CjG"}
|
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
* POST /api/shop/checkout
|
|
11
|
+
* Create a Stripe Checkout Session
|
|
12
|
+
*/
|
|
13
|
+
export async function createCheckoutSession(c) {
|
|
14
|
+
try {
|
|
15
|
+
const stripe = getStripe(c.env);
|
|
16
|
+
const body = await c.req.json();
|
|
17
|
+
const { priceId, quantity = 1, successUrl, cancelUrl, customerEmail, metadata, mode = 'payment' } = body;
|
|
18
|
+
const items = body.items || [];
|
|
19
|
+
if (priceId) {
|
|
20
|
+
items.push({ priceId, quantity });
|
|
21
|
+
}
|
|
22
|
+
if (items.length === 0) {
|
|
23
|
+
return c.json({ success: false, error: 'No items provided' }, 400);
|
|
24
|
+
}
|
|
25
|
+
if (!successUrl || !cancelUrl) {
|
|
26
|
+
return c.json({ success: false, error: 'Missing successUrl or cancelUrl' }, 400);
|
|
27
|
+
}
|
|
28
|
+
const lineItems = items.map((item) => ({
|
|
29
|
+
price: item.priceId,
|
|
30
|
+
quantity: item.quantity,
|
|
31
|
+
}));
|
|
32
|
+
const sessionParams = {
|
|
33
|
+
line_items: lineItems,
|
|
34
|
+
mode: mode,
|
|
35
|
+
success_url: successUrl,
|
|
36
|
+
cancel_url: cancelUrl,
|
|
37
|
+
customer_email: customerEmail,
|
|
38
|
+
metadata: metadata,
|
|
39
|
+
allow_promotion_codes: true,
|
|
40
|
+
};
|
|
41
|
+
// If customer is logged in or provided, we could attach it.
|
|
42
|
+
// For now, simple checkout.
|
|
43
|
+
const session = await stripe.checkout.sessions.create(sessionParams);
|
|
44
|
+
return c.json({ url: session.url, sessionId: session.id });
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
console.error('Checkout error:', error);
|
|
48
|
+
return c.json({ success: false, error: error.message }, 500);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -1,12 +1,5 @@
|
|
|
1
1
|
import type { Context } from 'hono';
|
|
2
2
|
import type { Bindings } from '../../../index';
|
|
3
|
-
/**
|
|
4
|
-
* GET /api/shop/products
|
|
5
|
-
* List all active products (public endpoint)
|
|
6
|
-
*/
|
|
7
|
-
export declare function getPublicProducts(c: Context<{
|
|
8
|
-
Bindings: Bindings;
|
|
9
|
-
}>): Promise<Response>;
|
|
10
3
|
/**
|
|
11
4
|
* GET /api/shop/admin/products
|
|
12
5
|
* List all products
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"products.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/shop/handlers/products.handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"products.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/shop/handlers/products.handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AA+D/C;;;GAGG;AACH,wBAAsB,WAAW,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CA8BvF;AAED;;;GAGG;AACH,wBAAsB,UAAU,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAqBtF;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CA+CzF;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAmCzF;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CA4BzF"}
|
|
@@ -43,29 +43,6 @@ function mapStripeProduct(product, defaultPrice) {
|
|
|
43
43
|
defaultPrice: defaultPrice ? mapStripePrice(defaultPrice) : undefined
|
|
44
44
|
};
|
|
45
45
|
}
|
|
46
|
-
/**
|
|
47
|
-
* GET /api/shop/products
|
|
48
|
-
* List all active products (public endpoint)
|
|
49
|
-
*/
|
|
50
|
-
export async function getPublicProducts(c) {
|
|
51
|
-
try {
|
|
52
|
-
const stripe = getStripe(c.env);
|
|
53
|
-
const products = await stripe.products.list({
|
|
54
|
-
active: true,
|
|
55
|
-
limit: 100,
|
|
56
|
-
expand: ['data.default_price']
|
|
57
|
-
});
|
|
58
|
-
const response = {
|
|
59
|
-
products: products.data.map((p) => mapStripeProduct(p, p.default_price)),
|
|
60
|
-
hasMore: products.has_more
|
|
61
|
-
};
|
|
62
|
-
return c.json(response);
|
|
63
|
-
}
|
|
64
|
-
catch (error) {
|
|
65
|
-
console.error('Failed to fetch products:', error);
|
|
66
|
-
return c.json({ success: false, error: 'Failed to fetch products' }, 500);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
46
|
/**
|
|
70
47
|
* GET /api/shop/admin/products
|
|
71
48
|
* List all products
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Context } from 'hono';
|
|
2
|
+
import type { Bindings } from '../../../index';
|
|
3
|
+
/**
|
|
4
|
+
* GET /api/shop/products
|
|
5
|
+
* List all active products (public endpoint)
|
|
6
|
+
*/
|
|
7
|
+
export declare function getPublicProducts(c: Context<{
|
|
8
|
+
Bindings: Bindings;
|
|
9
|
+
}>): Promise<Response>;
|
|
10
|
+
/**
|
|
11
|
+
* POST /api/shop/checkout
|
|
12
|
+
* Create a Stripe Checkout Session
|
|
13
|
+
*/
|
|
14
|
+
export declare function createCheckoutSession(c: Context<{
|
|
15
|
+
Bindings: Bindings;
|
|
16
|
+
}>): Promise<Response>;
|
|
17
|
+
//# sourceMappingURL=public.handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"public.handler.d.ts","sourceRoot":"","sources":["../../../../src/api/shop/handlers/public.handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAuD/C;;;GAGG;AACH,wBAAsB,iBAAiB,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAsB7F;AAED;;;GAGG;AACH,wBAAsB,qBAAqB,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CA2CjG"}
|
|
@@ -0,0 +1,108 @@
|
|
|
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
|
+
function mapStripePrice(price) {
|
|
10
|
+
return {
|
|
11
|
+
id: price.id,
|
|
12
|
+
productId: typeof price.product === 'string' ? price.product : price.product?.id || '',
|
|
13
|
+
active: price.active,
|
|
14
|
+
currency: price.currency,
|
|
15
|
+
unitAmount: price.unit_amount,
|
|
16
|
+
unitAmountDecimal: price.unit_amount_decimal,
|
|
17
|
+
billingScheme: price.billing_scheme,
|
|
18
|
+
type: price.type,
|
|
19
|
+
recurring: price.recurring
|
|
20
|
+
? {
|
|
21
|
+
interval: price.recurring.interval,
|
|
22
|
+
intervalCount: price.recurring.interval_count
|
|
23
|
+
}
|
|
24
|
+
: undefined,
|
|
25
|
+
metadata: price.metadata || {},
|
|
26
|
+
createdAt: price.created
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function mapStripeProduct(product, defaultPrice) {
|
|
30
|
+
return {
|
|
31
|
+
id: product.id,
|
|
32
|
+
name: product.name,
|
|
33
|
+
description: product.description,
|
|
34
|
+
active: product.active,
|
|
35
|
+
defaultPriceId: typeof product.default_price === 'string' ? product.default_price : product.default_price?.id || null,
|
|
36
|
+
images: product.images,
|
|
37
|
+
metadata: product.metadata || {},
|
|
38
|
+
statementDescriptor: product.statement_descriptor || null,
|
|
39
|
+
unitLabel: product.unit_label || null,
|
|
40
|
+
url: product.url,
|
|
41
|
+
createdAt: product.created,
|
|
42
|
+
updatedAt: product.updated,
|
|
43
|
+
defaultPrice: defaultPrice ? mapStripePrice(defaultPrice) : undefined
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* GET /api/shop/products
|
|
48
|
+
* List all active products (public endpoint)
|
|
49
|
+
*/
|
|
50
|
+
export async function getPublicProducts(c) {
|
|
51
|
+
try {
|
|
52
|
+
const stripe = getStripe(c.env);
|
|
53
|
+
const products = await stripe.products.list({
|
|
54
|
+
active: true,
|
|
55
|
+
limit: 100,
|
|
56
|
+
expand: ['data.default_price']
|
|
57
|
+
});
|
|
58
|
+
const response = {
|
|
59
|
+
products: products.data.map((p) => mapStripeProduct(p, p.default_price)),
|
|
60
|
+
hasMore: products.has_more
|
|
61
|
+
};
|
|
62
|
+
return c.json(response);
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
console.error('Failed to fetch products:', error);
|
|
66
|
+
return c.json({ success: false, error: 'Failed to fetch products' }, 500);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* POST /api/shop/checkout
|
|
71
|
+
* Create a Stripe Checkout Session
|
|
72
|
+
*/
|
|
73
|
+
export async function createCheckoutSession(c) {
|
|
74
|
+
try {
|
|
75
|
+
const stripe = getStripe(c.env);
|
|
76
|
+
const body = await c.req.json();
|
|
77
|
+
const { priceId, quantity = 1, successUrl, cancelUrl, customerEmail, metadata, mode = 'payment' } = body;
|
|
78
|
+
const items = body.items || [];
|
|
79
|
+
if (priceId) {
|
|
80
|
+
items.push({ priceId, quantity });
|
|
81
|
+
}
|
|
82
|
+
if (items.length === 0) {
|
|
83
|
+
return c.json({ success: false, error: 'No items provided' }, 400);
|
|
84
|
+
}
|
|
85
|
+
if (!successUrl || !cancelUrl) {
|
|
86
|
+
return c.json({ success: false, error: 'Missing successUrl or cancelUrl' }, 400);
|
|
87
|
+
}
|
|
88
|
+
const lineItems = items.map((item) => ({
|
|
89
|
+
price: item.priceId,
|
|
90
|
+
quantity: item.quantity,
|
|
91
|
+
}));
|
|
92
|
+
const sessionParams = {
|
|
93
|
+
line_items: lineItems,
|
|
94
|
+
mode: mode,
|
|
95
|
+
success_url: successUrl,
|
|
96
|
+
cancel_url: cancelUrl,
|
|
97
|
+
customer_email: customerEmail,
|
|
98
|
+
metadata: metadata,
|
|
99
|
+
allow_promotion_codes: true,
|
|
100
|
+
};
|
|
101
|
+
const session = await stripe.checkout.sessions.create(sessionParams);
|
|
102
|
+
return c.json({ url: session.url, sessionId: session.id });
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
console.error('Checkout error:', error);
|
|
106
|
+
return c.json({ success: false, error: error.message }, 500);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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;AAa/C;;;GAGG;AACH,wBAAsB,mBAAmB,CAAC,CAAC,EAAE,OAAO,CAAC;IAAE,QAAQ,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAsC/F"}
|