@paykit-sdk/polar 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,9 +1,7 @@
1
- import { WithPaymentProviderConfig } from '@paykit-sdk/core/src/types';
2
- import { PayKitProvider } from '@paykit-sdk/core/src/paykit-provider';
3
- import { CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, WebhookEventPayload } from '@paykit-sdk/core/src/resources';
1
+ import { PaykitProviderOptions, PayKitProvider, CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, $ExtWebhookHandlerConfig, WebhookEventPayload } from '@paykit-sdk/core';
4
2
  import { SDKOptions } from '@polar-sh/sdk';
5
3
 
6
- interface PolarConfig extends WithPaymentProviderConfig<Omit<SDKOptions, 'accessToken'>> {
4
+ interface PolarConfig extends PaykitProviderOptions<SDKOptions> {
7
5
  }
8
6
  declare class PolarProvider implements PayKitProvider {
9
7
  private config;
@@ -31,10 +29,10 @@ declare class PolarProvider implements PayKitProvider {
31
29
  /**
32
30
  * Webhook management
33
31
  */
34
- handleWebhook: (payload: string, signature: string, secret: string) => Promise<WebhookEventPayload>;
32
+ handleWebhook: (params: $ExtWebhookHandlerConfig) => Promise<WebhookEventPayload>;
35
33
  }
36
34
 
37
- declare const createPolar: (config: WithPaymentProviderConfig<PolarConfig>) => PolarProvider;
35
+ declare const createPolar: (config: PolarConfig) => PolarProvider;
38
36
  declare const polar: () => PolarProvider;
39
37
 
40
38
  export { createPolar, polar };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,7 @@
1
- import { WithPaymentProviderConfig } from '@paykit-sdk/core/src/types';
2
- import { PayKitProvider } from '@paykit-sdk/core/src/paykit-provider';
3
- import { CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, WebhookEventPayload } from '@paykit-sdk/core/src/resources';
1
+ import { PaykitProviderOptions, PayKitProvider, CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, $ExtWebhookHandlerConfig, WebhookEventPayload } from '@paykit-sdk/core';
4
2
  import { SDKOptions } from '@polar-sh/sdk';
5
3
 
6
- interface PolarConfig extends WithPaymentProviderConfig<Omit<SDKOptions, 'accessToken'>> {
4
+ interface PolarConfig extends PaykitProviderOptions<SDKOptions> {
7
5
  }
8
6
  declare class PolarProvider implements PayKitProvider {
9
7
  private config;
@@ -31,10 +29,10 @@ declare class PolarProvider implements PayKitProvider {
31
29
  /**
32
30
  * Webhook management
33
31
  */
34
- handleWebhook: (payload: string, signature: string, secret: string) => Promise<WebhookEventPayload>;
32
+ handleWebhook: (params: $ExtWebhookHandlerConfig) => Promise<WebhookEventPayload>;
35
33
  }
36
34
 
37
- declare const createPolar: (config: WithPaymentProviderConfig<PolarConfig>) => PolarProvider;
35
+ declare const createPolar: (config: PolarConfig) => PolarProvider;
38
36
  declare const polar: () => PolarProvider;
39
37
 
40
38
  export { createPolar, polar };
package/dist/index.js CHANGED
@@ -26,21 +26,22 @@ __export(index_exports, {
26
26
  module.exports = __toCommonJS(index_exports);
27
27
 
28
28
  // src/polar-provider.ts
29
- var import_resources2 = require("@paykit-sdk/core/src/resources");
29
+ var import_core2 = require("@paykit-sdk/core");
30
30
  var import_sdk = require("@polar-sh/sdk");
31
+ var import_webhooks = require("@polar-sh/sdk/webhooks");
31
32
 
32
33
  // lib/mapper.ts
33
- var import_resources = require("@paykit-sdk/core/src/resources/");
34
+ var import_core = require("@paykit-sdk/core");
34
35
  var toPaykitCheckout = (checkout) => {
35
36
  return {
36
37
  id: checkout.id,
37
- url: checkout.url,
38
- cancel_url: void 0,
38
+ payment_url: checkout.url,
39
39
  customer_id: checkout.customerId,
40
- mode: "payment",
41
- success_url: checkout.successUrl,
40
+ session_type: checkout.subscriptionId ? "recurring" : "one_time",
42
41
  products: checkout.products.map((product) => ({ id: product.id, quantity: 1 })),
43
- metadata: checkout.metadata
42
+ metadata: checkout.metadata ?? null,
43
+ currency: checkout.currency,
44
+ amount: checkout.amount
44
45
  };
45
46
  };
46
47
  var toPaykitCustomer = (customer) => {
@@ -50,7 +51,7 @@ var toPaykitSubscription = (subscription) => {
50
51
  return {
51
52
  id: subscription.id,
52
53
  customer_id: subscription.customerId,
53
- status: (0, import_resources.toPaykitSubscriptionStatus)(subscription.status),
54
+ status: (0, import_core.toPaykitSubscriptionStatus)(subscription.status),
54
55
  current_period_start: new Date(subscription.currentPeriodStart),
55
56
  current_period_end: new Date(subscription.currentPeriodEnd)
56
57
  };
@@ -60,14 +61,18 @@ var toPaykitSubscription = (subscription) => {
60
61
  var PolarProvider = class {
61
62
  constructor(config) {
62
63
  this.config = config;
63
- this.productionURL = "https://api.polar.sh";
64
- this.sandboxURL = "https://api.sandbox.polar.sh";
64
+ this.productionURL = import_sdk.ServerList["production"];
65
+ this.sandboxURL = import_sdk.ServerList["sandbox"];
65
66
  /**
66
67
  * Checkout management
67
68
  */
68
69
  this.createCheckout = async (params) => {
69
- const { metadata, success_url, price_id } = params;
70
- const response = await this.polar.checkouts.create({ ...metadata && { metadata }, successUrl: success_url, products: [price_id] });
70
+ const { metadata, item_id, provider_metadata } = params;
71
+ const response = await this.polar.checkouts.create({
72
+ metadata,
73
+ products: [item_id],
74
+ ...provider_metadata
75
+ });
71
76
  return toPaykitCheckout(response);
72
77
  };
73
78
  this.retrieveCheckout = async (id) => {
@@ -109,17 +114,46 @@ var PolarProvider = class {
109
114
  /**
110
115
  * Webhook management
111
116
  */
112
- this.handleWebhook = async (payload, signature, secret) => {
113
- const response = await this.polar.events.get({ id: payload });
114
- return (0, import_resources2.toPaykitEvent)({
115
- data: response,
116
- created: new Date(response.timestamp).getTime(),
117
- id: response.id,
118
- type: response.name
119
- });
117
+ this.handleWebhook = async (params) => {
118
+ const { body, headers, webhookSecret } = params;
119
+ const webhookHeaders = (0, import_core2.headersExtractor)(headers, ["webhook-id", "webhook-timestamp", "webhook-signature"]).reduce(
120
+ (acc, kv) => {
121
+ acc[kv.key] = Array.isArray(kv.value) ? kv.value.join(",") : kv.value;
122
+ return acc;
123
+ },
124
+ {}
125
+ );
126
+ const webhookEvent = (0, import_webhooks.validateEvent)(body, webhookHeaders, webhookSecret);
127
+ const id = webhookHeaders["webhook-id"];
128
+ const timestamp = webhookHeaders["webhook-timestamp"];
129
+ if (webhookEvent.type === "subscription.updated") {
130
+ const subscription = await this.retrieveSubscription(webhookEvent.data.id);
131
+ return (0, import_core2.toPaykitEvent)({ type: "$subscriptionUpdated", created: parseInt(timestamp), id, data: subscription });
132
+ } else if (webhookEvent.type === "subscription.created") {
133
+ const subscription = await this.retrieveSubscription(webhookEvent.data.id);
134
+ return (0, import_core2.toPaykitEvent)({ type: "$subscriptionCreated", created: parseInt(timestamp), id, data: subscription });
135
+ } else if (webhookEvent.type === "subscription.revoked") {
136
+ const subscription = await this.retrieveSubscription(webhookEvent.data.id);
137
+ return (0, import_core2.toPaykitEvent)({ type: "$subscriptionCanceled", created: parseInt(timestamp), id, data: subscription });
138
+ } else if (webhookEvent.type === "customer.created") {
139
+ const customer = await this.retrieveCustomer(webhookEvent.data.id);
140
+ return (0, import_core2.toPaykitEvent)({ type: "$customerCreated", created: parseInt(timestamp), id, data: customer });
141
+ } else if (webhookEvent.type === "customer.updated") {
142
+ const customer = await this.retrieveCustomer(webhookEvent.data.id);
143
+ return (0, import_core2.toPaykitEvent)({ type: "$customerUpdated", created: parseInt(timestamp), id, data: customer });
144
+ } else if (webhookEvent.type === "customer.deleted") {
145
+ return (0, import_core2.toPaykitEvent)({ type: "$customerDeleted", created: parseInt(timestamp), id, data: null });
146
+ } else if (webhookEvent.type === "checkout.created") {
147
+ const checkout = await this.retrieveCheckout(webhookEvent.data.id);
148
+ return (0, import_core2.toPaykitEvent)({ type: "$checkoutCreated", created: parseInt(timestamp), id, data: checkout });
149
+ } else if (webhookEvent.type === "order.created") {
150
+ const checkout = await this.retrieveCheckout(webhookEvent.data.id);
151
+ return (0, import_core2.toPaykitEvent)({ type: "$paymentReceived", created: parseInt(timestamp), id, data: checkout });
152
+ }
153
+ throw new Error(`Unknown event type: ${webhookEvent.type}`);
120
154
  };
121
- const { apiKey, environment = "test", ...rest } = config;
122
- this.polar = new import_sdk.Polar({ ...rest, accessToken: apiKey, serverURL: environment === "test" ? this.sandboxURL : this.productionURL });
155
+ const { accessToken, server, ...rest } = config;
156
+ this.polar = new import_sdk.Polar({ accessToken, serverURL: server === "sandbox" ? this.sandboxURL : this.productionURL, ...rest });
123
157
  }
124
158
  };
125
159
 
@@ -128,10 +162,10 @@ var createPolar = (config) => {
128
162
  return new PolarProvider(config);
129
163
  };
130
164
  var polar = () => {
131
- const apiKey = process.env.POLAR_ACCESS_TOKEN;
165
+ const accessToken = process.env.POLAR_ACCESS_TOKEN;
132
166
  const isDev = process.env.NODE_ENV === "development";
133
- if (!apiKey) throw new Error("POLAR_ACCESS_TOKEN is not set");
134
- return createPolar({ apiKey, environment: isDev ? "test" : "live" });
167
+ if (!accessToken) throw new Error("POLAR_ACCESS_TOKEN is not set");
168
+ return createPolar({ debug: true, accessToken, server: isDev ? "sandbox" : "production" });
135
169
  };
136
170
  // Annotate the CommonJS export names for ESM import in node:
137
171
  0 && (module.exports = {
package/dist/index.mjs CHANGED
@@ -1,23 +1,25 @@
1
1
  // src/polar-provider.ts
2
2
  import {
3
- toPaykitEvent
4
- } from "@paykit-sdk/core/src/resources";
5
- import { Polar } from "@polar-sh/sdk";
3
+ toPaykitEvent,
4
+ headersExtractor
5
+ } from "@paykit-sdk/core";
6
+ import { Polar, ServerList } from "@polar-sh/sdk";
7
+ import { validateEvent } from "@polar-sh/sdk/webhooks";
6
8
 
7
9
  // lib/mapper.ts
8
10
  import {
9
11
  toPaykitSubscriptionStatus
10
- } from "@paykit-sdk/core/src/resources/";
12
+ } from "@paykit-sdk/core";
11
13
  var toPaykitCheckout = (checkout) => {
12
14
  return {
13
15
  id: checkout.id,
14
- url: checkout.url,
15
- cancel_url: void 0,
16
+ payment_url: checkout.url,
16
17
  customer_id: checkout.customerId,
17
- mode: "payment",
18
- success_url: checkout.successUrl,
18
+ session_type: checkout.subscriptionId ? "recurring" : "one_time",
19
19
  products: checkout.products.map((product) => ({ id: product.id, quantity: 1 })),
20
- metadata: checkout.metadata
20
+ metadata: checkout.metadata ?? null,
21
+ currency: checkout.currency,
22
+ amount: checkout.amount
21
23
  };
22
24
  };
23
25
  var toPaykitCustomer = (customer) => {
@@ -37,14 +39,18 @@ var toPaykitSubscription = (subscription) => {
37
39
  var PolarProvider = class {
38
40
  constructor(config) {
39
41
  this.config = config;
40
- this.productionURL = "https://api.polar.sh";
41
- this.sandboxURL = "https://api.sandbox.polar.sh";
42
+ this.productionURL = ServerList["production"];
43
+ this.sandboxURL = ServerList["sandbox"];
42
44
  /**
43
45
  * Checkout management
44
46
  */
45
47
  this.createCheckout = async (params) => {
46
- const { metadata, success_url, price_id } = params;
47
- const response = await this.polar.checkouts.create({ ...metadata && { metadata }, successUrl: success_url, products: [price_id] });
48
+ const { metadata, item_id, provider_metadata } = params;
49
+ const response = await this.polar.checkouts.create({
50
+ metadata,
51
+ products: [item_id],
52
+ ...provider_metadata
53
+ });
48
54
  return toPaykitCheckout(response);
49
55
  };
50
56
  this.retrieveCheckout = async (id) => {
@@ -86,17 +92,46 @@ var PolarProvider = class {
86
92
  /**
87
93
  * Webhook management
88
94
  */
89
- this.handleWebhook = async (payload, signature, secret) => {
90
- const response = await this.polar.events.get({ id: payload });
91
- return toPaykitEvent({
92
- data: response,
93
- created: new Date(response.timestamp).getTime(),
94
- id: response.id,
95
- type: response.name
96
- });
95
+ this.handleWebhook = async (params) => {
96
+ const { body, headers, webhookSecret } = params;
97
+ const webhookHeaders = headersExtractor(headers, ["webhook-id", "webhook-timestamp", "webhook-signature"]).reduce(
98
+ (acc, kv) => {
99
+ acc[kv.key] = Array.isArray(kv.value) ? kv.value.join(",") : kv.value;
100
+ return acc;
101
+ },
102
+ {}
103
+ );
104
+ const webhookEvent = validateEvent(body, webhookHeaders, webhookSecret);
105
+ const id = webhookHeaders["webhook-id"];
106
+ const timestamp = webhookHeaders["webhook-timestamp"];
107
+ if (webhookEvent.type === "subscription.updated") {
108
+ const subscription = await this.retrieveSubscription(webhookEvent.data.id);
109
+ return toPaykitEvent({ type: "$subscriptionUpdated", created: parseInt(timestamp), id, data: subscription });
110
+ } else if (webhookEvent.type === "subscription.created") {
111
+ const subscription = await this.retrieveSubscription(webhookEvent.data.id);
112
+ return toPaykitEvent({ type: "$subscriptionCreated", created: parseInt(timestamp), id, data: subscription });
113
+ } else if (webhookEvent.type === "subscription.revoked") {
114
+ const subscription = await this.retrieveSubscription(webhookEvent.data.id);
115
+ return toPaykitEvent({ type: "$subscriptionCanceled", created: parseInt(timestamp), id, data: subscription });
116
+ } else if (webhookEvent.type === "customer.created") {
117
+ const customer = await this.retrieveCustomer(webhookEvent.data.id);
118
+ return toPaykitEvent({ type: "$customerCreated", created: parseInt(timestamp), id, data: customer });
119
+ } else if (webhookEvent.type === "customer.updated") {
120
+ const customer = await this.retrieveCustomer(webhookEvent.data.id);
121
+ return toPaykitEvent({ type: "$customerUpdated", created: parseInt(timestamp), id, data: customer });
122
+ } else if (webhookEvent.type === "customer.deleted") {
123
+ return toPaykitEvent({ type: "$customerDeleted", created: parseInt(timestamp), id, data: null });
124
+ } else if (webhookEvent.type === "checkout.created") {
125
+ const checkout = await this.retrieveCheckout(webhookEvent.data.id);
126
+ return toPaykitEvent({ type: "$checkoutCreated", created: parseInt(timestamp), id, data: checkout });
127
+ } else if (webhookEvent.type === "order.created") {
128
+ const checkout = await this.retrieveCheckout(webhookEvent.data.id);
129
+ return toPaykitEvent({ type: "$paymentReceived", created: parseInt(timestamp), id, data: checkout });
130
+ }
131
+ throw new Error(`Unknown event type: ${webhookEvent.type}`);
97
132
  };
98
- const { apiKey, environment = "test", ...rest } = config;
99
- this.polar = new Polar({ ...rest, accessToken: apiKey, serverURL: environment === "test" ? this.sandboxURL : this.productionURL });
133
+ const { accessToken, server, ...rest } = config;
134
+ this.polar = new Polar({ accessToken, serverURL: server === "sandbox" ? this.sandboxURL : this.productionURL, ...rest });
100
135
  }
101
136
  };
102
137
 
@@ -105,10 +140,10 @@ var createPolar = (config) => {
105
140
  return new PolarProvider(config);
106
141
  };
107
142
  var polar = () => {
108
- const apiKey = process.env.POLAR_ACCESS_TOKEN;
143
+ const accessToken = process.env.POLAR_ACCESS_TOKEN;
109
144
  const isDev = process.env.NODE_ENV === "development";
110
- if (!apiKey) throw new Error("POLAR_ACCESS_TOKEN is not set");
111
- return createPolar({ apiKey, environment: isDev ? "test" : "live" });
145
+ if (!accessToken) throw new Error("POLAR_ACCESS_TOKEN is not set");
146
+ return createPolar({ debug: true, accessToken, server: isDev ? "sandbox" : "production" });
112
147
  };
113
148
  export {
114
149
  createPolar,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paykit-sdk/polar",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Polar provider for PayKit",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",
@@ -8,10 +8,6 @@
8
8
  "files": [
9
9
  "dist"
10
10
  ],
11
- "scripts": {
12
- "build": "tsup src/index.ts --format cjs,esm --dts",
13
- "prepublishOnly": "npm run build"
14
- },
15
11
  "keywords": [
16
12
  "polar",
17
13
  "paykit",
@@ -24,12 +20,12 @@
24
20
  "@polar-sh/sdk": "^0.33.0"
25
21
  },
26
22
  "peerDependencies": {
27
- "@paykit-sdk/core": "^1.0.0"
23
+ "@paykit-sdk/core": "^1.1.0"
28
24
  },
29
25
  "devDependencies": {
30
26
  "tsup": "^8.0.0",
31
27
  "typescript": "^5.0.0",
32
- "@paykit-sdk/core": "workspace:*"
28
+ "@paykit-sdk/core": "1.1.0"
33
29
  },
34
30
  "publishConfig": {
35
31
  "access": "public"
@@ -40,5 +36,8 @@
40
36
  },
41
37
  "bugs": {
42
38
  "url": "https://github.com/devodii/paykit/issues"
39
+ },
40
+ "scripts": {
41
+ "build": "tsup src/index.ts --format cjs,esm --dts"
43
42
  }
44
- }
43
+ }