@paykit-sdk/stripe 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 +110 -0
- package/dist/index.js +3 -2
- package/dist/index.mjs +3 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -42,6 +42,116 @@ paykit.webhooks
|
|
|
42
42
|
});
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
+
## Webhook Implementation
|
|
46
|
+
|
|
47
|
+
### Next.js API Route
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { paykit } from '@/lib/paykit';
|
|
51
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
52
|
+
|
|
53
|
+
export async function POST(request: NextRequest) {
|
|
54
|
+
console.log('Stripe webhook received');
|
|
55
|
+
|
|
56
|
+
const webhook = paykit.webhooks
|
|
57
|
+
.setup({ webhookSecret: process.env.STRIPE_WEBHOOK_SECRET! })
|
|
58
|
+
.on('$checkoutCreated', async event => {
|
|
59
|
+
console.log('Checkout created:', event.data);
|
|
60
|
+
// Handle checkout creation
|
|
61
|
+
})
|
|
62
|
+
.on('$customerCreated', async event => {
|
|
63
|
+
console.log('Customer created:', event.data);
|
|
64
|
+
// Handle customer creation
|
|
65
|
+
})
|
|
66
|
+
.on('$customerUpdated', async event => {
|
|
67
|
+
console.log('Customer updated:', event.data);
|
|
68
|
+
// Handle customer updates
|
|
69
|
+
})
|
|
70
|
+
.on('$subscriptionCreated', async event => {
|
|
71
|
+
console.log('Subscription created:', event.data);
|
|
72
|
+
// Handle subscription creation
|
|
73
|
+
})
|
|
74
|
+
.on('$subscriptionUpdated', async event => {
|
|
75
|
+
console.log('Subscription updated:', event.data);
|
|
76
|
+
// Handle subscription updates
|
|
77
|
+
})
|
|
78
|
+
.on('$subscriptionCancelled', async event => {
|
|
79
|
+
console.log('Subscription cancelled:', event.data);
|
|
80
|
+
// Handle subscription cancellation
|
|
81
|
+
})
|
|
82
|
+
.on('$invoicePaid', async event => {
|
|
83
|
+
console.log('Payment received:', event.data);
|
|
84
|
+
// Handle successful payment
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const headers = Object.fromEntries(request.headers.entries());
|
|
88
|
+
const body = await request.text();
|
|
89
|
+
await webhook.handle({ body, headers });
|
|
90
|
+
|
|
91
|
+
return NextResponse.json({ success: true });
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Express.js Route
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
import { paykit } from '@/lib/paykit';
|
|
99
|
+
import express from 'express';
|
|
100
|
+
|
|
101
|
+
const app = express();
|
|
102
|
+
app.use(express.raw({ type: 'application/json' }));
|
|
103
|
+
|
|
104
|
+
app.post('/api/webhooks/stripe', async (req, res) => {
|
|
105
|
+
console.log('Stripe webhook received');
|
|
106
|
+
|
|
107
|
+
const webhook = paykit.webhooks
|
|
108
|
+
.setup({ webhookSecret: process.env.STRIPE_WEBHOOK_SECRET! })
|
|
109
|
+
.on('$checkoutCreated', async event => {
|
|
110
|
+
console.log('Checkout created:', event.data);
|
|
111
|
+
})
|
|
112
|
+
.on('$customerCreated', async event => {
|
|
113
|
+
console.log('Customer created:', event.data);
|
|
114
|
+
})
|
|
115
|
+
.on('$invoicePaid', async event => {
|
|
116
|
+
console.log('Payment received:', event.data);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const headers = req.headers;
|
|
120
|
+
const body = req.body;
|
|
121
|
+
await webhook.handle({ body, headers });
|
|
122
|
+
|
|
123
|
+
res.json({ success: true });
|
|
124
|
+
});
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Vite.js
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
import { paykit } from '@/lib/paykit';
|
|
131
|
+
|
|
132
|
+
export default defineEventHandler(async event => {
|
|
133
|
+
console.log('Stripe webhook received');
|
|
134
|
+
|
|
135
|
+
const webhook = paykit.webhooks
|
|
136
|
+
.setup({ webhookSecret: process.env.STRIPE_WEBHOOK_SECRET! })
|
|
137
|
+
.on('$checkoutCreated', async event => {
|
|
138
|
+
console.log('Checkout created:', event.data);
|
|
139
|
+
})
|
|
140
|
+
.on('$customerCreated', async event => {
|
|
141
|
+
console.log('Customer created:', event.data);
|
|
142
|
+
})
|
|
143
|
+
.on('$invoicePaid', async event => {
|
|
144
|
+
console.log('Payment received:', event.data);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const headers = getHeaders(event);
|
|
148
|
+
const body = await readBody(event);
|
|
149
|
+
await webhook.handle({ body, headers });
|
|
150
|
+
|
|
151
|
+
return { success: true };
|
|
152
|
+
});
|
|
153
|
+
```
|
|
154
|
+
|
|
45
155
|
## Subscription Management
|
|
46
156
|
|
|
47
157
|
```typescript
|
package/dist/index.js
CHANGED
|
@@ -99,9 +99,10 @@ var StripeProvider = class {
|
|
|
99
99
|
const { customer_id, item_id, metadata, session_type, provider_metadata } = params;
|
|
100
100
|
const checkout = await this.stripe.checkout.sessions.create({
|
|
101
101
|
customer: customer_id,
|
|
102
|
-
metadata,
|
|
103
|
-
line_items: [{ price: item_id }],
|
|
104
102
|
mode: session_type === "one_time" ? "payment" : "subscription",
|
|
103
|
+
...session_type == "one_time" && { metadata },
|
|
104
|
+
...session_type == "recurring" && { subscription_data: { metadata } },
|
|
105
|
+
line_items: [{ price: item_id }],
|
|
105
106
|
...provider_metadata
|
|
106
107
|
});
|
|
107
108
|
return toPaykitCheckout(checkout);
|
package/dist/index.mjs
CHANGED
|
@@ -66,9 +66,10 @@ var StripeProvider = class {
|
|
|
66
66
|
const { customer_id, item_id, metadata, session_type, provider_metadata } = params;
|
|
67
67
|
const checkout = await this.stripe.checkout.sessions.create({
|
|
68
68
|
customer: customer_id,
|
|
69
|
-
metadata,
|
|
70
|
-
line_items: [{ price: item_id }],
|
|
71
69
|
mode: session_type === "one_time" ? "payment" : "subscription",
|
|
70
|
+
...session_type == "one_time" && { metadata },
|
|
71
|
+
...session_type == "recurring" && { subscription_data: { metadata } },
|
|
72
|
+
line_items: [{ price: item_id }],
|
|
72
73
|
...provider_metadata
|
|
73
74
|
});
|
|
74
75
|
return toPaykitCheckout(checkout);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paykit-sdk/stripe",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "Stripe provider for PayKit",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"stripe": "^18.2.1"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
|
-
"@paykit-sdk/core": "^1.1.
|
|
27
|
+
"@paykit-sdk/core": "^1.1.2"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"tsup": "^8.0.0",
|