@paykit-sdk/polar 1.0.1 → 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 +5 -7
- package/dist/index.d.ts +5 -7
- package/dist/index.js +64 -30
- package/dist/index.mjs +66 -31
- package/package.json +5 -5
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
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { PayKitProvider } from '@paykit-sdk/core/src/paykit-provider';
|
|
3
|
-
import { CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, WebhookEventPayload } from '@paykit-sdk/core/src/resources';
|
|
1
|
+
import { PaykitProviderOptions, PayKitProvider, CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, $ExtWebhookHandlerConfig, WebhookEventPayload } from '@paykit-sdk/core';
|
|
4
2
|
import { SDKOptions } from '@polar-sh/sdk';
|
|
5
3
|
|
|
6
|
-
interface PolarConfig extends
|
|
4
|
+
interface PolarConfig extends PaykitProviderOptions<SDKOptions> {
|
|
7
5
|
}
|
|
8
6
|
declare class PolarProvider implements PayKitProvider {
|
|
9
7
|
private config;
|
|
@@ -25,16 +23,16 @@ declare class PolarProvider implements PayKitProvider {
|
|
|
25
23
|
/**
|
|
26
24
|
* Subscription management
|
|
27
25
|
*/
|
|
28
|
-
cancelSubscription: (id: string) => Promise<
|
|
26
|
+
cancelSubscription: (id: string) => Promise<null>;
|
|
29
27
|
retrieveSubscription: (id: string) => Promise<Subscription>;
|
|
30
28
|
updateSubscription: (id: string, params: UpdateSubscriptionParams) => Promise<Subscription>;
|
|
31
29
|
/**
|
|
32
30
|
* Webhook management
|
|
33
31
|
*/
|
|
34
|
-
handleWebhook: (
|
|
32
|
+
handleWebhook: (params: $ExtWebhookHandlerConfig) => Promise<WebhookEventPayload>;
|
|
35
33
|
}
|
|
36
34
|
|
|
37
|
-
declare const createPolar: (config:
|
|
35
|
+
declare const createPolar: (config: PolarConfig) => PolarProvider;
|
|
38
36
|
declare const polar: () => PolarProvider;
|
|
39
37
|
|
|
40
38
|
export { createPolar, polar };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { PayKitProvider } from '@paykit-sdk/core/src/paykit-provider';
|
|
3
|
-
import { CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, WebhookEventPayload } from '@paykit-sdk/core/src/resources';
|
|
1
|
+
import { PaykitProviderOptions, PayKitProvider, CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, $ExtWebhookHandlerConfig, WebhookEventPayload } from '@paykit-sdk/core';
|
|
4
2
|
import { SDKOptions } from '@polar-sh/sdk';
|
|
5
3
|
|
|
6
|
-
interface PolarConfig extends
|
|
4
|
+
interface PolarConfig extends PaykitProviderOptions<SDKOptions> {
|
|
7
5
|
}
|
|
8
6
|
declare class PolarProvider implements PayKitProvider {
|
|
9
7
|
private config;
|
|
@@ -25,16 +23,16 @@ declare class PolarProvider implements PayKitProvider {
|
|
|
25
23
|
/**
|
|
26
24
|
* Subscription management
|
|
27
25
|
*/
|
|
28
|
-
cancelSubscription: (id: string) => Promise<
|
|
26
|
+
cancelSubscription: (id: string) => Promise<null>;
|
|
29
27
|
retrieveSubscription: (id: string) => Promise<Subscription>;
|
|
30
28
|
updateSubscription: (id: string, params: UpdateSubscriptionParams) => Promise<Subscription>;
|
|
31
29
|
/**
|
|
32
30
|
* Webhook management
|
|
33
31
|
*/
|
|
34
|
-
handleWebhook: (
|
|
32
|
+
handleWebhook: (params: $ExtWebhookHandlerConfig) => Promise<WebhookEventPayload>;
|
|
35
33
|
}
|
|
36
34
|
|
|
37
|
-
declare const createPolar: (config:
|
|
35
|
+
declare const createPolar: (config: PolarConfig) => PolarProvider;
|
|
38
36
|
declare const polar: () => PolarProvider;
|
|
39
37
|
|
|
40
38
|
export { createPolar, polar };
|
package/dist/index.js
CHANGED
|
@@ -26,21 +26,22 @@ __export(index_exports, {
|
|
|
26
26
|
module.exports = __toCommonJS(index_exports);
|
|
27
27
|
|
|
28
28
|
// src/polar-provider.ts
|
|
29
|
-
var
|
|
29
|
+
var import_core2 = require("@paykit-sdk/core");
|
|
30
30
|
var import_sdk = require("@polar-sh/sdk");
|
|
31
|
+
var import_webhooks = require("@polar-sh/sdk/webhooks");
|
|
31
32
|
|
|
32
33
|
// lib/mapper.ts
|
|
33
|
-
var
|
|
34
|
+
var import_core = require("@paykit-sdk/core");
|
|
34
35
|
var toPaykitCheckout = (checkout) => {
|
|
35
36
|
return {
|
|
36
37
|
id: checkout.id,
|
|
37
|
-
|
|
38
|
-
cancel_url: void 0,
|
|
38
|
+
payment_url: checkout.url,
|
|
39
39
|
customer_id: checkout.customerId,
|
|
40
|
-
|
|
41
|
-
success_url: checkout.successUrl,
|
|
40
|
+
session_type: checkout.subscriptionId ? "recurring" : "one_time",
|
|
42
41
|
products: checkout.products.map((product) => ({ id: product.id, quantity: 1 })),
|
|
43
|
-
metadata: checkout.metadata
|
|
42
|
+
metadata: checkout.metadata ?? null,
|
|
43
|
+
currency: checkout.currency,
|
|
44
|
+
amount: checkout.amount
|
|
44
45
|
};
|
|
45
46
|
};
|
|
46
47
|
var toPaykitCustomer = (customer) => {
|
|
@@ -50,7 +51,7 @@ var toPaykitSubscription = (subscription) => {
|
|
|
50
51
|
return {
|
|
51
52
|
id: subscription.id,
|
|
52
53
|
customer_id: subscription.customerId,
|
|
53
|
-
status: (0,
|
|
54
|
+
status: (0, import_core.toPaykitSubscriptionStatus)(subscription.status),
|
|
54
55
|
current_period_start: new Date(subscription.currentPeriodStart),
|
|
55
56
|
current_period_end: new Date(subscription.currentPeriodEnd)
|
|
56
57
|
};
|
|
@@ -60,14 +61,14 @@ var toPaykitSubscription = (subscription) => {
|
|
|
60
61
|
var PolarProvider = class {
|
|
61
62
|
constructor(config) {
|
|
62
63
|
this.config = config;
|
|
63
|
-
this.productionURL =
|
|
64
|
-
this.sandboxURL = "
|
|
64
|
+
this.productionURL = import_sdk.ServerList["production"];
|
|
65
|
+
this.sandboxURL = import_sdk.ServerList["sandbox"];
|
|
65
66
|
/**
|
|
66
67
|
* Checkout management
|
|
67
68
|
*/
|
|
68
69
|
this.createCheckout = async (params) => {
|
|
69
|
-
const { metadata,
|
|
70
|
-
const response = await this.polar.checkouts.create({
|
|
70
|
+
const { metadata, item_id, provider_metadata } = params;
|
|
71
|
+
const response = await this.polar.checkouts.create({ metadata, products: [item_id], ...provider_metadata });
|
|
71
72
|
return toPaykitCheckout(response);
|
|
72
73
|
};
|
|
73
74
|
this.retrieveCheckout = async (id) => {
|
|
@@ -84,7 +85,10 @@ var PolarProvider = class {
|
|
|
84
85
|
};
|
|
85
86
|
this.updateCustomer = async (id, params) => {
|
|
86
87
|
const { email, name, metadata } = params;
|
|
87
|
-
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
|
+
});
|
|
88
92
|
return toPaykitCustomer(response);
|
|
89
93
|
};
|
|
90
94
|
this.retrieveCustomer = async (id) => {
|
|
@@ -95,31 +99,61 @@ var PolarProvider = class {
|
|
|
95
99
|
* Subscription management
|
|
96
100
|
*/
|
|
97
101
|
this.cancelSubscription = async (id) => {
|
|
98
|
-
|
|
99
|
-
return
|
|
102
|
+
await this.polar.subscriptions.revoke({ id });
|
|
103
|
+
return null;
|
|
100
104
|
};
|
|
101
105
|
this.retrieveSubscription = async (id) => {
|
|
102
106
|
const response = await this.polar.subscriptions.get({ id });
|
|
103
107
|
return toPaykitSubscription(response);
|
|
104
108
|
};
|
|
105
109
|
this.updateSubscription = async (id, params) => {
|
|
106
|
-
const
|
|
107
|
-
return
|
|
110
|
+
const response = await this.polar.subscriptions.update({ id, subscriptionUpdate: { ...params.metadata } });
|
|
111
|
+
return toPaykitSubscription(response);
|
|
108
112
|
};
|
|
109
113
|
/**
|
|
110
114
|
* Webhook management
|
|
111
115
|
*/
|
|
112
|
-
this.handleWebhook = async (
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
116
|
+
this.handleWebhook = async (params) => {
|
|
117
|
+
const { body, headers, webhookSecret } = params;
|
|
118
|
+
const webhookHeaders = (0, import_core2.headersExtractor)(headers, ["webhook-id", "webhook-timestamp", "webhook-signature"]).reduce(
|
|
119
|
+
(acc, kv) => {
|
|
120
|
+
acc[kv.key] = Array.isArray(kv.value) ? kv.value.join(",") : kv.value;
|
|
121
|
+
return acc;
|
|
122
|
+
},
|
|
123
|
+
{}
|
|
124
|
+
);
|
|
125
|
+
console.log({ headers, webhookHeaders, webhookSecret, body });
|
|
126
|
+
const webhookEvent = (0, import_webhooks.validateEvent)(body, webhookHeaders, webhookSecret);
|
|
127
|
+
const id = webhookHeaders["webhook-id"];
|
|
128
|
+
const timestamp = webhookHeaders["webhook-timestamp"];
|
|
129
|
+
if (webhookEvent.type === "subscription.updated") {
|
|
130
|
+
const subscription = await this.retrieveSubscription(webhookEvent.data.id);
|
|
131
|
+
return (0, import_core2.toPaykitEvent)({ type: "$subscriptionUpdated", created: parseInt(timestamp), id, data: subscription });
|
|
132
|
+
} else if (webhookEvent.type === "subscription.created") {
|
|
133
|
+
const subscription = await this.retrieveSubscription(webhookEvent.data.id);
|
|
134
|
+
return (0, import_core2.toPaykitEvent)({ type: "$subscriptionCreated", created: parseInt(timestamp), id, data: subscription });
|
|
135
|
+
} else if (webhookEvent.type === "subscription.revoked") {
|
|
136
|
+
const subscription = await this.retrieveSubscription(webhookEvent.data.id);
|
|
137
|
+
return (0, import_core2.toPaykitEvent)({ type: "$subscriptionCancelled", created: parseInt(timestamp), id, data: subscription });
|
|
138
|
+
} else if (webhookEvent.type === "customer.created") {
|
|
139
|
+
const customer = await this.retrieveCustomer(webhookEvent.data.id);
|
|
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
|
+
return (0, import_core2.toPaykitEvent)({ type: "$customerDeleted", created: parseInt(timestamp), id, data: null });
|
|
146
|
+
} else if (webhookEvent.type === "checkout.created") {
|
|
147
|
+
const checkout = await this.retrieveCheckout(webhookEvent.data.id);
|
|
148
|
+
return (0, import_core2.toPaykitEvent)({ type: "$checkoutCreated", created: parseInt(timestamp), id, data: checkout });
|
|
149
|
+
} else if (webhookEvent.type === "order.created") {
|
|
150
|
+
const checkout = await this.retrieveCheckout(webhookEvent.data.id);
|
|
151
|
+
return (0, import_core2.toPaykitEvent)({ type: "$paymentReceived", created: parseInt(timestamp), id, data: checkout });
|
|
152
|
+
}
|
|
153
|
+
throw new Error(`Unhandled event type: ${webhookEvent.type}`);
|
|
120
154
|
};
|
|
121
|
-
const {
|
|
122
|
-
this.polar = new import_sdk.Polar({
|
|
155
|
+
const { accessToken, server, ...rest } = config;
|
|
156
|
+
this.polar = new import_sdk.Polar({ accessToken, serverURL: server === "sandbox" ? this.sandboxURL : this.productionURL, ...rest });
|
|
123
157
|
}
|
|
124
158
|
};
|
|
125
159
|
|
|
@@ -128,10 +162,10 @@ var createPolar = (config) => {
|
|
|
128
162
|
return new PolarProvider(config);
|
|
129
163
|
};
|
|
130
164
|
var polar = () => {
|
|
131
|
-
const
|
|
165
|
+
const accessToken = process.env.POLAR_ACCESS_TOKEN;
|
|
132
166
|
const isDev = process.env.NODE_ENV === "development";
|
|
133
|
-
if (!
|
|
134
|
-
return createPolar({
|
|
167
|
+
if (!accessToken) throw new Error("POLAR_ACCESS_TOKEN is not set");
|
|
168
|
+
return createPolar({ debug: true, accessToken, server: isDev ? "sandbox" : "production" });
|
|
135
169
|
};
|
|
136
170
|
// Annotate the CommonJS export names for ESM import in node:
|
|
137
171
|
0 && (module.exports = {
|
package/dist/index.mjs
CHANGED
|
@@ -1,23 +1,25 @@
|
|
|
1
1
|
// src/polar-provider.ts
|
|
2
2
|
import {
|
|
3
|
-
toPaykitEvent
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
toPaykitEvent,
|
|
4
|
+
headersExtractor
|
|
5
|
+
} from "@paykit-sdk/core";
|
|
6
|
+
import { Polar, ServerList } from "@polar-sh/sdk";
|
|
7
|
+
import { validateEvent } from "@polar-sh/sdk/webhooks";
|
|
6
8
|
|
|
7
9
|
// lib/mapper.ts
|
|
8
10
|
import {
|
|
9
11
|
toPaykitSubscriptionStatus
|
|
10
|
-
} from "@paykit-sdk/core
|
|
12
|
+
} from "@paykit-sdk/core";
|
|
11
13
|
var toPaykitCheckout = (checkout) => {
|
|
12
14
|
return {
|
|
13
15
|
id: checkout.id,
|
|
14
|
-
|
|
15
|
-
cancel_url: void 0,
|
|
16
|
+
payment_url: checkout.url,
|
|
16
17
|
customer_id: checkout.customerId,
|
|
17
|
-
|
|
18
|
-
success_url: checkout.successUrl,
|
|
18
|
+
session_type: checkout.subscriptionId ? "recurring" : "one_time",
|
|
19
19
|
products: checkout.products.map((product) => ({ id: product.id, quantity: 1 })),
|
|
20
|
-
metadata: checkout.metadata
|
|
20
|
+
metadata: checkout.metadata ?? null,
|
|
21
|
+
currency: checkout.currency,
|
|
22
|
+
amount: checkout.amount
|
|
21
23
|
};
|
|
22
24
|
};
|
|
23
25
|
var toPaykitCustomer = (customer) => {
|
|
@@ -37,14 +39,14 @@ var toPaykitSubscription = (subscription) => {
|
|
|
37
39
|
var PolarProvider = class {
|
|
38
40
|
constructor(config) {
|
|
39
41
|
this.config = config;
|
|
40
|
-
this.productionURL = "
|
|
41
|
-
this.sandboxURL = "
|
|
42
|
+
this.productionURL = ServerList["production"];
|
|
43
|
+
this.sandboxURL = ServerList["sandbox"];
|
|
42
44
|
/**
|
|
43
45
|
* Checkout management
|
|
44
46
|
*/
|
|
45
47
|
this.createCheckout = async (params) => {
|
|
46
|
-
const { metadata,
|
|
47
|
-
const response = await this.polar.checkouts.create({
|
|
48
|
+
const { metadata, item_id, provider_metadata } = params;
|
|
49
|
+
const response = await this.polar.checkouts.create({ metadata, products: [item_id], ...provider_metadata });
|
|
48
50
|
return toPaykitCheckout(response);
|
|
49
51
|
};
|
|
50
52
|
this.retrieveCheckout = async (id) => {
|
|
@@ -61,7 +63,10 @@ var PolarProvider = class {
|
|
|
61
63
|
};
|
|
62
64
|
this.updateCustomer = async (id, params) => {
|
|
63
65
|
const { email, name, metadata } = params;
|
|
64
|
-
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
|
+
});
|
|
65
70
|
return toPaykitCustomer(response);
|
|
66
71
|
};
|
|
67
72
|
this.retrieveCustomer = async (id) => {
|
|
@@ -72,31 +77,61 @@ var PolarProvider = class {
|
|
|
72
77
|
* Subscription management
|
|
73
78
|
*/
|
|
74
79
|
this.cancelSubscription = async (id) => {
|
|
75
|
-
|
|
76
|
-
return
|
|
80
|
+
await this.polar.subscriptions.revoke({ id });
|
|
81
|
+
return null;
|
|
77
82
|
};
|
|
78
83
|
this.retrieveSubscription = async (id) => {
|
|
79
84
|
const response = await this.polar.subscriptions.get({ id });
|
|
80
85
|
return toPaykitSubscription(response);
|
|
81
86
|
};
|
|
82
87
|
this.updateSubscription = async (id, params) => {
|
|
83
|
-
const
|
|
84
|
-
return
|
|
88
|
+
const response = await this.polar.subscriptions.update({ id, subscriptionUpdate: { ...params.metadata } });
|
|
89
|
+
return toPaykitSubscription(response);
|
|
85
90
|
};
|
|
86
91
|
/**
|
|
87
92
|
* Webhook management
|
|
88
93
|
*/
|
|
89
|
-
this.handleWebhook = async (
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
94
|
+
this.handleWebhook = async (params) => {
|
|
95
|
+
const { body, headers, webhookSecret } = params;
|
|
96
|
+
const webhookHeaders = headersExtractor(headers, ["webhook-id", "webhook-timestamp", "webhook-signature"]).reduce(
|
|
97
|
+
(acc, kv) => {
|
|
98
|
+
acc[kv.key] = Array.isArray(kv.value) ? kv.value.join(",") : kv.value;
|
|
99
|
+
return acc;
|
|
100
|
+
},
|
|
101
|
+
{}
|
|
102
|
+
);
|
|
103
|
+
console.log({ headers, webhookHeaders, webhookSecret, body });
|
|
104
|
+
const webhookEvent = validateEvent(body, webhookHeaders, webhookSecret);
|
|
105
|
+
const id = webhookHeaders["webhook-id"];
|
|
106
|
+
const timestamp = webhookHeaders["webhook-timestamp"];
|
|
107
|
+
if (webhookEvent.type === "subscription.updated") {
|
|
108
|
+
const subscription = await this.retrieveSubscription(webhookEvent.data.id);
|
|
109
|
+
return toPaykitEvent({ type: "$subscriptionUpdated", created: parseInt(timestamp), id, data: subscription });
|
|
110
|
+
} else if (webhookEvent.type === "subscription.created") {
|
|
111
|
+
const subscription = await this.retrieveSubscription(webhookEvent.data.id);
|
|
112
|
+
return toPaykitEvent({ type: "$subscriptionCreated", created: parseInt(timestamp), id, data: subscription });
|
|
113
|
+
} else if (webhookEvent.type === "subscription.revoked") {
|
|
114
|
+
const subscription = await this.retrieveSubscription(webhookEvent.data.id);
|
|
115
|
+
return toPaykitEvent({ type: "$subscriptionCancelled", created: parseInt(timestamp), id, data: subscription });
|
|
116
|
+
} else if (webhookEvent.type === "customer.created") {
|
|
117
|
+
const customer = await this.retrieveCustomer(webhookEvent.data.id);
|
|
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
|
+
return toPaykitEvent({ type: "$customerDeleted", created: parseInt(timestamp), id, data: null });
|
|
124
|
+
} else if (webhookEvent.type === "checkout.created") {
|
|
125
|
+
const checkout = await this.retrieveCheckout(webhookEvent.data.id);
|
|
126
|
+
return toPaykitEvent({ type: "$checkoutCreated", created: parseInt(timestamp), id, data: checkout });
|
|
127
|
+
} else if (webhookEvent.type === "order.created") {
|
|
128
|
+
const checkout = await this.retrieveCheckout(webhookEvent.data.id);
|
|
129
|
+
return toPaykitEvent({ type: "$paymentReceived", created: parseInt(timestamp), id, data: checkout });
|
|
130
|
+
}
|
|
131
|
+
throw new Error(`Unhandled event type: ${webhookEvent.type}`);
|
|
97
132
|
};
|
|
98
|
-
const {
|
|
99
|
-
this.polar = new Polar({
|
|
133
|
+
const { accessToken, server, ...rest } = config;
|
|
134
|
+
this.polar = new Polar({ accessToken, serverURL: server === "sandbox" ? this.sandboxURL : this.productionURL, ...rest });
|
|
100
135
|
}
|
|
101
136
|
};
|
|
102
137
|
|
|
@@ -105,10 +140,10 @@ var createPolar = (config) => {
|
|
|
105
140
|
return new PolarProvider(config);
|
|
106
141
|
};
|
|
107
142
|
var polar = () => {
|
|
108
|
-
const
|
|
143
|
+
const accessToken = process.env.POLAR_ACCESS_TOKEN;
|
|
109
144
|
const isDev = process.env.NODE_ENV === "development";
|
|
110
|
-
if (!
|
|
111
|
-
return createPolar({
|
|
145
|
+
if (!accessToken) throw new Error("POLAR_ACCESS_TOKEN is not set");
|
|
146
|
+
return createPolar({ debug: true, accessToken, server: isDev ? "sandbox" : "production" });
|
|
112
147
|
};
|
|
113
148
|
export {
|
|
114
149
|
createPolar,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paykit-sdk/polar",
|
|
3
|
-
"version": "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",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
],
|
|
11
11
|
"scripts": {
|
|
12
12
|
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
13
|
-
"prepublishOnly": "npm run build"
|
|
13
|
+
"prepublishOnly": "rm -rf dist && npm run build"
|
|
14
14
|
},
|
|
15
15
|
"keywords": [
|
|
16
16
|
"polar",
|
|
@@ -24,12 +24,12 @@
|
|
|
24
24
|
"@polar-sh/sdk": "^0.33.0"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
|
-
"@paykit-sdk/core": "^1.
|
|
27
|
+
"@paykit-sdk/core": "^1.1.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
+
"@paykit-sdk/core": "workspace:*",
|
|
30
31
|
"tsup": "^8.0.0",
|
|
31
|
-
"typescript": "^5.0.0"
|
|
32
|
-
"@paykit-sdk/core": "workspace:*"
|
|
32
|
+
"typescript": "^5.0.0"
|
|
33
33
|
},
|
|
34
34
|
"publishConfig": {
|
|
35
35
|
"access": "public"
|