@paykit-sdk/polar 1.0.1 → 1.1.1-alpha.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 +73 -30
- package/dist/index.mjs +76 -32
- 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(); // 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
|
+
## 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,48 +26,65 @@ __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) => {
|
|
47
48
|
return { id: customer.id, email: customer.email, name: customer.name ?? void 0 };
|
|
48
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
|
+
};
|
|
49
57
|
var toPaykitSubscription = (subscription) => {
|
|
50
58
|
return {
|
|
51
59
|
id: subscription.id,
|
|
52
60
|
customer_id: subscription.customerId,
|
|
53
|
-
status:
|
|
61
|
+
status: toPaykitSubscriptionStatus(subscription.status),
|
|
54
62
|
current_period_start: new Date(subscription.currentPeriodStart),
|
|
55
63
|
current_period_end: new Date(subscription.currentPeriodEnd)
|
|
56
64
|
};
|
|
57
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
|
+
};
|
|
58
75
|
|
|
59
76
|
// src/polar-provider.ts
|
|
60
77
|
var PolarProvider = class {
|
|
61
78
|
constructor(config) {
|
|
62
79
|
this.config = config;
|
|
63
|
-
this.productionURL =
|
|
64
|
-
this.sandboxURL = "
|
|
80
|
+
this.productionURL = import_sdk.ServerList["production"];
|
|
81
|
+
this.sandboxURL = import_sdk.ServerList["sandbox"];
|
|
65
82
|
/**
|
|
66
83
|
* Checkout management
|
|
67
84
|
*/
|
|
68
85
|
this.createCheckout = async (params) => {
|
|
69
|
-
const { metadata,
|
|
70
|
-
const response = await this.polar.checkouts.create({
|
|
86
|
+
const { metadata, item_id, provider_metadata } = params;
|
|
87
|
+
const response = await this.polar.checkouts.create({ metadata, products: [item_id], ...provider_metadata });
|
|
71
88
|
return toPaykitCheckout(response);
|
|
72
89
|
};
|
|
73
90
|
this.retrieveCheckout = async (id) => {
|
|
@@ -84,7 +101,10 @@ var PolarProvider = class {
|
|
|
84
101
|
};
|
|
85
102
|
this.updateCustomer = async (id, params) => {
|
|
86
103
|
const { email, name, metadata } = params;
|
|
87
|
-
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
|
+
});
|
|
88
108
|
return toPaykitCustomer(response);
|
|
89
109
|
};
|
|
90
110
|
this.retrieveCustomer = async (id) => {
|
|
@@ -95,31 +115,54 @@ var PolarProvider = class {
|
|
|
95
115
|
* Subscription management
|
|
96
116
|
*/
|
|
97
117
|
this.cancelSubscription = async (id) => {
|
|
98
|
-
|
|
99
|
-
return
|
|
118
|
+
await this.polar.subscriptions.revoke({ id });
|
|
119
|
+
return null;
|
|
100
120
|
};
|
|
101
121
|
this.retrieveSubscription = async (id) => {
|
|
102
122
|
const response = await this.polar.subscriptions.get({ id });
|
|
103
123
|
return toPaykitSubscription(response);
|
|
104
124
|
};
|
|
105
125
|
this.updateSubscription = async (id, params) => {
|
|
106
|
-
const
|
|
107
|
-
return
|
|
126
|
+
const response = await this.polar.subscriptions.update({ id, subscriptionUpdate: { ...params.metadata } });
|
|
127
|
+
return toPaykitSubscription(response);
|
|
108
128
|
};
|
|
109
129
|
/**
|
|
110
130
|
* Webhook management
|
|
111
131
|
*/
|
|
112
|
-
this.handleWebhook = async (
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
132
|
+
this.handleWebhook = async (params) => {
|
|
133
|
+
const { body, headers, webhookSecret } = params;
|
|
134
|
+
const webhookHeaders = (0, import_core2.headersExtractor)(headers, ["webhook-id", "webhook-timestamp", "webhook-signature"]).reduce(
|
|
135
|
+
(acc, kv) => {
|
|
136
|
+
acc[kv.key] = Array.isArray(kv.value) ? kv.value.join(",") : kv.value;
|
|
137
|
+
return acc;
|
|
138
|
+
},
|
|
139
|
+
{}
|
|
140
|
+
);
|
|
141
|
+
console.log({ headers, webhookHeaders, webhookSecret, body });
|
|
142
|
+
const { data, type } = (0, import_webhooks.validateEvent)(body, webhookHeaders, webhookSecret);
|
|
143
|
+
const id = webhookHeaders["webhook-id"];
|
|
144
|
+
const timestamp = webhookHeaders["webhook-timestamp"];
|
|
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") {
|
|
156
|
+
return (0, import_core2.toPaykitEvent)({ type: "$customerDeleted", created: parseInt(timestamp), id, data: null });
|
|
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) });
|
|
161
|
+
}
|
|
162
|
+
throw new Error(`Unhandled event type: ${type}`);
|
|
120
163
|
};
|
|
121
|
-
const {
|
|
122
|
-
this.polar = new import_sdk.Polar({
|
|
164
|
+
const { accessToken, server, ...rest } = config;
|
|
165
|
+
this.polar = new import_sdk.Polar({ accessToken, serverURL: server === "sandbox" ? this.sandboxURL : this.productionURL, ...rest });
|
|
123
166
|
}
|
|
124
167
|
};
|
|
125
168
|
|
|
@@ -128,10 +171,10 @@ var createPolar = (config) => {
|
|
|
128
171
|
return new PolarProvider(config);
|
|
129
172
|
};
|
|
130
173
|
var polar = () => {
|
|
131
|
-
const
|
|
174
|
+
const accessToken = process.env.POLAR_ACCESS_TOKEN;
|
|
132
175
|
const isDev = process.env.NODE_ENV === "development";
|
|
133
|
-
if (!
|
|
134
|
-
return createPolar({
|
|
176
|
+
if (!accessToken) throw new Error("POLAR_ACCESS_TOKEN is not set");
|
|
177
|
+
return createPolar({ debug: true, accessToken, server: isDev ? "sandbox" : "production" });
|
|
135
178
|
};
|
|
136
179
|
// Annotate the CommonJS export names for ESM import in node:
|
|
137
180
|
0 && (module.exports = {
|
package/dist/index.mjs
CHANGED
|
@@ -1,28 +1,37 @@
|
|
|
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
|
-
|
|
10
|
-
} from "@paykit-sdk/core
|
|
11
|
+
stringifyObjectValues
|
|
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) => {
|
|
24
26
|
return { id: customer.id, email: customer.email, name: customer.name ?? void 0 };
|
|
25
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
|
+
};
|
|
26
35
|
var toPaykitSubscription = (subscription) => {
|
|
27
36
|
return {
|
|
28
37
|
id: subscription.id,
|
|
@@ -32,19 +41,28 @@ var toPaykitSubscription = (subscription) => {
|
|
|
32
41
|
current_period_end: new Date(subscription.currentPeriodEnd)
|
|
33
42
|
};
|
|
34
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
|
+
};
|
|
35
53
|
|
|
36
54
|
// src/polar-provider.ts
|
|
37
55
|
var PolarProvider = class {
|
|
38
56
|
constructor(config) {
|
|
39
57
|
this.config = config;
|
|
40
|
-
this.productionURL = "
|
|
41
|
-
this.sandboxURL = "
|
|
58
|
+
this.productionURL = ServerList["production"];
|
|
59
|
+
this.sandboxURL = ServerList["sandbox"];
|
|
42
60
|
/**
|
|
43
61
|
* Checkout management
|
|
44
62
|
*/
|
|
45
63
|
this.createCheckout = async (params) => {
|
|
46
|
-
const { metadata,
|
|
47
|
-
const response = await this.polar.checkouts.create({
|
|
64
|
+
const { metadata, item_id, provider_metadata } = params;
|
|
65
|
+
const response = await this.polar.checkouts.create({ metadata, products: [item_id], ...provider_metadata });
|
|
48
66
|
return toPaykitCheckout(response);
|
|
49
67
|
};
|
|
50
68
|
this.retrieveCheckout = async (id) => {
|
|
@@ -61,7 +79,10 @@ var PolarProvider = class {
|
|
|
61
79
|
};
|
|
62
80
|
this.updateCustomer = async (id, params) => {
|
|
63
81
|
const { email, name, metadata } = params;
|
|
64
|
-
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
|
+
});
|
|
65
86
|
return toPaykitCustomer(response);
|
|
66
87
|
};
|
|
67
88
|
this.retrieveCustomer = async (id) => {
|
|
@@ -72,31 +93,54 @@ var PolarProvider = class {
|
|
|
72
93
|
* Subscription management
|
|
73
94
|
*/
|
|
74
95
|
this.cancelSubscription = async (id) => {
|
|
75
|
-
|
|
76
|
-
return
|
|
96
|
+
await this.polar.subscriptions.revoke({ id });
|
|
97
|
+
return null;
|
|
77
98
|
};
|
|
78
99
|
this.retrieveSubscription = async (id) => {
|
|
79
100
|
const response = await this.polar.subscriptions.get({ id });
|
|
80
101
|
return toPaykitSubscription(response);
|
|
81
102
|
};
|
|
82
103
|
this.updateSubscription = async (id, params) => {
|
|
83
|
-
const
|
|
84
|
-
return
|
|
104
|
+
const response = await this.polar.subscriptions.update({ id, subscriptionUpdate: { ...params.metadata } });
|
|
105
|
+
return toPaykitSubscription(response);
|
|
85
106
|
};
|
|
86
107
|
/**
|
|
87
108
|
* Webhook management
|
|
88
109
|
*/
|
|
89
|
-
this.handleWebhook = async (
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
110
|
+
this.handleWebhook = async (params) => {
|
|
111
|
+
const { body, headers, webhookSecret } = params;
|
|
112
|
+
const webhookHeaders = headersExtractor(headers, ["webhook-id", "webhook-timestamp", "webhook-signature"]).reduce(
|
|
113
|
+
(acc, kv) => {
|
|
114
|
+
acc[kv.key] = Array.isArray(kv.value) ? kv.value.join(",") : kv.value;
|
|
115
|
+
return acc;
|
|
116
|
+
},
|
|
117
|
+
{}
|
|
118
|
+
);
|
|
119
|
+
console.log({ headers, webhookHeaders, webhookSecret, body });
|
|
120
|
+
const { data, type } = validateEvent(body, webhookHeaders, webhookSecret);
|
|
121
|
+
const id = webhookHeaders["webhook-id"];
|
|
122
|
+
const timestamp = webhookHeaders["webhook-timestamp"];
|
|
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") {
|
|
134
|
+
return toPaykitEvent({ type: "$customerDeleted", created: parseInt(timestamp), id, data: null });
|
|
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) });
|
|
139
|
+
}
|
|
140
|
+
throw new Error(`Unhandled event type: ${type}`);
|
|
97
141
|
};
|
|
98
|
-
const {
|
|
99
|
-
this.polar = new Polar({
|
|
142
|
+
const { accessToken, server, ...rest } = config;
|
|
143
|
+
this.polar = new Polar({ accessToken, serverURL: server === "sandbox" ? this.sandboxURL : this.productionURL, ...rest });
|
|
100
144
|
}
|
|
101
145
|
};
|
|
102
146
|
|
|
@@ -105,10 +149,10 @@ var createPolar = (config) => {
|
|
|
105
149
|
return new PolarProvider(config);
|
|
106
150
|
};
|
|
107
151
|
var polar = () => {
|
|
108
|
-
const
|
|
152
|
+
const accessToken = process.env.POLAR_ACCESS_TOKEN;
|
|
109
153
|
const isDev = process.env.NODE_ENV === "development";
|
|
110
|
-
if (!
|
|
111
|
-
return createPolar({
|
|
154
|
+
if (!accessToken) throw new Error("POLAR_ACCESS_TOKEN is not set");
|
|
155
|
+
return createPolar({ debug: true, accessToken, server: isDev ? "sandbox" : "production" });
|
|
112
156
|
};
|
|
113
157
|
export {
|
|
114
158
|
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-alpha.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.1"
|
|
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"
|