@paykit-sdk/polar 1.1.91 → 1.1.93
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 +126 -110
- package/dist/index.d.mts +4 -34
- package/dist/index.d.ts +4 -34
- package/dist/index.js +4501 -110
- package/dist/index.mjs +4500 -91
- package/dist/polar-provider.d.mts +52 -0
- package/dist/polar-provider.d.ts +52 -0
- package/dist/polar-provider.js +4616 -0
- package/dist/polar-provider.mjs +4614 -0
- package/package.json +12 -12
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@ Polar provider for PayKit
|
|
|
5
5
|
## Quick Start
|
|
6
6
|
|
|
7
7
|
```typescript
|
|
8
|
-
import { PayKit } from '@paykit-sdk/core';
|
|
8
|
+
import { createEndpointHandlers, PayKit } from '@paykit-sdk/core';
|
|
9
9
|
import { polar, createPolar } from '@paykit-sdk/polar';
|
|
10
10
|
|
|
11
11
|
// Method 1: Using environment variables
|
|
@@ -14,167 +14,183 @@ const provider = polar(); // Ensure POLAR_ACCESS_TOKEN environment variable is s
|
|
|
14
14
|
// Method 2: Direct configuration
|
|
15
15
|
const provider = createPolar({
|
|
16
16
|
accessToken: process.env.POLAR_ACCESS_TOKEN,
|
|
17
|
-
server: 'sandbox', // or 'production'
|
|
18
|
-
debug: true,
|
|
19
17
|
});
|
|
20
18
|
|
|
21
|
-
const paykit = new PayKit(provider);
|
|
19
|
+
export const paykit = new PayKit(provider);
|
|
20
|
+
export const endpoints = createEndpointHandlers(paykit);
|
|
21
|
+
```
|
|
22
22
|
|
|
23
|
-
|
|
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
|
-
});
|
|
23
|
+
### Next.js Catch All API Route (/api/paykit/[...endpoint]/route.ts)
|
|
31
24
|
|
|
32
|
-
|
|
33
|
-
paykit
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
console.log('Checkout created:', event.data);
|
|
37
|
-
})
|
|
38
|
-
.on('$invoicePaid', async event => {
|
|
39
|
-
console.log('Payment received:', event.data);
|
|
40
|
-
});
|
|
41
|
-
```
|
|
25
|
+
```typescript
|
|
26
|
+
import { endpoints } from '@/lib/paykit';
|
|
27
|
+
import { EndpointPath } from '@paykit-sdk/core';
|
|
28
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
42
29
|
|
|
43
|
-
|
|
30
|
+
export async function POST(
|
|
31
|
+
request: NextRequest,
|
|
32
|
+
{ params }: { params: { endpoint: string[] } },
|
|
33
|
+
) {
|
|
34
|
+
try {
|
|
35
|
+
// Construct the endpoint path with full type safety
|
|
36
|
+
const endpoint = ('/' + params.endpoint.join('/')) as EndpointPath;
|
|
37
|
+
const handler = endpoints[endpoint];
|
|
38
|
+
|
|
39
|
+
if (!handler) {
|
|
40
|
+
return NextResponse.json({ message: 'Endpoint not found' }, { status: 404 });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Parse request body
|
|
44
|
+
const body = await request.json();
|
|
45
|
+
const { args } = body;
|
|
46
|
+
|
|
47
|
+
const result = await handler(...args);
|
|
48
|
+
|
|
49
|
+
return NextResponse.json({ result });
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.error('PayKit API Error:', error);
|
|
52
|
+
return NextResponse.json(
|
|
53
|
+
{ message: error instanceof Error ? error.message : 'Internal server error' },
|
|
54
|
+
{ status: 500 },
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
```
|
|
44
59
|
|
|
45
|
-
### Next.js
|
|
60
|
+
### Next.js Webhooks (/api/paykit/webhooks/route.ts)
|
|
46
61
|
|
|
47
62
|
```typescript
|
|
48
63
|
import { paykit } from '@/lib/paykit';
|
|
49
64
|
import { NextRequest, NextResponse } from 'next/server';
|
|
50
65
|
|
|
51
66
|
export async function POST(request: NextRequest) {
|
|
52
|
-
|
|
67
|
+
const webhookSecret = process.env.POLAR_WEBHOOK_SECRET;
|
|
68
|
+
|
|
69
|
+
if (!webhookSecret) {
|
|
70
|
+
return NextResponse.json({ error: 'Webhook secret not configured' }, { status: 500 });
|
|
71
|
+
}
|
|
53
72
|
|
|
54
73
|
const webhook = paykit.webhooks
|
|
55
|
-
.setup({ webhookSecret
|
|
56
|
-
.on('
|
|
57
|
-
console.log('Checkout created:', event.data);
|
|
58
|
-
})
|
|
59
|
-
.on('$customerCreated', async event => {
|
|
74
|
+
.setup({ webhookSecret })
|
|
75
|
+
.on('customer.created', async event => {
|
|
60
76
|
console.log('Customer created:', event.data);
|
|
61
77
|
})
|
|
62
|
-
.on('
|
|
63
|
-
console.log('Customer updated:', event.data);
|
|
64
|
-
})
|
|
65
|
-
.on('$customerDeleted', async event => {
|
|
66
|
-
console.log('Customer deleted:', event.data);
|
|
67
|
-
})
|
|
68
|
-
.on('$subscriptionCreated', async event => {
|
|
78
|
+
.on('subscription.created', async event => {
|
|
69
79
|
console.log('Subscription created:', event.data);
|
|
70
80
|
})
|
|
71
|
-
.on('
|
|
72
|
-
console.log('
|
|
73
|
-
})
|
|
74
|
-
.on('$subscriptionCancelled', async event => {
|
|
75
|
-
console.log('Subscription cancelled:', event.data);
|
|
81
|
+
.on('payment.created', async event => {
|
|
82
|
+
console.log('Payment created:', event.data);
|
|
76
83
|
})
|
|
77
|
-
.on('
|
|
78
|
-
console.log('
|
|
84
|
+
.on('refund.created', async event => {
|
|
85
|
+
console.log('Refund created:', event.data);
|
|
79
86
|
});
|
|
80
87
|
|
|
81
|
-
const headers = Object.fromEntries(request.headers.entries());
|
|
82
88
|
const body = await request.text();
|
|
83
|
-
|
|
89
|
+
const headers = Object.fromEntries(request.headers.entries());
|
|
90
|
+
await webhook.handle({ body, headers, fullUrl: request.url });
|
|
84
91
|
|
|
92
|
+
// Return immediately, processing happens in background
|
|
85
93
|
return NextResponse.json({ success: true });
|
|
86
94
|
}
|
|
87
95
|
```
|
|
88
96
|
|
|
89
|
-
### Express.js
|
|
97
|
+
### Express.js with Webhook Handler
|
|
90
98
|
|
|
91
99
|
```typescript
|
|
92
|
-
import { paykit } from '@/lib/paykit';
|
|
100
|
+
import { endpoints, paykit } from '@/lib/paykit';
|
|
101
|
+
import { EndpointPath } from '@paykit-sdk/core';
|
|
93
102
|
import express from 'express';
|
|
94
103
|
|
|
95
104
|
const app = express();
|
|
96
|
-
app.use(express.raw({ type: 'application/json' }));
|
|
97
105
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
106
|
+
// IMPORTANT: Webhook route must come BEFORE express.json() middleware
|
|
107
|
+
// This ensures we get the raw body for signature verification
|
|
108
|
+
app.post(
|
|
109
|
+
'/api/webhooks/polar',
|
|
110
|
+
express.raw({ type: 'application/json' }),
|
|
111
|
+
async (req, res) => {
|
|
112
|
+
try {
|
|
113
|
+
const webhookSecret = process.env.POLAR_WEBHOOK_SECRET;
|
|
114
|
+
|
|
115
|
+
if (!webhookSecret) {
|
|
116
|
+
return res.status(500).json({ error: 'Webhook secret not configured' });
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const webhook = paykit.webhooks
|
|
120
|
+
.setup({ webhookSecret })
|
|
121
|
+
.on('customer.created', async event => {
|
|
122
|
+
console.log('Customer created:', event.data);
|
|
123
|
+
})
|
|
124
|
+
.on('subscription.created', async event => {
|
|
125
|
+
console.log('Subscription created:', event.data);
|
|
126
|
+
})
|
|
127
|
+
.on('payment.created', async event => {
|
|
128
|
+
console.log('Payment created:', event.data);
|
|
129
|
+
})
|
|
130
|
+
.on('refund.created', async event => {
|
|
131
|
+
console.log('Refund created:', event.data);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const body = req.body; // Raw buffer from express.raw()
|
|
135
|
+
const headers = req.headers;
|
|
136
|
+
await webhook.handle({ body, headers });
|
|
137
|
+
|
|
138
|
+
// Return immediately, processing happens in background
|
|
139
|
+
res.json({ success: true });
|
|
140
|
+
} catch (error) {
|
|
141
|
+
console.error('Webhook error:', error);
|
|
142
|
+
res.status(500).json({
|
|
143
|
+
message: error instanceof Error ? error.message : 'Webhook processing failed',
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
);
|
|
116
148
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
```
|
|
149
|
+
// Regular API routes use JSON middleware
|
|
150
|
+
app.use(express.json());
|
|
120
151
|
|
|
121
|
-
|
|
152
|
+
app.post('/api/paykit/*', async (req, res) => {
|
|
153
|
+
try {
|
|
154
|
+
const endpoint = req.path.replace('/api/paykit', '') as EndpointPath;
|
|
155
|
+
const handler = endpoints[endpoint];
|
|
122
156
|
|
|
123
|
-
|
|
124
|
-
|
|
157
|
+
if (!handler) {
|
|
158
|
+
return res.status(404).json({ message: 'Endpoint not found' });
|
|
159
|
+
}
|
|
125
160
|
|
|
126
|
-
|
|
127
|
-
|
|
161
|
+
const { args } = req.body;
|
|
162
|
+
const result = await handler(...args);
|
|
128
163
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
.
|
|
132
|
-
|
|
133
|
-
})
|
|
134
|
-
.on('$customerCreated', async event => {
|
|
135
|
-
console.log('Customer created:', event.data);
|
|
136
|
-
})
|
|
137
|
-
.on('$invoicePaid', async event => {
|
|
138
|
-
console.log('Payment received:', event.data);
|
|
164
|
+
res.json({ result });
|
|
165
|
+
} catch (error) {
|
|
166
|
+
res.status(500).json({
|
|
167
|
+
message: error instanceof Error ? error.message : 'Internal server error',
|
|
139
168
|
});
|
|
169
|
+
}
|
|
170
|
+
});
|
|
140
171
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
await webhook.handle({ body, headers });
|
|
144
|
-
|
|
145
|
-
return { success: true };
|
|
172
|
+
app.listen(3000, () => {
|
|
173
|
+
console.log('Server running on port 3000');
|
|
146
174
|
});
|
|
147
175
|
```
|
|
148
176
|
|
|
149
|
-
## Subscription
|
|
150
|
-
|
|
151
|
-
Polar requires specific update types:
|
|
177
|
+
## Subscription Management
|
|
152
178
|
|
|
153
179
|
```typescript
|
|
154
|
-
//
|
|
155
|
-
await paykit.subscriptions.update('sub_123', {
|
|
156
|
-
metadata: { productId: 'new-product-id' },
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
// Cancel at period end
|
|
180
|
+
// Update subscription
|
|
160
181
|
await paykit.subscriptions.update('sub_123', {
|
|
161
|
-
metadata: {
|
|
182
|
+
metadata: { plan: 'enterprise' },
|
|
162
183
|
});
|
|
163
184
|
|
|
164
|
-
// Cancel
|
|
165
|
-
await paykit.subscriptions.
|
|
166
|
-
metadata: {
|
|
167
|
-
cancelAtPeriodEnd: 'true',
|
|
168
|
-
customerCancellationReason: 'too_expensive',
|
|
169
|
-
},
|
|
170
|
-
});
|
|
185
|
+
// Cancel subscription
|
|
186
|
+
await paykit.subscriptions.cancel('sub_123');
|
|
171
187
|
```
|
|
172
188
|
|
|
173
189
|
## Environment Variables
|
|
174
190
|
|
|
175
191
|
```bash
|
|
176
|
-
POLAR_ACCESS_TOKEN=
|
|
177
|
-
POLAR_WEBHOOK_SECRET=
|
|
192
|
+
POLAR_ACCESS_TOKEN=polar_at_...
|
|
193
|
+
POLAR_WEBHOOK_SECRET=polar_wh_...
|
|
178
194
|
```
|
|
179
195
|
|
|
180
196
|
## Support
|
|
@@ -183,4 +199,4 @@ POLAR_WEBHOOK_SECRET=your-webhook-secret
|
|
|
183
199
|
|
|
184
200
|
## License
|
|
185
201
|
|
|
186
|
-
|
|
202
|
+
ISC
|
package/dist/index.d.mts
CHANGED
|
@@ -1,38 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import { PolarOptions, PolarProvider } from './polar-provider.mjs';
|
|
2
|
+
import '@paykit-sdk/core';
|
|
3
|
+
import '@polar-sh/sdk';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
declare class PolarProvider implements PayKitProvider {
|
|
7
|
-
private config;
|
|
8
|
-
private polar;
|
|
9
|
-
private readonly productionURL;
|
|
10
|
-
private readonly sandboxURL;
|
|
11
|
-
constructor(config: PolarConfig);
|
|
12
|
-
/**
|
|
13
|
-
* Checkout management
|
|
14
|
-
*/
|
|
15
|
-
createCheckout: (params: CreateCheckoutParams) => Promise<Checkout>;
|
|
16
|
-
retrieveCheckout: (id: string) => Promise<Checkout>;
|
|
17
|
-
/**
|
|
18
|
-
* Customer management
|
|
19
|
-
*/
|
|
20
|
-
createCustomer: (params: CreateCustomerParams) => Promise<Customer>;
|
|
21
|
-
updateCustomer: (id: string, params: UpdateCustomerParams) => Promise<Customer>;
|
|
22
|
-
retrieveCustomer: (id: string) => Promise<Customer>;
|
|
23
|
-
/**
|
|
24
|
-
* Subscription management
|
|
25
|
-
*/
|
|
26
|
-
cancelSubscription: (id: string) => Promise<null>;
|
|
27
|
-
retrieveSubscription: (id: string) => Promise<Subscription>;
|
|
28
|
-
updateSubscription: (id: string, params: UpdateSubscriptionParams) => Promise<Subscription>;
|
|
29
|
-
/**
|
|
30
|
-
* Webhook management
|
|
31
|
-
*/
|
|
32
|
-
handleWebhook: (params: HandleWebhookParams) => Promise<WebhookEventPayload>;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
declare const createPolar: (config: PolarConfig) => PolarProvider;
|
|
5
|
+
declare const createPolar: (config: PolarOptions) => PolarProvider;
|
|
36
6
|
declare const polar: () => PolarProvider;
|
|
37
7
|
|
|
38
8
|
export { createPolar, polar };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,38 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import { PolarOptions, PolarProvider } from './polar-provider.js';
|
|
2
|
+
import '@paykit-sdk/core';
|
|
3
|
+
import '@polar-sh/sdk';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
declare class PolarProvider implements PayKitProvider {
|
|
7
|
-
private config;
|
|
8
|
-
private polar;
|
|
9
|
-
private readonly productionURL;
|
|
10
|
-
private readonly sandboxURL;
|
|
11
|
-
constructor(config: PolarConfig);
|
|
12
|
-
/**
|
|
13
|
-
* Checkout management
|
|
14
|
-
*/
|
|
15
|
-
createCheckout: (params: CreateCheckoutParams) => Promise<Checkout>;
|
|
16
|
-
retrieveCheckout: (id: string) => Promise<Checkout>;
|
|
17
|
-
/**
|
|
18
|
-
* Customer management
|
|
19
|
-
*/
|
|
20
|
-
createCustomer: (params: CreateCustomerParams) => Promise<Customer>;
|
|
21
|
-
updateCustomer: (id: string, params: UpdateCustomerParams) => Promise<Customer>;
|
|
22
|
-
retrieveCustomer: (id: string) => Promise<Customer>;
|
|
23
|
-
/**
|
|
24
|
-
* Subscription management
|
|
25
|
-
*/
|
|
26
|
-
cancelSubscription: (id: string) => Promise<null>;
|
|
27
|
-
retrieveSubscription: (id: string) => Promise<Subscription>;
|
|
28
|
-
updateSubscription: (id: string, params: UpdateSubscriptionParams) => Promise<Subscription>;
|
|
29
|
-
/**
|
|
30
|
-
* Webhook management
|
|
31
|
-
*/
|
|
32
|
-
handleWebhook: (params: HandleWebhookParams) => Promise<WebhookEventPayload>;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
declare const createPolar: (config: PolarConfig) => PolarProvider;
|
|
5
|
+
declare const createPolar: (config: PolarOptions) => PolarProvider;
|
|
36
6
|
declare const polar: () => PolarProvider;
|
|
37
7
|
|
|
38
8
|
export { createPolar, polar };
|