@paykit-sdk/polar 1.1.1-alpha.1 → 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 +106 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -40,6 +40,112 @@ paykit.webhooks
|
|
|
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:
|