@polar-sh/better-auth 1.5.0 → 1.6.1
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-5QR5Y5PS.js +34 -0
- package/dist/chunk-5QR5Y5PS.js.map +1 -0
- package/dist/client.cjs +58 -0
- package/dist/client.cjs.map +1 -0
- package/dist/client.d.cts +41 -0
- package/dist/client.d.ts +41 -0
- package/dist/client.js +7 -0
- package/dist/client.js.map +1 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +605 -1183
- package/dist/index.d.ts +605 -1183
- package/dist/index.js +4 -30
- package/dist/index.js.map +1 -1
- package/package.json +24 -12
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
import { PolarEmbedCheckout } from "@polar-sh/checkout/embed";
|
|
3
|
+
var polarClient = () => {
|
|
4
|
+
return {
|
|
5
|
+
id: "polar-client",
|
|
6
|
+
$InferServerPlugin: {},
|
|
7
|
+
getActions: ($fetch) => {
|
|
8
|
+
return {
|
|
9
|
+
checkoutEmbed: async (data, fetchOptions) => {
|
|
10
|
+
const res = await $fetch("/checkout", {
|
|
11
|
+
method: "POST",
|
|
12
|
+
body: {
|
|
13
|
+
...data,
|
|
14
|
+
redirect: false,
|
|
15
|
+
embedOrigin: window.location.origin
|
|
16
|
+
},
|
|
17
|
+
...fetchOptions
|
|
18
|
+
});
|
|
19
|
+
if (res.error) {
|
|
20
|
+
throw new Error(res.error.message);
|
|
21
|
+
}
|
|
22
|
+
const checkout = res.data;
|
|
23
|
+
const theme = new URL(checkout.url).searchParams.get("theme") ?? "light";
|
|
24
|
+
return await PolarEmbedCheckout.create(checkout.url, theme);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export {
|
|
32
|
+
polarClient
|
|
33
|
+
};
|
|
34
|
+
//# sourceMappingURL=chunk-5QR5Y5PS.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/client.ts"],"sourcesContent":["import { PolarEmbedCheckout } from \"@polar-sh/checkout/embed\";\nimport type { PolarEmbedCheckout as PolarEmbedCheckoutType } from \"@polar-sh/checkout/embed\";\nimport type { BetterAuthClientPlugin } from \"better-auth\";\nimport type { BetterFetchOption } from \"better-auth/client\";\nimport type { CheckoutParams, polar } from \"./index\";\n\nexport type { PolarEmbedCheckoutType as PolarEmbedCheckout };\n\nexport const polarClient = () => {\n\treturn {\n\t\tid: \"polar-client\",\n\t\t$InferServerPlugin: {} as ReturnType<typeof polar>,\n\t\tgetActions: ($fetch) => {\n\t\t\treturn {\n\t\t\t\tcheckoutEmbed: async (\n\t\t\t\t\tdata: Omit<CheckoutParams, \"redirect\" | \"embedOrigin\">,\n\t\t\t\t\tfetchOptions?: BetterFetchOption,\n\t\t\t\t): Promise<PolarEmbedCheckoutType> => {\n\t\t\t\t\tconst res = await $fetch(\"/checkout\", {\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t...data,\n\t\t\t\t\t\t\tredirect: false,\n\t\t\t\t\t\t\tembedOrigin: window.location.origin,\n\t\t\t\t\t\t},\n\t\t\t\t\t\t...fetchOptions,\n\t\t\t\t\t});\n\n\t\t\t\t\tif (res.error) {\n\t\t\t\t\t\tthrow new Error(res.error.message);\n\t\t\t\t\t}\n\n\t\t\t\t\tconst checkout = res.data as { url: string };\n\n\t\t\t\t\tconst theme =\n\t\t\t\t\t\t(new URL(checkout.url).searchParams.get(\"theme\") as\n\t\t\t\t\t\t\t| \"light\"\n\t\t\t\t\t\t\t| \"dark\"\n\t\t\t\t\t\t\t| undefined) ?? \"light\";\n\n\t\t\t\t\treturn await PolarEmbedCheckout.create(checkout.url, theme);\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t} satisfies BetterAuthClientPlugin;\n};\n"],"mappings":";AAAA,SAAS,0BAA0B;AAQ5B,IAAM,cAAc,MAAM;AAChC,SAAO;AAAA,IACN,IAAI;AAAA,IACJ,oBAAoB,CAAC;AAAA,IACrB,YAAY,CAAC,WAAW;AACvB,aAAO;AAAA,QACN,eAAe,OACd,MACA,iBACqC;AACrC,gBAAM,MAAM,MAAM,OAAO,aAAa;AAAA,YACrC,QAAQ;AAAA,YACR,MAAM;AAAA,cACL,GAAG;AAAA,cACH,UAAU;AAAA,cACV,aAAa,OAAO,SAAS;AAAA,YAC9B;AAAA,YACA,GAAG;AAAA,UACJ,CAAC;AAED,cAAI,IAAI,OAAO;AACd,kBAAM,IAAI,MAAM,IAAI,MAAM,OAAO;AAAA,UAClC;AAEA,gBAAM,WAAW,IAAI;AAErB,gBAAM,QACJ,IAAI,IAAI,SAAS,GAAG,EAAE,aAAa,IAAI,OAAO,KAG9B;AAElB,iBAAO,MAAM,mBAAmB,OAAO,SAAS,KAAK,KAAK;AAAA,QAC3D;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;","names":[]}
|
package/dist/client.cjs
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/client.ts
|
|
21
|
+
var client_exports = {};
|
|
22
|
+
__export(client_exports, {
|
|
23
|
+
polarClient: () => polarClient
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(client_exports);
|
|
26
|
+
var import_embed = require("@polar-sh/checkout/embed");
|
|
27
|
+
var polarClient = () => {
|
|
28
|
+
return {
|
|
29
|
+
id: "polar-client",
|
|
30
|
+
$InferServerPlugin: {},
|
|
31
|
+
getActions: ($fetch) => {
|
|
32
|
+
return {
|
|
33
|
+
checkoutEmbed: async (data, fetchOptions) => {
|
|
34
|
+
const res = await $fetch("/checkout", {
|
|
35
|
+
method: "POST",
|
|
36
|
+
body: {
|
|
37
|
+
...data,
|
|
38
|
+
redirect: false,
|
|
39
|
+
embedOrigin: window.location.origin
|
|
40
|
+
},
|
|
41
|
+
...fetchOptions
|
|
42
|
+
});
|
|
43
|
+
if (res.error) {
|
|
44
|
+
throw new Error(res.error.message);
|
|
45
|
+
}
|
|
46
|
+
const checkout = res.data;
|
|
47
|
+
const theme = new URL(checkout.url).searchParams.get("theme") ?? "light";
|
|
48
|
+
return await import_embed.PolarEmbedCheckout.create(checkout.url, theme);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
55
|
+
0 && (module.exports = {
|
|
56
|
+
polarClient
|
|
57
|
+
});
|
|
58
|
+
//# sourceMappingURL=client.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/client.ts"],"sourcesContent":["import { PolarEmbedCheckout } from \"@polar-sh/checkout/embed\";\nimport type { PolarEmbedCheckout as PolarEmbedCheckoutType } from \"@polar-sh/checkout/embed\";\nimport type { BetterAuthClientPlugin } from \"better-auth\";\nimport type { BetterFetchOption } from \"better-auth/client\";\nimport type { CheckoutParams, polar } from \"./index\";\n\nexport type { PolarEmbedCheckoutType as PolarEmbedCheckout };\n\nexport const polarClient = () => {\n\treturn {\n\t\tid: \"polar-client\",\n\t\t$InferServerPlugin: {} as ReturnType<typeof polar>,\n\t\tgetActions: ($fetch) => {\n\t\t\treturn {\n\t\t\t\tcheckoutEmbed: async (\n\t\t\t\t\tdata: Omit<CheckoutParams, \"redirect\" | \"embedOrigin\">,\n\t\t\t\t\tfetchOptions?: BetterFetchOption,\n\t\t\t\t): Promise<PolarEmbedCheckoutType> => {\n\t\t\t\t\tconst res = await $fetch(\"/checkout\", {\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t...data,\n\t\t\t\t\t\t\tredirect: false,\n\t\t\t\t\t\t\tembedOrigin: window.location.origin,\n\t\t\t\t\t\t},\n\t\t\t\t\t\t...fetchOptions,\n\t\t\t\t\t});\n\n\t\t\t\t\tif (res.error) {\n\t\t\t\t\t\tthrow new Error(res.error.message);\n\t\t\t\t\t}\n\n\t\t\t\t\tconst checkout = res.data as { url: string };\n\n\t\t\t\t\tconst theme =\n\t\t\t\t\t\t(new URL(checkout.url).searchParams.get(\"theme\") as\n\t\t\t\t\t\t\t| \"light\"\n\t\t\t\t\t\t\t| \"dark\"\n\t\t\t\t\t\t\t| undefined) ?? \"light\";\n\n\t\t\t\t\treturn await PolarEmbedCheckout.create(checkout.url, theme);\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t} satisfies BetterAuthClientPlugin;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAmC;AAQ5B,IAAM,cAAc,MAAM;AAChC,SAAO;AAAA,IACN,IAAI;AAAA,IACJ,oBAAoB,CAAC;AAAA,IACrB,YAAY,CAAC,WAAW;AACvB,aAAO;AAAA,QACN,eAAe,OACd,MACA,iBACqC;AACrC,gBAAM,MAAM,MAAM,OAAO,aAAa;AAAA,YACrC,QAAQ;AAAA,YACR,MAAM;AAAA,cACL,GAAG;AAAA,cACH,UAAU;AAAA,cACV,aAAa,OAAO,SAAS;AAAA,YAC9B;AAAA,YACA,GAAG;AAAA,UACJ,CAAC;AAED,cAAI,IAAI,OAAO;AACd,kBAAM,IAAI,MAAM,IAAI,MAAM,OAAO;AAAA,UAClC;AAEA,gBAAM,WAAW,IAAI;AAErB,gBAAM,QACJ,IAAI,IAAI,SAAS,GAAG,EAAE,aAAa,IAAI,OAAO,KAG9B;AAElB,iBAAO,MAAM,gCAAmB,OAAO,SAAS,KAAK,KAAK;AAAA,QAC3D;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;","names":[]}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import 'better-auth/client';
|
|
2
|
+
export { PolarEmbedCheckout } from '@polar-sh/checkout/embed';
|
|
3
|
+
export { polarClient } from './index.cjs';
|
|
4
|
+
import '@polar-sh/sdk/models/operations/customerportalorderslist.js';
|
|
5
|
+
import '@polar-sh/sdk/models/operations/customerportalsubscriptionslist.js';
|
|
6
|
+
import '@polar-sh/sdk/models/operations/subscriptionslist.js';
|
|
7
|
+
import '@polar-sh/sdk/models/operations/customerportalbenefitgrantslist.js';
|
|
8
|
+
import '@polar-sh/sdk/models/components/customerstate.js';
|
|
9
|
+
import '@polar-sh/sdk/models/components/eventsingestresponse.js';
|
|
10
|
+
import '@polar-sh/sdk/types/operations.js';
|
|
11
|
+
import '@polar-sh/sdk/models/operations/customerportalcustomermeterslist.js';
|
|
12
|
+
import 'better-auth';
|
|
13
|
+
import 'zod';
|
|
14
|
+
import '@polar-sh/sdk';
|
|
15
|
+
import '@polar-sh/sdk/models/components/webhookbenefitcreatedpayload';
|
|
16
|
+
import '@polar-sh/sdk/models/components/webhookbenefitgrantcreatedpayload';
|
|
17
|
+
import '@polar-sh/sdk/models/components/webhookbenefitgrantrevokedpayload';
|
|
18
|
+
import '@polar-sh/sdk/models/components/webhookbenefitgrantupdatedpayload';
|
|
19
|
+
import '@polar-sh/sdk/models/components/webhookbenefitupdatedpayload';
|
|
20
|
+
import '@polar-sh/sdk/models/components/webhookcheckoutcreatedpayload';
|
|
21
|
+
import '@polar-sh/sdk/models/components/webhookcheckoutupdatedpayload';
|
|
22
|
+
import '@polar-sh/sdk/models/components/webhookcustomercreatedpayload';
|
|
23
|
+
import '@polar-sh/sdk/models/components/webhookcustomerdeletedpayload';
|
|
24
|
+
import '@polar-sh/sdk/models/components/webhookcustomerstatechangedpayload';
|
|
25
|
+
import '@polar-sh/sdk/models/components/webhookcustomerupdatedpayload';
|
|
26
|
+
import '@polar-sh/sdk/models/components/webhookordercreatedpayload';
|
|
27
|
+
import '@polar-sh/sdk/models/components/webhookorderpaidpayload';
|
|
28
|
+
import '@polar-sh/sdk/models/components/webhookorderrefundedpayload';
|
|
29
|
+
import '@polar-sh/sdk/models/components/webhookorderupdatedpayload';
|
|
30
|
+
import '@polar-sh/sdk/models/components/webhookorganizationupdatedpayload';
|
|
31
|
+
import '@polar-sh/sdk/models/components/webhookproductcreatedpayload';
|
|
32
|
+
import '@polar-sh/sdk/models/components/webhookproductupdatedpayload';
|
|
33
|
+
import '@polar-sh/sdk/models/components/webhookrefundcreatedpayload';
|
|
34
|
+
import '@polar-sh/sdk/models/components/webhookrefundupdatedpayload';
|
|
35
|
+
import '@polar-sh/sdk/models/components/webhooksubscriptionactivepayload';
|
|
36
|
+
import '@polar-sh/sdk/models/components/webhooksubscriptioncanceledpayload';
|
|
37
|
+
import '@polar-sh/sdk/models/components/webhooksubscriptioncreatedpayload';
|
|
38
|
+
import '@polar-sh/sdk/models/components/webhooksubscriptionrevokedpayload';
|
|
39
|
+
import '@polar-sh/sdk/models/components/webhooksubscriptionuncanceledpayload';
|
|
40
|
+
import '@polar-sh/sdk/models/components/webhooksubscriptionupdatedpayload';
|
|
41
|
+
import '@polar-sh/sdk/webhooks';
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import 'better-auth/client';
|
|
2
|
+
export { PolarEmbedCheckout } from '@polar-sh/checkout/embed';
|
|
3
|
+
export { polarClient } from './index.js';
|
|
4
|
+
import '@polar-sh/sdk/models/operations/customerportalorderslist.js';
|
|
5
|
+
import '@polar-sh/sdk/models/operations/customerportalsubscriptionslist.js';
|
|
6
|
+
import '@polar-sh/sdk/models/operations/subscriptionslist.js';
|
|
7
|
+
import '@polar-sh/sdk/models/operations/customerportalbenefitgrantslist.js';
|
|
8
|
+
import '@polar-sh/sdk/models/components/customerstate.js';
|
|
9
|
+
import '@polar-sh/sdk/models/components/eventsingestresponse.js';
|
|
10
|
+
import '@polar-sh/sdk/types/operations.js';
|
|
11
|
+
import '@polar-sh/sdk/models/operations/customerportalcustomermeterslist.js';
|
|
12
|
+
import 'better-auth';
|
|
13
|
+
import 'zod';
|
|
14
|
+
import '@polar-sh/sdk';
|
|
15
|
+
import '@polar-sh/sdk/models/components/webhookbenefitcreatedpayload';
|
|
16
|
+
import '@polar-sh/sdk/models/components/webhookbenefitgrantcreatedpayload';
|
|
17
|
+
import '@polar-sh/sdk/models/components/webhookbenefitgrantrevokedpayload';
|
|
18
|
+
import '@polar-sh/sdk/models/components/webhookbenefitgrantupdatedpayload';
|
|
19
|
+
import '@polar-sh/sdk/models/components/webhookbenefitupdatedpayload';
|
|
20
|
+
import '@polar-sh/sdk/models/components/webhookcheckoutcreatedpayload';
|
|
21
|
+
import '@polar-sh/sdk/models/components/webhookcheckoutupdatedpayload';
|
|
22
|
+
import '@polar-sh/sdk/models/components/webhookcustomercreatedpayload';
|
|
23
|
+
import '@polar-sh/sdk/models/components/webhookcustomerdeletedpayload';
|
|
24
|
+
import '@polar-sh/sdk/models/components/webhookcustomerstatechangedpayload';
|
|
25
|
+
import '@polar-sh/sdk/models/components/webhookcustomerupdatedpayload';
|
|
26
|
+
import '@polar-sh/sdk/models/components/webhookordercreatedpayload';
|
|
27
|
+
import '@polar-sh/sdk/models/components/webhookorderpaidpayload';
|
|
28
|
+
import '@polar-sh/sdk/models/components/webhookorderrefundedpayload';
|
|
29
|
+
import '@polar-sh/sdk/models/components/webhookorderupdatedpayload';
|
|
30
|
+
import '@polar-sh/sdk/models/components/webhookorganizationupdatedpayload';
|
|
31
|
+
import '@polar-sh/sdk/models/components/webhookproductcreatedpayload';
|
|
32
|
+
import '@polar-sh/sdk/models/components/webhookproductupdatedpayload';
|
|
33
|
+
import '@polar-sh/sdk/models/components/webhookrefundcreatedpayload';
|
|
34
|
+
import '@polar-sh/sdk/models/components/webhookrefundupdatedpayload';
|
|
35
|
+
import '@polar-sh/sdk/models/components/webhooksubscriptionactivepayload';
|
|
36
|
+
import '@polar-sh/sdk/models/components/webhooksubscriptioncanceledpayload';
|
|
37
|
+
import '@polar-sh/sdk/models/components/webhooksubscriptioncreatedpayload';
|
|
38
|
+
import '@polar-sh/sdk/models/components/webhooksubscriptionrevokedpayload';
|
|
39
|
+
import '@polar-sh/sdk/models/components/webhooksubscriptionuncanceledpayload';
|
|
40
|
+
import '@polar-sh/sdk/models/components/webhooksubscriptionupdatedpayload';
|
|
41
|
+
import '@polar-sh/sdk/webhooks';
|
package/dist/client.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/hooks/customer.ts","../src/client.ts","../src/plugins/portal.ts","../src/plugins/checkout.ts","../src/plugins/usage.ts","../../adapter-utils/dist/index.js","../src/plugins/webhooks.ts"],"sourcesContent":["import type { BetterAuthPlugin } from \"better-auth\";\nimport {\n\tonAfterUserCreate,\n\tonBeforeUserCreate,\n\tonUserDelete,\n\tonUserUpdate,\n} from \"./hooks/customer\";\nimport type { PolarEndpoints, PolarOptions } from \"./types\";\n\nexport { polarClient } from \"./client\";\n\nexport * from \"./plugins/portal\";\nexport * from \"./plugins/checkout\";\nexport * from \"./plugins/usage\";\nexport * from \"./plugins/webhooks\";\n\nexport const polar = <O extends PolarOptions>(options: O) => {\n\tconst plugins = options.use\n\t\t.map((use) => use(options.client))\n\t\t.reduce((acc, plugin) => {\n\t\t\tObject.assign(acc, plugin);\n\t\t\treturn acc;\n\t\t}, {} as PolarEndpoints);\n\n\treturn {\n\t\tid: \"polar\",\n\t\tendpoints: {\n\t\t\t...plugins,\n\t\t},\n\t\tinit() {\n\t\t\treturn {\n\t\t\t\toptions: {\n\t\t\t\t\tdatabaseHooks: {\n\t\t\t\t\t\tuser: {\n\t\t\t\t\t\t\tcreate: {\n\t\t\t\t\t\t\t\tbefore: onBeforeUserCreate(options),\n\t\t\t\t\t\t\t\tafter: onAfterUserCreate(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tupdate: {\n\t\t\t\t\t\t\t\tafter: onUserUpdate(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tdelete: {\n\t\t\t\t\t\t\t\tafter: onUserDelete(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t} satisfies BetterAuthPlugin;\n};\n","import type { GenericEndpointContext, User } from \"better-auth\";\nimport { APIError } from \"better-auth/api\";\nimport type { PolarOptions } from \"../types\";\n\nexport const onBeforeUserCreate =\n\t(options: PolarOptions) =>\n\tasync (user: Partial<User>, context?: GenericEndpointContext | undefined) => {\n\t\tif (context && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tconst params = options.getCustomerCreateParams\n\t\t\t\t\t? await options.getCustomerCreateParams({\n\t\t\t\t\t\t\tuser,\n\t\t\t\t\t\t})\n\t\t\t\t\t: {};\n\n\t\t\t\tif (!user.email) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"An associated email is required\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\t// Check if customer already exists\n\t\t\t\tconst { result: existingCustomers } =\n\t\t\t\t\tawait options.client.customers.list({ email: user.email });\n\t\t\t\tconst existingCustomer = existingCustomers.items[0];\n\n\t\t\t\t// Skip creation if customer already exists\n\t\t\t\tif (!existingCustomer) {\n\t\t\t\t\tawait options.client.customers.create({\n\t\t\t\t\t\t...params,\n\t\t\t\t\t\temail: user.email,\n\t\t\t\t\t\tname: user.name,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: `Polar customer creation failed. Error: ${e.message}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: `Polar customer creation failed. Error: ${e}`,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t};\n\nexport const onAfterUserCreate =\n\t(options: PolarOptions) =>\n\tasync (user: User, context?: GenericEndpointContext | undefined) => {\n\t\tif (context && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tconst { result: existingCustomers } =\n\t\t\t\t\tawait options.client.customers.list({ email: user.email });\n\t\t\t\tconst existingCustomer = existingCustomers.items[0];\n\n\t\t\t\tif (existingCustomer) {\n\t\t\t\t\tif (existingCustomer.externalId !== user.id) {\n\t\t\t\t\t\tawait options.client.customers.update({\n\t\t\t\t\t\t\tid: existingCustomer.id,\n\t\t\t\t\t\t\tcustomerUpdate: {\n\t\t\t\t\t\t\t\texternalId: user.id,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: `Polar customer creation failed. Error: ${e.message}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: `Polar customer creation failed. Error: ${e}`,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t};\n\nexport const onUserUpdate =\n\t(options: PolarOptions) =>\n\tasync (user: User, context?: GenericEndpointContext | undefined) => {\n\t\tif (context && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tawait options.client.customers.updateExternal({\n\t\t\t\t\texternalId: user.id,\n\t\t\t\t\tcustomerUpdateExternalID: {\n\t\t\t\t\t\temail: user.email,\n\t\t\t\t\t\tname: user.name,\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tcontext.context.logger.error(\n\t\t\t\t\t\t`Polar customer update failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tcontext.context.logger.error(\n\t\t\t\t\t\t`Polar customer update failed. Error: ${e}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n\nexport const onUserDelete =\n\t(options: PolarOptions) =>\n\tasync (user: User, context?: GenericEndpointContext | undefined) => {\n\t\tif (context && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tif (user.email) {\n\t\t\t\t\tconst { result: existingCustomers } =\n\t\t\t\t\t\tawait options.client.customers.list({ email: user.email });\n\t\t\t\t\tconst existingCustomer = existingCustomers.items[0];\n\t\t\t\t\tif (existingCustomer) {\n\t\t\t\t\t\tawait options.client.customers.delete({\n\t\t\t\t\t\t\tid: existingCustomer.id,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tcontext?.context.logger.error(\n\t\t\t\t\t\t`Polar customer delete failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tcontext?.context.logger.error(\n\t\t\t\t\t`Polar customer delete failed. Error: ${e}`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t};\n","import { PolarEmbedCheckout } from \"@polar-sh/checkout/embed\";\nimport type { BetterAuthClientPlugin } from \"better-auth\";\nimport type { BetterFetchOption } from \"better-auth/client\";\nimport type { CheckoutParams, polar } from \"./index\";\n\nexport const polarClient = () => {\n\treturn {\n\t\tid: \"polar-client\",\n\t\t$InferServerPlugin: {} as ReturnType<typeof polar>,\n\t\tgetActions: ($fetch) => {\n\t\t\treturn {\n\t\t\t\tcheckoutEmbed: async (\n\t\t\t\t\tdata: Omit<CheckoutParams, \"redirect\" | \"embedOrigin\">,\n\t\t\t\t\tfetchOptions?: BetterFetchOption,\n\t\t\t\t) => {\n\t\t\t\t\tconst res = await $fetch(\"/checkout\", {\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t...data,\n\t\t\t\t\t\t\tredirect: false,\n\t\t\t\t\t\t\tembedOrigin: window.location.origin,\n\t\t\t\t\t\t},\n\t\t\t\t\t\t...fetchOptions,\n\t\t\t\t\t});\n\n\t\t\t\t\tif (res.error) {\n\t\t\t\t\t\tthrow new Error(res.error.message);\n\t\t\t\t\t}\n\n\t\t\t\t\tconst checkout = res.data as { url: string };\n\n\t\t\t\t\tconst theme =\n\t\t\t\t\t\t(new URL(checkout.url).searchParams.get(\"theme\") as\n\t\t\t\t\t\t\t| \"light\"\n\t\t\t\t\t\t\t| \"dark\"\n\t\t\t\t\t\t\t| undefined) ?? \"light\";\n\n\t\t\t\t\treturn await PolarEmbedCheckout.create(checkout.url, theme);\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t} satisfies BetterAuthClientPlugin;\n};\n","import type { Polar } from \"@polar-sh/sdk\";\nimport { APIError } from \"better-auth/api\";\nimport { sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\n\nexport interface PortalConfig {\n\treturnUrl?: string;\n}\n\nexport const portal =\n\t({ returnUrl }: PortalConfig = {}) =>\n\t(polar: Polar) => {\n\t\tconst retUrl = returnUrl ? new URL(returnUrl) : undefined;\n\n\t\treturn {\n\t\t\tportal: createAuthEndpoint(\n\t\t\t\t\"/customer/portal\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tif (!ctx.context.session?.user.id) {\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t\t\treturnUrl: retUrl ? decodeURI(retUrl.toString()) : undefined,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\t\turl: customerSession.customerPortalUrl,\n\t\t\t\t\t\t\tredirect: true,\n\t\t\t\t\t\t});\n\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t`Polar customer portal creation failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Customer portal creation failed\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t\tstate: createAuthEndpoint(\n\t\t\t\t\"/customer/state\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst state = await polar.customers.getStateExternal({\n\t\t\t\t\t\t\texternalId: ctx.context.session?.user.id,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\treturn ctx.json(state);\n\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t`Polar subscriptions list failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Subscriptions list failed\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t\tbenefits: createAuthEndpoint(\n\t\t\t\t\"/customer/benefits/list\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\tquery: z\n\t\t\t\t\t\t.object({\n\t\t\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t\t\t})\n\t\t\t\t\t\t.optional(),\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tconst benefits = await polar.customerPortal.benefitGrants.list(\n\t\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\treturn ctx.json(benefits);\n\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t`Polar benefits list failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Benefits list failed\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t\tsubscriptions: createAuthEndpoint(\n\t\t\t\t\"/customer/subscriptions/list\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\tquery: z\n\t\t\t\t\t\t.object({\n\t\t\t\t\t\t\treferenceId: z.string().optional(),\n\t\t\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t\t\t\tactive: z.coerce.boolean().optional(),\n\t\t\t\t\t\t})\n\t\t\t\t\t\t.optional(),\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tif (ctx.query?.referenceId) {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst subscriptions = await polar.subscriptions.list({\n\t\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t\t\tactive: ctx.query?.active,\n\t\t\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\t\t\treferenceId: ctx.query?.referenceId,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\treturn ctx.json(subscriptions);\n\t\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\t\tconsole.log(e);\n\t\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t\t`Polar subscriptions list with referenceId failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\t\tmessage: \"Subscriptions list with referenceId failed\",\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tconst subscriptions = await polar.customerPortal.subscriptions.list(\n\t\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t\t\tactive: ctx.query?.active,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\treturn ctx.json(subscriptions);\n\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t`Polar subscriptions list failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Polar subscriptions list failed\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t\torders: createAuthEndpoint(\n\t\t\t\t\"/customer/orders/list\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\tquery: z\n\t\t\t\t\t\t.object({\n\t\t\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t\t\t\tproductBillingType: z.enum([\"recurring\", \"one_time\"]).optional(),\n\t\t\t\t\t\t})\n\t\t\t\t\t\t.optional(),\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tconst orders = await polar.customerPortal.orders.list(\n\t\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t\t\tproductBillingType: ctx.query?.productBillingType,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\treturn ctx.json(orders);\n\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t`Polar orders list failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Orders list failed\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t};\n\t};\n","import type { Polar } from \"@polar-sh/sdk\";\nimport { APIError, getSessionFromCtx } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\nimport type { Product } from \"../types\";\n\nexport interface CheckoutOptions {\n\t/**\n\t * Optional list of slug -> productId mappings for easy slug checkouts\n\t */\n\tproducts?: Product[] | (() => Promise<Product[]>);\n\t/**\n\t * Checkout Success URL\n\t */\n\tsuccessUrl?: string;\n\t/**\n\t * Checkout Return URL\n\t */\n\treturnUrl?: string;\n\t/**\n\t * Only allow authenticated customers to checkout\n\t */\n\tauthenticatedUsersOnly?: boolean;\n\t/**\n\t * Checkout theme\n\t */\n\ttheme?: \"light\" | \"dark\";\n\t/**\n\t * Redirect to checkout page\n\t */\n\tredirect?: boolean;\n}\n\nexport const CheckoutParams = z.object({\n\tproducts: z.union([z.array(z.string()), z.string()]).optional(),\n\tslug: z.string().optional(),\n\treferenceId: z.string().optional(),\n\tcustomFieldData: z\n\t\t.record(z.string(), z.union([z.string(), z.number(), z.boolean()]))\n\t\t.optional(),\n\tmetadata: z\n\t\t.record(z.string(), z.union([z.string(), z.number(), z.boolean()]))\n\t\t.optional(),\n\tallowDiscountCodes: z.coerce.boolean().optional(),\n\tdiscountId: z.string().optional(),\n\tredirect: z.coerce.boolean().optional(),\n\tembedOrigin: z.string().url().optional(),\n});\n\nexport type CheckoutParams = z.infer<typeof CheckoutParams>;\n\nexport const checkout =\n\t(checkoutOptions: CheckoutOptions = {}) =>\n\t(polar: Polar) => {\n\t\treturn {\n\t\t\tcheckout: createAuthEndpoint(\n\t\t\t\t\"/checkout\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: CheckoutParams,\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\t\t\tlet productIds: string[] = [];\n\n\t\t\t\t\tif (ctx.body.slug) {\n\t\t\t\t\t\tconst resolvedProducts = await (typeof checkoutOptions.products ===\n\t\t\t\t\t\t\"function\"\n\t\t\t\t\t\t\t? checkoutOptions.products()\n\t\t\t\t\t\t\t: checkoutOptions.products);\n\n\t\t\t\t\t\tconst productId = resolvedProducts?.find(\n\t\t\t\t\t\t\t(product) => product.slug === ctx.body.slug,\n\t\t\t\t\t\t)?.productId;\n\n\t\t\t\t\t\tif (!productId) {\n\t\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\t\tmessage: \"Product not found\",\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tproductIds = [productId];\n\t\t\t\t\t} else {\n\t\t\t\t\t\tproductIds = Array.isArray(ctx.body.products)\n\t\t\t\t\t\t\t? ctx.body.products.filter((id) => id !== undefined)\n\t\t\t\t\t\t\t: [ctx.body.products].filter((id) => id !== undefined);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (checkoutOptions.authenticatedUsersOnly && !session?.user.id) {\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"You must be logged in to checkout\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst checkout = await polar.checkouts.create({\n\t\t\t\t\t\t\texternalCustomerId: session?.user.id,\n\t\t\t\t\t\t\tproducts: productIds,\n\t\t\t\t\t\t\tsuccessUrl: checkoutOptions.successUrl\n\t\t\t\t\t\t\t\t? new URL(\n\t\t\t\t\t\t\t\t\t\tcheckoutOptions.successUrl,\n\t\t\t\t\t\t\t\t\t\tctx.request?.url ?? ctx.context.baseURL,\n\t\t\t\t\t\t\t\t\t).toString()\n\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t\t\tmetadata: ctx.body.referenceId\n\t\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\t\treferenceId: ctx.body.referenceId,\n\t\t\t\t\t\t\t\t\t\t...ctx.body.metadata,\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t: ctx.body.metadata,\n\t\t\t\t\t\t\tcustomFieldData: ctx.body.customFieldData,\n\t\t\t\t\t\t\tallowDiscountCodes: ctx.body.allowDiscountCodes ?? true,\n\t\t\t\t\t\t\tdiscountId: ctx.body.discountId,\n\t\t\t\t\t\t\tembedOrigin: ctx.body.embedOrigin,\n\t\t\t\t\t\t\treturnUrl: checkoutOptions.returnUrl\n\t\t\t\t\t\t\t\t? new URL(\n\t\t\t\t\t\t\t\t\t\tcheckoutOptions.returnUrl,\n\t\t\t\t\t\t\t\t\t\tctx.request?.url ?? ctx.context.baseURL,\n\t\t\t\t\t\t\t\t\t).toString()\n\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tconst redirectUrl = new URL(checkout.url);\n\n\t\t\t\t\t\tif (checkoutOptions.theme) {\n\t\t\t\t\t\t\tredirectUrl.searchParams.set(\"theme\", checkoutOptions.theme);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\t\turl: redirectUrl.toString(),\n\t\t\t\t\t\t\tredirect: ctx.body.redirect ?? true,\n\t\t\t\t\t\t});\n\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t`Polar checkout creation failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Checkout creation failed\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t};\n\t};\n","import type { Polar } from \"@polar-sh/sdk\";\nimport {\n\tAPIError,\n\tcreateAuthEndpoint,\n\tsessionMiddleware,\n} from \"better-auth/api\";\nimport { z } from \"zod\";\nimport type { Product } from \"../types\";\n\nexport interface UsageOptions {\n\t/**\n\t * Products to use for topping up credits\n\t */\n\tcreditProducts?: Product[] | (() => Promise<Product[]>);\n}\n\nexport const usage = (_usageOptions?: UsageOptions) => (polar: Polar) => {\n\treturn {\n\t\tmeters: createAuthEndpoint(\n\t\t\t\"/usage/meters/list\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\tquery: z.object({\n\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t}),\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\tconst customerMeters = await polar.customerPortal.customerMeters.list(\n\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\n\t\t\t\t\treturn ctx.json(customerMeters);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar meters list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Meters list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\tingestion: createAuthEndpoint(\n\t\t\t\"/usage/ingest\",\n\t\t\t{\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tbody: z.object({\n\t\t\t\t\tevent: z.string(),\n\t\t\t\t\tmetadata: z.record(\n\t\t\t\t\t\tz.string(),\n\t\t\t\t\t\tz.union([z.string(), z.number(), z.boolean()]),\n\t\t\t\t\t),\n\t\t\t\t}),\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst ingestion = await polar.events.ingest({\n\t\t\t\t\t\tevents: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tname: ctx.body.event,\n\t\t\t\t\t\t\t\tmetadata: ctx.body.metadata,\n\t\t\t\t\t\t\t\texternalCustomerId: ctx.context.session.user.id,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json(ingestion);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar ingestion failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Ingestion failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t};\n};\n","// src/webhooks/webhooks.ts\nvar handleWebhookPayload = async (payload, { webhookSecret, entitlements, onPayload, ...eventHandlers }) => {\n const promises = [];\n if (onPayload) {\n promises.push(onPayload(payload));\n }\n switch (payload.type) {\n case \"checkout.created\":\n if (eventHandlers.onCheckoutCreated) {\n promises.push(eventHandlers.onCheckoutCreated(payload));\n }\n break;\n case \"checkout.updated\":\n if (eventHandlers.onCheckoutUpdated) {\n promises.push(eventHandlers.onCheckoutUpdated(payload));\n }\n break;\n case \"order.created\":\n if (eventHandlers.onOrderCreated) {\n promises.push(eventHandlers.onOrderCreated(payload));\n }\n break;\n case \"order.updated\":\n if (eventHandlers.onOrderUpdated) {\n promises.push(eventHandlers.onOrderUpdated(payload));\n }\n break;\n case \"order.paid\":\n if (eventHandlers.onOrderPaid) {\n promises.push(eventHandlers.onOrderPaid(payload));\n }\n break;\n case \"subscription.created\":\n if (eventHandlers.onSubscriptionCreated) {\n promises.push(eventHandlers.onSubscriptionCreated(payload));\n }\n break;\n case \"subscription.updated\":\n if (eventHandlers.onSubscriptionUpdated) {\n promises.push(eventHandlers.onSubscriptionUpdated(payload));\n }\n break;\n case \"subscription.active\":\n if (eventHandlers.onSubscriptionActive) {\n promises.push(eventHandlers.onSubscriptionActive(payload));\n }\n break;\n case \"subscription.canceled\":\n if (eventHandlers.onSubscriptionCanceled) {\n promises.push(eventHandlers.onSubscriptionCanceled(payload));\n }\n break;\n case \"subscription.uncanceled\":\n if (eventHandlers.onSubscriptionUncanceled) {\n promises.push(eventHandlers.onSubscriptionUncanceled(payload));\n }\n break;\n case \"subscription.revoked\":\n if (eventHandlers.onSubscriptionRevoked) {\n promises.push(eventHandlers.onSubscriptionRevoked(payload));\n }\n break;\n case \"product.created\":\n if (eventHandlers.onProductCreated) {\n promises.push(eventHandlers.onProductCreated(payload));\n }\n break;\n case \"product.updated\":\n if (eventHandlers.onProductUpdated) {\n promises.push(eventHandlers.onProductUpdated(payload));\n }\n break;\n case \"organization.updated\":\n if (eventHandlers.onOrganizationUpdated) {\n promises.push(eventHandlers.onOrganizationUpdated(payload));\n }\n break;\n case \"benefit.created\":\n if (eventHandlers.onBenefitCreated) {\n promises.push(eventHandlers.onBenefitCreated(payload));\n }\n break;\n case \"benefit.updated\":\n if (eventHandlers.onBenefitUpdated) {\n promises.push(eventHandlers.onBenefitUpdated(payload));\n }\n break;\n case \"benefit_grant.created\":\n if (eventHandlers.onBenefitGrantCreated) {\n promises.push(eventHandlers.onBenefitGrantCreated(payload));\n }\n break;\n case \"benefit_grant.updated\":\n if (eventHandlers.onBenefitGrantUpdated) {\n promises.push(eventHandlers.onBenefitGrantUpdated(payload));\n }\n break;\n case \"benefit_grant.revoked\":\n if (eventHandlers.onBenefitGrantRevoked) {\n promises.push(eventHandlers.onBenefitGrantRevoked(payload));\n }\n break;\n case \"customer.created\":\n if (eventHandlers.onCustomerCreated) {\n promises.push(eventHandlers.onCustomerCreated(payload));\n }\n break;\n case \"customer.updated\":\n if (eventHandlers.onCustomerUpdated) {\n promises.push(eventHandlers.onCustomerUpdated(payload));\n }\n break;\n case \"customer.deleted\":\n if (eventHandlers.onCustomerDeleted) {\n promises.push(eventHandlers.onCustomerDeleted(payload));\n }\n break;\n case \"customer.state_changed\":\n if (eventHandlers.onCustomerStateChanged) {\n promises.push(eventHandlers.onCustomerStateChanged(payload));\n }\n break;\n case \"order.refunded\":\n if (eventHandlers.onOrderRefunded) {\n promises.push(eventHandlers.onOrderRefunded(payload));\n }\n break;\n case \"refund.created\":\n if (eventHandlers.onRefundCreated) {\n promises.push(eventHandlers.onRefundCreated(payload));\n }\n break;\n case \"refund.updated\":\n if (eventHandlers.onRefundUpdated) {\n promises.push(eventHandlers.onRefundUpdated(payload));\n }\n break;\n }\n switch (payload.type) {\n case \"benefit_grant.created\":\n case \"benefit_grant.revoked\":\n if (entitlements) {\n for (const handler of entitlements.handlers) {\n promises.push(handler(payload));\n }\n }\n }\n return Promise.all(promises);\n};\n\n// src/entitlement/entitlement.ts\nvar EntitlementStrategy = class {\n grantCallbacks = [];\n revokeCallbacks = [];\n grant(callback) {\n this.grantCallbacks.push(callback);\n return this;\n }\n revoke(callback) {\n this.revokeCallbacks.push(callback);\n return this;\n }\n handler(slug) {\n return async (payload) => {\n if (payload.data.benefit.description === slug) {\n switch (payload.type) {\n case \"benefit_grant.created\":\n await Promise.all(\n this.grantCallbacks.map(\n (callback) => callback({\n customer: payload.data.customer,\n properties: payload.data.properties,\n payload\n })\n )\n );\n break;\n case \"benefit_grant.revoked\":\n await Promise.all(\n this.revokeCallbacks.map(\n (callback) => callback({\n customer: payload.data.customer,\n properties: payload.data.properties,\n payload\n })\n )\n );\n break;\n }\n }\n };\n }\n};\nvar Entitlements = class {\n static handlers = [];\n static use(slug, strategy) {\n this.handlers.push(strategy.handler(slug));\n return this;\n }\n};\nexport {\n EntitlementStrategy,\n Entitlements,\n handleWebhookPayload\n};\n","import { handleWebhookPayload } from \"@polar-sh/adapter-utils\";\nimport type { Polar } from \"@polar-sh/sdk\";\nimport type { WebhookBenefitCreatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitcreatedpayload\";\nimport type { WebhookBenefitGrantCreatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantcreatedpayload\";\nimport type { WebhookBenefitGrantRevokedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantrevokedpayload\";\nimport type { WebhookBenefitGrantUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantupdatedpayload\";\nimport type { WebhookBenefitUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitupdatedpayload\";\nimport type { WebhookCheckoutCreatedPayload } from \"@polar-sh/sdk/models/components/webhookcheckoutcreatedpayload\";\nimport type { WebhookCheckoutUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookcheckoutupdatedpayload\";\nimport type { WebhookCustomerCreatedPayload } from \"@polar-sh/sdk/models/components/webhookcustomercreatedpayload\";\nimport type { WebhookCustomerDeletedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerdeletedpayload\";\nimport type { WebhookCustomerStateChangedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerstatechangedpayload\";\nimport type { WebhookCustomerUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerupdatedpayload\";\nimport type { WebhookOrderCreatedPayload } from \"@polar-sh/sdk/models/components/webhookordercreatedpayload\";\nimport type { WebhookOrderPaidPayload } from \"@polar-sh/sdk/models/components/webhookorderpaidpayload\";\nimport type { WebhookOrderRefundedPayload } from \"@polar-sh/sdk/models/components/webhookorderrefundedpayload\";\nimport type { WebhookOrderUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookorderupdatedpayload\";\nimport type { WebhookOrganizationUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookorganizationupdatedpayload\";\nimport type { WebhookProductCreatedPayload } from \"@polar-sh/sdk/models/components/webhookproductcreatedpayload\";\nimport type { WebhookProductUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookproductupdatedpayload\";\nimport type { WebhookRefundCreatedPayload } from \"@polar-sh/sdk/models/components/webhookrefundcreatedpayload\";\nimport type { WebhookRefundUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookrefundupdatedpayload\";\nimport type { WebhookSubscriptionActivePayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionactivepayload\";\nimport type { WebhookSubscriptionCanceledPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptioncanceledpayload\";\nimport type { WebhookSubscriptionCreatedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptioncreatedpayload\";\nimport type { WebhookSubscriptionRevokedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionrevokedpayload\";\nimport type { WebhookSubscriptionUncanceledPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionuncanceledpayload\";\nimport type { WebhookSubscriptionUpdatedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionupdatedpayload\";\nimport { validateEvent } from \"@polar-sh/sdk/webhooks\";\nimport { APIError, createAuthEndpoint } from \"better-auth/api\";\n\nexport interface WebhooksOptions {\n\t/**\n\t * Webhook Secret\n\t */\n\tsecret: string;\n\t/**\n\t * Generic handler for all webhooks\n\t */\n\tonPayload?: (payload: ReturnType<typeof validateEvent>) => Promise<void>;\n\t/**\n\t * Webhook for checkout created\n\t */\n\tonCheckoutCreated?: (payload: WebhookCheckoutCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for checkout updated\n\t */\n\tonCheckoutUpdated?: (payload: WebhookCheckoutUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order created\n\t */\n\tonOrderCreated?: (payload: WebhookOrderCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order refunded\n\t */\n\tonOrderRefunded?: (payload: WebhookOrderRefundedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order paid\n\t */\n\tonOrderPaid?: (payload: WebhookOrderPaidPayload) => Promise<void>;\n\t/**\n\t * Webhook for order updated\n\t */\n\tonOrderUpdated?: (payload: WebhookOrderUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for refund created\n\t */\n\tonRefundCreated?: (payload: WebhookRefundCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for refund updated\n\t */\n\tonRefundUpdated?: (payload: WebhookRefundUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for subscription created\n\t */\n\tonSubscriptionCreated?: (\n\t\tpayload: WebhookSubscriptionCreatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription updated\n\t */\n\tonSubscriptionUpdated?: (\n\t\tpayload: WebhookSubscriptionUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription active\n\t */\n\tonSubscriptionActive?: (\n\t\tpayload: WebhookSubscriptionActivePayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription canceled\n\t */\n\tonSubscriptionCanceled?: (\n\t\tpayload: WebhookSubscriptionCanceledPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription revoked\n\t */\n\tonSubscriptionRevoked?: (\n\t\tpayload: WebhookSubscriptionRevokedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription uncanceled\n\t */\n\tonSubscriptionUncanceled?: (\n\t\tpayload: WebhookSubscriptionUncanceledPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for product created\n\t */\n\tonProductCreated?: (payload: WebhookProductCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for product updated\n\t */\n\tonProductUpdated?: (payload: WebhookProductUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for organization updated\n\t */\n\tonOrganizationUpdated?: (\n\t\tpayload: WebhookOrganizationUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit created\n\t */\n\tonBenefitCreated?: (payload: WebhookBenefitCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for benefit updated\n\t */\n\tonBenefitUpdated?: (payload: WebhookBenefitUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant created\n\t */\n\tonBenefitGrantCreated?: (\n\t\tpayload: WebhookBenefitGrantCreatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant updated\n\t */\n\tonBenefitGrantUpdated?: (\n\t\tpayload: WebhookBenefitGrantUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant revoked\n\t */\n\tonBenefitGrantRevoked?: (\n\t\tpayload: WebhookBenefitGrantRevokedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for customer created\n\t */\n\tonCustomerCreated?: (payload: WebhookCustomerCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer updated\n\t */\n\tonCustomerUpdated?: (payload: WebhookCustomerUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer deleted\n\t */\n\tonCustomerDeleted?: (payload: WebhookCustomerDeletedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer state changed\n\t */\n\tonCustomerStateChanged?: (\n\t\tpayload: WebhookCustomerStateChangedPayload,\n\t) => Promise<void>;\n}\n\nexport const webhooks = (options: WebhooksOptions) => (_polar: Polar) => {\n\treturn {\n\t\tpolarWebhooks: createAuthEndpoint(\n\t\t\t\"/polar/webhooks\",\n\t\t\t{\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tmetadata: {\n\t\t\t\t\tisAction: false,\n\t\t\t\t},\n\t\t\t\tcloneRequest: true,\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tconst { secret, ...eventHandlers } = options;\n\n\t\t\t\tif (!ctx.request?.body) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\");\n\t\t\t\t}\n\t\t\t\tconst buf = await ctx.request.text();\n\t\t\t\tlet event: ReturnType<typeof validateEvent>;\n\t\t\t\ttry {\n\t\t\t\t\tif (!secret) {\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Polar webhook secret not found\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst headers = {\n\t\t\t\t\t\t\"webhook-id\": ctx.request.headers.get(\"webhook-id\") as string,\n\t\t\t\t\t\t\"webhook-timestamp\": ctx.request.headers.get(\n\t\t\t\t\t\t\t\"webhook-timestamp\",\n\t\t\t\t\t\t) as string,\n\t\t\t\t\t\t\"webhook-signature\": ctx.request.headers.get(\n\t\t\t\t\t\t\t\"webhook-signature\",\n\t\t\t\t\t\t) as string,\n\t\t\t\t\t};\n\n\t\t\t\t\tevent = validateEvent(buf, headers, secret);\n\t\t\t\t} catch (err: unknown) {\n\t\t\t\t\tif (err instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(`${err.message}`);\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: `Webhook Error: ${err.message}`,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: `Webhook Error: ${err}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tawait handleWebhookPayload(event, {\n\t\t\t\t\t\twebhookSecret: secret,\n\t\t\t\t\t\t...eventHandlers,\n\t\t\t\t\t});\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar webhook failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e}`);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"Webhook error: See server logs for more information.\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\treturn ctx.json({ received: true });\n\t\t\t},\n\t\t),\n\t};\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,iBAAyB;AAGlB,IAAM,qBACZ,CAAC,YACD,OAAO,MAAqB,YAAiD;AAC5E,MAAI,WAAW,QAAQ,wBAAwB;AAC9C,QAAI;AACH,YAAM,SAAS,QAAQ,0BACpB,MAAM,QAAQ,wBAAwB;AAAA,QACtC;AAAA,MACD,CAAC,IACA,CAAC;AAEJ,UAAI,CAAC,KAAK,OAAO;AAChB,cAAM,IAAI,oBAAS,eAAe;AAAA,UACjC,SAAS;AAAA,QACV,CAAC;AAAA,MACF;AAGA,YAAM,EAAE,QAAQ,kBAAkB,IACjC,MAAM,QAAQ,OAAO,UAAU,KAAK,EAAE,OAAO,KAAK,MAAM,CAAC;AAC1D,YAAM,mBAAmB,kBAAkB,MAAM,CAAC;AAGlD,UAAI,CAAC,kBAAkB;AACtB,cAAM,QAAQ,OAAO,UAAU,OAAO;AAAA,UACrC,GAAG;AAAA,UACH,OAAO,KAAK;AAAA,UACZ,MAAM,KAAK;AAAA,QACZ,CAAC;AAAA,MACF;AAAA,IACD,SAAS,GAAY;AACpB,UAAI,aAAa,OAAO;AACvB,cA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ACvB,gBAAI,QAAQ,OAAO;AAAA,cAClB,gCAAgC,EAAE,OAAO;AAAA,YAC1C;AAAA,UACD,OAAO;AACN,gBAAI,QAAQ,OAAO,MAAM,gCAAgC,CAAC,EAAE;AAAA,UAC7D;AAEA,gBAAM,IAAI,qBAAS,eAAe;AAAA,YACjC,SAAS;AAAA,UACV,CAAC;AAAA,QACF;AAEA,eAAO,IAAI,KAAK,EAAE,UAAU,KAAK,CAAC;AAAA,MACnC;AAAA,IACD;AAAA,EACD;AACD;;;APhOO,IAAM,QAAQ,CAAyB,YAAe;AAC5D,QAAM,UAAU,QAAQ,IACtB,IAAI,CAAC,QAAQ,IAAI,QAAQ,MAAM,CAAC,EAChC,OAAO,CAAC,KAAK,WAAW;AACxB,WAAO,OAAO,KAAK,MAAM;AACzB,WAAO;AAAA,EACR,GAAG,CAAC,CAAmB;AAExB,SAAO;AAAA,IACN,IAAI;AAAA,IACJ,WAAW;AAAA,MACV,GAAG;AAAA,IACJ;AAAA,IACA,OAAO;AACN,aAAO;AAAA,QACN,SAAS;AAAA,UACR,eAAe;AAAA,YACd,MAAM;AAAA,cACL,QAAQ;AAAA,gBACP,QAAQ,mBAAmB,OAAO;AAAA,gBAClC,OAAO,kBAAkB,OAAO;AAAA,cACjC;AAAA,cACA,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,cACA,QAAQ;AAAA,gBACP,OAAO,aAAa,OAAO;AAAA,cAC5B;AAAA,YACD;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;","names":["checkout","import_api","polar","import_api","import_plugins","import_zod","polar","checkout","import_api","import_zod","polar","import_api"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/hooks/customer.ts","../src/client.ts","../src/plugins/portal.ts","../src/plugins/checkout.ts","../src/plugins/usage.ts","../../adapter-utils/dist/index.js","../src/plugins/webhooks.ts"],"sourcesContent":["import type { BetterAuthPlugin } from \"better-auth\";\nimport {\n\tonAfterUserCreate,\n\tonBeforeUserCreate,\n\tonUserDelete,\n\tonUserUpdate,\n} from \"./hooks/customer\";\nimport type { PolarEndpoints, PolarOptions } from \"./types\";\n\nexport { polarClient } from \"./client\";\n\nexport * from \"./plugins/portal\";\nexport * from \"./plugins/checkout\";\nexport * from \"./plugins/usage\";\nexport * from \"./plugins/webhooks\";\n\nexport const polar = <O extends PolarOptions>(options: O) => {\n\tconst plugins = options.use\n\t\t.map((use) => use(options.client))\n\t\t.reduce((acc, plugin) => {\n\t\t\tObject.assign(acc, plugin);\n\t\t\treturn acc;\n\t\t}, {} as PolarEndpoints);\n\n\treturn {\n\t\tid: \"polar\",\n\t\tendpoints: {\n\t\t\t...plugins,\n\t\t},\n\t\tinit() {\n\t\t\treturn {\n\t\t\t\toptions: {\n\t\t\t\t\tdatabaseHooks: {\n\t\t\t\t\t\tuser: {\n\t\t\t\t\t\t\tcreate: {\n\t\t\t\t\t\t\t\tbefore: onBeforeUserCreate(options),\n\t\t\t\t\t\t\t\tafter: onAfterUserCreate(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tupdate: {\n\t\t\t\t\t\t\t\tafter: onUserUpdate(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tdelete: {\n\t\t\t\t\t\t\t\tafter: onUserDelete(options),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t} satisfies BetterAuthPlugin;\n};\n","import type { GenericEndpointContext, User } from \"better-auth\";\nimport { APIError } from \"better-auth/api\";\nimport type { PolarOptions } from \"../types\";\n\nexport const onBeforeUserCreate =\n\t(options: PolarOptions) =>\n\tasync (user: Partial<User>, context: GenericEndpointContext | null) => {\n\t\tif (context && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tconst params = options.getCustomerCreateParams\n\t\t\t\t\t? await options.getCustomerCreateParams({\n\t\t\t\t\t\t\tuser,\n\t\t\t\t\t\t})\n\t\t\t\t\t: {};\n\n\t\t\t\tif (!user.email) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"An associated email is required\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\t// Check if customer already exists\n\t\t\t\tconst { result: existingCustomers } =\n\t\t\t\t\tawait options.client.customers.list({ email: user.email });\n\t\t\t\tconst existingCustomer = existingCustomers.items[0];\n\n\t\t\t\t// Skip creation if customer already exists\n\t\t\t\tif (!existingCustomer) {\n\t\t\t\t\tawait options.client.customers.create({\n\t\t\t\t\t\t...params,\n\t\t\t\t\t\temail: user.email,\n\t\t\t\t\t\tname: user.name,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: `Polar customer creation failed. Error: ${e.message}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: `Polar customer creation failed. Error: ${e}`,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t};\n\nexport const onAfterUserCreate =\n\t(options: PolarOptions) =>\n\tasync (user: User, context: GenericEndpointContext | null) => {\n\t\tif (context && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tconst { result: existingCustomers } =\n\t\t\t\t\tawait options.client.customers.list({ email: user.email });\n\t\t\t\tconst existingCustomer = existingCustomers.items[0];\n\n\t\t\t\tif (existingCustomer) {\n\t\t\t\t\tif (existingCustomer.externalId !== user.id) {\n\t\t\t\t\t\tawait options.client.customers.update({\n\t\t\t\t\t\t\tid: existingCustomer.id,\n\t\t\t\t\t\t\tcustomerUpdate: {\n\t\t\t\t\t\t\t\texternalId: user.id,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: `Polar customer creation failed. Error: ${e.message}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\tmessage: `Polar customer creation failed. Error: ${e}`,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t};\n\nexport const onUserUpdate =\n\t(options: PolarOptions) =>\n\tasync (user: User, context: GenericEndpointContext | null) => {\n\t\tif (context && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tawait options.client.customers.updateExternal({\n\t\t\t\t\texternalId: user.id,\n\t\t\t\t\tcustomerUpdateExternalID: {\n\t\t\t\t\t\temail: user.email,\n\t\t\t\t\t\tname: user.name,\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tcontext.context.logger.error(\n\t\t\t\t\t\t`Polar customer update failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tcontext.context.logger.error(\n\t\t\t\t\t\t`Polar customer update failed. Error: ${e}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n\nexport const onUserDelete =\n\t(options: PolarOptions) =>\n\tasync (user: User, context: GenericEndpointContext | null) => {\n\t\tif (context && options.createCustomerOnSignUp) {\n\t\t\ttry {\n\t\t\t\tif (user.email) {\n\t\t\t\t\tconst { result: existingCustomers } =\n\t\t\t\t\t\tawait options.client.customers.list({ email: user.email });\n\t\t\t\t\tconst existingCustomer = existingCustomers.items[0];\n\t\t\t\t\tif (existingCustomer) {\n\t\t\t\t\t\tawait options.client.customers.delete({\n\t\t\t\t\t\t\tid: existingCustomer.id,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} catch (e: unknown) {\n\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\tcontext?.context.logger.error(\n\t\t\t\t\t\t`Polar customer delete failed. Error: ${e.message}`,\n\t\t\t\t\t);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tcontext?.context.logger.error(\n\t\t\t\t\t`Polar customer delete failed. Error: ${e}`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t};\n","import { PolarEmbedCheckout } from \"@polar-sh/checkout/embed\";\nimport type { PolarEmbedCheckout as PolarEmbedCheckoutType } from \"@polar-sh/checkout/embed\";\nimport type { BetterAuthClientPlugin } from \"better-auth\";\nimport type { BetterFetchOption } from \"better-auth/client\";\nimport type { CheckoutParams, polar } from \"./index\";\n\nexport type { PolarEmbedCheckoutType as PolarEmbedCheckout };\n\nexport const polarClient = () => {\n\treturn {\n\t\tid: \"polar-client\",\n\t\t$InferServerPlugin: {} as ReturnType<typeof polar>,\n\t\tgetActions: ($fetch) => {\n\t\t\treturn {\n\t\t\t\tcheckoutEmbed: async (\n\t\t\t\t\tdata: Omit<CheckoutParams, \"redirect\" | \"embedOrigin\">,\n\t\t\t\t\tfetchOptions?: BetterFetchOption,\n\t\t\t\t): Promise<PolarEmbedCheckoutType> => {\n\t\t\t\t\tconst res = await $fetch(\"/checkout\", {\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t...data,\n\t\t\t\t\t\t\tredirect: false,\n\t\t\t\t\t\t\tembedOrigin: window.location.origin,\n\t\t\t\t\t\t},\n\t\t\t\t\t\t...fetchOptions,\n\t\t\t\t\t});\n\n\t\t\t\t\tif (res.error) {\n\t\t\t\t\t\tthrow new Error(res.error.message);\n\t\t\t\t\t}\n\n\t\t\t\t\tconst checkout = res.data as { url: string };\n\n\t\t\t\t\tconst theme =\n\t\t\t\t\t\t(new URL(checkout.url).searchParams.get(\"theme\") as\n\t\t\t\t\t\t\t| \"light\"\n\t\t\t\t\t\t\t| \"dark\"\n\t\t\t\t\t\t\t| undefined) ?? \"light\";\n\n\t\t\t\t\treturn await PolarEmbedCheckout.create(checkout.url, theme);\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t} satisfies BetterAuthClientPlugin;\n};\n","import type { Polar } from \"@polar-sh/sdk\";\nimport { APIError } from \"better-auth/api\";\nimport { sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\n\nexport interface PortalConfig {\n\treturnUrl?: string;\n}\n\nexport const portal =\n\t({ returnUrl }: PortalConfig = {}) =>\n\t(polar: Polar) => {\n\t\tconst retUrl = returnUrl ? new URL(returnUrl) : undefined;\n\n\t\treturn {\n\t\t\tportal: createAuthEndpoint(\n\t\t\t\t\"/customer/portal\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tif (!ctx.context.session?.user.id) {\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t\t\treturnUrl: retUrl ? decodeURI(retUrl.toString()) : undefined,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\t\turl: customerSession.customerPortalUrl,\n\t\t\t\t\t\t\tredirect: true,\n\t\t\t\t\t\t});\n\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t`Polar customer portal creation failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Customer portal creation failed\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t\tstate: createAuthEndpoint(\n\t\t\t\t\"/customer/state\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst state = await polar.customers.getStateExternal({\n\t\t\t\t\t\t\texternalId: ctx.context.session?.user.id,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\treturn ctx.json(state);\n\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t`Polar subscriptions list failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Subscriptions list failed\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t\tbenefits: createAuthEndpoint(\n\t\t\t\t\"/customer/benefits/list\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\tquery: z\n\t\t\t\t\t\t.object({\n\t\t\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t\t\t})\n\t\t\t\t\t\t.optional(),\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tconst benefits = await polar.customerPortal.benefitGrants.list(\n\t\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\treturn ctx.json(benefits);\n\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t`Polar benefits list failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Benefits list failed\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t\tsubscriptions: createAuthEndpoint(\n\t\t\t\t\"/customer/subscriptions/list\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\tquery: z\n\t\t\t\t\t\t.object({\n\t\t\t\t\t\t\treferenceId: z.string().optional(),\n\t\t\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t\t\t\tactive: z.coerce.boolean().optional(),\n\t\t\t\t\t\t})\n\t\t\t\t\t\t.optional(),\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tif (ctx.query?.referenceId) {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst subscriptions = await polar.subscriptions.list({\n\t\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t\t\tactive: ctx.query?.active,\n\t\t\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\t\t\treferenceId: ctx.query?.referenceId,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\treturn ctx.json(subscriptions);\n\t\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\t\tconsole.log(e);\n\t\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t\t`Polar subscriptions list with referenceId failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\t\tmessage: \"Subscriptions list with referenceId failed\",\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tconst subscriptions = await polar.customerPortal.subscriptions.list(\n\t\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t\t\tactive: ctx.query?.active,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\treturn ctx.json(subscriptions);\n\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t`Polar subscriptions list failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Polar subscriptions list failed\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t\torders: createAuthEndpoint(\n\t\t\t\t\"/customer/orders/list\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\tquery: z\n\t\t\t\t\t\t.object({\n\t\t\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t\t\t\tproductBillingType: z.enum([\"recurring\", \"one_time\"]).optional(),\n\t\t\t\t\t\t})\n\t\t\t\t\t\t.optional(),\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\t\texternalCustomerId: ctx.context.session?.user.id,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tconst orders = await polar.customerPortal.orders.list(\n\t\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t\t\tproductBillingType: ctx.query?.productBillingType,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\treturn ctx.json(orders);\n\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t`Polar orders list failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Orders list failed\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t};\n\t};\n","import type { Polar } from \"@polar-sh/sdk\";\nimport { APIError, getSessionFromCtx } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\nimport type { Product } from \"../types\";\n\nexport interface CheckoutOptions {\n\t/**\n\t * Optional list of slug -> productId mappings for easy slug checkouts\n\t */\n\tproducts?: Product[] | (() => Promise<Product[]>);\n\t/**\n\t * Checkout Success URL\n\t */\n\tsuccessUrl?: string;\n\t/**\n\t * Checkout Return URL\n\t */\n\treturnUrl?: string;\n\t/**\n\t * Only allow authenticated customers to checkout\n\t */\n\tauthenticatedUsersOnly?: boolean;\n\t/**\n\t * Checkout theme\n\t */\n\ttheme?: \"light\" | \"dark\";\n\t/**\n\t * Redirect to checkout page\n\t */\n\tredirect?: boolean;\n}\n\nexport const CheckoutParams = z.object({\n\tproducts: z.union([z.array(z.string()), z.string()]).optional(),\n\tslug: z.string().optional(),\n\treferenceId: z.string().optional(),\n\tcustomFieldData: z\n\t\t.record(z.string(), z.union([z.string(), z.number(), z.boolean()]))\n\t\t.optional(),\n\tmetadata: z\n\t\t.record(z.string(), z.union([z.string(), z.number(), z.boolean()]))\n\t\t.optional(),\n\tallowDiscountCodes: z.coerce.boolean().optional(),\n\tdiscountId: z.string().optional(),\n\tredirect: z.coerce.boolean().optional(),\n\tembedOrigin: z.string().url().optional(),\n});\n\nexport type CheckoutParams = z.infer<typeof CheckoutParams>;\n\nexport const checkout =\n\t(checkoutOptions: CheckoutOptions = {}) =>\n\t(polar: Polar) => {\n\t\treturn {\n\t\t\tcheckout: createAuthEndpoint(\n\t\t\t\t\"/checkout\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: CheckoutParams,\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst session = await getSessionFromCtx(ctx);\n\n\t\t\t\t\tlet productIds: string[] = [];\n\n\t\t\t\t\tif (ctx.body.slug) {\n\t\t\t\t\t\tconst resolvedProducts = await (typeof checkoutOptions.products ===\n\t\t\t\t\t\t\"function\"\n\t\t\t\t\t\t\t? checkoutOptions.products()\n\t\t\t\t\t\t\t: checkoutOptions.products);\n\n\t\t\t\t\t\tconst productId = resolvedProducts?.find(\n\t\t\t\t\t\t\t(product) => product.slug === ctx.body.slug,\n\t\t\t\t\t\t)?.productId;\n\n\t\t\t\t\t\tif (!productId) {\n\t\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\t\tmessage: \"Product not found\",\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tproductIds = [productId];\n\t\t\t\t\t} else {\n\t\t\t\t\t\tproductIds = Array.isArray(ctx.body.products)\n\t\t\t\t\t\t\t? ctx.body.products.filter((id) => id !== undefined)\n\t\t\t\t\t\t\t: [ctx.body.products].filter((id) => id !== undefined);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (checkoutOptions.authenticatedUsersOnly && !session?.user.id) {\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"You must be logged in to checkout\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst checkout = await polar.checkouts.create({\n\t\t\t\t\t\t\texternalCustomerId: session?.user.id,\n\t\t\t\t\t\t\tproducts: productIds,\n\t\t\t\t\t\t\tsuccessUrl: checkoutOptions.successUrl\n\t\t\t\t\t\t\t\t? new URL(\n\t\t\t\t\t\t\t\t\t\tcheckoutOptions.successUrl,\n\t\t\t\t\t\t\t\t\t\tctx.request?.url ?? ctx.context.baseURL,\n\t\t\t\t\t\t\t\t\t).toString()\n\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t\t\tmetadata: ctx.body.referenceId\n\t\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\t\treferenceId: ctx.body.referenceId,\n\t\t\t\t\t\t\t\t\t\t...ctx.body.metadata,\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t: ctx.body.metadata,\n\t\t\t\t\t\t\tcustomFieldData: ctx.body.customFieldData,\n\t\t\t\t\t\t\tallowDiscountCodes: ctx.body.allowDiscountCodes ?? true,\n\t\t\t\t\t\t\tdiscountId: ctx.body.discountId,\n\t\t\t\t\t\t\tembedOrigin: ctx.body.embedOrigin,\n\t\t\t\t\t\t\treturnUrl: checkoutOptions.returnUrl\n\t\t\t\t\t\t\t\t? new URL(\n\t\t\t\t\t\t\t\t\t\tcheckoutOptions.returnUrl,\n\t\t\t\t\t\t\t\t\t\tctx.request?.url ?? ctx.context.baseURL,\n\t\t\t\t\t\t\t\t\t).toString()\n\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tconst redirectUrl = new URL(checkout.url);\n\n\t\t\t\t\t\tif (checkoutOptions.theme) {\n\t\t\t\t\t\t\tredirectUrl.searchParams.set(\"theme\", checkoutOptions.theme);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\t\turl: redirectUrl.toString(),\n\t\t\t\t\t\t\tredirect: ctx.body.redirect ?? true,\n\t\t\t\t\t\t});\n\t\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t\t`Polar checkout creation failed. Error: ${e.message}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Checkout creation failed\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t};\n\t};\n","import type { Polar } from \"@polar-sh/sdk\";\nimport {\n\tAPIError,\n\tcreateAuthEndpoint,\n\tsessionMiddleware,\n} from \"better-auth/api\";\nimport { z } from \"zod\";\nimport type { Product } from \"../types\";\n\nexport interface UsageOptions {\n\t/**\n\t * Products to use for topping up credits\n\t */\n\tcreditProducts?: Product[] | (() => Promise<Product[]>);\n}\n\nexport const usage = (_usageOptions?: UsageOptions) => (polar: Polar) => {\n\treturn {\n\t\tmeters: createAuthEndpoint(\n\t\t\t\"/usage/meters/list\",\n\t\t\t{\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\tquery: z.object({\n\t\t\t\t\tpage: z.coerce.number().optional(),\n\t\t\t\t\tlimit: z.coerce.number().optional(),\n\t\t\t\t}),\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst customerSession = await polar.customerSessions.create({\n\t\t\t\t\t\texternalCustomerId: ctx.context.session.user.id,\n\t\t\t\t\t});\n\n\t\t\t\t\tconst customerMeters = await polar.customerPortal.customerMeters.list(\n\t\t\t\t\t\t{ customerSession: customerSession.token },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tpage: ctx.query?.page,\n\t\t\t\t\t\t\tlimit: ctx.query?.limit,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\n\t\t\t\t\treturn ctx.json(customerMeters);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar meters list failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Meters list failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t\tingestion: createAuthEndpoint(\n\t\t\t\"/usage/ingest\",\n\t\t\t{\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tbody: z.object({\n\t\t\t\t\tevent: z.string(),\n\t\t\t\t\tmetadata: z.record(\n\t\t\t\t\t\tz.string(),\n\t\t\t\t\t\tz.union([z.string(), z.number(), z.boolean()]),\n\t\t\t\t\t),\n\t\t\t\t}),\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tif (!ctx.context.session.user.id) {\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"User not found\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tconst ingestion = await polar.events.ingest({\n\t\t\t\t\t\tevents: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tname: ctx.body.event,\n\t\t\t\t\t\t\t\tmetadata: ctx.body.metadata,\n\t\t\t\t\t\t\t\texternalCustomerId: ctx.context.session.user.id,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json(ingestion);\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar ingestion failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\tmessage: \"Ingestion failed\",\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t},\n\t\t),\n\t};\n};\n","// src/webhooks/webhooks.ts\nvar handleWebhookPayload = async (payload, { webhookSecret, entitlements, onPayload, ...eventHandlers }) => {\n const promises = [];\n if (onPayload) {\n promises.push(onPayload(payload));\n }\n switch (payload.type) {\n case \"checkout.created\":\n if (eventHandlers.onCheckoutCreated) {\n promises.push(eventHandlers.onCheckoutCreated(payload));\n }\n break;\n case \"checkout.updated\":\n if (eventHandlers.onCheckoutUpdated) {\n promises.push(eventHandlers.onCheckoutUpdated(payload));\n }\n break;\n case \"order.created\":\n if (eventHandlers.onOrderCreated) {\n promises.push(eventHandlers.onOrderCreated(payload));\n }\n break;\n case \"order.updated\":\n if (eventHandlers.onOrderUpdated) {\n promises.push(eventHandlers.onOrderUpdated(payload));\n }\n break;\n case \"order.paid\":\n if (eventHandlers.onOrderPaid) {\n promises.push(eventHandlers.onOrderPaid(payload));\n }\n break;\n case \"subscription.created\":\n if (eventHandlers.onSubscriptionCreated) {\n promises.push(eventHandlers.onSubscriptionCreated(payload));\n }\n break;\n case \"subscription.updated\":\n if (eventHandlers.onSubscriptionUpdated) {\n promises.push(eventHandlers.onSubscriptionUpdated(payload));\n }\n break;\n case \"subscription.active\":\n if (eventHandlers.onSubscriptionActive) {\n promises.push(eventHandlers.onSubscriptionActive(payload));\n }\n break;\n case \"subscription.canceled\":\n if (eventHandlers.onSubscriptionCanceled) {\n promises.push(eventHandlers.onSubscriptionCanceled(payload));\n }\n break;\n case \"subscription.uncanceled\":\n if (eventHandlers.onSubscriptionUncanceled) {\n promises.push(eventHandlers.onSubscriptionUncanceled(payload));\n }\n break;\n case \"subscription.revoked\":\n if (eventHandlers.onSubscriptionRevoked) {\n promises.push(eventHandlers.onSubscriptionRevoked(payload));\n }\n break;\n case \"product.created\":\n if (eventHandlers.onProductCreated) {\n promises.push(eventHandlers.onProductCreated(payload));\n }\n break;\n case \"product.updated\":\n if (eventHandlers.onProductUpdated) {\n promises.push(eventHandlers.onProductUpdated(payload));\n }\n break;\n case \"organization.updated\":\n if (eventHandlers.onOrganizationUpdated) {\n promises.push(eventHandlers.onOrganizationUpdated(payload));\n }\n break;\n case \"benefit.created\":\n if (eventHandlers.onBenefitCreated) {\n promises.push(eventHandlers.onBenefitCreated(payload));\n }\n break;\n case \"benefit.updated\":\n if (eventHandlers.onBenefitUpdated) {\n promises.push(eventHandlers.onBenefitUpdated(payload));\n }\n break;\n case \"benefit_grant.created\":\n if (eventHandlers.onBenefitGrantCreated) {\n promises.push(eventHandlers.onBenefitGrantCreated(payload));\n }\n break;\n case \"benefit_grant.updated\":\n if (eventHandlers.onBenefitGrantUpdated) {\n promises.push(eventHandlers.onBenefitGrantUpdated(payload));\n }\n break;\n case \"benefit_grant.revoked\":\n if (eventHandlers.onBenefitGrantRevoked) {\n promises.push(eventHandlers.onBenefitGrantRevoked(payload));\n }\n break;\n case \"customer.created\":\n if (eventHandlers.onCustomerCreated) {\n promises.push(eventHandlers.onCustomerCreated(payload));\n }\n break;\n case \"customer.updated\":\n if (eventHandlers.onCustomerUpdated) {\n promises.push(eventHandlers.onCustomerUpdated(payload));\n }\n break;\n case \"customer.deleted\":\n if (eventHandlers.onCustomerDeleted) {\n promises.push(eventHandlers.onCustomerDeleted(payload));\n }\n break;\n case \"customer.state_changed\":\n if (eventHandlers.onCustomerStateChanged) {\n promises.push(eventHandlers.onCustomerStateChanged(payload));\n }\n break;\n case \"order.refunded\":\n if (eventHandlers.onOrderRefunded) {\n promises.push(eventHandlers.onOrderRefunded(payload));\n }\n break;\n case \"refund.created\":\n if (eventHandlers.onRefundCreated) {\n promises.push(eventHandlers.onRefundCreated(payload));\n }\n break;\n case \"refund.updated\":\n if (eventHandlers.onRefundUpdated) {\n promises.push(eventHandlers.onRefundUpdated(payload));\n }\n break;\n }\n switch (payload.type) {\n case \"benefit_grant.created\":\n case \"benefit_grant.revoked\":\n if (entitlements) {\n for (const handler of entitlements.handlers) {\n promises.push(handler(payload));\n }\n }\n }\n return Promise.all(promises);\n};\n\n// src/entitlement/entitlement.ts\nvar EntitlementStrategy = class {\n grantCallbacks = [];\n revokeCallbacks = [];\n grant(callback) {\n this.grantCallbacks.push(callback);\n return this;\n }\n revoke(callback) {\n this.revokeCallbacks.push(callback);\n return this;\n }\n handler(slug) {\n return async (payload) => {\n if (payload.data.benefit.description === slug) {\n switch (payload.type) {\n case \"benefit_grant.created\":\n await Promise.all(\n this.grantCallbacks.map(\n (callback) => callback({\n customer: payload.data.customer,\n properties: payload.data.properties,\n payload\n })\n )\n );\n break;\n case \"benefit_grant.revoked\":\n await Promise.all(\n this.revokeCallbacks.map(\n (callback) => callback({\n customer: payload.data.customer,\n properties: payload.data.properties,\n payload\n })\n )\n );\n break;\n }\n }\n };\n }\n};\nvar Entitlements = class {\n static handlers = [];\n static use(slug, strategy) {\n this.handlers.push(strategy.handler(slug));\n return this;\n }\n};\nexport {\n EntitlementStrategy,\n Entitlements,\n handleWebhookPayload\n};\n","import { handleWebhookPayload } from \"@polar-sh/adapter-utils\";\nimport type { Polar } from \"@polar-sh/sdk\";\nimport type { WebhookBenefitCreatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitcreatedpayload\";\nimport type { WebhookBenefitGrantCreatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantcreatedpayload\";\nimport type { WebhookBenefitGrantRevokedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantrevokedpayload\";\nimport type { WebhookBenefitGrantUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitgrantupdatedpayload\";\nimport type { WebhookBenefitUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookbenefitupdatedpayload\";\nimport type { WebhookCheckoutCreatedPayload } from \"@polar-sh/sdk/models/components/webhookcheckoutcreatedpayload\";\nimport type { WebhookCheckoutUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookcheckoutupdatedpayload\";\nimport type { WebhookCustomerCreatedPayload } from \"@polar-sh/sdk/models/components/webhookcustomercreatedpayload\";\nimport type { WebhookCustomerDeletedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerdeletedpayload\";\nimport type { WebhookCustomerStateChangedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerstatechangedpayload\";\nimport type { WebhookCustomerUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookcustomerupdatedpayload\";\nimport type { WebhookOrderCreatedPayload } from \"@polar-sh/sdk/models/components/webhookordercreatedpayload\";\nimport type { WebhookOrderPaidPayload } from \"@polar-sh/sdk/models/components/webhookorderpaidpayload\";\nimport type { WebhookOrderRefundedPayload } from \"@polar-sh/sdk/models/components/webhookorderrefundedpayload\";\nimport type { WebhookOrderUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookorderupdatedpayload\";\nimport type { WebhookOrganizationUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookorganizationupdatedpayload\";\nimport type { WebhookProductCreatedPayload } from \"@polar-sh/sdk/models/components/webhookproductcreatedpayload\";\nimport type { WebhookProductUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookproductupdatedpayload\";\nimport type { WebhookRefundCreatedPayload } from \"@polar-sh/sdk/models/components/webhookrefundcreatedpayload\";\nimport type { WebhookRefundUpdatedPayload } from \"@polar-sh/sdk/models/components/webhookrefundupdatedpayload\";\nimport type { WebhookSubscriptionActivePayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionactivepayload\";\nimport type { WebhookSubscriptionCanceledPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptioncanceledpayload\";\nimport type { WebhookSubscriptionCreatedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptioncreatedpayload\";\nimport type { WebhookSubscriptionRevokedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionrevokedpayload\";\nimport type { WebhookSubscriptionUncanceledPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionuncanceledpayload\";\nimport type { WebhookSubscriptionUpdatedPayload } from \"@polar-sh/sdk/models/components/webhooksubscriptionupdatedpayload\";\nimport { validateEvent } from \"@polar-sh/sdk/webhooks\";\nimport { APIError, createAuthEndpoint } from \"better-auth/api\";\n\nexport interface WebhooksOptions {\n\t/**\n\t * Webhook Secret\n\t */\n\tsecret: string;\n\t/**\n\t * Generic handler for all webhooks\n\t */\n\tonPayload?: (payload: ReturnType<typeof validateEvent>) => Promise<void>;\n\t/**\n\t * Webhook for checkout created\n\t */\n\tonCheckoutCreated?: (payload: WebhookCheckoutCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for checkout updated\n\t */\n\tonCheckoutUpdated?: (payload: WebhookCheckoutUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order created\n\t */\n\tonOrderCreated?: (payload: WebhookOrderCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order refunded\n\t */\n\tonOrderRefunded?: (payload: WebhookOrderRefundedPayload) => Promise<void>;\n\t/**\n\t * Webhook for order paid\n\t */\n\tonOrderPaid?: (payload: WebhookOrderPaidPayload) => Promise<void>;\n\t/**\n\t * Webhook for order updated\n\t */\n\tonOrderUpdated?: (payload: WebhookOrderUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for refund created\n\t */\n\tonRefundCreated?: (payload: WebhookRefundCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for refund updated\n\t */\n\tonRefundUpdated?: (payload: WebhookRefundUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for subscription created\n\t */\n\tonSubscriptionCreated?: (\n\t\tpayload: WebhookSubscriptionCreatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription updated\n\t */\n\tonSubscriptionUpdated?: (\n\t\tpayload: WebhookSubscriptionUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription active\n\t */\n\tonSubscriptionActive?: (\n\t\tpayload: WebhookSubscriptionActivePayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription canceled\n\t */\n\tonSubscriptionCanceled?: (\n\t\tpayload: WebhookSubscriptionCanceledPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription revoked\n\t */\n\tonSubscriptionRevoked?: (\n\t\tpayload: WebhookSubscriptionRevokedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for subscription uncanceled\n\t */\n\tonSubscriptionUncanceled?: (\n\t\tpayload: WebhookSubscriptionUncanceledPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for product created\n\t */\n\tonProductCreated?: (payload: WebhookProductCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for product updated\n\t */\n\tonProductUpdated?: (payload: WebhookProductUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for organization updated\n\t */\n\tonOrganizationUpdated?: (\n\t\tpayload: WebhookOrganizationUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit created\n\t */\n\tonBenefitCreated?: (payload: WebhookBenefitCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for benefit updated\n\t */\n\tonBenefitUpdated?: (payload: WebhookBenefitUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant created\n\t */\n\tonBenefitGrantCreated?: (\n\t\tpayload: WebhookBenefitGrantCreatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant updated\n\t */\n\tonBenefitGrantUpdated?: (\n\t\tpayload: WebhookBenefitGrantUpdatedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for benefit grant revoked\n\t */\n\tonBenefitGrantRevoked?: (\n\t\tpayload: WebhookBenefitGrantRevokedPayload,\n\t) => Promise<void>;\n\t/**\n\t * Webhook for customer created\n\t */\n\tonCustomerCreated?: (payload: WebhookCustomerCreatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer updated\n\t */\n\tonCustomerUpdated?: (payload: WebhookCustomerUpdatedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer deleted\n\t */\n\tonCustomerDeleted?: (payload: WebhookCustomerDeletedPayload) => Promise<void>;\n\t/**\n\t * Webhook for customer state changed\n\t */\n\tonCustomerStateChanged?: (\n\t\tpayload: WebhookCustomerStateChangedPayload,\n\t) => Promise<void>;\n}\n\nexport const webhooks = (options: WebhooksOptions) => (_polar: Polar) => {\n\treturn {\n\t\tpolarWebhooks: createAuthEndpoint(\n\t\t\t\"/polar/webhooks\",\n\t\t\t{\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tmetadata: {\n\t\t\t\t\tisAction: false,\n\t\t\t\t},\n\t\t\t\tcloneRequest: true,\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tconst { secret, ...eventHandlers } = options;\n\n\t\t\t\tif (!ctx.request?.body) {\n\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\");\n\t\t\t\t}\n\t\t\t\tconst buf = await ctx.request.text();\n\t\t\t\tlet event: ReturnType<typeof validateEvent>;\n\t\t\t\ttry {\n\t\t\t\t\tif (!secret) {\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: \"Polar webhook secret not found\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst headers = {\n\t\t\t\t\t\t\"webhook-id\": ctx.request.headers.get(\"webhook-id\") as string,\n\t\t\t\t\t\t\"webhook-timestamp\": ctx.request.headers.get(\n\t\t\t\t\t\t\t\"webhook-timestamp\",\n\t\t\t\t\t\t) as string,\n\t\t\t\t\t\t\"webhook-signature\": ctx.request.headers.get(\n\t\t\t\t\t\t\t\"webhook-signature\",\n\t\t\t\t\t\t) as string,\n\t\t\t\t\t};\n\n\t\t\t\t\tevent = validateEvent(buf, headers, secret);\n\t\t\t\t} catch (err: unknown) {\n\t\t\t\t\tif (err instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(`${err.message}`);\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: `Webhook Error: ${err.message}`,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: `Webhook Error: ${err}`,\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\ttry {\n\t\t\t\t\tawait handleWebhookPayload(event, {\n\t\t\t\t\t\twebhookSecret: secret,\n\t\t\t\t\t\t...eventHandlers,\n\t\t\t\t\t});\n\t\t\t\t} catch (e: unknown) {\n\t\t\t\t\tif (e instanceof Error) {\n\t\t\t\t\t\tctx.context.logger.error(\n\t\t\t\t\t\t\t`Polar webhook failed. Error: ${e.message}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tctx.context.logger.error(`Polar webhook failed. Error: ${e}`);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\tmessage: \"Webhook error: See server logs for more information.\",\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\treturn ctx.json({ received: true });\n\t\t\t},\n\t\t),\n\t};\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,iBAAyB;AAGlB,IAAM,qBACZ,CAAC,YACD,OAAO,MAAqB,YAA2C;AACtE,MAAI,WAAW,QAAQ,wBAAwB;AAC9C,QAAI;AACH,YAAM,SAAS,QAAQ,0BACpB,MAAM,QAAQ,wBAAwB;AAAA,QACtC;AAAA,MACD,CAAC,IACA,CAAC;AAEJ,UAAI,CAAC,KAAK,OAAO;AAChB,cAAM,IAAI,oBAAS,eAAe;AAAA,UACjC,SAAS;AAAA,QACV,CAAC;AAAA,MACF;AAGA,YAAM,EAAE,QAAQ,kBAAkB,IACjC,MAAM,QAAQ,OAAO,UAAU,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QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;","names":["checkout","import_api","polar","import_api","import_plugins","import_zod","polar","checkout","import_api","import_zod","polar","import_api"]}
|