@paykit-sdk/polar 1.1.6 → 1.1.7

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.js CHANGED
@@ -142,24 +142,70 @@ var PolarProvider = class {
142
142
  const { data, type } = (0, import_webhooks.validateEvent)(body, webhookHeaders, webhookSecret);
143
143
  const id = webhookHeaders["webhook-id"];
144
144
  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}`);
145
+ const webhookHandlers = {
146
+ /**
147
+ * Invoice
148
+ */
149
+ "order.paid": (data2) => {
150
+ const { status, metadata } = data2;
151
+ if (status !== "paid") return null;
152
+ return (0, import_core2.toPaykitEvent)({
153
+ type: "$invoicePaid",
154
+ created: parseInt(timestamp),
155
+ id,
156
+ data: toPaykitInvoice({ ...data2, metadata: { ...metadata ?? {}, $mode: "payment" } })
157
+ });
158
+ },
159
+ "order.created": (data2) => {
160
+ const { billingReason, metadata, status } = data2;
161
+ if (billingReason == "subscription_create") {
162
+ return (0, import_core2.toPaykitEvent)({
163
+ type: "$invoicePaid",
164
+ created: parseInt(timestamp),
165
+ id,
166
+ data: toPaykitInvoice({ ...data2, metadata: { ...metadata ?? {}, $mode: "subscription" } })
167
+ });
168
+ }
169
+ if (billingReason == "subscription_cycle") {
170
+ return (0, import_core2.toPaykitEvent)({
171
+ type: "$invoicePaid",
172
+ created: parseInt(timestamp),
173
+ id,
174
+ data: toPaykitInvoice({ ...data2, metadata: { ...metadata ?? {}, $mode: "subscription" } })
175
+ });
176
+ }
177
+ return null;
178
+ },
179
+ /**
180
+ * Customer
181
+ */
182
+ "customer.created": (data2) => {
183
+ return (0, import_core2.toPaykitEvent)({ type: "$customerCreated", created: parseInt(timestamp), id, data: toPaykitCustomer(data2) });
184
+ },
185
+ "customer.updated": (data2) => {
186
+ return (0, import_core2.toPaykitEvent)({ type: "$customerUpdated", created: parseInt(timestamp), id, data: toPaykitCustomer(data2) });
187
+ },
188
+ "customer.deleted": () => {
189
+ return (0, import_core2.toPaykitEvent)({ type: "$customerDeleted", created: parseInt(timestamp), id, data: null });
190
+ },
191
+ /**
192
+ * Subscription
193
+ */
194
+ "subscription.updated": (data2) => {
195
+ return (0, import_core2.toPaykitEvent)({ type: "$subscriptionUpdated", created: parseInt(timestamp), id, data: toPaykitSubscription(data2) });
196
+ },
197
+ "subscription.created": (data2) => {
198
+ return (0, import_core2.toPaykitEvent)({ type: "$subscriptionCreated", created: parseInt(timestamp), id, data: toPaykitSubscription(data2) });
199
+ },
200
+ "subscription.revoked": (data2) => {
201
+ return (0, import_core2.toPaykitEvent)({ type: "$subscriptionCancelled", created: parseInt(timestamp), id, data: toPaykitSubscription(data2) });
202
+ }
203
+ };
204
+ const handler = webhookHandlers[type];
205
+ if (!handler) throw new Error(`Unhandled event type: ${type}`);
206
+ const result = handler(data);
207
+ if (!result) throw new Error(`Unhandled event type: ${type}`);
208
+ return result;
163
209
  };
164
210
  const { accessToken, server, ...rest } = config;
165
211
  this.polar = new import_sdk.Polar({ accessToken, serverURL: server === "sandbox" ? this.sandboxURL : this.productionURL, ...rest });
package/dist/index.mjs CHANGED
@@ -120,24 +120,70 @@ var PolarProvider = class {
120
120
  const { data, type } = validateEvent(body, webhookHeaders, webhookSecret);
121
121
  const id = webhookHeaders["webhook-id"];
122
122
  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}`);
123
+ const webhookHandlers = {
124
+ /**
125
+ * Invoice
126
+ */
127
+ "order.paid": (data2) => {
128
+ const { status, metadata } = data2;
129
+ if (status !== "paid") return null;
130
+ return toPaykitEvent({
131
+ type: "$invoicePaid",
132
+ created: parseInt(timestamp),
133
+ id,
134
+ data: toPaykitInvoice({ ...data2, metadata: { ...metadata ?? {}, $mode: "payment" } })
135
+ });
136
+ },
137
+ "order.created": (data2) => {
138
+ const { billingReason, metadata, status } = data2;
139
+ if (billingReason == "subscription_create") {
140
+ return toPaykitEvent({
141
+ type: "$invoicePaid",
142
+ created: parseInt(timestamp),
143
+ id,
144
+ data: toPaykitInvoice({ ...data2, metadata: { ...metadata ?? {}, $mode: "subscription" } })
145
+ });
146
+ }
147
+ if (billingReason == "subscription_cycle") {
148
+ return toPaykitEvent({
149
+ type: "$invoicePaid",
150
+ created: parseInt(timestamp),
151
+ id,
152
+ data: toPaykitInvoice({ ...data2, metadata: { ...metadata ?? {}, $mode: "subscription" } })
153
+ });
154
+ }
155
+ return null;
156
+ },
157
+ /**
158
+ * Customer
159
+ */
160
+ "customer.created": (data2) => {
161
+ return toPaykitEvent({ type: "$customerCreated", created: parseInt(timestamp), id, data: toPaykitCustomer(data2) });
162
+ },
163
+ "customer.updated": (data2) => {
164
+ return toPaykitEvent({ type: "$customerUpdated", created: parseInt(timestamp), id, data: toPaykitCustomer(data2) });
165
+ },
166
+ "customer.deleted": () => {
167
+ return toPaykitEvent({ type: "$customerDeleted", created: parseInt(timestamp), id, data: null });
168
+ },
169
+ /**
170
+ * Subscription
171
+ */
172
+ "subscription.updated": (data2) => {
173
+ return toPaykitEvent({ type: "$subscriptionUpdated", created: parseInt(timestamp), id, data: toPaykitSubscription(data2) });
174
+ },
175
+ "subscription.created": (data2) => {
176
+ return toPaykitEvent({ type: "$subscriptionCreated", created: parseInt(timestamp), id, data: toPaykitSubscription(data2) });
177
+ },
178
+ "subscription.revoked": (data2) => {
179
+ return toPaykitEvent({ type: "$subscriptionCancelled", created: parseInt(timestamp), id, data: toPaykitSubscription(data2) });
180
+ }
181
+ };
182
+ const handler = webhookHandlers[type];
183
+ if (!handler) throw new Error(`Unhandled event type: ${type}`);
184
+ const result = handler(data);
185
+ if (!result) throw new Error(`Unhandled event type: ${type}`);
186
+ return result;
141
187
  };
142
188
  const { accessToken, server, ...rest } = config;
143
189
  this.polar = new Polar({ accessToken, serverURL: server === "sandbox" ? this.sandboxURL : this.productionURL, ...rest });
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.7",
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.5"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@paykit-sdk/core": "workspace:*",