@paykit-sdk/polar 1.1.0 → 1.1.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/README.md +81 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +12 -12
- package/dist/index.mjs +12 -12
- package/package.json +8 -7
package/README.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# @paykit-sdk/polar
|
|
2
|
+
|
|
3
|
+
Polar provider for PayKit
|
|
4
|
+
|
|
5
|
+
## Quick Start
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { PayKit } from '@paykit-sdk/core';
|
|
9
|
+
import { polar, createPolar } from '@paykit-sdk/polar';
|
|
10
|
+
|
|
11
|
+
// Method 1: Using environment variables
|
|
12
|
+
const provider = polar(); // Uses POLAR_ACCESS_TOKEN from env
|
|
13
|
+
|
|
14
|
+
// Method 2: Direct configuration
|
|
15
|
+
const provider = createPolar({
|
|
16
|
+
accessToken: process.env.POLAR_ACCESS_TOKEN,
|
|
17
|
+
server: 'sandbox', // or 'production'
|
|
18
|
+
debug: true,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const paykit = new PayKit(provider);
|
|
22
|
+
|
|
23
|
+
// Create checkout
|
|
24
|
+
const checkout = await paykit.checkouts.create({
|
|
25
|
+
item_id: 'product-id',
|
|
26
|
+
metadata: { plan: 'pro' },
|
|
27
|
+
provider_metadata: {
|
|
28
|
+
successUrl: 'https://your-app.com/success',
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// Handle webhooks
|
|
33
|
+
paykit.webhooks
|
|
34
|
+
.setup({ webhookSecret: process.env.POLAR_WEBHOOK_SECRET })
|
|
35
|
+
.on('$checkoutCreated', async event => {
|
|
36
|
+
console.log('Checkout created:', event.data);
|
|
37
|
+
})
|
|
38
|
+
.on('$paymentReceived', async event => {
|
|
39
|
+
console.log('Payment received:', event.data);
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Subscription Updates
|
|
44
|
+
|
|
45
|
+
Polar requires specific update types:
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
// Change product
|
|
49
|
+
await paykit.subscriptions.update('sub_123', {
|
|
50
|
+
metadata: { productId: 'new-product-id' },
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Cancel at period end
|
|
54
|
+
await paykit.subscriptions.update('sub_123', {
|
|
55
|
+
metadata: { cancelAtPeriodEnd: 'true' },
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// Cancel with reason
|
|
59
|
+
await paykit.subscriptions.update('sub_123', {
|
|
60
|
+
metadata: {
|
|
61
|
+
cancelAtPeriodEnd: 'true',
|
|
62
|
+
customerCancellationReason: 'too_expensive',
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Environment Variables
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
POLAR_ACCESS_TOKEN=polar_oat_...
|
|
71
|
+
POLAR_WEBHOOK_SECRET=your-webhook-secret
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Support
|
|
75
|
+
|
|
76
|
+
- [Polar Documentation](https://docs.polar.sh/)
|
|
77
|
+
- [PayKit Issues](https://github.com/devodii/paykit/issues)
|
|
78
|
+
|
|
79
|
+
## License
|
|
80
|
+
|
|
81
|
+
ISC
|
package/dist/index.d.mts
CHANGED
|
@@ -23,7 +23,7 @@ declare class PolarProvider implements PayKitProvider {
|
|
|
23
23
|
/**
|
|
24
24
|
* Subscription management
|
|
25
25
|
*/
|
|
26
|
-
cancelSubscription: (id: string) => Promise<
|
|
26
|
+
cancelSubscription: (id: string) => Promise<null>;
|
|
27
27
|
retrieveSubscription: (id: string) => Promise<Subscription>;
|
|
28
28
|
updateSubscription: (id: string, params: UpdateSubscriptionParams) => Promise<Subscription>;
|
|
29
29
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -23,7 +23,7 @@ declare class PolarProvider implements PayKitProvider {
|
|
|
23
23
|
/**
|
|
24
24
|
* Subscription management
|
|
25
25
|
*/
|
|
26
|
-
cancelSubscription: (id: string) => Promise<
|
|
26
|
+
cancelSubscription: (id: string) => Promise<null>;
|
|
27
27
|
retrieveSubscription: (id: string) => Promise<Subscription>;
|
|
28
28
|
updateSubscription: (id: string, params: UpdateSubscriptionParams) => Promise<Subscription>;
|
|
29
29
|
/**
|
package/dist/index.js
CHANGED
|
@@ -68,11 +68,7 @@ var PolarProvider = class {
|
|
|
68
68
|
*/
|
|
69
69
|
this.createCheckout = async (params) => {
|
|
70
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
|
+
const response = await this.polar.checkouts.create({ metadata, products: [item_id], ...provider_metadata });
|
|
76
72
|
return toPaykitCheckout(response);
|
|
77
73
|
};
|
|
78
74
|
this.retrieveCheckout = async (id) => {
|
|
@@ -89,7 +85,10 @@ var PolarProvider = class {
|
|
|
89
85
|
};
|
|
90
86
|
this.updateCustomer = async (id, params) => {
|
|
91
87
|
const { email, name, metadata } = params;
|
|
92
|
-
const response = await this.polar.customers.update({
|
|
88
|
+
const response = await this.polar.customers.update({
|
|
89
|
+
id,
|
|
90
|
+
customerUpdate: { ...email && { email }, ...name && { name }, ...metadata && { metadata } }
|
|
91
|
+
});
|
|
93
92
|
return toPaykitCustomer(response);
|
|
94
93
|
};
|
|
95
94
|
this.retrieveCustomer = async (id) => {
|
|
@@ -100,16 +99,16 @@ var PolarProvider = class {
|
|
|
100
99
|
* Subscription management
|
|
101
100
|
*/
|
|
102
101
|
this.cancelSubscription = async (id) => {
|
|
103
|
-
|
|
104
|
-
return
|
|
102
|
+
await this.polar.subscriptions.revoke({ id });
|
|
103
|
+
return null;
|
|
105
104
|
};
|
|
106
105
|
this.retrieveSubscription = async (id) => {
|
|
107
106
|
const response = await this.polar.subscriptions.get({ id });
|
|
108
107
|
return toPaykitSubscription(response);
|
|
109
108
|
};
|
|
110
109
|
this.updateSubscription = async (id, params) => {
|
|
111
|
-
const
|
|
112
|
-
return
|
|
110
|
+
const response = await this.polar.subscriptions.update({ id, subscriptionUpdate: { ...params.metadata } });
|
|
111
|
+
return toPaykitSubscription(response);
|
|
113
112
|
};
|
|
114
113
|
/**
|
|
115
114
|
* Webhook management
|
|
@@ -123,6 +122,7 @@ var PolarProvider = class {
|
|
|
123
122
|
},
|
|
124
123
|
{}
|
|
125
124
|
);
|
|
125
|
+
console.log({ headers, webhookHeaders, webhookSecret, body });
|
|
126
126
|
const webhookEvent = (0, import_webhooks.validateEvent)(body, webhookHeaders, webhookSecret);
|
|
127
127
|
const id = webhookHeaders["webhook-id"];
|
|
128
128
|
const timestamp = webhookHeaders["webhook-timestamp"];
|
|
@@ -134,7 +134,7 @@ var PolarProvider = class {
|
|
|
134
134
|
return (0, import_core2.toPaykitEvent)({ type: "$subscriptionCreated", created: parseInt(timestamp), id, data: subscription });
|
|
135
135
|
} else if (webhookEvent.type === "subscription.revoked") {
|
|
136
136
|
const subscription = await this.retrieveSubscription(webhookEvent.data.id);
|
|
137
|
-
return (0, import_core2.toPaykitEvent)({ type: "$
|
|
137
|
+
return (0, import_core2.toPaykitEvent)({ type: "$subscriptionCancelled", created: parseInt(timestamp), id, data: subscription });
|
|
138
138
|
} else if (webhookEvent.type === "customer.created") {
|
|
139
139
|
const customer = await this.retrieveCustomer(webhookEvent.data.id);
|
|
140
140
|
return (0, import_core2.toPaykitEvent)({ type: "$customerCreated", created: parseInt(timestamp), id, data: customer });
|
|
@@ -150,7 +150,7 @@ var PolarProvider = class {
|
|
|
150
150
|
const checkout = await this.retrieveCheckout(webhookEvent.data.id);
|
|
151
151
|
return (0, import_core2.toPaykitEvent)({ type: "$paymentReceived", created: parseInt(timestamp), id, data: checkout });
|
|
152
152
|
}
|
|
153
|
-
throw new Error(`
|
|
153
|
+
throw new Error(`Unhandled event type: ${webhookEvent.type}`);
|
|
154
154
|
};
|
|
155
155
|
const { accessToken, server, ...rest } = config;
|
|
156
156
|
this.polar = new import_sdk.Polar({ accessToken, serverURL: server === "sandbox" ? this.sandboxURL : this.productionURL, ...rest });
|
package/dist/index.mjs
CHANGED
|
@@ -46,11 +46,7 @@ var PolarProvider = class {
|
|
|
46
46
|
*/
|
|
47
47
|
this.createCheckout = async (params) => {
|
|
48
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
|
-
});
|
|
49
|
+
const response = await this.polar.checkouts.create({ metadata, products: [item_id], ...provider_metadata });
|
|
54
50
|
return toPaykitCheckout(response);
|
|
55
51
|
};
|
|
56
52
|
this.retrieveCheckout = async (id) => {
|
|
@@ -67,7 +63,10 @@ var PolarProvider = class {
|
|
|
67
63
|
};
|
|
68
64
|
this.updateCustomer = async (id, params) => {
|
|
69
65
|
const { email, name, metadata } = params;
|
|
70
|
-
const response = await this.polar.customers.update({
|
|
66
|
+
const response = await this.polar.customers.update({
|
|
67
|
+
id,
|
|
68
|
+
customerUpdate: { ...email && { email }, ...name && { name }, ...metadata && { metadata } }
|
|
69
|
+
});
|
|
71
70
|
return toPaykitCustomer(response);
|
|
72
71
|
};
|
|
73
72
|
this.retrieveCustomer = async (id) => {
|
|
@@ -78,16 +77,16 @@ var PolarProvider = class {
|
|
|
78
77
|
* Subscription management
|
|
79
78
|
*/
|
|
80
79
|
this.cancelSubscription = async (id) => {
|
|
81
|
-
|
|
82
|
-
return
|
|
80
|
+
await this.polar.subscriptions.revoke({ id });
|
|
81
|
+
return null;
|
|
83
82
|
};
|
|
84
83
|
this.retrieveSubscription = async (id) => {
|
|
85
84
|
const response = await this.polar.subscriptions.get({ id });
|
|
86
85
|
return toPaykitSubscription(response);
|
|
87
86
|
};
|
|
88
87
|
this.updateSubscription = async (id, params) => {
|
|
89
|
-
const
|
|
90
|
-
return
|
|
88
|
+
const response = await this.polar.subscriptions.update({ id, subscriptionUpdate: { ...params.metadata } });
|
|
89
|
+
return toPaykitSubscription(response);
|
|
91
90
|
};
|
|
92
91
|
/**
|
|
93
92
|
* Webhook management
|
|
@@ -101,6 +100,7 @@ var PolarProvider = class {
|
|
|
101
100
|
},
|
|
102
101
|
{}
|
|
103
102
|
);
|
|
103
|
+
console.log({ headers, webhookHeaders, webhookSecret, body });
|
|
104
104
|
const webhookEvent = validateEvent(body, webhookHeaders, webhookSecret);
|
|
105
105
|
const id = webhookHeaders["webhook-id"];
|
|
106
106
|
const timestamp = webhookHeaders["webhook-timestamp"];
|
|
@@ -112,7 +112,7 @@ var PolarProvider = class {
|
|
|
112
112
|
return toPaykitEvent({ type: "$subscriptionCreated", created: parseInt(timestamp), id, data: subscription });
|
|
113
113
|
} else if (webhookEvent.type === "subscription.revoked") {
|
|
114
114
|
const subscription = await this.retrieveSubscription(webhookEvent.data.id);
|
|
115
|
-
return toPaykitEvent({ type: "$
|
|
115
|
+
return toPaykitEvent({ type: "$subscriptionCancelled", created: parseInt(timestamp), id, data: subscription });
|
|
116
116
|
} else if (webhookEvent.type === "customer.created") {
|
|
117
117
|
const customer = await this.retrieveCustomer(webhookEvent.data.id);
|
|
118
118
|
return toPaykitEvent({ type: "$customerCreated", created: parseInt(timestamp), id, data: customer });
|
|
@@ -128,7 +128,7 @@ var PolarProvider = class {
|
|
|
128
128
|
const checkout = await this.retrieveCheckout(webhookEvent.data.id);
|
|
129
129
|
return toPaykitEvent({ type: "$paymentReceived", created: parseInt(timestamp), id, data: checkout });
|
|
130
130
|
}
|
|
131
|
-
throw new Error(`
|
|
131
|
+
throw new Error(`Unhandled event type: ${webhookEvent.type}`);
|
|
132
132
|
};
|
|
133
133
|
const { accessToken, server, ...rest } = config;
|
|
134
134
|
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.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Polar provider for PayKit",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
@@ -8,6 +8,10 @@
|
|
|
8
8
|
"files": [
|
|
9
9
|
"dist"
|
|
10
10
|
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
13
|
+
"prepublishOnly": "rm -rf dist && npm run build"
|
|
14
|
+
},
|
|
11
15
|
"keywords": [
|
|
12
16
|
"polar",
|
|
13
17
|
"paykit",
|
|
@@ -23,9 +27,9 @@
|
|
|
23
27
|
"@paykit-sdk/core": "^1.1.0"
|
|
24
28
|
},
|
|
25
29
|
"devDependencies": {
|
|
30
|
+
"@paykit-sdk/core": "workspace:*",
|
|
26
31
|
"tsup": "^8.0.0",
|
|
27
|
-
"typescript": "^5.0.0"
|
|
28
|
-
"@paykit-sdk/core": "1.1.0"
|
|
32
|
+
"typescript": "^5.0.0"
|
|
29
33
|
},
|
|
30
34
|
"publishConfig": {
|
|
31
35
|
"access": "public"
|
|
@@ -36,8 +40,5 @@
|
|
|
36
40
|
},
|
|
37
41
|
"bugs": {
|
|
38
42
|
"url": "https://github.com/devodii/paykit/issues"
|
|
39
|
-
},
|
|
40
|
-
"scripts": {
|
|
41
|
-
"build": "tsup src/index.ts --format cjs,esm --dts"
|
|
42
43
|
}
|
|
43
|
-
}
|
|
44
|
+
}
|