@paykit-sdk/polar 1.0.0 → 1.0.1-alpha.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/index.d.mts +6 -5
- package/dist/index.d.ts +6 -5
- package/dist/index.js +57 -22
- package/dist/index.mjs +57 -23
- package/package.json +6 -7
package/dist/index.d.mts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { PayKitProvider } from '@paykit-sdk/core/src/paykit-provider';
|
|
2
|
-
import { CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, WebhookEventPayload } from '@paykit-sdk/core/src/resources';
|
|
3
|
-
import {
|
|
2
|
+
import { CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, WebhookProviderPayload, WebhookEventPayload } from '@paykit-sdk/core/src/resources';
|
|
3
|
+
import { PaykitProviderOptions } from '@paykit-sdk/core/src/types';
|
|
4
4
|
import { SDKOptions } from '@polar-sh/sdk';
|
|
5
5
|
|
|
6
|
-
interface PolarConfig extends
|
|
6
|
+
interface PolarConfig extends PaykitProviderOptions<SDKOptions> {
|
|
7
7
|
}
|
|
8
8
|
declare class PolarProvider implements PayKitProvider {
|
|
9
9
|
private config;
|
|
@@ -31,9 +31,10 @@ declare class PolarProvider implements PayKitProvider {
|
|
|
31
31
|
/**
|
|
32
32
|
* Webhook management
|
|
33
33
|
*/
|
|
34
|
-
handleWebhook: (
|
|
34
|
+
handleWebhook: (params: WebhookProviderPayload) => Promise<WebhookEventPayload>;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
declare const createPolar: (config: PolarConfig) => PolarProvider;
|
|
37
38
|
declare const polar: () => PolarProvider;
|
|
38
39
|
|
|
39
|
-
export { polar };
|
|
40
|
+
export { createPolar, polar };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { PayKitProvider } from '@paykit-sdk/core/src/paykit-provider';
|
|
2
|
-
import { CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, WebhookEventPayload } from '@paykit-sdk/core/src/resources';
|
|
3
|
-
import {
|
|
2
|
+
import { CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, WebhookProviderPayload, WebhookEventPayload } from '@paykit-sdk/core/src/resources';
|
|
3
|
+
import { PaykitProviderOptions } from '@paykit-sdk/core/src/types';
|
|
4
4
|
import { SDKOptions } from '@polar-sh/sdk';
|
|
5
5
|
|
|
6
|
-
interface PolarConfig extends
|
|
6
|
+
interface PolarConfig extends PaykitProviderOptions<SDKOptions> {
|
|
7
7
|
}
|
|
8
8
|
declare class PolarProvider implements PayKitProvider {
|
|
9
9
|
private config;
|
|
@@ -31,9 +31,10 @@ declare class PolarProvider implements PayKitProvider {
|
|
|
31
31
|
/**
|
|
32
32
|
* Webhook management
|
|
33
33
|
*/
|
|
34
|
-
handleWebhook: (
|
|
34
|
+
handleWebhook: (params: WebhookProviderPayload) => Promise<WebhookEventPayload>;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
declare const createPolar: (config: PolarConfig) => PolarProvider;
|
|
37
38
|
declare const polar: () => PolarProvider;
|
|
38
39
|
|
|
39
|
-
export { polar };
|
|
40
|
+
export { createPolar, polar };
|
package/dist/index.js
CHANGED
|
@@ -20,26 +20,29 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
createPolar: () => createPolar,
|
|
23
24
|
polar: () => polar
|
|
24
25
|
});
|
|
25
26
|
module.exports = __toCommonJS(index_exports);
|
|
26
27
|
|
|
27
28
|
// src/polar-provider.ts
|
|
28
29
|
var import_resources2 = require("@paykit-sdk/core/src/resources");
|
|
30
|
+
var import_webhook = require("@paykit-sdk/core/src/tools/webhook");
|
|
29
31
|
var import_sdk = require("@polar-sh/sdk");
|
|
32
|
+
var import_webhooks = require("@polar-sh/sdk/src/webhooks");
|
|
30
33
|
|
|
31
34
|
// lib/mapper.ts
|
|
32
35
|
var import_resources = require("@paykit-sdk/core/src/resources/");
|
|
33
36
|
var toPaykitCheckout = (checkout) => {
|
|
34
37
|
return {
|
|
35
38
|
id: checkout.id,
|
|
36
|
-
|
|
37
|
-
cancel_url: void 0,
|
|
39
|
+
payment_url: checkout.url,
|
|
38
40
|
customer_id: checkout.customerId,
|
|
39
|
-
|
|
40
|
-
success_url: checkout.successUrl,
|
|
41
|
+
session_type: checkout.subscriptionId ? "recurring" : "one_time",
|
|
41
42
|
products: checkout.products.map((product) => ({ id: product.id, quantity: 1 })),
|
|
42
|
-
metadata: checkout.metadata
|
|
43
|
+
metadata: checkout.metadata ?? null,
|
|
44
|
+
currency: checkout.currency,
|
|
45
|
+
amount: checkout.amount
|
|
43
46
|
};
|
|
44
47
|
};
|
|
45
48
|
var toPaykitCustomer = (customer) => {
|
|
@@ -59,14 +62,18 @@ var toPaykitSubscription = (subscription) => {
|
|
|
59
62
|
var PolarProvider = class {
|
|
60
63
|
constructor(config) {
|
|
61
64
|
this.config = config;
|
|
62
|
-
this.productionURL =
|
|
63
|
-
this.sandboxURL = "
|
|
65
|
+
this.productionURL = import_sdk.ServerList["production"];
|
|
66
|
+
this.sandboxURL = import_sdk.ServerList["sandbox"];
|
|
64
67
|
/**
|
|
65
68
|
* Checkout management
|
|
66
69
|
*/
|
|
67
70
|
this.createCheckout = async (params) => {
|
|
68
|
-
const { metadata,
|
|
69
|
-
const response = await this.polar.checkouts.create({
|
|
71
|
+
const { metadata, item_id, provider_metadata } = params;
|
|
72
|
+
const response = await this.polar.checkouts.create({
|
|
73
|
+
metadata,
|
|
74
|
+
products: [item_id],
|
|
75
|
+
...provider_metadata
|
|
76
|
+
});
|
|
70
77
|
return toPaykitCheckout(response);
|
|
71
78
|
};
|
|
72
79
|
this.retrieveCheckout = async (id) => {
|
|
@@ -108,17 +115,43 @@ var PolarProvider = class {
|
|
|
108
115
|
/**
|
|
109
116
|
* Webhook management
|
|
110
117
|
*/
|
|
111
|
-
this.handleWebhook = async (
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
118
|
+
this.handleWebhook = async (params) => {
|
|
119
|
+
const { body, headers, webhookSecret } = params;
|
|
120
|
+
const webhookHeaders = (0, import_webhook.headersExtractor)(headers, ["webhook-id", "webhook-timestamp", "webhook-signature"]).reduce(
|
|
121
|
+
(acc, kv) => {
|
|
122
|
+
acc[kv.key] = Array.isArray(kv.value) ? kv.value.join(",") : kv.value;
|
|
123
|
+
return acc;
|
|
124
|
+
},
|
|
125
|
+
{}
|
|
126
|
+
);
|
|
127
|
+
const webhookEvent = (0, import_webhooks.validateEvent)(body, webhookHeaders, webhookSecret);
|
|
128
|
+
const id = webhookHeaders["webhook-id"];
|
|
129
|
+
const timestamp = webhookHeaders["webhook-timestamp"];
|
|
130
|
+
if (webhookEvent.type === "subscription.updated") {
|
|
131
|
+
const subscription = await this.retrieveSubscription(webhookEvent.data.id);
|
|
132
|
+
return (0, import_resources2.toPaykitEvent)({ type: "subscription.updated", created: parseInt(timestamp), id, data: subscription });
|
|
133
|
+
} else if (webhookEvent.type === "subscription.created") {
|
|
134
|
+
const subscription = await this.retrieveSubscription(webhookEvent.data.id);
|
|
135
|
+
return (0, import_resources2.toPaykitEvent)({ type: "subscription.created", created: parseInt(timestamp), id, data: subscription });
|
|
136
|
+
} else if (webhookEvent.type === "subscription.revoked") {
|
|
137
|
+
const subscription = await this.retrieveSubscription(webhookEvent.data.id);
|
|
138
|
+
return (0, import_resources2.toPaykitEvent)({ type: "subscription.canceled", created: parseInt(timestamp), id, data: subscription });
|
|
139
|
+
} else if (webhookEvent.type === "customer.created") {
|
|
140
|
+
const customer = await this.retrieveCustomer(webhookEvent.data.id);
|
|
141
|
+
return (0, import_resources2.toPaykitEvent)({ type: "customer.created", created: parseInt(timestamp), id, data: customer });
|
|
142
|
+
} else if (webhookEvent.type === "customer.updated") {
|
|
143
|
+
const customer = await this.retrieveCustomer(webhookEvent.data.id);
|
|
144
|
+
return (0, import_resources2.toPaykitEvent)({ type: "customer.updated", created: parseInt(timestamp), id, data: customer });
|
|
145
|
+
} else if (webhookEvent.type === "customer.deleted") {
|
|
146
|
+
return (0, import_resources2.toPaykitEvent)({ type: "customer.deleted", created: parseInt(timestamp), id, data: null });
|
|
147
|
+
} else if (webhookEvent.type === "checkout.created") {
|
|
148
|
+
const checkout = await this.retrieveCheckout(webhookEvent.data.id);
|
|
149
|
+
return (0, import_resources2.toPaykitEvent)({ type: "checkout.created", created: parseInt(timestamp), id, data: checkout });
|
|
150
|
+
}
|
|
151
|
+
throw new Error(`Unknown event type: ${webhookEvent.type}`);
|
|
119
152
|
};
|
|
120
|
-
const {
|
|
121
|
-
this.polar = new import_sdk.Polar({
|
|
153
|
+
const { accessToken, server, ...rest } = config;
|
|
154
|
+
this.polar = new import_sdk.Polar({ accessToken, serverURL: server === "sandbox" ? this.sandboxURL : this.productionURL, ...rest });
|
|
122
155
|
}
|
|
123
156
|
};
|
|
124
157
|
|
|
@@ -127,11 +160,13 @@ var createPolar = (config) => {
|
|
|
127
160
|
return new PolarProvider(config);
|
|
128
161
|
};
|
|
129
162
|
var polar = () => {
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
163
|
+
const accessToken = process.env.POLAR_ACCESS_TOKEN;
|
|
164
|
+
const isDev = process.env.NODE_ENV === "development";
|
|
165
|
+
if (!accessToken) throw new Error("POLAR_ACCESS_TOKEN is not set");
|
|
166
|
+
return createPolar({ debug: true, accessToken, server: isDev ? "sandbox" : "production" });
|
|
133
167
|
};
|
|
134
168
|
// Annotate the CommonJS export names for ESM import in node:
|
|
135
169
|
0 && (module.exports = {
|
|
170
|
+
createPolar,
|
|
136
171
|
polar
|
|
137
172
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
import {
|
|
3
3
|
toPaykitEvent
|
|
4
4
|
} from "@paykit-sdk/core/src/resources";
|
|
5
|
-
import {
|
|
5
|
+
import { headersExtractor } from "@paykit-sdk/core/src/tools/webhook";
|
|
6
|
+
import { Polar, ServerList } from "@polar-sh/sdk";
|
|
7
|
+
import { validateEvent } from "@polar-sh/sdk/src/webhooks";
|
|
6
8
|
|
|
7
9
|
// lib/mapper.ts
|
|
8
10
|
import {
|
|
@@ -11,13 +13,13 @@ import {
|
|
|
11
13
|
var toPaykitCheckout = (checkout) => {
|
|
12
14
|
return {
|
|
13
15
|
id: checkout.id,
|
|
14
|
-
|
|
15
|
-
cancel_url: void 0,
|
|
16
|
+
payment_url: checkout.url,
|
|
16
17
|
customer_id: checkout.customerId,
|
|
17
|
-
|
|
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 = "
|
|
41
|
-
this.sandboxURL = "
|
|
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,
|
|
47
|
-
const response = await this.polar.checkouts.create({
|
|
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,43 @@ var PolarProvider = class {
|
|
|
86
92
|
/**
|
|
87
93
|
* Webhook management
|
|
88
94
|
*/
|
|
89
|
-
this.handleWebhook = async (
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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: "subscription.updated", 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: "subscription.created", 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: "subscription.canceled", 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: "customer.created", 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: "customer.updated", created: parseInt(timestamp), id, data: customer });
|
|
122
|
+
} else if (webhookEvent.type === "customer.deleted") {
|
|
123
|
+
return toPaykitEvent({ type: "customer.deleted", 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: "checkout.created", created: parseInt(timestamp), id, data: checkout });
|
|
127
|
+
}
|
|
128
|
+
throw new Error(`Unknown event type: ${webhookEvent.type}`);
|
|
97
129
|
};
|
|
98
|
-
const {
|
|
99
|
-
this.polar = new Polar({
|
|
130
|
+
const { accessToken, server, ...rest } = config;
|
|
131
|
+
this.polar = new Polar({ accessToken, serverURL: server === "sandbox" ? this.sandboxURL : this.productionURL, ...rest });
|
|
100
132
|
}
|
|
101
133
|
};
|
|
102
134
|
|
|
@@ -105,10 +137,12 @@ var createPolar = (config) => {
|
|
|
105
137
|
return new PolarProvider(config);
|
|
106
138
|
};
|
|
107
139
|
var polar = () => {
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
140
|
+
const accessToken = process.env.POLAR_ACCESS_TOKEN;
|
|
141
|
+
const isDev = process.env.NODE_ENV === "development";
|
|
142
|
+
if (!accessToken) throw new Error("POLAR_ACCESS_TOKEN is not set");
|
|
143
|
+
return createPolar({ debug: true, accessToken, server: isDev ? "sandbox" : "production" });
|
|
111
144
|
};
|
|
112
145
|
export {
|
|
146
|
+
createPolar,
|
|
113
147
|
polar
|
|
114
148
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paykit-sdk/polar",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1-alpha.1",
|
|
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",
|
|
@@ -29,7 +25,7 @@
|
|
|
29
25
|
"devDependencies": {
|
|
30
26
|
"tsup": "^8.0.0",
|
|
31
27
|
"typescript": "^5.0.0",
|
|
32
|
-
"@paykit-sdk/core": "
|
|
28
|
+
"@paykit-sdk/core": "1.0.1-alpha.1"
|
|
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
|
+
}
|