@paykit-sdk/polar 1.1.1 → 1.1.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 CHANGED
@@ -9,7 +9,7 @@ import { PayKit } from '@paykit-sdk/core';
9
9
  import { polar, createPolar } from '@paykit-sdk/polar';
10
10
 
11
11
  // Method 1: Using environment variables
12
- const provider = polar(); // Uses POLAR_ACCESS_TOKEN from env
12
+ const provider = polar(); // Ensure POLAR_ACCESS_TOKEN environment variable is set
13
13
 
14
14
  // Method 2: Direct configuration
15
15
  const provider = createPolar({
@@ -35,11 +35,117 @@ paykit.webhooks
35
35
  .on('$checkoutCreated', async event => {
36
36
  console.log('Checkout created:', event.data);
37
37
  })
38
- .on('$paymentReceived', async event => {
38
+ .on('$invoicePaid', async event => {
39
39
  console.log('Payment received:', event.data);
40
40
  });
41
41
  ```
42
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
+
43
149
  ## Subscription Updates
44
150
 
45
151
  Polar requires specific update types:
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: (0, import_core.toPaykitSubscriptionStatus)(subscription.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 {
@@ -123,34 +139,27 @@ var PolarProvider = class {
123
139
  {}
124
140
  );
125
141
  console.log({ headers, webhookHeaders, webhookSecret, body });
126
- const webhookEvent = (0, import_webhooks.validateEvent)(body, webhookHeaders, webhookSecret);
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 (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
+ 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 (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 });
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(`Unhandled event type: ${webhookEvent.type}`);
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
- toPaykitSubscriptionStatus
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 {
@@ -101,34 +117,27 @@ var PolarProvider = class {
101
117
  {}
102
118
  );
103
119
  console.log({ headers, webhookHeaders, webhookSecret, body });
104
- const webhookEvent = validateEvent(body, webhookHeaders, webhookSecret);
120
+ const { data, type } = validateEvent(body, webhookHeaders, webhookSecret);
105
121
  const id = webhookHeaders["webhook-id"];
106
122
  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
+ 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 (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 });
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(`Unhandled event type: ${webhookEvent.type}`);
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.1",
3
+ "version": "1.1.2",
4
4
  "description": "Polar provider for PayKit",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",
@@ -24,7 +24,7 @@
24
24
  "@polar-sh/sdk": "^0.33.0"
25
25
  },
26
26
  "peerDependencies": {
27
- "@paykit-sdk/core": "^1.1.0"
27
+ "@paykit-sdk/core": "^1.1.2"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@paykit-sdk/core": "workspace:*",