@paykit-sdk/polar 1.1.91 → 1.1.92
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 +117 -108
- package/dist/index.d.mts +4 -34
- package/dist/index.d.ts +4 -34
- package/dist/index.js +4397 -101
- package/dist/index.mjs +4394 -80
- package/dist/polar-provider.d.mts +52 -0
- package/dist/polar-provider.d.ts +52 -0
- package/dist/polar-provider.js +4525 -0
- package/dist/polar-provider.mjs +4523 -0
- package/package.json +7 -6
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,176 @@ 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(request: NextRequest, { params }: { params: { endpoint: string[] } }) {
|
|
31
|
+
try {
|
|
32
|
+
// Construct the endpoint path with full type safety
|
|
33
|
+
const endpoint = ('/' + params.endpoint.join('/')) as EndpointPath;
|
|
34
|
+
const handler = endpoints[endpoint];
|
|
35
|
+
|
|
36
|
+
if (!handler) {
|
|
37
|
+
return NextResponse.json({ message: 'Endpoint not found' }, { status: 404 });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Parse request body
|
|
41
|
+
const body = await request.json();
|
|
42
|
+
const { args } = body;
|
|
43
|
+
|
|
44
|
+
const result = await handler(...args);
|
|
45
|
+
|
|
46
|
+
return NextResponse.json({ result });
|
|
47
|
+
} catch (error) {
|
|
48
|
+
console.error('PayKit API Error:', error);
|
|
49
|
+
return NextResponse.json(
|
|
50
|
+
{ message: error instanceof Error ? error.message : 'Internal server error' },
|
|
51
|
+
{ status: 500 },
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
```
|
|
44
56
|
|
|
45
|
-
### Next.js
|
|
57
|
+
### Next.js Webhooks (/api/paykit/webhooks/route.ts)
|
|
46
58
|
|
|
47
59
|
```typescript
|
|
48
60
|
import { paykit } from '@/lib/paykit';
|
|
49
61
|
import { NextRequest, NextResponse } from 'next/server';
|
|
50
62
|
|
|
51
63
|
export async function POST(request: NextRequest) {
|
|
52
|
-
|
|
64
|
+
const webhookSecret = process.env.POLAR_WEBHOOK_SECRET;
|
|
65
|
+
|
|
66
|
+
if (!webhookSecret) {
|
|
67
|
+
return NextResponse.json({ error: 'Webhook secret not configured' }, { status: 500 });
|
|
68
|
+
}
|
|
53
69
|
|
|
54
70
|
const webhook = paykit.webhooks
|
|
55
|
-
.setup({ webhookSecret
|
|
56
|
-
.on('
|
|
57
|
-
console.log('Checkout created:', event.data);
|
|
58
|
-
})
|
|
59
|
-
.on('$customerCreated', async event => {
|
|
71
|
+
.setup({ webhookSecret })
|
|
72
|
+
.on('customer.created', async event => {
|
|
60
73
|
console.log('Customer created:', event.data);
|
|
61
74
|
})
|
|
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 => {
|
|
75
|
+
.on('subscription.created', async event => {
|
|
69
76
|
console.log('Subscription created:', event.data);
|
|
70
77
|
})
|
|
71
|
-
.on('
|
|
72
|
-
console.log('
|
|
73
|
-
})
|
|
74
|
-
.on('$subscriptionCancelled', async event => {
|
|
75
|
-
console.log('Subscription cancelled:', event.data);
|
|
78
|
+
.on('payment.created', async event => {
|
|
79
|
+
console.log('Payment created:', event.data);
|
|
76
80
|
})
|
|
77
|
-
.on('
|
|
78
|
-
console.log('
|
|
81
|
+
.on('refund.created', async event => {
|
|
82
|
+
console.log('Refund created:', event.data);
|
|
79
83
|
});
|
|
80
84
|
|
|
81
|
-
const headers = Object.fromEntries(request.headers.entries());
|
|
82
85
|
const body = await request.text();
|
|
83
|
-
|
|
86
|
+
const headers = Object.fromEntries(request.headers.entries());
|
|
87
|
+
await webhook.handle({ body, headers, fullUrl: request.url });
|
|
84
88
|
|
|
89
|
+
// Return immediately, processing happens in background
|
|
85
90
|
return NextResponse.json({ success: true });
|
|
86
91
|
}
|
|
87
92
|
```
|
|
88
93
|
|
|
89
|
-
### Express.js
|
|
94
|
+
### Express.js with Webhook Handler
|
|
90
95
|
|
|
91
96
|
```typescript
|
|
92
|
-
import { paykit } from '@/lib/paykit';
|
|
97
|
+
import { endpoints, paykit } from '@/lib/paykit';
|
|
98
|
+
import { EndpointPath } from '@paykit-sdk/core';
|
|
93
99
|
import express from 'express';
|
|
94
100
|
|
|
95
101
|
const app = express();
|
|
96
|
-
app.use(express.raw({ type: 'application/json' }));
|
|
97
102
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
103
|
+
// IMPORTANT: Webhook route must come BEFORE express.json() middleware
|
|
104
|
+
// This ensures we get the raw body for signature verification
|
|
105
|
+
app.post('/api/webhooks/polar', express.raw({ type: 'application/json' }), async (req, res) => {
|
|
106
|
+
try {
|
|
107
|
+
const webhookSecret = process.env.POLAR_WEBHOOK_SECRET;
|
|
108
|
+
|
|
109
|
+
if (!webhookSecret) {
|
|
110
|
+
return res.status(500).json({ error: 'Webhook secret not configured' });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const webhook = paykit.webhooks
|
|
114
|
+
.setup({ webhookSecret })
|
|
115
|
+
.on('customer.created', async event => {
|
|
116
|
+
console.log('Customer created:', event.data);
|
|
117
|
+
})
|
|
118
|
+
.on('subscription.created', async event => {
|
|
119
|
+
console.log('Subscription created:', event.data);
|
|
120
|
+
})
|
|
121
|
+
.on('payment.created', async event => {
|
|
122
|
+
console.log('Payment created:', event.data);
|
|
123
|
+
})
|
|
124
|
+
.on('refund.created', async event => {
|
|
125
|
+
console.log('Refund created:', event.data);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const body = req.body; // Raw buffer from express.raw()
|
|
129
|
+
const headers = req.headers;
|
|
130
|
+
await webhook.handle({ body, headers });
|
|
131
|
+
|
|
132
|
+
// Return immediately, processing happens in background
|
|
133
|
+
res.json({ success: true });
|
|
134
|
+
} catch (error) {
|
|
135
|
+
console.error('Webhook error:', error);
|
|
136
|
+
res.status(500).json({
|
|
137
|
+
message: error instanceof Error ? error.message : 'Webhook processing failed',
|
|
111
138
|
});
|
|
112
|
-
|
|
113
|
-
const headers = req.headers;
|
|
114
|
-
const body = req.body;
|
|
115
|
-
await webhook.handle({ body, headers });
|
|
116
|
-
|
|
117
|
-
res.json({ success: true });
|
|
139
|
+
}
|
|
118
140
|
});
|
|
119
|
-
```
|
|
120
141
|
|
|
121
|
-
|
|
142
|
+
// Regular API routes use JSON middleware
|
|
143
|
+
app.use(express.json());
|
|
122
144
|
|
|
123
|
-
|
|
124
|
-
|
|
145
|
+
app.post('/api/paykit/*', async (req, res) => {
|
|
146
|
+
try {
|
|
147
|
+
const endpoint = req.path.replace('/api/paykit', '') as EndpointPath;
|
|
148
|
+
const handler = endpoints[endpoint];
|
|
125
149
|
|
|
126
|
-
|
|
127
|
-
|
|
150
|
+
if (!handler) {
|
|
151
|
+
return res.status(404).json({ message: 'Endpoint not found' });
|
|
152
|
+
}
|
|
128
153
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
.on('$checkoutCreated', async event => {
|
|
132
|
-
console.log('Checkout created:', event.data);
|
|
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);
|
|
139
|
-
});
|
|
154
|
+
const { args } = req.body;
|
|
155
|
+
const result = await handler(...args);
|
|
140
156
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
157
|
+
res.json({ result });
|
|
158
|
+
} catch (error) {
|
|
159
|
+
res.status(500).json({
|
|
160
|
+
message: error instanceof Error ? error.message : 'Internal server error',
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
});
|
|
144
164
|
|
|
145
|
-
|
|
165
|
+
app.listen(3000, () => {
|
|
166
|
+
console.log('Server running on port 3000');
|
|
146
167
|
});
|
|
147
168
|
```
|
|
148
169
|
|
|
149
|
-
## Subscription
|
|
150
|
-
|
|
151
|
-
Polar requires specific update types:
|
|
170
|
+
## Subscription Management
|
|
152
171
|
|
|
153
172
|
```typescript
|
|
154
|
-
//
|
|
173
|
+
// Update subscription
|
|
155
174
|
await paykit.subscriptions.update('sub_123', {
|
|
156
|
-
metadata: {
|
|
175
|
+
metadata: { plan: 'enterprise' },
|
|
157
176
|
});
|
|
158
177
|
|
|
159
|
-
// Cancel
|
|
160
|
-
await paykit.subscriptions.
|
|
161
|
-
metadata: { cancelAtPeriodEnd: 'true' },
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
// Cancel with reason
|
|
165
|
-
await paykit.subscriptions.update('sub_123', {
|
|
166
|
-
metadata: {
|
|
167
|
-
cancelAtPeriodEnd: 'true',
|
|
168
|
-
customerCancellationReason: 'too_expensive',
|
|
169
|
-
},
|
|
170
|
-
});
|
|
178
|
+
// Cancel subscription
|
|
179
|
+
await paykit.subscriptions.cancel('sub_123');
|
|
171
180
|
```
|
|
172
181
|
|
|
173
182
|
## Environment Variables
|
|
174
183
|
|
|
175
184
|
```bash
|
|
176
|
-
POLAR_ACCESS_TOKEN=
|
|
177
|
-
POLAR_WEBHOOK_SECRET=
|
|
185
|
+
POLAR_ACCESS_TOKEN=polar_at_...
|
|
186
|
+
POLAR_WEBHOOK_SECRET=polar_wh_...
|
|
178
187
|
```
|
|
179
188
|
|
|
180
189
|
## Support
|
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 };
|