@zodic/shared 0.0.393 → 0.0.395

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.
@@ -0,0 +1,116 @@
1
+ import { eq } from 'drizzle-orm';
2
+ import { injectable } from 'inversify';
3
+ import { AppContext } from '../base';
4
+ import { payments, userProducts } from '../../db/schema';
5
+
6
+ @injectable()
7
+ export class PaymentService {
8
+ constructor(private context: AppContext) {}
9
+
10
+ async handleCheckoutEvent(payload: any): Promise<void> {
11
+ const { event, checkout } = payload;
12
+ const checkoutId = checkout.id;
13
+
14
+ switch (event) {
15
+ case 'CHECKOUT_CREATED':
16
+ await this.handleCheckoutCreated(checkout);
17
+ break;
18
+ case 'CHECKOUT_PAID':
19
+ await this.handleCheckoutPaid(checkout);
20
+ break;
21
+ case 'CHECKOUT_CANCELED':
22
+ await this.handleCheckoutCanceled(checkout);
23
+ break;
24
+ case 'CHECKOUT_EXPIRED':
25
+ await this.handleCheckoutExpired(checkout);
26
+ break;
27
+ default:
28
+ console.log(`⚠️ Unsupported event: ${event}`);
29
+ return;
30
+ }
31
+ }
32
+
33
+ private async handleCheckoutCreated(checkout: any): Promise<void> {
34
+ const checkoutId = checkout.id;
35
+ const amount = checkout.items.reduce(
36
+ (sum: number, item: any) => sum + item.value * item.quantity,
37
+ 0
38
+ );
39
+ const db = this.context.drizzle();
40
+ // Create a payment record with 'pending' status
41
+ await db
42
+ .insert(payments)
43
+ .values({
44
+ id: `pay_${checkoutId}`,
45
+ userId: checkout.customer, // Assuming customer ID is the userId
46
+ checkoutId,
47
+ amount,
48
+ status: 'pending',
49
+ createdAt: new Date(),
50
+ updatedAt: new Date(),
51
+ })
52
+ .onConflictDoNothing(); // Avoid duplicate if already created
53
+
54
+ console.log(`✅ Created payment record for checkoutId: ${checkoutId}`);
55
+ }
56
+
57
+ private async handleCheckoutPaid(checkout: any): Promise<void> {
58
+ const checkoutId = checkout.id;
59
+ const db = this.context.drizzle();
60
+ // Update userProducts to 'unlocked'
61
+ await db
62
+ .update(userProducts)
63
+ .set({ status: 'unlocked', updatedAt: new Date() })
64
+ .where(eq(userProducts.checkoutId, checkoutId));
65
+
66
+ // Update payment to 'completed'
67
+ await db
68
+ .update(payments)
69
+ .set({ status: 'completed', updatedAt: new Date() })
70
+ .where(eq(payments.checkoutId, checkoutId));
71
+
72
+ console.log(
73
+ `✅ Updated userProducts to unlocked and payment to completed for checkoutId: ${checkoutId}`
74
+ );
75
+ }
76
+
77
+ private async handleCheckoutCanceled(checkout: any): Promise<void> {
78
+ const checkoutId = checkout.id;
79
+ const db = this.context.drizzle();
80
+ // Update userProducts to 'locked'
81
+ await db
82
+ .update(userProducts)
83
+ .set({ status: 'locked', updatedAt: new Date() })
84
+ .where(eq(userProducts.checkoutId, checkoutId));
85
+
86
+ // Update payment to 'canceled'
87
+ await db
88
+ .update(payments)
89
+ .set({ status: 'canceled', updatedAt: new Date() })
90
+ .where(eq(payments.checkoutId, checkoutId));
91
+
92
+ console.log(
93
+ `✅ Updated userProducts to locked and payment to canceled for checkoutId: ${checkoutId}`
94
+ );
95
+ }
96
+
97
+ private async handleCheckoutExpired(checkout: any): Promise<void> {
98
+ const checkoutId = checkout.id;
99
+ const db = this.context.drizzle();
100
+ // Update userProducts to 'locked'
101
+ await db
102
+ .update(userProducts)
103
+ .set({ status: 'locked', updatedAt: new Date() })
104
+ .where(eq(userProducts.checkoutId, checkoutId));
105
+
106
+ // Update payment to 'expired'
107
+ await db
108
+ .update(payments)
109
+ .set({ status: 'expired', updatedAt: new Date() })
110
+ .where(eq(payments.checkoutId, checkoutId));
111
+
112
+ console.log(
113
+ `✅ Updated userProducts to locked and payment to expired for checkoutId: ${checkoutId}`
114
+ );
115
+ }
116
+ }
@@ -3,3 +3,4 @@ export * from './ArtifactService';
3
3
  export * from './ConceptService';
4
4
  export * from './ImageDescriberService';
5
5
  export * from './LeonardoService';
6
+ export * from './PaymentService';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zodic/shared",
3
- "version": "0.0.393",
3
+ "version": "0.0.395",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -45,6 +45,7 @@ export type CentralBindings = {
45
45
  ORB_QUEUE: Queue;
46
46
  ARCHETYPE_POPULATION_QUEUE: Queue;
47
47
  FACESWAP_QUEUE: Queue;
48
+ PAYMENT_QUEUE: Queue;
48
49
 
49
50
  PROMPT_IMAGE_DESCRIBER: string;
50
51
  PROMPT_GENERATE_ARCHETYPE_BASIC_INFO: string;
@@ -154,6 +155,7 @@ export type BackendBindings = Env &
154
155
  | 'FACESWAP_QUEUE'
155
156
  | 'ASAAS_API_KEY'
156
157
  | 'ASAAS_API_URL'
158
+ | 'PAYMENT_QUEUE'
157
159
  >;
158
160
 
159
161
  export type BackendCtx = Context<