@paykit-sdk/polar 1.0.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.
@@ -0,0 +1,39 @@
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 { WithPaymentProviderConfig } from '@paykit-sdk/core/src/types';
4
+ import { SDKOptions } from '@polar-sh/sdk';
5
+
6
+ interface PolarConfig extends WithPaymentProviderConfig<Omit<SDKOptions, 'accessToken'>> {
7
+ }
8
+ declare class PolarProvider implements PayKitProvider {
9
+ private config;
10
+ private polar;
11
+ private readonly productionURL;
12
+ private readonly sandboxURL;
13
+ constructor(config: PolarConfig);
14
+ /**
15
+ * Checkout management
16
+ */
17
+ createCheckout: (params: CreateCheckoutParams) => Promise<Checkout>;
18
+ retrieveCheckout: (id: string) => Promise<Checkout>;
19
+ /**
20
+ * Customer management
21
+ */
22
+ createCustomer: (params: CreateCustomerParams) => Promise<Customer>;
23
+ updateCustomer: (id: string, params: UpdateCustomerParams) => Promise<Customer>;
24
+ retrieveCustomer: (id: string) => Promise<Customer>;
25
+ /**
26
+ * Subscription management
27
+ */
28
+ cancelSubscription: (id: string) => Promise<Subscription>;
29
+ retrieveSubscription: (id: string) => Promise<Subscription>;
30
+ updateSubscription: (id: string, params: UpdateSubscriptionParams) => Promise<Subscription>;
31
+ /**
32
+ * Webhook management
33
+ */
34
+ handleWebhook: (payload: string, signature: string, secret: string) => Promise<WebhookEventPayload>;
35
+ }
36
+
37
+ declare const polar: () => PolarProvider;
38
+
39
+ export { polar };
@@ -0,0 +1,39 @@
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 { WithPaymentProviderConfig } from '@paykit-sdk/core/src/types';
4
+ import { SDKOptions } from '@polar-sh/sdk';
5
+
6
+ interface PolarConfig extends WithPaymentProviderConfig<Omit<SDKOptions, 'accessToken'>> {
7
+ }
8
+ declare class PolarProvider implements PayKitProvider {
9
+ private config;
10
+ private polar;
11
+ private readonly productionURL;
12
+ private readonly sandboxURL;
13
+ constructor(config: PolarConfig);
14
+ /**
15
+ * Checkout management
16
+ */
17
+ createCheckout: (params: CreateCheckoutParams) => Promise<Checkout>;
18
+ retrieveCheckout: (id: string) => Promise<Checkout>;
19
+ /**
20
+ * Customer management
21
+ */
22
+ createCustomer: (params: CreateCustomerParams) => Promise<Customer>;
23
+ updateCustomer: (id: string, params: UpdateCustomerParams) => Promise<Customer>;
24
+ retrieveCustomer: (id: string) => Promise<Customer>;
25
+ /**
26
+ * Subscription management
27
+ */
28
+ cancelSubscription: (id: string) => Promise<Subscription>;
29
+ retrieveSubscription: (id: string) => Promise<Subscription>;
30
+ updateSubscription: (id: string, params: UpdateSubscriptionParams) => Promise<Subscription>;
31
+ /**
32
+ * Webhook management
33
+ */
34
+ handleWebhook: (payload: string, signature: string, secret: string) => Promise<WebhookEventPayload>;
35
+ }
36
+
37
+ declare const polar: () => PolarProvider;
38
+
39
+ export { polar };
package/dist/index.js ADDED
@@ -0,0 +1,137 @@
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/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ polar: () => polar
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+
27
+ // src/polar-provider.ts
28
+ var import_resources2 = require("@paykit-sdk/core/src/resources");
29
+ var import_sdk = require("@polar-sh/sdk");
30
+
31
+ // lib/mapper.ts
32
+ var import_resources = require("@paykit-sdk/core/src/resources/");
33
+ var toPaykitCheckout = (checkout) => {
34
+ return {
35
+ id: checkout.id,
36
+ url: checkout.url,
37
+ cancel_url: void 0,
38
+ customer_id: checkout.customerId,
39
+ mode: "payment",
40
+ success_url: checkout.successUrl,
41
+ products: checkout.products.map((product) => ({ id: product.id, quantity: 1 })),
42
+ metadata: checkout.metadata
43
+ };
44
+ };
45
+ var toPaykitCustomer = (customer) => {
46
+ return { id: customer.id, email: customer.email, name: customer.name ?? void 0 };
47
+ };
48
+ var toPaykitSubscription = (subscription) => {
49
+ return {
50
+ id: subscription.id,
51
+ customer_id: subscription.customerId,
52
+ status: (0, import_resources.toPaykitSubscriptionStatus)(subscription.status),
53
+ current_period_start: new Date(subscription.currentPeriodStart),
54
+ current_period_end: new Date(subscription.currentPeriodEnd)
55
+ };
56
+ };
57
+
58
+ // src/polar-provider.ts
59
+ var PolarProvider = class {
60
+ constructor(config) {
61
+ this.config = config;
62
+ this.productionURL = "https://api.polar.sh";
63
+ this.sandboxURL = "https://api.sandbox.polar.sh";
64
+ /**
65
+ * Checkout management
66
+ */
67
+ this.createCheckout = async (params) => {
68
+ const { metadata, success_url, price_id } = params;
69
+ const response = await this.polar.checkouts.create({ ...metadata && { metadata }, successUrl: success_url, products: [price_id] });
70
+ return toPaykitCheckout(response);
71
+ };
72
+ this.retrieveCheckout = async (id) => {
73
+ const response = await this.polar.checkouts.get({ id });
74
+ return toPaykitCheckout(response);
75
+ };
76
+ /**
77
+ * Customer management
78
+ */
79
+ this.createCustomer = async (params) => {
80
+ const { email, name, metadata } = params;
81
+ const response = await this.polar.customers.create({ email, name, ...metadata && { metadata } });
82
+ return toPaykitCustomer(response);
83
+ };
84
+ this.updateCustomer = async (id, params) => {
85
+ const { email, name, metadata } = params;
86
+ const response = await this.polar.customers.update({ id, customerUpdate: { email, name, ...metadata && { metadata } } });
87
+ return toPaykitCustomer(response);
88
+ };
89
+ this.retrieveCustomer = async (id) => {
90
+ const response = await this.polar.customers.get({ id });
91
+ return toPaykitCustomer(response);
92
+ };
93
+ /**
94
+ * Subscription management
95
+ */
96
+ this.cancelSubscription = async (id) => {
97
+ const response = await this.polar.subscriptions.revoke({ id });
98
+ return toPaykitSubscription(response);
99
+ };
100
+ this.retrieveSubscription = async (id) => {
101
+ const response = await this.polar.subscriptions.get({ id });
102
+ return toPaykitSubscription(response);
103
+ };
104
+ this.updateSubscription = async (id, params) => {
105
+ const subscription = await this.retrieveSubscription(id);
106
+ return subscription;
107
+ };
108
+ /**
109
+ * Webhook management
110
+ */
111
+ this.handleWebhook = async (payload, signature, secret) => {
112
+ const response = await this.polar.events.get({ id: payload });
113
+ return (0, import_resources2.toPaykitEvent)({
114
+ data: response,
115
+ created: new Date(response.timestamp).getTime(),
116
+ id: response.id,
117
+ type: response.name
118
+ });
119
+ };
120
+ const { apiKey, environment = "test", ...rest } = config;
121
+ this.polar = new import_sdk.Polar({ ...rest, accessToken: apiKey, serverURL: environment === "test" ? this.sandboxURL : this.productionURL });
122
+ }
123
+ };
124
+
125
+ // src/index.ts
126
+ var createPolar = (config) => {
127
+ return new PolarProvider(config);
128
+ };
129
+ var polar = () => {
130
+ const apiKey = process.env.POLAR_ACCESS_TOKEN;
131
+ if (!apiKey) throw new Error("POLAR_ACCESS_TOKEN is not set");
132
+ return createPolar({ apiKey, environment: "test" });
133
+ };
134
+ // Annotate the CommonJS export names for ESM import in node:
135
+ 0 && (module.exports = {
136
+ polar
137
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,114 @@
1
+ // src/polar-provider.ts
2
+ import {
3
+ toPaykitEvent
4
+ } from "@paykit-sdk/core/src/resources";
5
+ import { Polar } from "@polar-sh/sdk";
6
+
7
+ // lib/mapper.ts
8
+ import {
9
+ toPaykitSubscriptionStatus
10
+ } from "@paykit-sdk/core/src/resources/";
11
+ var toPaykitCheckout = (checkout) => {
12
+ return {
13
+ id: checkout.id,
14
+ url: checkout.url,
15
+ cancel_url: void 0,
16
+ customer_id: checkout.customerId,
17
+ mode: "payment",
18
+ success_url: checkout.successUrl,
19
+ products: checkout.products.map((product) => ({ id: product.id, quantity: 1 })),
20
+ metadata: checkout.metadata
21
+ };
22
+ };
23
+ var toPaykitCustomer = (customer) => {
24
+ return { id: customer.id, email: customer.email, name: customer.name ?? void 0 };
25
+ };
26
+ var toPaykitSubscription = (subscription) => {
27
+ return {
28
+ id: subscription.id,
29
+ customer_id: subscription.customerId,
30
+ status: toPaykitSubscriptionStatus(subscription.status),
31
+ current_period_start: new Date(subscription.currentPeriodStart),
32
+ current_period_end: new Date(subscription.currentPeriodEnd)
33
+ };
34
+ };
35
+
36
+ // src/polar-provider.ts
37
+ var PolarProvider = class {
38
+ constructor(config) {
39
+ this.config = config;
40
+ this.productionURL = "https://api.polar.sh";
41
+ this.sandboxURL = "https://api.sandbox.polar.sh";
42
+ /**
43
+ * Checkout management
44
+ */
45
+ 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
+ return toPaykitCheckout(response);
49
+ };
50
+ this.retrieveCheckout = async (id) => {
51
+ const response = await this.polar.checkouts.get({ id });
52
+ return toPaykitCheckout(response);
53
+ };
54
+ /**
55
+ * Customer management
56
+ */
57
+ this.createCustomer = async (params) => {
58
+ const { email, name, metadata } = params;
59
+ const response = await this.polar.customers.create({ email, name, ...metadata && { metadata } });
60
+ return toPaykitCustomer(response);
61
+ };
62
+ this.updateCustomer = async (id, params) => {
63
+ const { email, name, metadata } = params;
64
+ const response = await this.polar.customers.update({ id, customerUpdate: { email, name, ...metadata && { metadata } } });
65
+ return toPaykitCustomer(response);
66
+ };
67
+ this.retrieveCustomer = async (id) => {
68
+ const response = await this.polar.customers.get({ id });
69
+ return toPaykitCustomer(response);
70
+ };
71
+ /**
72
+ * Subscription management
73
+ */
74
+ this.cancelSubscription = async (id) => {
75
+ const response = await this.polar.subscriptions.revoke({ id });
76
+ return toPaykitSubscription(response);
77
+ };
78
+ this.retrieveSubscription = async (id) => {
79
+ const response = await this.polar.subscriptions.get({ id });
80
+ return toPaykitSubscription(response);
81
+ };
82
+ this.updateSubscription = async (id, params) => {
83
+ const subscription = await this.retrieveSubscription(id);
84
+ return subscription;
85
+ };
86
+ /**
87
+ * Webhook management
88
+ */
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
+ });
97
+ };
98
+ const { apiKey, environment = "test", ...rest } = config;
99
+ this.polar = new Polar({ ...rest, accessToken: apiKey, serverURL: environment === "test" ? this.sandboxURL : this.productionURL });
100
+ }
101
+ };
102
+
103
+ // src/index.ts
104
+ var createPolar = (config) => {
105
+ return new PolarProvider(config);
106
+ };
107
+ var polar = () => {
108
+ const apiKey = process.env.POLAR_ACCESS_TOKEN;
109
+ if (!apiKey) throw new Error("POLAR_ACCESS_TOKEN is not set");
110
+ return createPolar({ apiKey, environment: "test" });
111
+ };
112
+ export {
113
+ polar
114
+ };
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@paykit-sdk/polar",
3
+ "version": "1.0.0",
4
+ "description": "Polar provider for PayKit",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.esm.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsup src/index.ts --format cjs,esm --dts",
13
+ "prepublishOnly": "npm run build"
14
+ },
15
+ "keywords": [
16
+ "polar",
17
+ "paykit",
18
+ "payment",
19
+ "typescript"
20
+ ],
21
+ "author": "Emmanuel Odii",
22
+ "license": "ISC",
23
+ "dependencies": {
24
+ "@polar-sh/sdk": "^0.33.0"
25
+ },
26
+ "peerDependencies": {
27
+ "@paykit-sdk/core": "^1.0.0"
28
+ },
29
+ "devDependencies": {
30
+ "tsup": "^8.0.0",
31
+ "typescript": "^5.0.0",
32
+ "@paykit-sdk/core": "workspace:*"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "git+https://github.com/devodii/paykit.git"
40
+ },
41
+ "bugs": {
42
+ "url": "https://github.com/devodii/paykit/issues"
43
+ }
44
+ }