@paykit-sdk/stripe 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 +181 -0
- package/dist/index.d.mts +6 -8
- package/dist/index.d.ts +6 -8
- package/dist/index.js +105 -36
- package/dist/index.mjs +107 -36
- package/package.json +3 -3
package/README.md
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# @paykit-sdk/stripe
|
|
2
|
+
|
|
3
|
+
Stripe provider for PayKit
|
|
4
|
+
|
|
5
|
+
## Quick Start
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { PayKit } from '@paykit-sdk/core';
|
|
9
|
+
import { stripe, createStripe } from '@paykit-sdk/stripe';
|
|
10
|
+
|
|
11
|
+
// Method 1: Using environment variables
|
|
12
|
+
const provider = stripe(); // Ensure STRIPE_API_KEY environment variable is set
|
|
13
|
+
|
|
14
|
+
// Method 2: Direct configuration
|
|
15
|
+
const provider = createStripe({
|
|
16
|
+
apiKey: process.env.STRIPE_API_KEY,
|
|
17
|
+
apiVersion: '2024-12-18.acacia',
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const paykit = new PayKit(provider);
|
|
21
|
+
|
|
22
|
+
// Create checkout
|
|
23
|
+
const checkout = await paykit.checkouts.create({
|
|
24
|
+
customer_id: 'cus_123',
|
|
25
|
+
item_id: 'price_123',
|
|
26
|
+
session_type: 'one_time',
|
|
27
|
+
metadata: { plan: 'pro' },
|
|
28
|
+
provider_metadata: {
|
|
29
|
+
ui_mode: 'hosted',
|
|
30
|
+
success_url: 'https://your-app.com/success',
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Handle webhooks
|
|
35
|
+
paykit.webhooks
|
|
36
|
+
.setup({ webhookSecret: process.env.STRIPE_WEBHOOK_SECRET })
|
|
37
|
+
.on('$checkoutCreated', async event => {
|
|
38
|
+
console.log('Checkout created:', event.data);
|
|
39
|
+
})
|
|
40
|
+
.on('$invoicePaid', async event => {
|
|
41
|
+
console.log('Payment received:', event.data);
|
|
42
|
+
});
|
|
43
|
+
```
|
|
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
|
+
|
|
155
|
+
## Subscription Management
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
// Update subscription
|
|
159
|
+
await paykit.subscriptions.update('sub_123', {
|
|
160
|
+
metadata: { plan: 'enterprise' },
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// Cancel subscription
|
|
164
|
+
await paykit.subscriptions.cancel('sub_123');
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Environment Variables
|
|
168
|
+
|
|
169
|
+
```bash
|
|
170
|
+
STRIPE_SECRET_KEY=sk_test_...
|
|
171
|
+
STRIPE_WEBHOOK_SECRET=whsec_...
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Support
|
|
175
|
+
|
|
176
|
+
- [Stripe Documentation](https://stripe.com/docs)
|
|
177
|
+
- [PayKit Issues](https://github.com/devodii/paykit/issues)
|
|
178
|
+
|
|
179
|
+
## License
|
|
180
|
+
|
|
181
|
+
ISC
|
package/dist/index.d.mts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { PayKitProvider } from '@paykit-sdk/core
|
|
2
|
-
import { CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, WebhookEventPayload } from '@paykit-sdk/core/src/resources';
|
|
3
|
-
import { WithPaymentProviderConfig } from '@paykit-sdk/core/src/types';
|
|
1
|
+
import { PaykitProviderOptions, PayKitProvider, CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, UpdateSubscriptionParams, Subscription, $ExtWebhookHandlerConfig, WebhookEventPayload } from '@paykit-sdk/core';
|
|
4
2
|
import Stripe from 'stripe';
|
|
5
3
|
|
|
6
|
-
interface StripeConfig extends
|
|
7
|
-
|
|
4
|
+
interface StripeConfig extends PaykitProviderOptions<Stripe.StripeConfig> {
|
|
5
|
+
apiKey: string;
|
|
8
6
|
}
|
|
9
7
|
declare class StripeProvider implements PayKitProvider {
|
|
10
8
|
private stripe;
|
|
@@ -19,18 +17,18 @@ declare class StripeProvider implements PayKitProvider {
|
|
|
19
17
|
*/
|
|
20
18
|
createCustomer: (params: CreateCustomerParams) => Promise<Customer>;
|
|
21
19
|
updateCustomer: (id: string, params: UpdateCustomerParams) => Promise<Customer>;
|
|
22
|
-
deleteCustomer: (id: string) => Promise<
|
|
20
|
+
deleteCustomer: (id: string) => Promise<null>;
|
|
23
21
|
retrieveCustomer: (id: string) => Promise<Customer | null>;
|
|
24
22
|
/**
|
|
25
23
|
* Subscription management
|
|
26
24
|
*/
|
|
27
|
-
cancelSubscription: (id: string) => Promise<
|
|
25
|
+
cancelSubscription: (id: string) => Promise<null>;
|
|
28
26
|
updateSubscription: (id: string, params: UpdateSubscriptionParams) => Promise<Subscription>;
|
|
29
27
|
retrieveSubscription: (id: string) => Promise<Subscription>;
|
|
30
28
|
/**
|
|
31
29
|
* Webhook management
|
|
32
30
|
*/
|
|
33
|
-
handleWebhook: (
|
|
31
|
+
handleWebhook: (params: $ExtWebhookHandlerConfig) => Promise<WebhookEventPayload>;
|
|
34
32
|
}
|
|
35
33
|
|
|
36
34
|
declare const createStripe: (config: StripeConfig) => StripeProvider;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { PayKitProvider } from '@paykit-sdk/core
|
|
2
|
-
import { CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, WebhookEventPayload } from '@paykit-sdk/core/src/resources';
|
|
3
|
-
import { WithPaymentProviderConfig } from '@paykit-sdk/core/src/types';
|
|
1
|
+
import { PaykitProviderOptions, PayKitProvider, CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, UpdateSubscriptionParams, Subscription, $ExtWebhookHandlerConfig, WebhookEventPayload } from '@paykit-sdk/core';
|
|
4
2
|
import Stripe from 'stripe';
|
|
5
3
|
|
|
6
|
-
interface StripeConfig extends
|
|
7
|
-
|
|
4
|
+
interface StripeConfig extends PaykitProviderOptions<Stripe.StripeConfig> {
|
|
5
|
+
apiKey: string;
|
|
8
6
|
}
|
|
9
7
|
declare class StripeProvider implements PayKitProvider {
|
|
10
8
|
private stripe;
|
|
@@ -19,18 +17,18 @@ declare class StripeProvider implements PayKitProvider {
|
|
|
19
17
|
*/
|
|
20
18
|
createCustomer: (params: CreateCustomerParams) => Promise<Customer>;
|
|
21
19
|
updateCustomer: (id: string, params: UpdateCustomerParams) => Promise<Customer>;
|
|
22
|
-
deleteCustomer: (id: string) => Promise<
|
|
20
|
+
deleteCustomer: (id: string) => Promise<null>;
|
|
23
21
|
retrieveCustomer: (id: string) => Promise<Customer | null>;
|
|
24
22
|
/**
|
|
25
23
|
* Subscription management
|
|
26
24
|
*/
|
|
27
|
-
cancelSubscription: (id: string) => Promise<
|
|
25
|
+
cancelSubscription: (id: string) => Promise<null>;
|
|
28
26
|
updateSubscription: (id: string, params: UpdateSubscriptionParams) => Promise<Subscription>;
|
|
29
27
|
retrieveSubscription: (id: string) => Promise<Subscription>;
|
|
30
28
|
/**
|
|
31
29
|
* Webhook management
|
|
32
30
|
*/
|
|
33
|
-
handleWebhook: (
|
|
31
|
+
handleWebhook: (params: $ExtWebhookHandlerConfig) => Promise<WebhookEventPayload>;
|
|
34
32
|
}
|
|
35
33
|
|
|
36
34
|
declare const createStripe: (config: StripeConfig) => StripeProvider;
|
package/dist/index.js
CHANGED
|
@@ -36,33 +36,56 @@ __export(index_exports, {
|
|
|
36
36
|
module.exports = __toCommonJS(index_exports);
|
|
37
37
|
|
|
38
38
|
// src/stripe-provider.ts
|
|
39
|
-
var
|
|
39
|
+
var import_core2 = require("@paykit-sdk/core");
|
|
40
40
|
var import_stripe = __toESM(require("stripe"));
|
|
41
41
|
|
|
42
42
|
// lib/mapper.ts
|
|
43
|
-
var
|
|
43
|
+
var import_core = require("@paykit-sdk/core");
|
|
44
44
|
var toPaykitCheckout = (checkout) => {
|
|
45
45
|
return {
|
|
46
46
|
id: checkout.id,
|
|
47
47
|
customer_id: checkout.customer,
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
session_type: checkout.mode === "subscription" ? "recurring" : "one_time",
|
|
49
|
+
// todo: handle `setup` mode
|
|
50
|
+
payment_url: checkout.url,
|
|
51
51
|
products: checkout.line_items.data.map((item) => ({ id: item.price.id, quantity: item.quantity })),
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
currency: checkout.currency,
|
|
53
|
+
amount: checkout.amount_total,
|
|
54
|
+
metadata: checkout.metadata
|
|
54
55
|
};
|
|
55
56
|
};
|
|
56
57
|
var toPaykitCustomer = (customer) => {
|
|
57
|
-
return {
|
|
58
|
+
return {
|
|
59
|
+
id: customer.id,
|
|
60
|
+
email: customer.email ?? void 0,
|
|
61
|
+
name: customer.name ?? void 0,
|
|
62
|
+
metadata: (0, import_core.stringifyObjectValues)(customer.metadata ?? {})
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
var toPaykitSubscriptionStatus = (status) => {
|
|
66
|
+
if (["active", "trialing"].includes(status)) return "active";
|
|
67
|
+
if (["incomplete_expired", "incomplete", "past_due"].includes(status)) return "past_due";
|
|
68
|
+
if (["canceled"].includes(status)) return "canceled";
|
|
69
|
+
if (["expired"].includes(status)) return "expired";
|
|
70
|
+
throw new Error(`Unknown status: ${status}`);
|
|
58
71
|
};
|
|
59
72
|
var toPaykitSubscription = (subscription) => {
|
|
60
73
|
return {
|
|
61
74
|
id: subscription.id,
|
|
62
|
-
customer_id: subscription.customer,
|
|
63
|
-
status:
|
|
75
|
+
customer_id: subscription.customer?.toString(),
|
|
76
|
+
status: toPaykitSubscriptionStatus(subscription.status),
|
|
64
77
|
current_period_start: new Date(subscription.start_date),
|
|
65
|
-
current_period_end: new Date(subscription.cancel_at)
|
|
78
|
+
current_period_end: new Date(subscription.cancel_at),
|
|
79
|
+
metadata: (0, import_core.stringifyObjectValues)(subscription.metadata ?? {})
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
var toPaykitInvoice = (invoice) => {
|
|
83
|
+
return {
|
|
84
|
+
id: invoice.id,
|
|
85
|
+
amount: invoice.amount_paid,
|
|
86
|
+
currency: invoice.currency,
|
|
87
|
+
metadata: (0, import_core.stringifyObjectValues)(invoice.metadata ?? {}),
|
|
88
|
+
customer_id: invoice.customer?.toString() ?? ""
|
|
66
89
|
};
|
|
67
90
|
};
|
|
68
91
|
|
|
@@ -73,8 +96,14 @@ var StripeProvider = class {
|
|
|
73
96
|
* Checkout management
|
|
74
97
|
*/
|
|
75
98
|
this.createCheckout = async (params) => {
|
|
76
|
-
const { customer_id,
|
|
77
|
-
const checkout = await this.stripe.checkout.sessions.create({
|
|
99
|
+
const { customer_id, item_id, metadata, session_type, provider_metadata } = params;
|
|
100
|
+
const checkout = await this.stripe.checkout.sessions.create({
|
|
101
|
+
customer: customer_id,
|
|
102
|
+
metadata,
|
|
103
|
+
line_items: [{ price: item_id }],
|
|
104
|
+
mode: session_type === "one_time" ? "payment" : "subscription",
|
|
105
|
+
...provider_metadata
|
|
106
|
+
});
|
|
78
107
|
return toPaykitCheckout(checkout);
|
|
79
108
|
};
|
|
80
109
|
this.retrieveCheckout = async (id) => {
|
|
@@ -94,6 +123,7 @@ var StripeProvider = class {
|
|
|
94
123
|
};
|
|
95
124
|
this.deleteCustomer = async (id) => {
|
|
96
125
|
await this.stripe.customers.del(id);
|
|
126
|
+
return null;
|
|
97
127
|
};
|
|
98
128
|
this.retrieveCustomer = async (id) => {
|
|
99
129
|
const customer = await this.stripe.customers.retrieve(id);
|
|
@@ -104,8 +134,8 @@ var StripeProvider = class {
|
|
|
104
134
|
* Subscription management
|
|
105
135
|
*/
|
|
106
136
|
this.cancelSubscription = async (id) => {
|
|
107
|
-
|
|
108
|
-
return
|
|
137
|
+
await this.stripe.subscriptions.cancel(id);
|
|
138
|
+
return null;
|
|
109
139
|
};
|
|
110
140
|
this.updateSubscription = async (id, params) => {
|
|
111
141
|
const subscription = await this.stripe.subscriptions.update(id, { metadata: params.metadata });
|
|
@@ -118,35 +148,74 @@ var StripeProvider = class {
|
|
|
118
148
|
/**
|
|
119
149
|
* Webhook management
|
|
120
150
|
*/
|
|
121
|
-
this.handleWebhook = async (
|
|
122
|
-
const
|
|
151
|
+
this.handleWebhook = async (params) => {
|
|
152
|
+
const { body, headers, webhookSecret } = params;
|
|
153
|
+
const stripeHeaders = (0, import_core2.headersExtractor)(headers, ["x-stripe-signature"]);
|
|
154
|
+
const signature = stripeHeaders[0].value;
|
|
155
|
+
const event = this.stripe.webhooks.constructEvent(body, signature, webhookSecret);
|
|
123
156
|
if (event.type === "checkout.session.completed") {
|
|
124
|
-
const
|
|
125
|
-
return (0,
|
|
157
|
+
const data = event.data.object;
|
|
158
|
+
return (0, import_core2.toPaykitEvent)({
|
|
159
|
+
type: "$invoicePaid",
|
|
160
|
+
created: event.created,
|
|
161
|
+
id: event.id,
|
|
162
|
+
data: {
|
|
163
|
+
id: data.id,
|
|
164
|
+
amount: data.amount_total ?? 0,
|
|
165
|
+
currency: data.currency ?? "",
|
|
166
|
+
metadata: (0, import_core2.stringifyObjectValues)(data.metadata ?? {}),
|
|
167
|
+
customer_id: data.customer?.toString() ?? ""
|
|
168
|
+
}
|
|
169
|
+
});
|
|
126
170
|
} else if (event.type === "customer.created") {
|
|
127
|
-
const customer =
|
|
128
|
-
return (0,
|
|
171
|
+
const customer = event.data.object;
|
|
172
|
+
return (0, import_core2.toPaykitEvent)({
|
|
173
|
+
type: "$customerCreated",
|
|
174
|
+
created: event.created,
|
|
175
|
+
id: event.id,
|
|
176
|
+
data: toPaykitCustomer(customer)
|
|
177
|
+
});
|
|
129
178
|
} else if (event.type === "customer.updated") {
|
|
130
|
-
const customer =
|
|
131
|
-
return (0,
|
|
179
|
+
const customer = event.data.object;
|
|
180
|
+
return (0, import_core2.toPaykitEvent)({
|
|
181
|
+
type: "$customerUpdated",
|
|
182
|
+
created: event.created,
|
|
183
|
+
id: event.id,
|
|
184
|
+
data: toPaykitCustomer(customer)
|
|
185
|
+
});
|
|
132
186
|
} else if (event.type === "customer.deleted") {
|
|
133
|
-
return (0,
|
|
187
|
+
return (0, import_core2.toPaykitEvent)({ type: "$customerDeleted", created: event.created, id: event.id, data: null });
|
|
134
188
|
} else if (event.type === "customer.subscription.created") {
|
|
135
|
-
const subscription =
|
|
136
|
-
return (0,
|
|
189
|
+
const subscription = event.data.object;
|
|
190
|
+
return (0, import_core2.toPaykitEvent)({
|
|
191
|
+
type: "$subscriptionCreated",
|
|
192
|
+
created: event.created,
|
|
193
|
+
id: event.id,
|
|
194
|
+
data: toPaykitSubscription(subscription)
|
|
195
|
+
});
|
|
137
196
|
} else if (event.type === "customer.subscription.updated") {
|
|
138
|
-
const subscription =
|
|
139
|
-
return (0,
|
|
197
|
+
const subscription = event.data.object;
|
|
198
|
+
return (0, import_core2.toPaykitEvent)({
|
|
199
|
+
type: "$subscriptionUpdated",
|
|
200
|
+
created: event.created,
|
|
201
|
+
id: event.id,
|
|
202
|
+
data: toPaykitSubscription(subscription)
|
|
203
|
+
});
|
|
140
204
|
} else if (event.type === "customer.subscription.deleted") {
|
|
141
|
-
const subscription =
|
|
142
|
-
return (0,
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
205
|
+
const subscription = event.data.object;
|
|
206
|
+
return (0, import_core2.toPaykitEvent)({
|
|
207
|
+
type: "$subscriptionCancelled",
|
|
208
|
+
created: event.created,
|
|
209
|
+
id: event.id,
|
|
210
|
+
data: toPaykitSubscription(subscription)
|
|
211
|
+
});
|
|
212
|
+
} else if (event.type == "invoice.paid") {
|
|
213
|
+
const invoice = event.data.object;
|
|
214
|
+
return (0, import_core2.toPaykitEvent)({ type: "$invoicePaid", created: event.created, id: event.id, data: toPaykitInvoice(invoice) });
|
|
146
215
|
}
|
|
147
|
-
throw new Error(`
|
|
216
|
+
throw new Error(`Unhandled event type: ${event.type}`);
|
|
148
217
|
};
|
|
149
|
-
const { apiKey, ...rest } = config;
|
|
218
|
+
const { debug, apiKey, ...rest } = config;
|
|
150
219
|
this.stripe = new import_stripe.default(apiKey, rest);
|
|
151
220
|
}
|
|
152
221
|
};
|
|
@@ -161,7 +230,7 @@ var stripe = () => {
|
|
|
161
230
|
if (!apiKey) {
|
|
162
231
|
throw new Error("STRIPE_API_KEY is not set");
|
|
163
232
|
}
|
|
164
|
-
return createStripe({ apiKey, apiVersion: "2025-
|
|
233
|
+
return createStripe({ apiKey, debug: isDev, apiVersion: "2025-07-30.basil" });
|
|
165
234
|
};
|
|
166
235
|
// Annotate the CommonJS export names for ESM import in node:
|
|
167
236
|
0 && (module.exports = {
|
package/dist/index.mjs
CHANGED
|
@@ -1,33 +1,58 @@
|
|
|
1
1
|
// src/stripe-provider.ts
|
|
2
2
|
import {
|
|
3
|
-
toPaykitEvent
|
|
4
|
-
|
|
3
|
+
toPaykitEvent,
|
|
4
|
+
headersExtractor,
|
|
5
|
+
stringifyObjectValues as stringifyObjectValues2
|
|
6
|
+
} from "@paykit-sdk/core";
|
|
5
7
|
import Stripe from "stripe";
|
|
6
8
|
|
|
7
9
|
// lib/mapper.ts
|
|
8
|
-
import {
|
|
10
|
+
import { stringifyObjectValues } from "@paykit-sdk/core";
|
|
9
11
|
var toPaykitCheckout = (checkout) => {
|
|
10
12
|
return {
|
|
11
13
|
id: checkout.id,
|
|
12
14
|
customer_id: checkout.customer,
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
session_type: checkout.mode === "subscription" ? "recurring" : "one_time",
|
|
16
|
+
// todo: handle `setup` mode
|
|
17
|
+
payment_url: checkout.url,
|
|
16
18
|
products: checkout.line_items.data.map((item) => ({ id: item.price.id, quantity: item.quantity })),
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
currency: checkout.currency,
|
|
20
|
+
amount: checkout.amount_total,
|
|
21
|
+
metadata: checkout.metadata
|
|
19
22
|
};
|
|
20
23
|
};
|
|
21
24
|
var toPaykitCustomer = (customer) => {
|
|
22
|
-
return {
|
|
25
|
+
return {
|
|
26
|
+
id: customer.id,
|
|
27
|
+
email: customer.email ?? void 0,
|
|
28
|
+
name: customer.name ?? void 0,
|
|
29
|
+
metadata: stringifyObjectValues(customer.metadata ?? {})
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
var toPaykitSubscriptionStatus = (status) => {
|
|
33
|
+
if (["active", "trialing"].includes(status)) return "active";
|
|
34
|
+
if (["incomplete_expired", "incomplete", "past_due"].includes(status)) return "past_due";
|
|
35
|
+
if (["canceled"].includes(status)) return "canceled";
|
|
36
|
+
if (["expired"].includes(status)) return "expired";
|
|
37
|
+
throw new Error(`Unknown status: ${status}`);
|
|
23
38
|
};
|
|
24
39
|
var toPaykitSubscription = (subscription) => {
|
|
25
40
|
return {
|
|
26
41
|
id: subscription.id,
|
|
27
|
-
customer_id: subscription.customer,
|
|
42
|
+
customer_id: subscription.customer?.toString(),
|
|
28
43
|
status: toPaykitSubscriptionStatus(subscription.status),
|
|
29
44
|
current_period_start: new Date(subscription.start_date),
|
|
30
|
-
current_period_end: new Date(subscription.cancel_at)
|
|
45
|
+
current_period_end: new Date(subscription.cancel_at),
|
|
46
|
+
metadata: stringifyObjectValues(subscription.metadata ?? {})
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
var toPaykitInvoice = (invoice) => {
|
|
50
|
+
return {
|
|
51
|
+
id: invoice.id,
|
|
52
|
+
amount: invoice.amount_paid,
|
|
53
|
+
currency: invoice.currency,
|
|
54
|
+
metadata: stringifyObjectValues(invoice.metadata ?? {}),
|
|
55
|
+
customer_id: invoice.customer?.toString() ?? ""
|
|
31
56
|
};
|
|
32
57
|
};
|
|
33
58
|
|
|
@@ -38,8 +63,14 @@ var StripeProvider = class {
|
|
|
38
63
|
* Checkout management
|
|
39
64
|
*/
|
|
40
65
|
this.createCheckout = async (params) => {
|
|
41
|
-
const { customer_id,
|
|
42
|
-
const checkout = await this.stripe.checkout.sessions.create({
|
|
66
|
+
const { customer_id, item_id, metadata, session_type, provider_metadata } = params;
|
|
67
|
+
const checkout = await this.stripe.checkout.sessions.create({
|
|
68
|
+
customer: customer_id,
|
|
69
|
+
metadata,
|
|
70
|
+
line_items: [{ price: item_id }],
|
|
71
|
+
mode: session_type === "one_time" ? "payment" : "subscription",
|
|
72
|
+
...provider_metadata
|
|
73
|
+
});
|
|
43
74
|
return toPaykitCheckout(checkout);
|
|
44
75
|
};
|
|
45
76
|
this.retrieveCheckout = async (id) => {
|
|
@@ -59,6 +90,7 @@ var StripeProvider = class {
|
|
|
59
90
|
};
|
|
60
91
|
this.deleteCustomer = async (id) => {
|
|
61
92
|
await this.stripe.customers.del(id);
|
|
93
|
+
return null;
|
|
62
94
|
};
|
|
63
95
|
this.retrieveCustomer = async (id) => {
|
|
64
96
|
const customer = await this.stripe.customers.retrieve(id);
|
|
@@ -69,8 +101,8 @@ var StripeProvider = class {
|
|
|
69
101
|
* Subscription management
|
|
70
102
|
*/
|
|
71
103
|
this.cancelSubscription = async (id) => {
|
|
72
|
-
|
|
73
|
-
return
|
|
104
|
+
await this.stripe.subscriptions.cancel(id);
|
|
105
|
+
return null;
|
|
74
106
|
};
|
|
75
107
|
this.updateSubscription = async (id, params) => {
|
|
76
108
|
const subscription = await this.stripe.subscriptions.update(id, { metadata: params.metadata });
|
|
@@ -83,35 +115,74 @@ var StripeProvider = class {
|
|
|
83
115
|
/**
|
|
84
116
|
* Webhook management
|
|
85
117
|
*/
|
|
86
|
-
this.handleWebhook = async (
|
|
87
|
-
const
|
|
118
|
+
this.handleWebhook = async (params) => {
|
|
119
|
+
const { body, headers, webhookSecret } = params;
|
|
120
|
+
const stripeHeaders = headersExtractor(headers, ["x-stripe-signature"]);
|
|
121
|
+
const signature = stripeHeaders[0].value;
|
|
122
|
+
const event = this.stripe.webhooks.constructEvent(body, signature, webhookSecret);
|
|
88
123
|
if (event.type === "checkout.session.completed") {
|
|
89
|
-
const
|
|
90
|
-
return toPaykitEvent({
|
|
124
|
+
const data = event.data.object;
|
|
125
|
+
return toPaykitEvent({
|
|
126
|
+
type: "$invoicePaid",
|
|
127
|
+
created: event.created,
|
|
128
|
+
id: event.id,
|
|
129
|
+
data: {
|
|
130
|
+
id: data.id,
|
|
131
|
+
amount: data.amount_total ?? 0,
|
|
132
|
+
currency: data.currency ?? "",
|
|
133
|
+
metadata: stringifyObjectValues2(data.metadata ?? {}),
|
|
134
|
+
customer_id: data.customer?.toString() ?? ""
|
|
135
|
+
}
|
|
136
|
+
});
|
|
91
137
|
} else if (event.type === "customer.created") {
|
|
92
|
-
const customer =
|
|
93
|
-
return toPaykitEvent({
|
|
138
|
+
const customer = event.data.object;
|
|
139
|
+
return toPaykitEvent({
|
|
140
|
+
type: "$customerCreated",
|
|
141
|
+
created: event.created,
|
|
142
|
+
id: event.id,
|
|
143
|
+
data: toPaykitCustomer(customer)
|
|
144
|
+
});
|
|
94
145
|
} else if (event.type === "customer.updated") {
|
|
95
|
-
const customer =
|
|
96
|
-
return toPaykitEvent({
|
|
146
|
+
const customer = event.data.object;
|
|
147
|
+
return toPaykitEvent({
|
|
148
|
+
type: "$customerUpdated",
|
|
149
|
+
created: event.created,
|
|
150
|
+
id: event.id,
|
|
151
|
+
data: toPaykitCustomer(customer)
|
|
152
|
+
});
|
|
97
153
|
} else if (event.type === "customer.deleted") {
|
|
98
|
-
return toPaykitEvent({ type: "
|
|
154
|
+
return toPaykitEvent({ type: "$customerDeleted", created: event.created, id: event.id, data: null });
|
|
99
155
|
} else if (event.type === "customer.subscription.created") {
|
|
100
|
-
const subscription =
|
|
101
|
-
return toPaykitEvent({
|
|
156
|
+
const subscription = event.data.object;
|
|
157
|
+
return toPaykitEvent({
|
|
158
|
+
type: "$subscriptionCreated",
|
|
159
|
+
created: event.created,
|
|
160
|
+
id: event.id,
|
|
161
|
+
data: toPaykitSubscription(subscription)
|
|
162
|
+
});
|
|
102
163
|
} else if (event.type === "customer.subscription.updated") {
|
|
103
|
-
const subscription =
|
|
104
|
-
return toPaykitEvent({
|
|
164
|
+
const subscription = event.data.object;
|
|
165
|
+
return toPaykitEvent({
|
|
166
|
+
type: "$subscriptionUpdated",
|
|
167
|
+
created: event.created,
|
|
168
|
+
id: event.id,
|
|
169
|
+
data: toPaykitSubscription(subscription)
|
|
170
|
+
});
|
|
105
171
|
} else if (event.type === "customer.subscription.deleted") {
|
|
106
|
-
const subscription =
|
|
107
|
-
return toPaykitEvent({
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
172
|
+
const subscription = event.data.object;
|
|
173
|
+
return toPaykitEvent({
|
|
174
|
+
type: "$subscriptionCancelled",
|
|
175
|
+
created: event.created,
|
|
176
|
+
id: event.id,
|
|
177
|
+
data: toPaykitSubscription(subscription)
|
|
178
|
+
});
|
|
179
|
+
} else if (event.type == "invoice.paid") {
|
|
180
|
+
const invoice = event.data.object;
|
|
181
|
+
return toPaykitEvent({ type: "$invoicePaid", created: event.created, id: event.id, data: toPaykitInvoice(invoice) });
|
|
111
182
|
}
|
|
112
|
-
throw new Error(`
|
|
183
|
+
throw new Error(`Unhandled event type: ${event.type}`);
|
|
113
184
|
};
|
|
114
|
-
const { apiKey, ...rest } = config;
|
|
185
|
+
const { debug, apiKey, ...rest } = config;
|
|
115
186
|
this.stripe = new Stripe(apiKey, rest);
|
|
116
187
|
}
|
|
117
188
|
};
|
|
@@ -126,7 +197,7 @@ var stripe = () => {
|
|
|
126
197
|
if (!apiKey) {
|
|
127
198
|
throw new Error("STRIPE_API_KEY is not set");
|
|
128
199
|
}
|
|
129
|
-
return createStripe({ apiKey, apiVersion: "2025-
|
|
200
|
+
return createStripe({ apiKey, debug: isDev, apiVersion: "2025-07-30.basil" });
|
|
130
201
|
};
|
|
131
202
|
export {
|
|
132
203
|
createStripe,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paykit-sdk/stripe",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1-alpha.1",
|
|
4
4
|
"description": "Stripe 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
|
"stripe",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"stripe": "^18.2.1"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
|
-
"@paykit-sdk/core": "^1.
|
|
27
|
+
"@paykit-sdk/core": "^1.1.1-alpha.1"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"tsup": "^8.0.0",
|