@dodopayments/better-auth 1.0.1 → 1.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/dist/client.d.ts CHANGED
@@ -3,4 +3,3 @@ export declare const dodopaymentsClient: () => {
3
3
  id: "dodopayments-client";
4
4
  $InferServerPlugin: ReturnType<typeof dodopayments>;
5
5
  };
6
- //# sourceMappingURL=client.d.ts.map
package/dist/client.js ADDED
@@ -0,0 +1,6 @@
1
+ export const dodopaymentsClient = () => {
2
+ return {
3
+ id: "dodopayments-client",
4
+ $InferServerPlugin: {},
5
+ };
6
+ };
@@ -2,4 +2,3 @@ import type { GenericEndpointContext, User } from "better-auth";
2
2
  import type { DodoPaymentsOptions } from "../types";
3
3
  export declare const onUserCreate: (options: DodoPaymentsOptions) => (user: User, ctx?: GenericEndpointContext) => Promise<void>;
4
4
  export declare const onUserUpdate: (options: DodoPaymentsOptions) => (user: User, ctx?: GenericEndpointContext) => Promise<void>;
5
- //# sourceMappingURL=customer.d.ts.map
@@ -0,0 +1,63 @@
1
+ import { APIError } from "better-auth/api";
2
+ export const onUserCreate = (options) => async (user, ctx) => {
3
+ if (ctx && options.createCustomerOnSignUp) {
4
+ try {
5
+ const customers = await options.client.customers.list({
6
+ email: user.email,
7
+ });
8
+ const existingCustomer = customers.items[0];
9
+ if (existingCustomer) {
10
+ if (existingCustomer.email !== user.email) {
11
+ await options.client.customers.update(existingCustomer.customer_id, {
12
+ name: user.name,
13
+ });
14
+ }
15
+ }
16
+ else {
17
+ // TODO: Add metadata to customer object via
18
+ // getCustomerCreateParams option when it becomes
19
+ // available in the API
20
+ await options.client.customers.create({
21
+ email: user.email,
22
+ name: user.name,
23
+ });
24
+ }
25
+ }
26
+ catch (e) {
27
+ if (e instanceof Error) {
28
+ throw new APIError("INTERNAL_SERVER_ERROR", {
29
+ message: `DodoPayments customer creation failed. Error: ${e.message}`,
30
+ });
31
+ }
32
+ throw new APIError("INTERNAL_SERVER_ERROR", {
33
+ message: `DodoPayments customer creation failed. Error: ${e}`,
34
+ });
35
+ }
36
+ }
37
+ };
38
+ export const onUserUpdate = (options) => async (user, ctx) => {
39
+ if (ctx && options.createCustomerOnSignUp) {
40
+ try {
41
+ const customers = await options.client.customers.list({
42
+ email: user.email,
43
+ });
44
+ const existingCustomer = customers.items[0];
45
+ if (existingCustomer) {
46
+ // TODO: Add metadata to customer object via
47
+ // getCustomerCreateParams option when it becomes
48
+ // available in the API
49
+ await options.client.customers.update(existingCustomer.customer_id, {
50
+ name: user.name,
51
+ });
52
+ }
53
+ }
54
+ catch (e) {
55
+ if (e instanceof Error) {
56
+ ctx.context.logger.error(`DodoPayments customer update failed. Error: ${e.message}`);
57
+ }
58
+ else {
59
+ ctx.context.logger.error(`DodoPayments customer update failed. Error: ${e}`);
60
+ }
61
+ }
62
+ }
63
+ };