@dodopayments/better-auth 1.3.2 → 1.3.4
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/chunk-52LQJCF4.js +199 -0
- package/dist/chunk-52LQJCF4.js.map +1 -0
- package/dist/chunk-FBAIEKOL.js +70 -0
- package/dist/chunk-FBAIEKOL.js.map +1 -0
- package/dist/chunk-PXI4EHZC.js +12 -0
- package/dist/chunk-PXI4EHZC.js.map +1 -0
- package/dist/chunk-QR7PLJJQ.js +13943 -0
- package/dist/chunk-QR7PLJJQ.js.map +1 -0
- package/dist/chunk-WIFOXVZ3.js +291 -0
- package/dist/chunk-WIFOXVZ3.js.map +1 -0
- package/dist/chunk-YK6XO66Y.js +84 -0
- package/dist/chunk-YK6XO66Y.js.map +1 -0
- package/dist/chunk-Z7VSWPPK.js +181 -0
- package/dist/chunk-Z7VSWPPK.js.map +1 -0
- package/dist/client.cjs +36 -0
- package/dist/client.cjs.map +1 -0
- package/dist/client.d.cts +7 -0
- package/dist/client.d.ts +7 -5
- package/dist/client.js +6 -5
- package/dist/client.js.map +1 -0
- package/dist/hooks/customer.cjs +14036 -0
- package/dist/hooks/customer.cjs.map +1 -0
- package/dist/hooks/customer.d.cts +11 -0
- package/dist/hooks/customer.d.ts +11 -4
- package/dist/hooks/customer.js +9 -62
- package/dist/hooks/customer.js.map +1 -0
- package/dist/index.cjs +14793 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +797 -0
- package/dist/index.d.ts +205 -196
- package/dist/index.js +55 -34
- package/dist/index.js.map +1 -0
- package/dist/plugins/checkout.cjs +14424 -0
- package/dist/plugins/checkout.cjs.map +1 -0
- package/dist/plugins/checkout.d.cts +6 -0
- package/dist/plugins/checkout.d.ts +6 -548
- package/dist/plugins/checkout.js +8 -170
- package/dist/plugins/checkout.js.map +1 -0
- package/dist/plugins/portal.cjs +14446 -0
- package/dist/plugins/portal.cjs.map +1 -0
- package/dist/plugins/portal.d.cts +6 -0
- package/dist/plugins/portal.d.ts +6 -204
- package/dist/plugins/portal.js +8 -171
- package/dist/plugins/portal.js.map +1 -0
- package/dist/plugins/webhooks.cjs +14046 -0
- package/dist/plugins/webhooks.cjs.map +1 -0
- package/dist/plugins/webhooks.d.cts +6 -0
- package/dist/plugins/webhooks.d.ts +6 -41
- package/dist/plugins/webhooks.js +7 -61
- package/dist/plugins/webhooks.js.map +1 -0
- package/dist/types-CbotWcHh.d.cts +841 -0
- package/dist/types-CbotWcHh.d.ts +841 -0
- package/dist/types.cjs +19 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +6 -0
- package/dist/types.d.ts +6 -52
- package/dist/types.js +1 -1
- package/dist/types.js.map +1 -0
- package/package.json +5 -10
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import {
|
|
2
|
+
APIError,
|
|
3
|
+
createAuthEndpoint,
|
|
4
|
+
sessionMiddleware
|
|
5
|
+
} from "./chunk-QR7PLJJQ.js";
|
|
6
|
+
|
|
7
|
+
// src/plugins/portal.ts
|
|
8
|
+
import { z } from "zod/v3";
|
|
9
|
+
var portal = () => (dodopayments) => {
|
|
10
|
+
return {
|
|
11
|
+
dodoPortal: createAuthEndpoint(
|
|
12
|
+
"/dodopayments/customer/portal",
|
|
13
|
+
{
|
|
14
|
+
method: "GET",
|
|
15
|
+
use: [sessionMiddleware]
|
|
16
|
+
},
|
|
17
|
+
async (ctx) => {
|
|
18
|
+
if (!ctx.context.session?.user.id) {
|
|
19
|
+
throw new APIError("BAD_REQUEST", {
|
|
20
|
+
message: "User not found"
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
if (!ctx.context.session?.user.emailVerified) {
|
|
24
|
+
throw new APIError("UNAUTHORIZED", {
|
|
25
|
+
message: "User email not verified"
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
const customers = await dodopayments.customers.list({
|
|
30
|
+
email: ctx.context.session?.user.email
|
|
31
|
+
});
|
|
32
|
+
let customer = customers.items[0];
|
|
33
|
+
if (!customer) {
|
|
34
|
+
customer = await createCustomer(
|
|
35
|
+
dodopayments,
|
|
36
|
+
ctx.context.session.user.email,
|
|
37
|
+
ctx.context.session.user.name
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
const customerSession = await dodopayments.customers.customerPortal.create(
|
|
41
|
+
customer.customer_id
|
|
42
|
+
);
|
|
43
|
+
return ctx.json({
|
|
44
|
+
url: customerSession.link,
|
|
45
|
+
redirect: true
|
|
46
|
+
});
|
|
47
|
+
} catch (e) {
|
|
48
|
+
if (e instanceof Error) {
|
|
49
|
+
ctx.context.logger.error(
|
|
50
|
+
`DodoPayments customer portal creation failed. Error: ${e.message}`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
throw new APIError("INTERNAL_SERVER_ERROR", {
|
|
54
|
+
message: "Customer portal creation failed"
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
),
|
|
59
|
+
dodoSubscriptions: createAuthEndpoint(
|
|
60
|
+
"/dodopayments/customer/subscriptions/list",
|
|
61
|
+
{
|
|
62
|
+
method: "GET",
|
|
63
|
+
query: z.object({
|
|
64
|
+
page: z.coerce.number().optional(),
|
|
65
|
+
limit: z.coerce.number().optional(),
|
|
66
|
+
status: z.enum([
|
|
67
|
+
"active",
|
|
68
|
+
"cancelled",
|
|
69
|
+
"on_hold",
|
|
70
|
+
"pending",
|
|
71
|
+
"failed",
|
|
72
|
+
"expired"
|
|
73
|
+
]).optional()
|
|
74
|
+
}).optional(),
|
|
75
|
+
use: [sessionMiddleware]
|
|
76
|
+
},
|
|
77
|
+
async (ctx) => {
|
|
78
|
+
if (!ctx.context.session.user.id) {
|
|
79
|
+
throw new APIError("BAD_REQUEST", {
|
|
80
|
+
message: "User not found"
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
if (!ctx.context.session?.user.emailVerified) {
|
|
84
|
+
throw new APIError("UNAUTHORIZED", {
|
|
85
|
+
message: "User email not verified"
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
const customers = await dodopayments.customers.list({
|
|
90
|
+
email: ctx.context.session?.user.email
|
|
91
|
+
});
|
|
92
|
+
let customer = customers.items[0];
|
|
93
|
+
if (!customer) {
|
|
94
|
+
customer = await createCustomer(
|
|
95
|
+
dodopayments,
|
|
96
|
+
ctx.context.session.user.email,
|
|
97
|
+
ctx.context.session.user.name
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
const subscriptions = await dodopayments.subscriptions.list({
|
|
101
|
+
customer_id: customer.customer_id,
|
|
102
|
+
// page number is 0-indexed
|
|
103
|
+
page_number: ctx.query?.page ? ctx.query.page - 1 : void 0,
|
|
104
|
+
page_size: ctx.query?.limit,
|
|
105
|
+
status: ctx.query?.status
|
|
106
|
+
});
|
|
107
|
+
return ctx.json({ items: subscriptions.items });
|
|
108
|
+
} catch (e) {
|
|
109
|
+
if (e instanceof Error) {
|
|
110
|
+
ctx.context.logger.error(
|
|
111
|
+
`DodoPayments subscriptions list failed. Error: ${e.message}`
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
throw new APIError("INTERNAL_SERVER_ERROR", {
|
|
115
|
+
message: "DodoPayments subscriptions list failed"
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
),
|
|
120
|
+
dodoPayments: createAuthEndpoint(
|
|
121
|
+
"/dodopayments/customer/payments/list",
|
|
122
|
+
{
|
|
123
|
+
method: "GET",
|
|
124
|
+
query: z.object({
|
|
125
|
+
page: z.coerce.number().optional(),
|
|
126
|
+
limit: z.coerce.number().optional(),
|
|
127
|
+
status: z.enum([
|
|
128
|
+
"succeeded",
|
|
129
|
+
"failed",
|
|
130
|
+
"cancelled",
|
|
131
|
+
"processing",
|
|
132
|
+
"requires_customer_action",
|
|
133
|
+
"requires_merchant_action",
|
|
134
|
+
"requires_payment_method",
|
|
135
|
+
"requires_confirmation",
|
|
136
|
+
"requires_capture",
|
|
137
|
+
"partially_captured",
|
|
138
|
+
"partially_captured_and_capturable"
|
|
139
|
+
]).optional()
|
|
140
|
+
}).optional(),
|
|
141
|
+
use: [sessionMiddleware]
|
|
142
|
+
},
|
|
143
|
+
async (ctx) => {
|
|
144
|
+
if (!ctx.context.session.user.id) {
|
|
145
|
+
throw new APIError("BAD_REQUEST", {
|
|
146
|
+
message: "User not found"
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
if (!ctx.context.session?.user.emailVerified) {
|
|
150
|
+
throw new APIError("UNAUTHORIZED", {
|
|
151
|
+
message: "User email not verified"
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
try {
|
|
155
|
+
const customers = await dodopayments.customers.list({
|
|
156
|
+
email: ctx.context.session?.user.email
|
|
157
|
+
});
|
|
158
|
+
let customer = customers.items[0];
|
|
159
|
+
if (!customer) {
|
|
160
|
+
customer = await createCustomer(
|
|
161
|
+
dodopayments,
|
|
162
|
+
ctx.context.session.user.email,
|
|
163
|
+
ctx.context.session.user.name
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
const payments = await dodopayments.payments.list({
|
|
167
|
+
customer_id: customer.customer_id,
|
|
168
|
+
// page number is 0-indexed
|
|
169
|
+
page_number: ctx.query?.page ? ctx.query.page - 1 : void 0,
|
|
170
|
+
page_size: ctx.query?.limit,
|
|
171
|
+
status: ctx.query?.status
|
|
172
|
+
});
|
|
173
|
+
return ctx.json({ items: payments.items });
|
|
174
|
+
} catch (e) {
|
|
175
|
+
if (e instanceof Error) {
|
|
176
|
+
ctx.context.logger.error(
|
|
177
|
+
`DodoPayments orders list failed. Error: ${e.message}`
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
throw new APIError("INTERNAL_SERVER_ERROR", {
|
|
181
|
+
message: "Orders list failed"
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
)
|
|
186
|
+
};
|
|
187
|
+
};
|
|
188
|
+
async function createCustomer(dodopayments, email, name) {
|
|
189
|
+
const customer = await dodopayments.customers.create({
|
|
190
|
+
email,
|
|
191
|
+
name
|
|
192
|
+
});
|
|
193
|
+
return customer;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export {
|
|
197
|
+
portal
|
|
198
|
+
};
|
|
199
|
+
//# sourceMappingURL=chunk-52LQJCF4.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/plugins/portal.ts"],"sourcesContent":["import type { DodoPayments } from \"dodopayments\";\nimport { APIError } from \"better-auth/api\";\nimport { sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod/v3\";\nimport {\n CustomerPortalResponse,\n PaymentItems,\n SubscriptionItems,\n} from \"../types\";\n\nexport const portal = () => (dodopayments: DodoPayments) => {\n return {\n dodoPortal: createAuthEndpoint(\n \"/dodopayments/customer/portal\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx): Promise<CustomerPortalResponse> => {\n if (!ctx.context.session?.user.id) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"User not found\",\n });\n }\n\n if (!ctx.context.session?.user.emailVerified) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"User email not verified\",\n });\n }\n\n try {\n const customers = await dodopayments.customers.list({\n email: ctx.context.session?.user.email,\n });\n let customer = customers.items[0];\n\n if (!customer) {\n // upsert the customer, if they don't exist in DodoPayments\n customer = await createCustomer(\n dodopayments,\n ctx.context.session.user.email,\n ctx.context.session.user.name,\n );\n }\n\n const customerSession =\n await dodopayments.customers.customerPortal.create(\n customer.customer_id,\n );\n\n return ctx.json({\n url: customerSession.link,\n redirect: true,\n });\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `DodoPayments customer portal creation failed. Error: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Customer portal creation failed\",\n });\n }\n },\n ),\n dodoSubscriptions: createAuthEndpoint(\n \"/dodopayments/customer/subscriptions/list\",\n {\n method: \"GET\",\n query: z\n .object({\n page: z.coerce.number().optional(),\n limit: z.coerce.number().optional(),\n status: z\n .enum([\n \"active\",\n \"cancelled\",\n \"on_hold\",\n \"pending\",\n \"failed\",\n \"expired\",\n ])\n .optional(),\n })\n .optional(),\n use: [sessionMiddleware],\n },\n async (ctx): Promise<SubscriptionItems> => {\n if (!ctx.context.session.user.id) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"User not found\",\n });\n }\n\n if (!ctx.context.session?.user.emailVerified) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"User email not verified\",\n });\n }\n\n try {\n const customers = await dodopayments.customers.list({\n email: ctx.context.session?.user.email,\n });\n let customer = customers.items[0];\n\n if (!customer) {\n // upsert the customer, if they don't exist in DodoPayments\n customer = await createCustomer(\n dodopayments,\n ctx.context.session.user.email,\n ctx.context.session.user.name,\n );\n }\n\n const subscriptions = await dodopayments.subscriptions.list({\n customer_id: customer.customer_id,\n // page number is 0-indexed\n page_number: ctx.query?.page ? ctx.query.page - 1 : undefined,\n page_size: ctx.query?.limit,\n status: ctx.query?.status,\n });\n\n return ctx.json({ items: subscriptions.items });\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `DodoPayments subscriptions list failed. Error: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"DodoPayments subscriptions list failed\",\n });\n }\n },\n ),\n dodoPayments: createAuthEndpoint(\n \"/dodopayments/customer/payments/list\",\n {\n method: \"GET\",\n query: z\n .object({\n page: z.coerce.number().optional(),\n limit: z.coerce.number().optional(),\n status: z\n .enum([\n \"succeeded\",\n \"failed\",\n \"cancelled\",\n \"processing\",\n \"requires_customer_action\",\n \"requires_merchant_action\",\n \"requires_payment_method\",\n \"requires_confirmation\",\n \"requires_capture\",\n \"partially_captured\",\n \"partially_captured_and_capturable\",\n ])\n .optional(),\n })\n .optional(),\n use: [sessionMiddleware],\n },\n async (ctx): Promise<PaymentItems> => {\n if (!ctx.context.session.user.id) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"User not found\",\n });\n }\n\n if (!ctx.context.session?.user.emailVerified) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"User email not verified\",\n });\n }\n\n try {\n const customers = await dodopayments.customers.list({\n email: ctx.context.session?.user.email,\n });\n let customer = customers.items[0];\n\n if (!customer) {\n // upsert the customer, if they don't exist in DodoPayments\n customer = await createCustomer(\n dodopayments,\n ctx.context.session.user.email,\n ctx.context.session.user.name,\n );\n }\n\n const payments = await dodopayments.payments.list({\n customer_id: customer.customer_id,\n // page number is 0-indexed\n page_number: ctx.query?.page ? ctx.query.page - 1 : undefined,\n page_size: ctx.query?.limit,\n status: ctx.query?.status,\n });\n\n return ctx.json({ items: payments.items });\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `DodoPayments orders list failed. Error: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Orders list failed\",\n });\n }\n },\n ),\n };\n};\n\nasync function createCustomer(\n dodopayments: DodoPayments,\n email: string,\n name: string,\n) {\n const customer = await dodopayments.customers.create({\n email,\n name,\n });\n\n return customer;\n}\n"],"mappings":";;;;;;;AAIA,SAAS,SAAS;AAOX,IAAM,SAAS,MAAM,CAAC,iBAA+B;AAC1D,SAAO;AAAA,IACL,YAAY;AAAA,MACV;AAAA,MACA;AAAA,QACE,QAAQ;AAAA,QACR,KAAK,CAAC,iBAAiB;AAAA,MACzB;AAAA,MACA,OAAO,QAAyC;AAC9C,YAAI,CAAC,IAAI,QAAQ,SAAS,KAAK,IAAI;AACjC,gBAAM,IAAI,SAAS,eAAe;AAAA,YAChC,SAAS;AAAA,UACX,CAAC;AAAA,QACH;AAEA,YAAI,CAAC,IAAI,QAAQ,SAAS,KAAK,eAAe;AAC5C,gBAAM,IAAI,SAAS,gBAAg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UACX,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAEA,eAAe,eACb,cACA,OACA,MACA;AACA,QAAM,WAAW,MAAM,aAAa,UAAU,OAAO;AAAA,IACnD;AAAA,IACA;AAAA,EACF,CAAC;AAED,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {
|
|
2
|
+
APIError
|
|
3
|
+
} from "./chunk-QR7PLJJQ.js";
|
|
4
|
+
|
|
5
|
+
// src/hooks/customer.ts
|
|
6
|
+
var onUserCreate = (options) => async (user, ctx) => {
|
|
7
|
+
if (ctx && options.createCustomerOnSignUp) {
|
|
8
|
+
try {
|
|
9
|
+
const customers = await options.client.customers.list({
|
|
10
|
+
email: user.email
|
|
11
|
+
});
|
|
12
|
+
const existingCustomer = customers.items[0];
|
|
13
|
+
if (existingCustomer) {
|
|
14
|
+
if (existingCustomer.email !== user.email) {
|
|
15
|
+
await options.client.customers.update(
|
|
16
|
+
existingCustomer.customer_id,
|
|
17
|
+
{
|
|
18
|
+
name: user.name
|
|
19
|
+
}
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
} else {
|
|
23
|
+
await options.client.customers.create({
|
|
24
|
+
email: user.email,
|
|
25
|
+
name: user.name
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
} catch (e) {
|
|
29
|
+
if (e instanceof Error) {
|
|
30
|
+
throw new APIError("INTERNAL_SERVER_ERROR", {
|
|
31
|
+
message: `DodoPayments customer creation failed. Error: ${e.message}`
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
throw new APIError("INTERNAL_SERVER_ERROR", {
|
|
35
|
+
message: `DodoPayments customer creation failed. Error: ${e}`
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var onUserUpdate = (options) => async (user, ctx) => {
|
|
41
|
+
if (ctx && options.createCustomerOnSignUp) {
|
|
42
|
+
try {
|
|
43
|
+
const customers = await options.client.customers.list({
|
|
44
|
+
email: user.email
|
|
45
|
+
});
|
|
46
|
+
const existingCustomer = customers.items[0];
|
|
47
|
+
if (existingCustomer) {
|
|
48
|
+
await options.client.customers.update(existingCustomer.customer_id, {
|
|
49
|
+
name: user.name
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
} catch (e) {
|
|
53
|
+
if (e instanceof Error) {
|
|
54
|
+
ctx.context.logger.error(
|
|
55
|
+
`DodoPayments customer update failed. Error: ${e.message}`
|
|
56
|
+
);
|
|
57
|
+
} else {
|
|
58
|
+
ctx.context.logger.error(
|
|
59
|
+
`DodoPayments customer update failed. Error: ${e}`
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export {
|
|
67
|
+
onUserCreate,
|
|
68
|
+
onUserUpdate
|
|
69
|
+
};
|
|
70
|
+
//# sourceMappingURL=chunk-FBAIEKOL.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/hooks/customer.ts"],"sourcesContent":["import type { GenericEndpointContext, User } from \"better-auth\";\nimport { APIError } from \"better-auth/api\";\nimport type { DodoPaymentsOptions } from \"../types\";\n\nexport const onUserCreate =\n (options: DodoPaymentsOptions) =>\n async (user: User, ctx?: GenericEndpointContext) => {\n if (ctx && options.createCustomerOnSignUp) {\n try {\n const customers = await options.client.customers.list({\n email: user.email,\n });\n const existingCustomer = customers.items[0];\n\n if (existingCustomer) {\n if (existingCustomer.email !== user.email) {\n await options.client.customers.update(\n existingCustomer.customer_id,\n {\n name: user.name,\n },\n );\n }\n } else {\n // TODO: Add metadata to customer object via\n // getCustomerCreateParams option when it becomes\n // available in the API\n await options.client.customers.create({\n email: user.email,\n name: user.name,\n });\n }\n } catch (e: unknown) {\n if (e instanceof Error) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: `DodoPayments customer creation failed. Error: ${e.message}`,\n });\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: `DodoPayments customer creation failed. Error: ${e}`,\n });\n }\n }\n };\n\nexport const onUserUpdate =\n (options: DodoPaymentsOptions) =>\n async (user: User, ctx?: GenericEndpointContext) => {\n if (ctx && options.createCustomerOnSignUp) {\n try {\n const customers = await options.client.customers.list({\n email: user.email,\n });\n const existingCustomer = customers.items[0];\n\n if (existingCustomer) {\n // TODO: Add metadata to customer object via\n // getCustomerCreateParams option when it becomes\n // available in the API\n await options.client.customers.update(existingCustomer.customer_id, {\n name: user.name,\n });\n }\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `DodoPayments customer update failed. Error: ${e.message}`,\n );\n } else {\n ctx.context.logger.error(\n `DodoPayments customer update failed. Error: ${e}`,\n );\n }\n }\n }\n };\n"],"mappings":";;;;;AAIO,IAAM,eACX,CAAC,YACD,OAAO,MAAY,QAAiC;AAClD,MAAI,OAAO,QAAQ,wBAAwB;AACzC,QAAI;AACF,YAAM,YAAY,MAAM,QAAQ,OAAO,UAAU,KAAK;AAAA,QACpD,OAAO,KAAK;AAAA,MACd,CAAC;AACD,YAAM,mBAAmB,UAAU,MAAM,CAAC;AAE1C,UAAI,kBAAkB;AACpB,YAAI,iBAAiB,UAAU,KAAK,OAAO;AACzC,gBAAM,QAAQ,OAAO,UAAU;AAAA,YAC7B,iBAAiB;AAAA,YACjB;AAAA,cACE,MAAM,KAAK;AAAA,YACb;AAAA,UACF;AAAA,QACF;AAAA,MACF,OAAO;AAIL,cAAM,QAAQ,OAAO,UAAU,OAAO;AAAA,UACpC,OAAO,KAAK;AAAA,UACZ,MAAM,KAAK;AAAA,QACb,CAAC;AAAA,MACH;AAAA,IACF,SAAS,GAAY;AACnB,UAAI,aAAa,OAAO;AACtB,cAAM,IAAI,SAAS,yBAAyB;AAAA,UAC1C,SAAS,iDAAiD,EAAE,OAAO;AAAA,QACrE,CAAC;AAAA,MACH;AAEA,YAAM,IAAI,SAAS,yBAAyB;AAAA,QAC1C,SAAS,iDAAiD,CAAC;AAAA,MAC7D,CAAC;AAAA,IACH;AAAA,EACF;AACF;AAEK,IAAM,eACX,CAAC,YACD,OAAO,MAAY,QAAiC;AAClD,MAAI,OAAO,QAAQ,wBAAwB;AACzC,QAAI;AACF,YAAM,YAAY,MAAM,QAAQ,OAAO,UAAU,KAAK;AAAA,QACpD,OAAO,KAAK;AAAA,MACd,CAAC;AACD,YAAM,mBAAmB,UAAU,MAAM,CAAC;AAE1C,UAAI,kBAAkB;AAIpB,cAAM,QAAQ,OAAO,UAAU,OAAO,iBAAiB,aAAa;AAAA,UAClE,MAAM,KAAK;AAAA,QACb,CAAC;AAAA,MACH;AAAA,IACF,SAAS,GAAY;AACnB,UAAI,aAAa,OAAO;AACtB,YAAI,QAAQ,OAAO;AAAA,UACjB,+CAA+C,EAAE,OAAO;AAAA,QAC1D;AAAA,MACF,OAAO;AACL,YAAI,QAAQ,OAAO;AAAA,UACjB,+CAA+C,CAAC;AAAA,QAClD;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/client.ts"],"sourcesContent":["import type { BetterAuthClientPlugin } from \"better-auth\";\nimport type { dodopayments } from \"./index\";\n\nexport const dodopaymentsClient = () => {\n return {\n id: \"dodopayments-client\",\n $InferServerPlugin: {} as ReturnType<typeof dodopayments>,\n } satisfies BetterAuthClientPlugin;\n};\n"],"mappings":";AAGO,IAAM,qBAAqB,MAAM;AACtC,SAAO;AAAA,IACL,IAAI;AAAA,IACJ,oBAAoB,CAAC;AAAA,EACvB;AACF;","names":[]}
|