@paykit-sdk/polar 1.1.0 → 1.1.1-alpha.2
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 +187 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +44 -35
- package/dist/index.mjs +44 -35
- package/package.json +9 -8
package/README.md
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
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(); // Ensure POLAR_ACCESS_TOKEN environment variable is set
|
|
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('$invoicePaid', async event => {
|
|
39
|
+
console.log('Payment received:', event.data);
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Webhook Implementation
|
|
44
|
+
|
|
45
|
+
### Next.js API Route
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { paykit } from '@/lib/paykit';
|
|
49
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
50
|
+
|
|
51
|
+
export async function POST(request: NextRequest) {
|
|
52
|
+
console.log('Polar webhook received');
|
|
53
|
+
|
|
54
|
+
const webhook = paykit.webhooks
|
|
55
|
+
.setup({ webhookSecret: process.env.POLAR_WEBHOOK_SECRET! })
|
|
56
|
+
.on('$checkoutCreated', async event => {
|
|
57
|
+
console.log('Checkout created:', event.data);
|
|
58
|
+
})
|
|
59
|
+
.on('$customerCreated', async event => {
|
|
60
|
+
console.log('Customer created:', event.data);
|
|
61
|
+
})
|
|
62
|
+
.on('$customerUpdated', async event => {
|
|
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 => {
|
|
69
|
+
console.log('Subscription created:', event.data);
|
|
70
|
+
})
|
|
71
|
+
.on('$subscriptionUpdated', async event => {
|
|
72
|
+
console.log('Subscription updated:', event.data);
|
|
73
|
+
})
|
|
74
|
+
.on('$subscriptionCancelled', async event => {
|
|
75
|
+
console.log('Subscription cancelled:', event.data);
|
|
76
|
+
})
|
|
77
|
+
.on('$invoicePaid', async event => {
|
|
78
|
+
console.log('Payment received:', event.data);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const headers = Object.fromEntries(request.headers.entries());
|
|
82
|
+
const body = await request.text();
|
|
83
|
+
await webhook.handle({ body, headers });
|
|
84
|
+
|
|
85
|
+
return NextResponse.json({ success: true });
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Express.js Route
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import { paykit } from '@/lib/paykit';
|
|
93
|
+
import express from 'express';
|
|
94
|
+
|
|
95
|
+
const app = express();
|
|
96
|
+
app.use(express.raw({ type: 'application/json' }));
|
|
97
|
+
|
|
98
|
+
app.post('/api/webhooks/polar', async (req, res) => {
|
|
99
|
+
console.log('Polar webhook received');
|
|
100
|
+
|
|
101
|
+
const webhook = paykit.webhooks
|
|
102
|
+
.setup({ webhookSecret: process.env.POLAR_WEBHOOK_SECRET! })
|
|
103
|
+
.on('$checkoutCreated', async event => {
|
|
104
|
+
console.log('Checkout created:', event.data);
|
|
105
|
+
})
|
|
106
|
+
.on('$customerCreated', async event => {
|
|
107
|
+
console.log('Customer created:', event.data);
|
|
108
|
+
})
|
|
109
|
+
.on('$invoicePaid', async event => {
|
|
110
|
+
console.log('Payment received:', event.data);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const headers = req.headers;
|
|
114
|
+
const body = req.body;
|
|
115
|
+
await webhook.handle({ body, headers });
|
|
116
|
+
|
|
117
|
+
res.json({ success: true });
|
|
118
|
+
});
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Vite.js/Nuxt.js
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
import { paykit } from '@/lib/paykit';
|
|
125
|
+
|
|
126
|
+
export default defineEventHandler(async event => {
|
|
127
|
+
console.log('Polar webhook received');
|
|
128
|
+
|
|
129
|
+
const webhook = paykit.webhooks
|
|
130
|
+
.setup({ webhookSecret: process.env.POLAR_WEBHOOK_SECRET! })
|
|
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
|
+
});
|
|
140
|
+
|
|
141
|
+
const headers = getHeaders(event);
|
|
142
|
+
const body = await readBody(event);
|
|
143
|
+
await webhook.handle({ body, headers });
|
|
144
|
+
|
|
145
|
+
return { success: true };
|
|
146
|
+
});
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Subscription Updates
|
|
150
|
+
|
|
151
|
+
Polar requires specific update types:
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
// Change product
|
|
155
|
+
await paykit.subscriptions.update('sub_123', {
|
|
156
|
+
metadata: { productId: 'new-product-id' },
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// Cancel at period end
|
|
160
|
+
await paykit.subscriptions.update('sub_123', {
|
|
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
|
+
});
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## Environment Variables
|
|
174
|
+
|
|
175
|
+
```bash
|
|
176
|
+
POLAR_ACCESS_TOKEN=polar_oat_...
|
|
177
|
+
POLAR_WEBHOOK_SECRET=your-webhook-secret
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Support
|
|
181
|
+
|
|
182
|
+
- [Polar Documentation](https://docs.polar.sh/)
|
|
183
|
+
- [PayKit Issues](https://github.com/devodii/paykit/issues)
|
|
184
|
+
|
|
185
|
+
## License
|
|
186
|
+
|
|
187
|
+
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
|
@@ -47,15 +47,31 @@ var toPaykitCheckout = (checkout) => {
|
|
|
47
47
|
var toPaykitCustomer = (customer) => {
|
|
48
48
|
return { id: customer.id, email: customer.email, name: customer.name ?? void 0 };
|
|
49
49
|
};
|
|
50
|
+
var toPaykitSubscriptionStatus = (status) => {
|
|
51
|
+
if (status === "active") return "active";
|
|
52
|
+
if (status === "past_due" || status === "incomplete") return "past_due";
|
|
53
|
+
if (status === "canceled" || status === "unpaid") return "canceled";
|
|
54
|
+
if (status === "incomplete_expired") return "expired";
|
|
55
|
+
throw new Error(`Unhandled status: ${status}`);
|
|
56
|
+
};
|
|
50
57
|
var toPaykitSubscription = (subscription) => {
|
|
51
58
|
return {
|
|
52
59
|
id: subscription.id,
|
|
53
60
|
customer_id: subscription.customerId,
|
|
54
|
-
status:
|
|
61
|
+
status: toPaykitSubscriptionStatus(subscription.status),
|
|
55
62
|
current_period_start: new Date(subscription.currentPeriodStart),
|
|
56
63
|
current_period_end: new Date(subscription.currentPeriodEnd)
|
|
57
64
|
};
|
|
58
65
|
};
|
|
66
|
+
var toPaykitInvoice = (invoice) => {
|
|
67
|
+
return {
|
|
68
|
+
id: invoice.id,
|
|
69
|
+
amount: invoice.totalAmount,
|
|
70
|
+
currency: invoice.currency,
|
|
71
|
+
metadata: (0, import_core.stringifyObjectValues)(invoice.metadata ?? {}),
|
|
72
|
+
customer_id: invoice.customerId
|
|
73
|
+
};
|
|
74
|
+
};
|
|
59
75
|
|
|
60
76
|
// src/polar-provider.ts
|
|
61
77
|
var PolarProvider = class {
|
|
@@ -68,11 +84,7 @@ var PolarProvider = class {
|
|
|
68
84
|
*/
|
|
69
85
|
this.createCheckout = async (params) => {
|
|
70
86
|
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
|
-
});
|
|
87
|
+
const response = await this.polar.checkouts.create({ metadata, products: [item_id], ...provider_metadata });
|
|
76
88
|
return toPaykitCheckout(response);
|
|
77
89
|
};
|
|
78
90
|
this.retrieveCheckout = async (id) => {
|
|
@@ -89,7 +101,10 @@ var PolarProvider = class {
|
|
|
89
101
|
};
|
|
90
102
|
this.updateCustomer = async (id, params) => {
|
|
91
103
|
const { email, name, metadata } = params;
|
|
92
|
-
const response = await this.polar.customers.update({
|
|
104
|
+
const response = await this.polar.customers.update({
|
|
105
|
+
id,
|
|
106
|
+
customerUpdate: { ...email && { email }, ...name && { name }, ...metadata && { metadata } }
|
|
107
|
+
});
|
|
93
108
|
return toPaykitCustomer(response);
|
|
94
109
|
};
|
|
95
110
|
this.retrieveCustomer = async (id) => {
|
|
@@ -100,16 +115,16 @@ var PolarProvider = class {
|
|
|
100
115
|
* Subscription management
|
|
101
116
|
*/
|
|
102
117
|
this.cancelSubscription = async (id) => {
|
|
103
|
-
|
|
104
|
-
return
|
|
118
|
+
await this.polar.subscriptions.revoke({ id });
|
|
119
|
+
return null;
|
|
105
120
|
};
|
|
106
121
|
this.retrieveSubscription = async (id) => {
|
|
107
122
|
const response = await this.polar.subscriptions.get({ id });
|
|
108
123
|
return toPaykitSubscription(response);
|
|
109
124
|
};
|
|
110
125
|
this.updateSubscription = async (id, params) => {
|
|
111
|
-
const
|
|
112
|
-
return
|
|
126
|
+
const response = await this.polar.subscriptions.update({ id, subscriptionUpdate: { ...params.metadata } });
|
|
127
|
+
return toPaykitSubscription(response);
|
|
113
128
|
};
|
|
114
129
|
/**
|
|
115
130
|
* Webhook management
|
|
@@ -123,34 +138,28 @@ var PolarProvider = class {
|
|
|
123
138
|
},
|
|
124
139
|
{}
|
|
125
140
|
);
|
|
126
|
-
|
|
141
|
+
console.log({ headers, webhookHeaders, webhookSecret, body });
|
|
142
|
+
const { data, type } = (0, import_webhooks.validateEvent)(body, webhookHeaders, webhookSecret);
|
|
127
143
|
const id = webhookHeaders["webhook-id"];
|
|
128
144
|
const timestamp = webhookHeaders["webhook-timestamp"];
|
|
129
|
-
if (
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
return (0, import_core2.toPaykitEvent)({ type: "$
|
|
135
|
-
} else if (
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
return (0, import_core2.toPaykitEvent)({ type: "$customerCreated", created: parseInt(timestamp), id, data: customer });
|
|
141
|
-
} else if (webhookEvent.type === "customer.updated") {
|
|
142
|
-
const customer = await this.retrieveCustomer(webhookEvent.data.id);
|
|
143
|
-
return (0, import_core2.toPaykitEvent)({ type: "$customerUpdated", created: parseInt(timestamp), id, data: customer });
|
|
144
|
-
} else if (webhookEvent.type === "customer.deleted") {
|
|
145
|
+
if (type === "subscription.updated") {
|
|
146
|
+
return (0, import_core2.toPaykitEvent)({ type: "$subscriptionUpdated", created: parseInt(timestamp), id, data: toPaykitSubscription(data) });
|
|
147
|
+
} else if (type === "subscription.created") {
|
|
148
|
+
return (0, import_core2.toPaykitEvent)({ type: "$subscriptionCreated", created: parseInt(timestamp), id, data: toPaykitSubscription(data) });
|
|
149
|
+
} else if (type === "subscription.revoked") {
|
|
150
|
+
return (0, import_core2.toPaykitEvent)({ type: "$subscriptionCancelled", created: parseInt(timestamp), id, data: toPaykitSubscription(data) });
|
|
151
|
+
} else if (type === "customer.created") {
|
|
152
|
+
return (0, import_core2.toPaykitEvent)({ type: "$customerCreated", created: parseInt(timestamp), id, data: toPaykitCustomer(data) });
|
|
153
|
+
} else if (type === "customer.updated") {
|
|
154
|
+
return (0, import_core2.toPaykitEvent)({ type: "$customerUpdated", created: parseInt(timestamp), id, data: toPaykitCustomer(data) });
|
|
155
|
+
} else if (type === "customer.deleted") {
|
|
145
156
|
return (0, import_core2.toPaykitEvent)({ type: "$customerDeleted", created: parseInt(timestamp), id, data: null });
|
|
146
|
-
} else if (
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
const checkout = await this.retrieveCheckout(webhookEvent.data.id);
|
|
151
|
-
return (0, import_core2.toPaykitEvent)({ type: "$paymentReceived", created: parseInt(timestamp), id, data: checkout });
|
|
157
|
+
} else if (type === "checkout.created") {
|
|
158
|
+
return (0, import_core2.toPaykitEvent)({ type: "$checkoutCreated", created: parseInt(timestamp), id, data: toPaykitCheckout(data) });
|
|
159
|
+
} else if (type === "order.created") {
|
|
160
|
+
return (0, import_core2.toPaykitEvent)({ type: "$invoicePaid", created: parseInt(timestamp), id, data: toPaykitInvoice(data) });
|
|
152
161
|
}
|
|
153
|
-
throw new Error(`
|
|
162
|
+
throw new Error(`Unhandled event type: ${type}`);
|
|
154
163
|
};
|
|
155
164
|
const { accessToken, server, ...rest } = config;
|
|
156
165
|
this.polar = new import_sdk.Polar({ accessToken, serverURL: server === "sandbox" ? this.sandboxURL : this.productionURL, ...rest });
|
package/dist/index.mjs
CHANGED
|
@@ -8,7 +8,7 @@ import { validateEvent } from "@polar-sh/sdk/webhooks";
|
|
|
8
8
|
|
|
9
9
|
// lib/mapper.ts
|
|
10
10
|
import {
|
|
11
|
-
|
|
11
|
+
stringifyObjectValues
|
|
12
12
|
} from "@paykit-sdk/core";
|
|
13
13
|
var toPaykitCheckout = (checkout) => {
|
|
14
14
|
return {
|
|
@@ -25,6 +25,13 @@ var toPaykitCheckout = (checkout) => {
|
|
|
25
25
|
var toPaykitCustomer = (customer) => {
|
|
26
26
|
return { id: customer.id, email: customer.email, name: customer.name ?? void 0 };
|
|
27
27
|
};
|
|
28
|
+
var toPaykitSubscriptionStatus = (status) => {
|
|
29
|
+
if (status === "active") return "active";
|
|
30
|
+
if (status === "past_due" || status === "incomplete") return "past_due";
|
|
31
|
+
if (status === "canceled" || status === "unpaid") return "canceled";
|
|
32
|
+
if (status === "incomplete_expired") return "expired";
|
|
33
|
+
throw new Error(`Unhandled status: ${status}`);
|
|
34
|
+
};
|
|
28
35
|
var toPaykitSubscription = (subscription) => {
|
|
29
36
|
return {
|
|
30
37
|
id: subscription.id,
|
|
@@ -34,6 +41,15 @@ var toPaykitSubscription = (subscription) => {
|
|
|
34
41
|
current_period_end: new Date(subscription.currentPeriodEnd)
|
|
35
42
|
};
|
|
36
43
|
};
|
|
44
|
+
var toPaykitInvoice = (invoice) => {
|
|
45
|
+
return {
|
|
46
|
+
id: invoice.id,
|
|
47
|
+
amount: invoice.totalAmount,
|
|
48
|
+
currency: invoice.currency,
|
|
49
|
+
metadata: stringifyObjectValues(invoice.metadata ?? {}),
|
|
50
|
+
customer_id: invoice.customerId
|
|
51
|
+
};
|
|
52
|
+
};
|
|
37
53
|
|
|
38
54
|
// src/polar-provider.ts
|
|
39
55
|
var PolarProvider = class {
|
|
@@ -46,11 +62,7 @@ var PolarProvider = class {
|
|
|
46
62
|
*/
|
|
47
63
|
this.createCheckout = async (params) => {
|
|
48
64
|
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
|
-
});
|
|
65
|
+
const response = await this.polar.checkouts.create({ metadata, products: [item_id], ...provider_metadata });
|
|
54
66
|
return toPaykitCheckout(response);
|
|
55
67
|
};
|
|
56
68
|
this.retrieveCheckout = async (id) => {
|
|
@@ -67,7 +79,10 @@ var PolarProvider = class {
|
|
|
67
79
|
};
|
|
68
80
|
this.updateCustomer = async (id, params) => {
|
|
69
81
|
const { email, name, metadata } = params;
|
|
70
|
-
const response = await this.polar.customers.update({
|
|
82
|
+
const response = await this.polar.customers.update({
|
|
83
|
+
id,
|
|
84
|
+
customerUpdate: { ...email && { email }, ...name && { name }, ...metadata && { metadata } }
|
|
85
|
+
});
|
|
71
86
|
return toPaykitCustomer(response);
|
|
72
87
|
};
|
|
73
88
|
this.retrieveCustomer = async (id) => {
|
|
@@ -78,16 +93,16 @@ var PolarProvider = class {
|
|
|
78
93
|
* Subscription management
|
|
79
94
|
*/
|
|
80
95
|
this.cancelSubscription = async (id) => {
|
|
81
|
-
|
|
82
|
-
return
|
|
96
|
+
await this.polar.subscriptions.revoke({ id });
|
|
97
|
+
return null;
|
|
83
98
|
};
|
|
84
99
|
this.retrieveSubscription = async (id) => {
|
|
85
100
|
const response = await this.polar.subscriptions.get({ id });
|
|
86
101
|
return toPaykitSubscription(response);
|
|
87
102
|
};
|
|
88
103
|
this.updateSubscription = async (id, params) => {
|
|
89
|
-
const
|
|
90
|
-
return
|
|
104
|
+
const response = await this.polar.subscriptions.update({ id, subscriptionUpdate: { ...params.metadata } });
|
|
105
|
+
return toPaykitSubscription(response);
|
|
91
106
|
};
|
|
92
107
|
/**
|
|
93
108
|
* Webhook management
|
|
@@ -101,34 +116,28 @@ var PolarProvider = class {
|
|
|
101
116
|
},
|
|
102
117
|
{}
|
|
103
118
|
);
|
|
104
|
-
|
|
119
|
+
console.log({ headers, webhookHeaders, webhookSecret, body });
|
|
120
|
+
const { data, type } = validateEvent(body, webhookHeaders, webhookSecret);
|
|
105
121
|
const id = webhookHeaders["webhook-id"];
|
|
106
122
|
const timestamp = webhookHeaders["webhook-timestamp"];
|
|
107
|
-
if (
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return toPaykitEvent({ type: "$
|
|
113
|
-
} else if (
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
return toPaykitEvent({ type: "$customerCreated", 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: "$customerUpdated", created: parseInt(timestamp), id, data: customer });
|
|
122
|
-
} else if (webhookEvent.type === "customer.deleted") {
|
|
123
|
+
if (type === "subscription.updated") {
|
|
124
|
+
return toPaykitEvent({ type: "$subscriptionUpdated", created: parseInt(timestamp), id, data: toPaykitSubscription(data) });
|
|
125
|
+
} else if (type === "subscription.created") {
|
|
126
|
+
return toPaykitEvent({ type: "$subscriptionCreated", created: parseInt(timestamp), id, data: toPaykitSubscription(data) });
|
|
127
|
+
} else if (type === "subscription.revoked") {
|
|
128
|
+
return toPaykitEvent({ type: "$subscriptionCancelled", created: parseInt(timestamp), id, data: toPaykitSubscription(data) });
|
|
129
|
+
} else if (type === "customer.created") {
|
|
130
|
+
return toPaykitEvent({ type: "$customerCreated", created: parseInt(timestamp), id, data: toPaykitCustomer(data) });
|
|
131
|
+
} else if (type === "customer.updated") {
|
|
132
|
+
return toPaykitEvent({ type: "$customerUpdated", created: parseInt(timestamp), id, data: toPaykitCustomer(data) });
|
|
133
|
+
} else if (type === "customer.deleted") {
|
|
123
134
|
return toPaykitEvent({ type: "$customerDeleted", created: parseInt(timestamp), id, data: null });
|
|
124
|
-
} else if (
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const checkout = await this.retrieveCheckout(webhookEvent.data.id);
|
|
129
|
-
return toPaykitEvent({ type: "$paymentReceived", created: parseInt(timestamp), id, data: checkout });
|
|
135
|
+
} else if (type === "checkout.created") {
|
|
136
|
+
return toPaykitEvent({ type: "$checkoutCreated", created: parseInt(timestamp), id, data: toPaykitCheckout(data) });
|
|
137
|
+
} else if (type === "order.created") {
|
|
138
|
+
return toPaykitEvent({ type: "$invoicePaid", created: parseInt(timestamp), id, data: toPaykitInvoice(data) });
|
|
130
139
|
}
|
|
131
|
-
throw new Error(`
|
|
140
|
+
throw new Error(`Unhandled event type: ${type}`);
|
|
132
141
|
};
|
|
133
142
|
const { accessToken, server, ...rest } = config;
|
|
134
143
|
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-alpha.2",
|
|
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",
|
|
@@ -20,12 +24,12 @@
|
|
|
20
24
|
"@polar-sh/sdk": "^0.33.0"
|
|
21
25
|
},
|
|
22
26
|
"peerDependencies": {
|
|
23
|
-
"@paykit-sdk/core": "^1.1.
|
|
27
|
+
"@paykit-sdk/core": "^1.1.1"
|
|
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
|
+
}
|