@paykit-sdk/stripe 1.0.0 → 1.0.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/dist/index.d.mts +7 -6
- package/dist/index.d.ts +7 -6
- package/dist/index.js +31 -13
- package/dist/index.mjs +30 -13
- package/package.json +6 -7
package/dist/index.d.mts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { PayKitProvider } from '@paykit-sdk/core/src/paykit-provider';
|
|
2
|
-
import { CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, WebhookEventPayload } from '@paykit-sdk/core/src/resources';
|
|
3
|
-
import {
|
|
2
|
+
import { CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, WebhookProviderPayload, WebhookEventPayload } from '@paykit-sdk/core/src/resources';
|
|
3
|
+
import { PaykitProviderOptions } from '@paykit-sdk/core/src/types';
|
|
4
4
|
import Stripe from 'stripe';
|
|
5
5
|
|
|
6
|
-
interface StripeConfig extends
|
|
7
|
-
|
|
6
|
+
interface StripeConfig extends PaykitProviderOptions<Stripe.StripeConfig> {
|
|
7
|
+
apiKey: string;
|
|
8
8
|
}
|
|
9
9
|
declare class StripeProvider implements PayKitProvider {
|
|
10
10
|
private stripe;
|
|
@@ -30,9 +30,10 @@ declare class StripeProvider implements PayKitProvider {
|
|
|
30
30
|
/**
|
|
31
31
|
* Webhook management
|
|
32
32
|
*/
|
|
33
|
-
handleWebhook: (
|
|
33
|
+
handleWebhook: (params: WebhookProviderPayload) => Promise<WebhookEventPayload>;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
declare const createStripe: (config: StripeConfig) => StripeProvider;
|
|
36
37
|
declare const stripe: () => StripeProvider;
|
|
37
38
|
|
|
38
|
-
export { stripe };
|
|
39
|
+
export { createStripe, stripe };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { PayKitProvider } from '@paykit-sdk/core/src/paykit-provider';
|
|
2
|
-
import { CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, WebhookEventPayload } from '@paykit-sdk/core/src/resources';
|
|
3
|
-
import {
|
|
2
|
+
import { CreateCheckoutParams, Checkout, CreateCustomerParams, Customer, UpdateCustomerParams, Subscription, UpdateSubscriptionParams, WebhookProviderPayload, WebhookEventPayload } from '@paykit-sdk/core/src/resources';
|
|
3
|
+
import { PaykitProviderOptions } from '@paykit-sdk/core/src/types';
|
|
4
4
|
import Stripe from 'stripe';
|
|
5
5
|
|
|
6
|
-
interface StripeConfig extends
|
|
7
|
-
|
|
6
|
+
interface StripeConfig extends PaykitProviderOptions<Stripe.StripeConfig> {
|
|
7
|
+
apiKey: string;
|
|
8
8
|
}
|
|
9
9
|
declare class StripeProvider implements PayKitProvider {
|
|
10
10
|
private stripe;
|
|
@@ -30,9 +30,10 @@ declare class StripeProvider implements PayKitProvider {
|
|
|
30
30
|
/**
|
|
31
31
|
* Webhook management
|
|
32
32
|
*/
|
|
33
|
-
handleWebhook: (
|
|
33
|
+
handleWebhook: (params: WebhookProviderPayload) => Promise<WebhookEventPayload>;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
declare const createStripe: (config: StripeConfig) => StripeProvider;
|
|
36
37
|
declare const stripe: () => StripeProvider;
|
|
37
38
|
|
|
38
|
-
export { stripe };
|
|
39
|
+
export { createStripe, stripe };
|
package/dist/index.js
CHANGED
|
@@ -30,12 +30,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
+
createStripe: () => createStripe,
|
|
33
34
|
stripe: () => stripe
|
|
34
35
|
});
|
|
35
36
|
module.exports = __toCommonJS(index_exports);
|
|
36
37
|
|
|
37
38
|
// src/stripe-provider.ts
|
|
38
39
|
var import_resources2 = require("@paykit-sdk/core/src/resources");
|
|
40
|
+
var import_webhook = require("@paykit-sdk/core/src/tools/webhook");
|
|
39
41
|
var import_stripe = __toESM(require("stripe"));
|
|
40
42
|
|
|
41
43
|
// lib/mapper.ts
|
|
@@ -44,12 +46,13 @@ var toPaykitCheckout = (checkout) => {
|
|
|
44
46
|
return {
|
|
45
47
|
id: checkout.id,
|
|
46
48
|
customer_id: checkout.customer,
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
49
|
+
session_type: checkout.mode === "subscription" ? "recurring" : "one_time",
|
|
50
|
+
// todo: handle `setup` mode
|
|
51
|
+
payment_url: checkout.url,
|
|
50
52
|
products: checkout.line_items.data.map((item) => ({ id: item.price.id, quantity: item.quantity })),
|
|
51
|
-
|
|
52
|
-
|
|
53
|
+
currency: checkout.currency,
|
|
54
|
+
amount: checkout.amount_total,
|
|
55
|
+
metadata: checkout.metadata
|
|
53
56
|
};
|
|
54
57
|
};
|
|
55
58
|
var toPaykitCustomer = (customer) => {
|
|
@@ -72,8 +75,14 @@ var StripeProvider = class {
|
|
|
72
75
|
* Checkout management
|
|
73
76
|
*/
|
|
74
77
|
this.createCheckout = async (params) => {
|
|
75
|
-
const { customer_id,
|
|
76
|
-
const checkout = await this.stripe.checkout.sessions.create({
|
|
78
|
+
const { customer_id, item_id, metadata, session_type, provider_metadata } = params;
|
|
79
|
+
const checkout = await this.stripe.checkout.sessions.create({
|
|
80
|
+
customer: customer_id,
|
|
81
|
+
metadata,
|
|
82
|
+
line_items: [{ price: item_id }],
|
|
83
|
+
mode: session_type === "one_time" ? "payment" : "subscription",
|
|
84
|
+
...provider_metadata
|
|
85
|
+
});
|
|
77
86
|
return toPaykitCheckout(checkout);
|
|
78
87
|
};
|
|
79
88
|
this.retrieveCheckout = async (id) => {
|
|
@@ -117,18 +126,25 @@ var StripeProvider = class {
|
|
|
117
126
|
/**
|
|
118
127
|
* Webhook management
|
|
119
128
|
*/
|
|
120
|
-
this.handleWebhook = async (
|
|
121
|
-
const
|
|
129
|
+
this.handleWebhook = async (params) => {
|
|
130
|
+
const { body, headers, webhookSecret } = params;
|
|
131
|
+
const stripeHeaders = (0, import_webhook.headersExtractor)(headers, ["x-stripe-signature"]);
|
|
132
|
+
const signature = stripeHeaders[0].value;
|
|
133
|
+
const event = this.stripe.webhooks.constructEvent(body, signature, webhookSecret);
|
|
122
134
|
if (event.type === "checkout.session.completed") {
|
|
123
135
|
const checkout = await this.retrieveCheckout(event.data.object.id);
|
|
124
136
|
return (0, import_resources2.toPaykitEvent)({ type: "checkout.created", created: event.created, id: event.id, data: checkout });
|
|
125
137
|
} else if (event.type === "customer.created") {
|
|
126
|
-
const customer = await this.
|
|
138
|
+
const customer = await this.createCustomer({ email: event.data.object.email, name: event.data.object.name ?? void 0 });
|
|
127
139
|
return (0, import_resources2.toPaykitEvent)({ type: "customer.created", created: event.created, id: event.id, data: customer });
|
|
128
140
|
} else if (event.type === "customer.updated") {
|
|
129
|
-
const customer = await this.
|
|
141
|
+
const customer = await this.updateCustomer(event.data.object.id, {
|
|
142
|
+
email: event.data.object.email,
|
|
143
|
+
name: event.data.object.name ?? void 0
|
|
144
|
+
});
|
|
130
145
|
return (0, import_resources2.toPaykitEvent)({ type: "customer.updated", created: event.created, id: event.id, data: customer });
|
|
131
146
|
} else if (event.type === "customer.deleted") {
|
|
147
|
+
await this.deleteCustomer(event.data.object.id);
|
|
132
148
|
return (0, import_resources2.toPaykitEvent)({ type: "customer.deleted", created: event.created, id: event.id, data: null });
|
|
133
149
|
} else if (event.type === "customer.subscription.created") {
|
|
134
150
|
const subscription = await this.retrieveSubscription(event.data.object.id);
|
|
@@ -145,7 +161,7 @@ var StripeProvider = class {
|
|
|
145
161
|
}
|
|
146
162
|
throw new Error(`Unknown event type: ${event.type}`);
|
|
147
163
|
};
|
|
148
|
-
const { apiKey, ...rest } = config;
|
|
164
|
+
const { debug, apiKey, ...rest } = config;
|
|
149
165
|
this.stripe = new import_stripe.default(apiKey, rest);
|
|
150
166
|
}
|
|
151
167
|
};
|
|
@@ -156,12 +172,14 @@ var createStripe = (config) => {
|
|
|
156
172
|
};
|
|
157
173
|
var stripe = () => {
|
|
158
174
|
const apiKey = process.env.STRIPE_API_KEY;
|
|
175
|
+
const isDev = process.env.NODE_ENV === "development";
|
|
159
176
|
if (!apiKey) {
|
|
160
177
|
throw new Error("STRIPE_API_KEY is not set");
|
|
161
178
|
}
|
|
162
|
-
return createStripe({ apiKey, apiVersion: "2025-05-28.basil" });
|
|
179
|
+
return createStripe({ apiKey, debug: isDev, apiVersion: "2025-05-28.basil" });
|
|
163
180
|
};
|
|
164
181
|
// Annotate the CommonJS export names for ESM import in node:
|
|
165
182
|
0 && (module.exports = {
|
|
183
|
+
createStripe,
|
|
166
184
|
stripe
|
|
167
185
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import {
|
|
3
3
|
toPaykitEvent
|
|
4
4
|
} from "@paykit-sdk/core/src/resources";
|
|
5
|
+
import { headersExtractor } from "@paykit-sdk/core/src/tools/webhook";
|
|
5
6
|
import Stripe from "stripe";
|
|
6
7
|
|
|
7
8
|
// lib/mapper.ts
|
|
@@ -10,12 +11,13 @@ 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,18 +91,25 @@ 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
|
return toPaykitEvent({ type: "checkout.created", created: event.created, id: event.id, data: checkout });
|
|
91
102
|
} else if (event.type === "customer.created") {
|
|
92
|
-
const customer = await this.
|
|
103
|
+
const customer = await this.createCustomer({ email: event.data.object.email, name: event.data.object.name ?? void 0 });
|
|
93
104
|
return toPaykitEvent({ type: "customer.created", created: event.created, id: event.id, data: customer });
|
|
94
105
|
} else if (event.type === "customer.updated") {
|
|
95
|
-
const customer = await this.
|
|
106
|
+
const customer = await this.updateCustomer(event.data.object.id, {
|
|
107
|
+
email: event.data.object.email,
|
|
108
|
+
name: event.data.object.name ?? void 0
|
|
109
|
+
});
|
|
96
110
|
return toPaykitEvent({ type: "customer.updated", created: event.created, id: event.id, data: customer });
|
|
97
111
|
} else if (event.type === "customer.deleted") {
|
|
112
|
+
await this.deleteCustomer(event.data.object.id);
|
|
98
113
|
return toPaykitEvent({ type: "customer.deleted", created: event.created, id: event.id, data: null });
|
|
99
114
|
} else if (event.type === "customer.subscription.created") {
|
|
100
115
|
const subscription = await this.retrieveSubscription(event.data.object.id);
|
|
@@ -111,7 +126,7 @@ var StripeProvider = class {
|
|
|
111
126
|
}
|
|
112
127
|
throw new Error(`Unknown event type: ${event.type}`);
|
|
113
128
|
};
|
|
114
|
-
const { apiKey, ...rest } = config;
|
|
129
|
+
const { debug, apiKey, ...rest } = config;
|
|
115
130
|
this.stripe = new Stripe(apiKey, rest);
|
|
116
131
|
}
|
|
117
132
|
};
|
|
@@ -122,11 +137,13 @@ var createStripe = (config) => {
|
|
|
122
137
|
};
|
|
123
138
|
var stripe = () => {
|
|
124
139
|
const apiKey = process.env.STRIPE_API_KEY;
|
|
140
|
+
const isDev = process.env.NODE_ENV === "development";
|
|
125
141
|
if (!apiKey) {
|
|
126
142
|
throw new Error("STRIPE_API_KEY is not set");
|
|
127
143
|
}
|
|
128
|
-
return createStripe({ apiKey, apiVersion: "2025-05-28.basil" });
|
|
144
|
+
return createStripe({ apiKey, debug: isDev, apiVersion: "2025-05-28.basil" });
|
|
129
145
|
};
|
|
130
146
|
export {
|
|
147
|
+
createStripe,
|
|
131
148
|
stripe
|
|
132
149
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paykit-sdk/stripe",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1-alpha.1",
|
|
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",
|
|
@@ -29,7 +25,7 @@
|
|
|
29
25
|
"devDependencies": {
|
|
30
26
|
"tsup": "^8.0.0",
|
|
31
27
|
"typescript": "^5.0.0",
|
|
32
|
-
"@paykit-sdk/core": "
|
|
28
|
+
"@paykit-sdk/core": "1.0.1-alpha.1"
|
|
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
|
+
}
|