@abdssamie/adyen-payments 0.1.0
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/LICENSE +201 -0
- package/README.md +258 -0
- package/dist/client/_generated/_ignore.d.ts +1 -0
- package/dist/client/_generated/_ignore.d.ts.map +1 -0
- package/dist/client/_generated/_ignore.js +3 -0
- package/dist/client/_generated/_ignore.js.map +1 -0
- package/dist/client/index.d.ts +206 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +566 -0
- package/dist/client/index.js.map +1 -0
- package/dist/component/_generated/api.d.ts +36 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +215 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +121 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +78 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +3 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/private.d.ts +71 -0
- package/dist/component/private.d.ts.map +1 -0
- package/dist/component/private.js +250 -0
- package/dist/component/private.js.map +1 -0
- package/dist/component/public.d.ts +170 -0
- package/dist/component/public.d.ts.map +1 -0
- package/dist/component/public.js +210 -0
- package/dist/component/public.js.map +1 -0
- package/dist/component/schema.d.ts +101 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +63 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/react/hooks.d.ts +182 -0
- package/dist/react/hooks.d.ts.map +1 -0
- package/dist/react/hooks.js +215 -0
- package/dist/react/hooks.js.map +1 -0
- package/dist/react/index.d.ts +3 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +3 -0
- package/dist/react/index.js.map +1 -0
- package/package.json +104 -0
- package/src/client/_generated/_ignore.ts +1 -0
- package/src/client/index.test.ts +196 -0
- package/src/client/index.ts +823 -0
- package/src/client/setup.test.ts +26 -0
- package/src/client/webhooks.test.ts +182 -0
- package/src/component/_generated/api.ts +52 -0
- package/src/component/_generated/component.ts +293 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +156 -0
- package/src/component/convex.config.ts +3 -0
- package/src/component/private.ts +277 -0
- package/src/component/public.test.ts +92 -0
- package/src/component/public.ts +229 -0
- package/src/component/schema.ts +67 -0
- package/src/component/setup.test.ts +11 -0
- package/src/react/hooks.ts +488 -0
- package/src/react/index.ts +18 -0
- package/src/test.ts +18 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* Generated utilities for implementing server-side Convex query and mutation functions.
|
|
4
|
+
*
|
|
5
|
+
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
+
*
|
|
7
|
+
* To regenerate, run `npx convex dev`.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type {
|
|
12
|
+
ActionBuilder,
|
|
13
|
+
HttpActionBuilder,
|
|
14
|
+
MutationBuilder,
|
|
15
|
+
QueryBuilder,
|
|
16
|
+
GenericActionCtx,
|
|
17
|
+
GenericMutationCtx,
|
|
18
|
+
GenericQueryCtx,
|
|
19
|
+
GenericDatabaseReader,
|
|
20
|
+
GenericDatabaseWriter,
|
|
21
|
+
} from "convex/server";
|
|
22
|
+
import {
|
|
23
|
+
actionGeneric,
|
|
24
|
+
httpActionGeneric,
|
|
25
|
+
queryGeneric,
|
|
26
|
+
mutationGeneric,
|
|
27
|
+
internalActionGeneric,
|
|
28
|
+
internalMutationGeneric,
|
|
29
|
+
internalQueryGeneric,
|
|
30
|
+
} from "convex/server";
|
|
31
|
+
import type { DataModel } from "./dataModel.js";
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Define a query in this Convex app's public API.
|
|
35
|
+
*
|
|
36
|
+
* This function will be allowed to read your Convex database and will be accessible from the client.
|
|
37
|
+
*
|
|
38
|
+
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
|
|
39
|
+
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
|
|
40
|
+
*/
|
|
41
|
+
export const query: QueryBuilder<DataModel, "public"> = queryGeneric;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Define a query that is only accessible from other Convex functions (but not from the client).
|
|
45
|
+
*
|
|
46
|
+
* This function will be allowed to read from your Convex database. It will not be accessible from the client.
|
|
47
|
+
*
|
|
48
|
+
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
|
|
49
|
+
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
|
|
50
|
+
*/
|
|
51
|
+
export const internalQuery: QueryBuilder<DataModel, "internal"> =
|
|
52
|
+
internalQueryGeneric;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Define a mutation in this Convex app's public API.
|
|
56
|
+
*
|
|
57
|
+
* This function will be allowed to modify your Convex database and will be accessible from the client.
|
|
58
|
+
*
|
|
59
|
+
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
|
|
60
|
+
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
|
|
61
|
+
*/
|
|
62
|
+
export const mutation: MutationBuilder<DataModel, "public"> = mutationGeneric;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Define a mutation that is only accessible from other Convex functions (but not from the client).
|
|
66
|
+
*
|
|
67
|
+
* This function will be allowed to modify your Convex database. It will not be accessible from the client.
|
|
68
|
+
*
|
|
69
|
+
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
|
|
70
|
+
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
|
|
71
|
+
*/
|
|
72
|
+
export const internalMutation: MutationBuilder<DataModel, "internal"> =
|
|
73
|
+
internalMutationGeneric;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Define an action in this Convex app's public API.
|
|
77
|
+
*
|
|
78
|
+
* An action is a function which can execute any JavaScript code, including non-deterministic
|
|
79
|
+
* code and code with side-effects, like calling third-party services.
|
|
80
|
+
* They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive.
|
|
81
|
+
* They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}.
|
|
82
|
+
*
|
|
83
|
+
* @param func - The action. It receives an {@link ActionCtx} as its first argument.
|
|
84
|
+
* @returns The wrapped action. Include this as an `export` to name it and make it accessible.
|
|
85
|
+
*/
|
|
86
|
+
export const action: ActionBuilder<DataModel, "public"> = actionGeneric;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Define an action that is only accessible from other Convex functions (but not from the client).
|
|
90
|
+
*
|
|
91
|
+
* @param func - The function. It receives an {@link ActionCtx} as its first argument.
|
|
92
|
+
* @returns The wrapped function. Include this as an `export` to name it and make it accessible.
|
|
93
|
+
*/
|
|
94
|
+
export const internalAction: ActionBuilder<DataModel, "internal"> =
|
|
95
|
+
internalActionGeneric;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Define an HTTP action.
|
|
99
|
+
*
|
|
100
|
+
* The wrapped function will be used to respond to HTTP requests received
|
|
101
|
+
* by a Convex deployment if the requests matches the path and method where
|
|
102
|
+
* this action is routed. Be sure to route your httpAction in `convex/http.js`.
|
|
103
|
+
*
|
|
104
|
+
* @param func - The function. It receives an {@link ActionCtx} as its first argument
|
|
105
|
+
* and a Fetch API `Request` object as its second.
|
|
106
|
+
* @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
|
|
107
|
+
*/
|
|
108
|
+
export const httpAction: HttpActionBuilder = httpActionGeneric;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* A set of services for use within Convex query functions.
|
|
112
|
+
*
|
|
113
|
+
* The query context is passed as the first argument to any Convex query
|
|
114
|
+
* function run on the server.
|
|
115
|
+
*
|
|
116
|
+
* If you're using code generation, use the `QueryCtx` type in `convex/_generated/server.d.ts` instead.
|
|
117
|
+
*/
|
|
118
|
+
export type QueryCtx = GenericQueryCtx<DataModel>;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* A set of services for use within Convex mutation functions.
|
|
122
|
+
*
|
|
123
|
+
* The mutation context is passed as the first argument to any Convex mutation
|
|
124
|
+
* function run on the server.
|
|
125
|
+
*
|
|
126
|
+
* If you're using code generation, use the `MutationCtx` type in `convex/_generated/server.d.ts` instead.
|
|
127
|
+
*/
|
|
128
|
+
export type MutationCtx = GenericMutationCtx<DataModel>;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* A set of services for use within Convex action functions.
|
|
132
|
+
*
|
|
133
|
+
* The action context is passed as the first argument to any Convex action
|
|
134
|
+
* function run on the server.
|
|
135
|
+
*/
|
|
136
|
+
export type ActionCtx = GenericActionCtx<DataModel>;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* An interface to read from the database within Convex query functions.
|
|
140
|
+
*
|
|
141
|
+
* The two entry points are {@link DatabaseReader.get}, which fetches a single
|
|
142
|
+
* document by its {@link Id}, or {@link DatabaseReader.query}, which starts
|
|
143
|
+
* building a query.
|
|
144
|
+
*/
|
|
145
|
+
export type DatabaseReader = GenericDatabaseReader<DataModel>;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* An interface to read from and write to the database within Convex mutation
|
|
149
|
+
* functions.
|
|
150
|
+
*
|
|
151
|
+
* Convex guarantees that all writes within a single mutation are
|
|
152
|
+
* executed atomically, so you never have to worry about partial writes leaving
|
|
153
|
+
* your data in an inconsistent state. See [the Convex Guide](https://docs.convex.dev/understanding/convex-fundamentals/functions#atomicity-and-optimistic-concurrency-control)
|
|
154
|
+
* for the guarantees Convex provides your functions.
|
|
155
|
+
*/
|
|
156
|
+
export type DatabaseWriter = GenericDatabaseWriter<DataModel>;
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { mutation } from "./_generated/server.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Record a newly created checkout session.
|
|
6
|
+
*/
|
|
7
|
+
export const insertCheckoutSession = mutation({
|
|
8
|
+
args: {
|
|
9
|
+
sessionId: v.string(),
|
|
10
|
+
sessionData: v.string(),
|
|
11
|
+
shopperReference: v.optional(v.string()),
|
|
12
|
+
merchantReference: v.string(),
|
|
13
|
+
amount: v.number(),
|
|
14
|
+
currency: v.string(),
|
|
15
|
+
url: v.optional(v.string()),
|
|
16
|
+
autoCapture: v.optional(v.boolean()),
|
|
17
|
+
},
|
|
18
|
+
returns: v.null(),
|
|
19
|
+
handler: async (ctx, args) => {
|
|
20
|
+
const existing = await ctx.db
|
|
21
|
+
.query("checkout_sessions")
|
|
22
|
+
.withIndex("by_session_id", (q) => q.eq("sessionId", args.sessionId))
|
|
23
|
+
.unique();
|
|
24
|
+
|
|
25
|
+
if (!existing) {
|
|
26
|
+
await ctx.db.insert("checkout_sessions", {
|
|
27
|
+
sessionId: args.sessionId,
|
|
28
|
+
sessionData: args.sessionData,
|
|
29
|
+
shopperReference: args.shopperReference,
|
|
30
|
+
merchantReference: args.merchantReference,
|
|
31
|
+
status: "active",
|
|
32
|
+
amount: args.amount,
|
|
33
|
+
currency: args.currency,
|
|
34
|
+
url: args.url,
|
|
35
|
+
autoCapture: args.autoCapture,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Record a payment transaction (e.g. authorization success/failure).
|
|
44
|
+
*/
|
|
45
|
+
export const recordPayment = mutation({
|
|
46
|
+
args: {
|
|
47
|
+
pspReference: v.string(),
|
|
48
|
+
originalReference: v.optional(v.string()),
|
|
49
|
+
shopperReference: v.optional(v.string()),
|
|
50
|
+
merchantReference: v.string(),
|
|
51
|
+
amount: v.number(),
|
|
52
|
+
currency: v.string(),
|
|
53
|
+
status: v.string(),
|
|
54
|
+
paymentMethod: v.optional(v.string()),
|
|
55
|
+
userId: v.optional(v.string()),
|
|
56
|
+
orgId: v.optional(v.string()),
|
|
57
|
+
metadata: v.optional(v.any()),
|
|
58
|
+
},
|
|
59
|
+
returns: v.null(),
|
|
60
|
+
handler: async (ctx, args) => {
|
|
61
|
+
const existing = await ctx.db
|
|
62
|
+
.query("payments")
|
|
63
|
+
.withIndex("by_psp_reference", (q) =>
|
|
64
|
+
q.eq("pspReference", args.pspReference)
|
|
65
|
+
)
|
|
66
|
+
.unique();
|
|
67
|
+
|
|
68
|
+
if (!existing) {
|
|
69
|
+
await ctx.db.insert("payments", {
|
|
70
|
+
pspReference: args.pspReference,
|
|
71
|
+
originalReference: args.originalReference,
|
|
72
|
+
shopperReference: args.shopperReference,
|
|
73
|
+
merchantReference: args.merchantReference,
|
|
74
|
+
amount: args.amount,
|
|
75
|
+
currency: args.currency,
|
|
76
|
+
status: args.status,
|
|
77
|
+
paymentMethod: args.paymentMethod,
|
|
78
|
+
created: Date.now(),
|
|
79
|
+
userId: args.userId,
|
|
80
|
+
orgId: args.orgId,
|
|
81
|
+
metadata: args.metadata,
|
|
82
|
+
});
|
|
83
|
+
} else {
|
|
84
|
+
await ctx.db.patch("payments", existing._id, {
|
|
85
|
+
status: args.status,
|
|
86
|
+
...(args.shopperReference !== undefined && { shopperReference: args.shopperReference }),
|
|
87
|
+
...(args.userId !== undefined && { userId: args.userId }),
|
|
88
|
+
...(args.orgId !== undefined && { orgId: args.orgId }),
|
|
89
|
+
...(args.metadata !== undefined && { metadata: args.metadata }),
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
return null;
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Update the status of a payment transaction (e.g. captured, refunded, cancelled).
|
|
98
|
+
*/
|
|
99
|
+
export const updatePaymentStatus = mutation({
|
|
100
|
+
args: {
|
|
101
|
+
pspReference: v.string(),
|
|
102
|
+
status: v.string(),
|
|
103
|
+
originalReference: v.optional(v.string()),
|
|
104
|
+
},
|
|
105
|
+
returns: v.null(),
|
|
106
|
+
handler: async (ctx, args) => {
|
|
107
|
+
const payment = await ctx.db
|
|
108
|
+
.query("payments")
|
|
109
|
+
.withIndex("by_psp_reference", (q) =>
|
|
110
|
+
q.eq("pspReference", args.pspReference)
|
|
111
|
+
)
|
|
112
|
+
.unique();
|
|
113
|
+
|
|
114
|
+
if (payment) {
|
|
115
|
+
await ctx.db.patch("payments", payment._id, {
|
|
116
|
+
status: args.status,
|
|
117
|
+
...(args.originalReference !== undefined && { originalReference: args.originalReference }),
|
|
118
|
+
});
|
|
119
|
+
} else {
|
|
120
|
+
// If payment is not found locally, create a placeholder payment that will be backfilled/updated.
|
|
121
|
+
// (This handles cases where captures/refunds are triggered but original auth record is not in db)
|
|
122
|
+
await ctx.db.insert("payments", {
|
|
123
|
+
pspReference: args.pspReference,
|
|
124
|
+
originalReference: args.originalReference,
|
|
125
|
+
merchantReference: "unknown",
|
|
126
|
+
amount: 0,
|
|
127
|
+
currency: "unknown",
|
|
128
|
+
status: args.status,
|
|
129
|
+
created: Date.now(),
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Insert or update a single tokenized payment method.
|
|
138
|
+
*/
|
|
139
|
+
export const insertPaymentMethod = mutation({
|
|
140
|
+
args: {
|
|
141
|
+
shopperReference: v.string(),
|
|
142
|
+
recurringDetailReference: v.string(),
|
|
143
|
+
variant: v.string(),
|
|
144
|
+
cardLast4: v.optional(v.string()),
|
|
145
|
+
cardExpiryMonth: v.optional(v.string()),
|
|
146
|
+
cardExpiryYear: v.optional(v.string()),
|
|
147
|
+
metadata: v.optional(v.any()),
|
|
148
|
+
},
|
|
149
|
+
returns: v.null(),
|
|
150
|
+
handler: async (ctx, args) => {
|
|
151
|
+
const existing = await ctx.db
|
|
152
|
+
.query("payment_methods")
|
|
153
|
+
.withIndex("by_recurring_detail_reference", (q) =>
|
|
154
|
+
q.eq("recurringDetailReference", args.recurringDetailReference)
|
|
155
|
+
)
|
|
156
|
+
.unique();
|
|
157
|
+
|
|
158
|
+
if (existing) {
|
|
159
|
+
await ctx.db.patch("payment_methods", existing._id, {
|
|
160
|
+
status: "active",
|
|
161
|
+
variant: args.variant,
|
|
162
|
+
cardLast4: args.cardLast4,
|
|
163
|
+
cardExpiryMonth: args.cardExpiryMonth,
|
|
164
|
+
cardExpiryYear: args.cardExpiryYear,
|
|
165
|
+
...(args.metadata !== undefined && { metadata: args.metadata }),
|
|
166
|
+
});
|
|
167
|
+
} else {
|
|
168
|
+
await ctx.db.insert("payment_methods", {
|
|
169
|
+
shopperReference: args.shopperReference,
|
|
170
|
+
recurringDetailReference: args.recurringDetailReference,
|
|
171
|
+
variant: args.variant,
|
|
172
|
+
cardLast4: args.cardLast4,
|
|
173
|
+
cardExpiryMonth: args.cardExpiryMonth,
|
|
174
|
+
cardExpiryYear: args.cardExpiryYear,
|
|
175
|
+
status: "active",
|
|
176
|
+
metadata: args.metadata,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
return null;
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Synchronize the active stored card/payment method tokens for a shopper.
|
|
185
|
+
*/
|
|
186
|
+
export const syncPaymentMethods = mutation({
|
|
187
|
+
args: {
|
|
188
|
+
shopperReference: v.string(),
|
|
189
|
+
paymentMethods: v.array(
|
|
190
|
+
v.object({
|
|
191
|
+
recurringDetailReference: v.string(),
|
|
192
|
+
variant: v.string(),
|
|
193
|
+
cardLast4: v.optional(v.string()),
|
|
194
|
+
cardExpiryMonth: v.optional(v.string()),
|
|
195
|
+
cardExpiryYear: v.optional(v.string()),
|
|
196
|
+
metadata: v.optional(v.any()),
|
|
197
|
+
})
|
|
198
|
+
),
|
|
199
|
+
},
|
|
200
|
+
returns: v.null(),
|
|
201
|
+
handler: async (ctx, args) => {
|
|
202
|
+
// Get all existing payment methods for the shopper
|
|
203
|
+
const existing = await ctx.db
|
|
204
|
+
.query("payment_methods")
|
|
205
|
+
.withIndex("by_shopper_reference", (q) =>
|
|
206
|
+
q.eq("shopperReference", args.shopperReference)
|
|
207
|
+
)
|
|
208
|
+
.collect();
|
|
209
|
+
|
|
210
|
+
const incomingRefs = new Set(
|
|
211
|
+
args.paymentMethods.map((m) => m.recurringDetailReference)
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
// Mark methods that are no longer active as disabled
|
|
215
|
+
for (const method of existing) {
|
|
216
|
+
if (
|
|
217
|
+
!incomingRefs.has(method.recurringDetailReference) &&
|
|
218
|
+
method.status !== "disabled"
|
|
219
|
+
) {
|
|
220
|
+
await ctx.db.patch("payment_methods", method._id, { status: "disabled" });
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Insert or update incoming active methods
|
|
225
|
+
for (const method of args.paymentMethods) {
|
|
226
|
+
const match = existing.find(
|
|
227
|
+
(m) => m.recurringDetailReference === method.recurringDetailReference
|
|
228
|
+
);
|
|
229
|
+
if (match) {
|
|
230
|
+
await ctx.db.patch("payment_methods", match._id, {
|
|
231
|
+
status: "active",
|
|
232
|
+
variant: method.variant,
|
|
233
|
+
cardLast4: method.cardLast4,
|
|
234
|
+
cardExpiryMonth: method.cardExpiryMonth,
|
|
235
|
+
cardExpiryYear: method.cardExpiryYear,
|
|
236
|
+
...(method.metadata !== undefined && { metadata: method.metadata }),
|
|
237
|
+
});
|
|
238
|
+
} else {
|
|
239
|
+
await ctx.db.insert("payment_methods", {
|
|
240
|
+
shopperReference: args.shopperReference,
|
|
241
|
+
recurringDetailReference: method.recurringDetailReference,
|
|
242
|
+
variant: method.variant,
|
|
243
|
+
cardLast4: method.cardLast4,
|
|
244
|
+
cardExpiryMonth: method.cardExpiryMonth,
|
|
245
|
+
cardExpiryYear: method.cardExpiryYear,
|
|
246
|
+
status: "active",
|
|
247
|
+
metadata: method.metadata,
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return null;
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Update the status of a checkout session.
|
|
257
|
+
*/
|
|
258
|
+
export const updateCheckoutSessionStatus = mutation({
|
|
259
|
+
args: {
|
|
260
|
+
merchantReference: v.string(),
|
|
261
|
+
status: v.string(),
|
|
262
|
+
},
|
|
263
|
+
returns: v.null(),
|
|
264
|
+
handler: async (ctx, args) => {
|
|
265
|
+
const session = await ctx.db
|
|
266
|
+
.query("checkout_sessions")
|
|
267
|
+
.withIndex("by_merchant_reference", (q) =>
|
|
268
|
+
q.eq("merchantReference", args.merchantReference)
|
|
269
|
+
)
|
|
270
|
+
.unique();
|
|
271
|
+
|
|
272
|
+
if (session) {
|
|
273
|
+
await ctx.db.patch("checkout_sessions", session._id, { status: args.status });
|
|
274
|
+
}
|
|
275
|
+
return null;
|
|
276
|
+
},
|
|
277
|
+
});
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { test, expect } from "vitest";
|
|
2
|
+
import { initConvexTest } from "./setup.test.js";
|
|
3
|
+
import { api } from "./_generated/api.js";
|
|
4
|
+
|
|
5
|
+
test("shopper operations", async () => {
|
|
6
|
+
const t = initConvexTest();
|
|
7
|
+
|
|
8
|
+
// Create a shopper
|
|
9
|
+
const shopperRef = await t.mutation(api.public.createOrUpdateShopper, {
|
|
10
|
+
shopperReference: "user_123",
|
|
11
|
+
email: "test@example.com",
|
|
12
|
+
name: "Test User",
|
|
13
|
+
userId: "user_123",
|
|
14
|
+
metadata: { plan: "pro" },
|
|
15
|
+
});
|
|
16
|
+
expect(shopperRef).toBe("user_123");
|
|
17
|
+
|
|
18
|
+
// Get shopper by shopperReference
|
|
19
|
+
const shopper = await t.query(api.public.getShopper, {
|
|
20
|
+
shopperReference: "user_123",
|
|
21
|
+
});
|
|
22
|
+
expect(shopper).not.toBeNull();
|
|
23
|
+
expect(shopper!.email).toBe("test@example.com");
|
|
24
|
+
expect(shopper!.userId).toBe("user_123");
|
|
25
|
+
|
|
26
|
+
// Get shopper by userId
|
|
27
|
+
const shopperByUserId = await t.query(api.public.getShopperByUserId, {
|
|
28
|
+
userId: "user_123",
|
|
29
|
+
});
|
|
30
|
+
expect(shopperByUserId).not.toBeNull();
|
|
31
|
+
expect(shopperByUserId!.shopperReference).toBe("user_123");
|
|
32
|
+
|
|
33
|
+
// Get shopper by email
|
|
34
|
+
const shopperByEmail = await t.query(api.public.getShopperByEmail, {
|
|
35
|
+
email: "test@example.com",
|
|
36
|
+
});
|
|
37
|
+
expect(shopperByEmail).not.toBeNull();
|
|
38
|
+
expect(shopperByEmail!.shopperReference).toBe("user_123");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("checkout session operations", async () => {
|
|
42
|
+
const t = initConvexTest();
|
|
43
|
+
|
|
44
|
+
await t.mutation(api.private.insertCheckoutSession, {
|
|
45
|
+
sessionId: "sess_123",
|
|
46
|
+
sessionData: "data_blob",
|
|
47
|
+
shopperReference: "user_123",
|
|
48
|
+
merchantReference: "ref_123",
|
|
49
|
+
amount: 1000,
|
|
50
|
+
currency: "EUR",
|
|
51
|
+
url: "https://checkout.adyen.com/pay",
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const session = await t.query(api.public.getCheckoutSession, {
|
|
55
|
+
sessionId: "sess_123",
|
|
56
|
+
});
|
|
57
|
+
expect(session).not.toBeNull();
|
|
58
|
+
expect(session!.amount).toBe(1000);
|
|
59
|
+
expect(session!.url).toBe("https://checkout.adyen.com/pay");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("payment operations", async () => {
|
|
63
|
+
const t = initConvexTest();
|
|
64
|
+
|
|
65
|
+
await t.mutation(api.private.recordPayment, {
|
|
66
|
+
pspReference: "psp_123",
|
|
67
|
+
shopperReference: "user_123",
|
|
68
|
+
merchantReference: "ref_123",
|
|
69
|
+
amount: 2500,
|
|
70
|
+
currency: "USD",
|
|
71
|
+
status: "authorised",
|
|
72
|
+
paymentMethod: "visa",
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const payment = await t.query(api.public.getPayment, {
|
|
76
|
+
pspReference: "psp_123",
|
|
77
|
+
});
|
|
78
|
+
expect(payment).not.toBeNull();
|
|
79
|
+
expect(payment!.amount).toBe(2500);
|
|
80
|
+
expect(payment!.status).toBe("authorised");
|
|
81
|
+
|
|
82
|
+
// Update status
|
|
83
|
+
await t.mutation(api.private.updatePaymentStatus, {
|
|
84
|
+
pspReference: "psp_123",
|
|
85
|
+
status: "captured",
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const updated = await t.query(api.public.getPayment, {
|
|
89
|
+
pspReference: "psp_123",
|
|
90
|
+
});
|
|
91
|
+
expect(updated!.status).toBe("captured");
|
|
92
|
+
});
|