@paykit-sdk/stripe 1.0.1 → 1.1.0
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/dist/index.d.mts +4 -6
- package/dist/index.d.ts +4 -6
- package/dist/index.js +47 -24
- package/dist/index.mjs +48 -24
- package/package.json +7 -8
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, Subscription, UpdateSubscriptionParams, $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;
|
|
@@ -30,7 +28,7 @@ declare class StripeProvider implements PayKitProvider {
|
|
|
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, Subscription, UpdateSubscriptionParams, $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;
|
|
@@ -30,7 +28,7 @@ declare class StripeProvider implements PayKitProvider {
|
|
|
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,21 +36,22 @@ __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) => {
|
|
@@ -60,7 +61,7 @@ var toPaykitSubscription = (subscription) => {
|
|
|
60
61
|
return {
|
|
61
62
|
id: subscription.id,
|
|
62
63
|
customer_id: subscription.customer,
|
|
63
|
-
status: (0,
|
|
64
|
+
status: (0, import_core.toPaykitSubscriptionStatus)(subscription.status),
|
|
64
65
|
current_period_start: new Date(subscription.start_date),
|
|
65
66
|
current_period_end: new Date(subscription.cancel_at)
|
|
66
67
|
};
|
|
@@ -73,8 +74,14 @@ var StripeProvider = class {
|
|
|
73
74
|
* Checkout management
|
|
74
75
|
*/
|
|
75
76
|
this.createCheckout = async (params) => {
|
|
76
|
-
const { customer_id,
|
|
77
|
-
const checkout = await this.stripe.checkout.sessions.create({
|
|
77
|
+
const { customer_id, item_id, metadata, session_type, provider_metadata } = params;
|
|
78
|
+
const checkout = await this.stripe.checkout.sessions.create({
|
|
79
|
+
customer: customer_id,
|
|
80
|
+
metadata,
|
|
81
|
+
line_items: [{ price: item_id }],
|
|
82
|
+
mode: session_type === "one_time" ? "payment" : "subscription",
|
|
83
|
+
...provider_metadata
|
|
84
|
+
});
|
|
78
85
|
return toPaykitCheckout(checkout);
|
|
79
86
|
};
|
|
80
87
|
this.retrieveCheckout = async (id) => {
|
|
@@ -118,35 +125,51 @@ var StripeProvider = class {
|
|
|
118
125
|
/**
|
|
119
126
|
* Webhook management
|
|
120
127
|
*/
|
|
121
|
-
this.handleWebhook = async (
|
|
122
|
-
const
|
|
128
|
+
this.handleWebhook = async (params) => {
|
|
129
|
+
const { body, headers, webhookSecret } = params;
|
|
130
|
+
const stripeHeaders = (0, import_core2.headersExtractor)(headers, ["x-stripe-signature"]);
|
|
131
|
+
const signature = stripeHeaders[0].value;
|
|
132
|
+
const event = this.stripe.webhooks.constructEvent(body, signature, webhookSecret);
|
|
123
133
|
if (event.type === "checkout.session.completed") {
|
|
124
134
|
const checkout = await this.retrieveCheckout(event.data.object.id);
|
|
125
|
-
|
|
135
|
+
const response = (0, import_core2.toPaykitEvent)({ type: "$checkoutCreated", created: event.created, id: event.id, data: checkout });
|
|
136
|
+
return response;
|
|
126
137
|
} else if (event.type === "customer.created") {
|
|
127
|
-
const customer = await this.
|
|
128
|
-
return (0,
|
|
138
|
+
const customer = await this.createCustomer({ email: event.data.object.email, name: event.data.object.name ?? void 0 });
|
|
139
|
+
return (0, import_core2.toPaykitEvent)({ type: "$customerCreated", created: event.created, id: event.id, data: customer });
|
|
129
140
|
} else if (event.type === "customer.updated") {
|
|
130
|
-
const
|
|
131
|
-
|
|
141
|
+
const { email, name } = event.data.object;
|
|
142
|
+
const customer = await this.updateCustomer(event.data.object.id, { email, name: name ?? void 0 });
|
|
143
|
+
return (0, import_core2.toPaykitEvent)({ type: "$customerUpdated", created: event.created, id: event.id, data: customer });
|
|
132
144
|
} else if (event.type === "customer.deleted") {
|
|
133
|
-
|
|
145
|
+
await this.deleteCustomer(event.data.object.id);
|
|
146
|
+
return (0, import_core2.toPaykitEvent)({ type: "$customerDeleted", created: event.created, id: event.id, data: null });
|
|
134
147
|
} else if (event.type === "customer.subscription.created") {
|
|
135
148
|
const subscription = await this.retrieveSubscription(event.data.object.id);
|
|
136
|
-
return (0,
|
|
149
|
+
return (0, import_core2.toPaykitEvent)({ type: "$subscriptionCreated", created: event.created, id: event.id, data: subscription });
|
|
137
150
|
} else if (event.type === "customer.subscription.updated") {
|
|
138
151
|
const subscription = await this.retrieveSubscription(event.data.object.id);
|
|
139
|
-
return (0,
|
|
152
|
+
return (0, import_core2.toPaykitEvent)({ type: "$subscriptionUpdated", created: event.created, id: event.id, data: subscription });
|
|
140
153
|
} else if (event.type === "customer.subscription.deleted") {
|
|
141
154
|
const subscription = await this.retrieveSubscription(event.data.object.id);
|
|
142
|
-
return (0,
|
|
155
|
+
return (0, import_core2.toPaykitEvent)({ type: "$subscriptionCanceled", created: event.created, id: event.id, data: subscription });
|
|
143
156
|
} else if (event.type === "customer.subscription.paused") {
|
|
144
157
|
const subscription = await this.retrieveSubscription(event.data.object.id);
|
|
145
|
-
return (0,
|
|
158
|
+
return (0, import_core2.toPaykitEvent)({ type: "$subscriptionUpdated", created: event.created, id: event.id, data: subscription });
|
|
159
|
+
} else if (event.type == "invoice.payment_succeeded") {
|
|
160
|
+
const checkoutId = event.data.object.metadata?.checkoutId;
|
|
161
|
+
if (!checkoutId) throw new Error("Checkout ID not found in invoice metadata");
|
|
162
|
+
const checkout = await this.retrieveCheckout(checkoutId);
|
|
163
|
+
return (0, import_core2.toPaykitEvent)({
|
|
164
|
+
type: "$paymentReceived",
|
|
165
|
+
created: event.created,
|
|
166
|
+
id: event.id,
|
|
167
|
+
data: { ...checkout, checkoutId }
|
|
168
|
+
});
|
|
146
169
|
}
|
|
147
170
|
throw new Error(`Unknown event type: ${event.type}`);
|
|
148
171
|
};
|
|
149
|
-
const { apiKey, ...rest } = config;
|
|
172
|
+
const { debug, apiKey, ...rest } = config;
|
|
150
173
|
this.stripe = new import_stripe.default(apiKey, rest);
|
|
151
174
|
}
|
|
152
175
|
};
|
|
@@ -161,7 +184,7 @@ var stripe = () => {
|
|
|
161
184
|
if (!apiKey) {
|
|
162
185
|
throw new Error("STRIPE_API_KEY is not set");
|
|
163
186
|
}
|
|
164
|
-
return createStripe({ apiKey, apiVersion: "2025-05-28.basil"
|
|
187
|
+
return createStripe({ apiKey, debug: isDev, apiVersion: "2025-05-28.basil" });
|
|
165
188
|
};
|
|
166
189
|
// Annotate the CommonJS export names for ESM import in node:
|
|
167
190
|
0 && (module.exports = {
|
package/dist/index.mjs
CHANGED
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
// src/stripe-provider.ts
|
|
2
2
|
import {
|
|
3
|
-
toPaykitEvent
|
|
4
|
-
|
|
3
|
+
toPaykitEvent,
|
|
4
|
+
headersExtractor
|
|
5
|
+
} from "@paykit-sdk/core";
|
|
5
6
|
import Stripe from "stripe";
|
|
6
7
|
|
|
7
8
|
// lib/mapper.ts
|
|
8
|
-
import { toPaykitSubscriptionStatus } from "@paykit-sdk/core
|
|
9
|
+
import { toPaykitSubscriptionStatus } from "@paykit-sdk/core";
|
|
9
10
|
var toPaykitCheckout = (checkout) => {
|
|
10
11
|
return {
|
|
11
12
|
id: checkout.id,
|
|
12
13
|
customer_id: checkout.customer,
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
session_type: checkout.mode === "subscription" ? "recurring" : "one_time",
|
|
15
|
+
// todo: handle `setup` mode
|
|
16
|
+
payment_url: checkout.url,
|
|
16
17
|
products: checkout.line_items.data.map((item) => ({ id: item.price.id, quantity: item.quantity })),
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
currency: checkout.currency,
|
|
19
|
+
amount: checkout.amount_total,
|
|
20
|
+
metadata: checkout.metadata
|
|
19
21
|
};
|
|
20
22
|
};
|
|
21
23
|
var toPaykitCustomer = (customer) => {
|
|
@@ -38,8 +40,14 @@ var StripeProvider = class {
|
|
|
38
40
|
* Checkout management
|
|
39
41
|
*/
|
|
40
42
|
this.createCheckout = async (params) => {
|
|
41
|
-
const { customer_id,
|
|
42
|
-
const checkout = await this.stripe.checkout.sessions.create({
|
|
43
|
+
const { customer_id, item_id, metadata, session_type, provider_metadata } = params;
|
|
44
|
+
const checkout = await this.stripe.checkout.sessions.create({
|
|
45
|
+
customer: customer_id,
|
|
46
|
+
metadata,
|
|
47
|
+
line_items: [{ price: item_id }],
|
|
48
|
+
mode: session_type === "one_time" ? "payment" : "subscription",
|
|
49
|
+
...provider_metadata
|
|
50
|
+
});
|
|
43
51
|
return toPaykitCheckout(checkout);
|
|
44
52
|
};
|
|
45
53
|
this.retrieveCheckout = async (id) => {
|
|
@@ -83,35 +91,51 @@ var StripeProvider = class {
|
|
|
83
91
|
/**
|
|
84
92
|
* Webhook management
|
|
85
93
|
*/
|
|
86
|
-
this.handleWebhook = async (
|
|
87
|
-
const
|
|
94
|
+
this.handleWebhook = async (params) => {
|
|
95
|
+
const { body, headers, webhookSecret } = params;
|
|
96
|
+
const stripeHeaders = headersExtractor(headers, ["x-stripe-signature"]);
|
|
97
|
+
const signature = stripeHeaders[0].value;
|
|
98
|
+
const event = this.stripe.webhooks.constructEvent(body, signature, webhookSecret);
|
|
88
99
|
if (event.type === "checkout.session.completed") {
|
|
89
100
|
const checkout = await this.retrieveCheckout(event.data.object.id);
|
|
90
|
-
|
|
101
|
+
const response = toPaykitEvent({ type: "$checkoutCreated", created: event.created, id: event.id, data: checkout });
|
|
102
|
+
return response;
|
|
91
103
|
} else if (event.type === "customer.created") {
|
|
92
|
-
const customer = await this.
|
|
93
|
-
return toPaykitEvent({ type: "
|
|
104
|
+
const customer = await this.createCustomer({ email: event.data.object.email, name: event.data.object.name ?? void 0 });
|
|
105
|
+
return toPaykitEvent({ type: "$customerCreated", created: event.created, id: event.id, data: customer });
|
|
94
106
|
} else if (event.type === "customer.updated") {
|
|
95
|
-
const
|
|
96
|
-
|
|
107
|
+
const { email, name } = event.data.object;
|
|
108
|
+
const customer = await this.updateCustomer(event.data.object.id, { email, name: name ?? void 0 });
|
|
109
|
+
return toPaykitEvent({ type: "$customerUpdated", created: event.created, id: event.id, data: customer });
|
|
97
110
|
} else if (event.type === "customer.deleted") {
|
|
98
|
-
|
|
111
|
+
await this.deleteCustomer(event.data.object.id);
|
|
112
|
+
return toPaykitEvent({ type: "$customerDeleted", created: event.created, id: event.id, data: null });
|
|
99
113
|
} else if (event.type === "customer.subscription.created") {
|
|
100
114
|
const subscription = await this.retrieveSubscription(event.data.object.id);
|
|
101
|
-
return toPaykitEvent({ type: "
|
|
115
|
+
return toPaykitEvent({ type: "$subscriptionCreated", created: event.created, id: event.id, data: subscription });
|
|
102
116
|
} else if (event.type === "customer.subscription.updated") {
|
|
103
117
|
const subscription = await this.retrieveSubscription(event.data.object.id);
|
|
104
|
-
return toPaykitEvent({ type: "
|
|
118
|
+
return toPaykitEvent({ type: "$subscriptionUpdated", created: event.created, id: event.id, data: subscription });
|
|
105
119
|
} else if (event.type === "customer.subscription.deleted") {
|
|
106
120
|
const subscription = await this.retrieveSubscription(event.data.object.id);
|
|
107
|
-
return toPaykitEvent({ type: "
|
|
121
|
+
return toPaykitEvent({ type: "$subscriptionCanceled", created: event.created, id: event.id, data: subscription });
|
|
108
122
|
} else if (event.type === "customer.subscription.paused") {
|
|
109
123
|
const subscription = await this.retrieveSubscription(event.data.object.id);
|
|
110
|
-
return toPaykitEvent({ type: "
|
|
124
|
+
return toPaykitEvent({ type: "$subscriptionUpdated", created: event.created, id: event.id, data: subscription });
|
|
125
|
+
} else if (event.type == "invoice.payment_succeeded") {
|
|
126
|
+
const checkoutId = event.data.object.metadata?.checkoutId;
|
|
127
|
+
if (!checkoutId) throw new Error("Checkout ID not found in invoice metadata");
|
|
128
|
+
const checkout = await this.retrieveCheckout(checkoutId);
|
|
129
|
+
return toPaykitEvent({
|
|
130
|
+
type: "$paymentReceived",
|
|
131
|
+
created: event.created,
|
|
132
|
+
id: event.id,
|
|
133
|
+
data: { ...checkout, checkoutId }
|
|
134
|
+
});
|
|
111
135
|
}
|
|
112
136
|
throw new Error(`Unknown event type: ${event.type}`);
|
|
113
137
|
};
|
|
114
|
-
const { apiKey, ...rest } = config;
|
|
138
|
+
const { debug, apiKey, ...rest } = config;
|
|
115
139
|
this.stripe = new Stripe(apiKey, rest);
|
|
116
140
|
}
|
|
117
141
|
};
|
|
@@ -126,7 +150,7 @@ var stripe = () => {
|
|
|
126
150
|
if (!apiKey) {
|
|
127
151
|
throw new Error("STRIPE_API_KEY is not set");
|
|
128
152
|
}
|
|
129
|
-
return createStripe({ apiKey, apiVersion: "2025-05-28.basil"
|
|
153
|
+
return createStripe({ apiKey, debug: isDev, apiVersion: "2025-05-28.basil" });
|
|
130
154
|
};
|
|
131
155
|
export {
|
|
132
156
|
createStripe,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paykit-sdk/stripe",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Stripe provider for PayKit",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
@@ -8,10 +8,6 @@
|
|
|
8
8
|
"files": [
|
|
9
9
|
"dist"
|
|
10
10
|
],
|
|
11
|
-
"scripts": {
|
|
12
|
-
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
13
|
-
"prepublishOnly": "npm run build"
|
|
14
|
-
},
|
|
15
11
|
"keywords": [
|
|
16
12
|
"stripe",
|
|
17
13
|
"paykit",
|
|
@@ -24,12 +20,12 @@
|
|
|
24
20
|
"stripe": "^18.2.1"
|
|
25
21
|
},
|
|
26
22
|
"peerDependencies": {
|
|
27
|
-
"@paykit-sdk/core": "^1.
|
|
23
|
+
"@paykit-sdk/core": "^1.1.0"
|
|
28
24
|
},
|
|
29
25
|
"devDependencies": {
|
|
30
26
|
"tsup": "^8.0.0",
|
|
31
27
|
"typescript": "^5.0.0",
|
|
32
|
-
"@paykit-sdk/core": "
|
|
28
|
+
"@paykit-sdk/core": "1.1.0"
|
|
33
29
|
},
|
|
34
30
|
"publishConfig": {
|
|
35
31
|
"access": "public"
|
|
@@ -40,5 +36,8 @@
|
|
|
40
36
|
},
|
|
41
37
|
"bugs": {
|
|
42
38
|
"url": "https://github.com/devodii/paykit/issues"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsup src/index.ts --format cjs,esm --dts"
|
|
43
42
|
}
|
|
44
|
-
}
|
|
43
|
+
}
|