@paykit-sdk/polar 1.1.6 → 1.1.8

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/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { PaykitProviderOptions, PayKitProvider, CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, $ExtWebhookHandlerConfig, WebhookEventPayload } from '@paykit-sdk/core';
1
+ import { PaykitProviderOptions, PayKitProvider, CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, HandleWebhookParams, WebhookEventPayload } from '@paykit-sdk/core';
2
2
  import { SDKOptions } from '@polar-sh/sdk';
3
3
 
4
4
  interface PolarConfig extends PaykitProviderOptions<SDKOptions> {
@@ -29,7 +29,7 @@ declare class PolarProvider implements PayKitProvider {
29
29
  /**
30
30
  * Webhook management
31
31
  */
32
- handleWebhook: (params: $ExtWebhookHandlerConfig) => Promise<WebhookEventPayload>;
32
+ handleWebhook: (params: HandleWebhookParams) => Promise<WebhookEventPayload>;
33
33
  }
34
34
 
35
35
  declare const createPolar: (config: PolarConfig) => PolarProvider;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { PaykitProviderOptions, PayKitProvider, CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, $ExtWebhookHandlerConfig, WebhookEventPayload } from '@paykit-sdk/core';
1
+ import { PaykitProviderOptions, PayKitProvider, CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, HandleWebhookParams, WebhookEventPayload } from '@paykit-sdk/core';
2
2
  import { SDKOptions } from '@polar-sh/sdk';
3
3
 
4
4
  interface PolarConfig extends PaykitProviderOptions<SDKOptions> {
@@ -29,7 +29,7 @@ declare class PolarProvider implements PayKitProvider {
29
29
  /**
30
30
  * Webhook management
31
31
  */
32
- handleWebhook: (params: $ExtWebhookHandlerConfig) => Promise<WebhookEventPayload>;
32
+ handleWebhook: (params: HandleWebhookParams) => Promise<WebhookEventPayload>;
33
33
  }
34
34
 
35
35
  declare const createPolar: (config: PolarConfig) => PolarProvider;
package/dist/index.js CHANGED
@@ -24,6 +24,7 @@ __export(index_exports, {
24
24
  polar: () => polar
25
25
  });
26
26
  module.exports = __toCommonJS(index_exports);
27
+ var import_core3 = require("@paykit-sdk/core");
27
28
 
28
29
  // src/polar-provider.ts
29
30
  var import_core2 = require("@paykit-sdk/core");
@@ -142,24 +143,70 @@ var PolarProvider = class {
142
143
  const { data, type } = (0, import_webhooks.validateEvent)(body, webhookHeaders, webhookSecret);
143
144
  const id = webhookHeaders["webhook-id"];
144
145
  const timestamp = webhookHeaders["webhook-timestamp"];
145
- if (type === "subscription.updated") {
146
- return (0, import_core2.toPaykitEvent)({ type: "$subscriptionUpdated", created: parseInt(timestamp), id, data: toPaykitSubscription(data) });
147
- } else if (type === "subscription.created") {
148
- return (0, import_core2.toPaykitEvent)({ type: "$subscriptionCreated", created: parseInt(timestamp), id, data: toPaykitSubscription(data) });
149
- } else if (type === "subscription.revoked") {
150
- return (0, import_core2.toPaykitEvent)({ type: "$subscriptionCancelled", created: parseInt(timestamp), id, data: toPaykitSubscription(data) });
151
- } else if (type === "customer.created") {
152
- return (0, import_core2.toPaykitEvent)({ type: "$customerCreated", created: parseInt(timestamp), id, data: toPaykitCustomer(data) });
153
- } else if (type === "customer.updated") {
154
- return (0, import_core2.toPaykitEvent)({ type: "$customerUpdated", created: parseInt(timestamp), id, data: toPaykitCustomer(data) });
155
- } else if (type === "customer.deleted") {
156
- return (0, import_core2.toPaykitEvent)({ type: "$customerDeleted", created: parseInt(timestamp), id, data: null });
157
- } else if (type === "checkout.created") {
158
- return (0, import_core2.toPaykitEvent)({ type: "$checkoutCreated", created: parseInt(timestamp), id, data: toPaykitCheckout(data) });
159
- } else if (type === "order.paid" && data.status == "paid") {
160
- return (0, import_core2.toPaykitEvent)({ type: "$invoicePaid", created: parseInt(timestamp), id, data: toPaykitInvoice(data) });
161
- }
162
- throw new Error(`Unhandled event type: ${type}`);
146
+ const webhookHandlers = {
147
+ /**
148
+ * Invoice
149
+ */
150
+ "order.paid": (data2) => {
151
+ const { status, metadata } = data2;
152
+ if (status !== "paid") return null;
153
+ return (0, import_core2.toPaykitEvent)({
154
+ type: "$invoicePaid",
155
+ created: parseInt(timestamp),
156
+ id,
157
+ data: toPaykitInvoice({ ...data2, metadata: { ...metadata ?? {}, $mode: "payment" } })
158
+ });
159
+ },
160
+ "order.created": (data2) => {
161
+ const { billingReason, metadata, status } = data2;
162
+ if (billingReason == "subscription_create") {
163
+ return (0, import_core2.toPaykitEvent)({
164
+ type: "$invoicePaid",
165
+ created: parseInt(timestamp),
166
+ id,
167
+ data: toPaykitInvoice({ ...data2, metadata: { ...metadata ?? {}, $mode: "subscription" } })
168
+ });
169
+ }
170
+ if (billingReason == "subscription_cycle") {
171
+ return (0, import_core2.toPaykitEvent)({
172
+ type: "$invoicePaid",
173
+ created: parseInt(timestamp),
174
+ id,
175
+ data: toPaykitInvoice({ ...data2, metadata: { ...metadata ?? {}, $mode: "subscription" } })
176
+ });
177
+ }
178
+ return null;
179
+ },
180
+ /**
181
+ * Customer
182
+ */
183
+ "customer.created": (data2) => {
184
+ return (0, import_core2.toPaykitEvent)({ type: "$customerCreated", created: parseInt(timestamp), id, data: toPaykitCustomer(data2) });
185
+ },
186
+ "customer.updated": (data2) => {
187
+ return (0, import_core2.toPaykitEvent)({ type: "$customerUpdated", created: parseInt(timestamp), id, data: toPaykitCustomer(data2) });
188
+ },
189
+ "customer.deleted": () => {
190
+ return (0, import_core2.toPaykitEvent)({ type: "$customerDeleted", created: parseInt(timestamp), id, data: null });
191
+ },
192
+ /**
193
+ * Subscription
194
+ */
195
+ "subscription.updated": (data2) => {
196
+ return (0, import_core2.toPaykitEvent)({ type: "$subscriptionUpdated", created: parseInt(timestamp), id, data: toPaykitSubscription(data2) });
197
+ },
198
+ "subscription.created": (data2) => {
199
+ return (0, import_core2.toPaykitEvent)({ type: "$subscriptionCreated", created: parseInt(timestamp), id, data: toPaykitSubscription(data2) });
200
+ },
201
+ "subscription.revoked": (data2) => {
202
+ return (0, import_core2.toPaykitEvent)({ type: "$subscriptionCancelled", created: parseInt(timestamp), id, data: toPaykitSubscription(data2) });
203
+ }
204
+ };
205
+ const handler = webhookHandlers[type];
206
+ if (!handler) throw new Error(`Unhandled event type: ${type}`);
207
+ const result = handler(data);
208
+ if (!result) throw new Error(`Unhandled event type: ${type}`);
209
+ return result;
163
210
  };
164
211
  const { accessToken, server, ...rest } = config;
165
212
  this.polar = new import_sdk.Polar({ accessToken, serverURL: server === "sandbox" ? this.sandboxURL : this.productionURL, ...rest });
@@ -171,10 +218,9 @@ var createPolar = (config) => {
171
218
  return new PolarProvider(config);
172
219
  };
173
220
  var polar = () => {
174
- const accessToken = process.env.POLAR_ACCESS_TOKEN;
221
+ const envVars = (0, import_core3.validateEnvVars)(["POLAR_ACCESS_TOKEN"], process.env);
175
222
  const isDev = process.env.NODE_ENV === "development";
176
- if (!accessToken) throw new Error("POLAR_ACCESS_TOKEN is not set");
177
- return createPolar({ debug: true, accessToken, server: isDev ? "sandbox" : "production" });
223
+ return createPolar({ debug: true, accessToken: envVars.POLAR_ACCESS_TOKEN, server: isDev ? "sandbox" : "production" });
178
224
  };
179
225
  // Annotate the CommonJS export names for ESM import in node:
180
226
  0 && (module.exports = {
package/dist/index.mjs CHANGED
@@ -1,3 +1,6 @@
1
+ // src/index.ts
2
+ import { validateEnvVars } from "@paykit-sdk/core";
3
+
1
4
  // src/polar-provider.ts
2
5
  import {
3
6
  toPaykitEvent,
@@ -120,24 +123,70 @@ var PolarProvider = class {
120
123
  const { data, type } = validateEvent(body, webhookHeaders, webhookSecret);
121
124
  const id = webhookHeaders["webhook-id"];
122
125
  const timestamp = webhookHeaders["webhook-timestamp"];
123
- if (type === "subscription.updated") {
124
- return toPaykitEvent({ type: "$subscriptionUpdated", created: parseInt(timestamp), id, data: toPaykitSubscription(data) });
125
- } else if (type === "subscription.created") {
126
- return toPaykitEvent({ type: "$subscriptionCreated", created: parseInt(timestamp), id, data: toPaykitSubscription(data) });
127
- } else if (type === "subscription.revoked") {
128
- return toPaykitEvent({ type: "$subscriptionCancelled", created: parseInt(timestamp), id, data: toPaykitSubscription(data) });
129
- } else if (type === "customer.created") {
130
- return toPaykitEvent({ type: "$customerCreated", created: parseInt(timestamp), id, data: toPaykitCustomer(data) });
131
- } else if (type === "customer.updated") {
132
- return toPaykitEvent({ type: "$customerUpdated", created: parseInt(timestamp), id, data: toPaykitCustomer(data) });
133
- } else if (type === "customer.deleted") {
134
- return toPaykitEvent({ type: "$customerDeleted", created: parseInt(timestamp), id, data: null });
135
- } else if (type === "checkout.created") {
136
- return toPaykitEvent({ type: "$checkoutCreated", created: parseInt(timestamp), id, data: toPaykitCheckout(data) });
137
- } else if (type === "order.paid" && data.status == "paid") {
138
- return toPaykitEvent({ type: "$invoicePaid", created: parseInt(timestamp), id, data: toPaykitInvoice(data) });
139
- }
140
- throw new Error(`Unhandled event type: ${type}`);
126
+ const webhookHandlers = {
127
+ /**
128
+ * Invoice
129
+ */
130
+ "order.paid": (data2) => {
131
+ const { status, metadata } = data2;
132
+ if (status !== "paid") return null;
133
+ return toPaykitEvent({
134
+ type: "$invoicePaid",
135
+ created: parseInt(timestamp),
136
+ id,
137
+ data: toPaykitInvoice({ ...data2, metadata: { ...metadata ?? {}, $mode: "payment" } })
138
+ });
139
+ },
140
+ "order.created": (data2) => {
141
+ const { billingReason, metadata, status } = data2;
142
+ if (billingReason == "subscription_create") {
143
+ return toPaykitEvent({
144
+ type: "$invoicePaid",
145
+ created: parseInt(timestamp),
146
+ id,
147
+ data: toPaykitInvoice({ ...data2, metadata: { ...metadata ?? {}, $mode: "subscription" } })
148
+ });
149
+ }
150
+ if (billingReason == "subscription_cycle") {
151
+ return toPaykitEvent({
152
+ type: "$invoicePaid",
153
+ created: parseInt(timestamp),
154
+ id,
155
+ data: toPaykitInvoice({ ...data2, metadata: { ...metadata ?? {}, $mode: "subscription" } })
156
+ });
157
+ }
158
+ return null;
159
+ },
160
+ /**
161
+ * Customer
162
+ */
163
+ "customer.created": (data2) => {
164
+ return toPaykitEvent({ type: "$customerCreated", created: parseInt(timestamp), id, data: toPaykitCustomer(data2) });
165
+ },
166
+ "customer.updated": (data2) => {
167
+ return toPaykitEvent({ type: "$customerUpdated", created: parseInt(timestamp), id, data: toPaykitCustomer(data2) });
168
+ },
169
+ "customer.deleted": () => {
170
+ return toPaykitEvent({ type: "$customerDeleted", created: parseInt(timestamp), id, data: null });
171
+ },
172
+ /**
173
+ * Subscription
174
+ */
175
+ "subscription.updated": (data2) => {
176
+ return toPaykitEvent({ type: "$subscriptionUpdated", created: parseInt(timestamp), id, data: toPaykitSubscription(data2) });
177
+ },
178
+ "subscription.created": (data2) => {
179
+ return toPaykitEvent({ type: "$subscriptionCreated", created: parseInt(timestamp), id, data: toPaykitSubscription(data2) });
180
+ },
181
+ "subscription.revoked": (data2) => {
182
+ return toPaykitEvent({ type: "$subscriptionCancelled", created: parseInt(timestamp), id, data: toPaykitSubscription(data2) });
183
+ }
184
+ };
185
+ const handler = webhookHandlers[type];
186
+ if (!handler) throw new Error(`Unhandled event type: ${type}`);
187
+ const result = handler(data);
188
+ if (!result) throw new Error(`Unhandled event type: ${type}`);
189
+ return result;
141
190
  };
142
191
  const { accessToken, server, ...rest } = config;
143
192
  this.polar = new Polar({ accessToken, serverURL: server === "sandbox" ? this.sandboxURL : this.productionURL, ...rest });
@@ -149,10 +198,9 @@ var createPolar = (config) => {
149
198
  return new PolarProvider(config);
150
199
  };
151
200
  var polar = () => {
152
- const accessToken = process.env.POLAR_ACCESS_TOKEN;
201
+ const envVars = validateEnvVars(["POLAR_ACCESS_TOKEN"], process.env);
153
202
  const isDev = process.env.NODE_ENV === "development";
154
- if (!accessToken) throw new Error("POLAR_ACCESS_TOKEN is not set");
155
- return createPolar({ debug: true, accessToken, server: isDev ? "sandbox" : "production" });
203
+ return createPolar({ debug: true, accessToken: envVars.POLAR_ACCESS_TOKEN, server: isDev ? "sandbox" : "production" });
156
204
  };
157
205
  export {
158
206
  createPolar,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paykit-sdk/polar",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "description": "Polar provider for PayKit",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",
@@ -24,7 +24,7 @@
24
24
  "@polar-sh/sdk": "^0.33.0"
25
25
  },
26
26
  "peerDependencies": {
27
- "@paykit-sdk/core": "^1.1.3"
27
+ "@paykit-sdk/core": "^1.1.6"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@paykit-sdk/core": "workspace:*",