@paykit-sdk/stripe 1.0.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.
@@ -0,0 +1,38 @@
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 { WithPaymentProviderConfig } from '@paykit-sdk/core/src/types';
4
+ import Stripe from 'stripe';
5
+
6
+ interface StripeConfig extends WithPaymentProviderConfig<Stripe.StripeConfig> {
7
+ apiVersion?: Stripe.LatestApiVersion;
8
+ }
9
+ declare class StripeProvider implements PayKitProvider {
10
+ private stripe;
11
+ constructor(config: StripeConfig);
12
+ /**
13
+ * Checkout management
14
+ */
15
+ createCheckout: (params: CreateCheckoutParams) => Promise<Checkout>;
16
+ retrieveCheckout: (id: string) => Promise<Checkout>;
17
+ /**
18
+ * Customer management
19
+ */
20
+ createCustomer: (params: CreateCustomerParams) => Promise<Customer>;
21
+ updateCustomer: (id: string, params: UpdateCustomerParams) => Promise<Customer>;
22
+ deleteCustomer: (id: string) => Promise<void>;
23
+ retrieveCustomer: (id: string) => Promise<Customer | null>;
24
+ /**
25
+ * Subscription management
26
+ */
27
+ cancelSubscription: (id: string) => Promise<Subscription>;
28
+ updateSubscription: (id: string, params: UpdateSubscriptionParams) => Promise<Subscription>;
29
+ retrieveSubscription: (id: string) => Promise<Subscription>;
30
+ /**
31
+ * Webhook management
32
+ */
33
+ handleWebhook: (payload: string, signature: string, secret: string) => Promise<WebhookEventPayload>;
34
+ }
35
+
36
+ declare const stripe: () => StripeProvider;
37
+
38
+ export { stripe };
@@ -0,0 +1,38 @@
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 { WithPaymentProviderConfig } from '@paykit-sdk/core/src/types';
4
+ import Stripe from 'stripe';
5
+
6
+ interface StripeConfig extends WithPaymentProviderConfig<Stripe.StripeConfig> {
7
+ apiVersion?: Stripe.LatestApiVersion;
8
+ }
9
+ declare class StripeProvider implements PayKitProvider {
10
+ private stripe;
11
+ constructor(config: StripeConfig);
12
+ /**
13
+ * Checkout management
14
+ */
15
+ createCheckout: (params: CreateCheckoutParams) => Promise<Checkout>;
16
+ retrieveCheckout: (id: string) => Promise<Checkout>;
17
+ /**
18
+ * Customer management
19
+ */
20
+ createCustomer: (params: CreateCustomerParams) => Promise<Customer>;
21
+ updateCustomer: (id: string, params: UpdateCustomerParams) => Promise<Customer>;
22
+ deleteCustomer: (id: string) => Promise<void>;
23
+ retrieveCustomer: (id: string) => Promise<Customer | null>;
24
+ /**
25
+ * Subscription management
26
+ */
27
+ cancelSubscription: (id: string) => Promise<Subscription>;
28
+ updateSubscription: (id: string, params: UpdateSubscriptionParams) => Promise<Subscription>;
29
+ retrieveSubscription: (id: string) => Promise<Subscription>;
30
+ /**
31
+ * Webhook management
32
+ */
33
+ handleWebhook: (payload: string, signature: string, secret: string) => Promise<WebhookEventPayload>;
34
+ }
35
+
36
+ declare const stripe: () => StripeProvider;
37
+
38
+ export { stripe };
package/dist/index.js ADDED
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ stripe: () => stripe
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+
37
+ // src/stripe-provider.ts
38
+ var import_resources2 = require("@paykit-sdk/core/src/resources");
39
+ var import_stripe = __toESM(require("stripe"));
40
+
41
+ // lib/mapper.ts
42
+ var import_resources = require("@paykit-sdk/core/src/resources");
43
+ var toPaykitCheckout = (checkout) => {
44
+ return {
45
+ id: checkout.id,
46
+ customer_id: checkout.customer,
47
+ mode: checkout.mode,
48
+ success_url: checkout.success_url,
49
+ cancel_url: checkout.cancel_url,
50
+ products: checkout.line_items.data.map((item) => ({ id: item.price.id, quantity: item.quantity })),
51
+ url: checkout.url,
52
+ ...checkout.metadata && { metadata: checkout.metadata }
53
+ };
54
+ };
55
+ var toPaykitCustomer = (customer) => {
56
+ return { id: customer.id, email: customer.email ?? void 0, name: customer.name ?? void 0 };
57
+ };
58
+ var toPaykitSubscription = (subscription) => {
59
+ return {
60
+ id: subscription.id,
61
+ customer_id: subscription.customer,
62
+ status: (0, import_resources.toPaykitSubscriptionStatus)(subscription.status),
63
+ current_period_start: new Date(subscription.start_date),
64
+ current_period_end: new Date(subscription.cancel_at)
65
+ };
66
+ };
67
+
68
+ // src/stripe-provider.ts
69
+ var StripeProvider = class {
70
+ constructor(config) {
71
+ /**
72
+ * Checkout management
73
+ */
74
+ this.createCheckout = async (params) => {
75
+ const { customer_id, price_id, quantity, ...rest } = params;
76
+ const checkout = await this.stripe.checkout.sessions.create({ customer: customer_id, line_items: [{ price: price_id, quantity }], ...rest });
77
+ return toPaykitCheckout(checkout);
78
+ };
79
+ this.retrieveCheckout = async (id) => {
80
+ const checkout = await this.stripe.checkout.sessions.retrieve(id);
81
+ return toPaykitCheckout(checkout);
82
+ };
83
+ /**
84
+ * Customer management
85
+ */
86
+ this.createCustomer = async (params) => {
87
+ const customer = await this.stripe.customers.create(params);
88
+ return toPaykitCustomer(customer);
89
+ };
90
+ this.updateCustomer = async (id, params) => {
91
+ const customer = await this.stripe.customers.update(id, params);
92
+ return toPaykitCustomer(customer);
93
+ };
94
+ this.deleteCustomer = async (id) => {
95
+ await this.stripe.customers.del(id);
96
+ };
97
+ this.retrieveCustomer = async (id) => {
98
+ const customer = await this.stripe.customers.retrieve(id);
99
+ if ("deleted" in customer) return null;
100
+ return toPaykitCustomer(customer);
101
+ };
102
+ /**
103
+ * Subscription management
104
+ */
105
+ this.cancelSubscription = async (id) => {
106
+ const subscription = await this.stripe.subscriptions.cancel(id);
107
+ return toPaykitSubscription(subscription);
108
+ };
109
+ this.updateSubscription = async (id, params) => {
110
+ const subscription = await this.stripe.subscriptions.update(id, { metadata: params.metadata });
111
+ return toPaykitSubscription(subscription);
112
+ };
113
+ this.retrieveSubscription = async (id) => {
114
+ const subscription = await this.stripe.subscriptions.retrieve(id);
115
+ return toPaykitSubscription(subscription);
116
+ };
117
+ /**
118
+ * Webhook management
119
+ */
120
+ this.handleWebhook = async (payload, signature, secret) => {
121
+ const event = this.stripe.webhooks.constructEvent(payload, signature, secret);
122
+ if (event.type === "checkout.session.completed") {
123
+ const checkout = await this.retrieveCheckout(event.data.object.id);
124
+ return (0, import_resources2.toPaykitEvent)({ type: "checkout.created", created: event.created, id: event.id, data: checkout });
125
+ } else if (event.type === "customer.created") {
126
+ const customer = await this.retrieveCustomer(event.data.object.id);
127
+ return (0, import_resources2.toPaykitEvent)({ type: "customer.created", created: event.created, id: event.id, data: customer });
128
+ } else if (event.type === "customer.updated") {
129
+ const customer = await this.retrieveCustomer(event.data.object.id);
130
+ return (0, import_resources2.toPaykitEvent)({ type: "customer.updated", created: event.created, id: event.id, data: customer });
131
+ } else if (event.type === "customer.deleted") {
132
+ return (0, import_resources2.toPaykitEvent)({ type: "customer.deleted", created: event.created, id: event.id, data: null });
133
+ } else if (event.type === "customer.subscription.created") {
134
+ const subscription = await this.retrieveSubscription(event.data.object.id);
135
+ return (0, import_resources2.toPaykitEvent)({ type: "subscription.created", created: event.created, id: event.id, data: subscription });
136
+ } else if (event.type === "customer.subscription.updated") {
137
+ const subscription = await this.retrieveSubscription(event.data.object.id);
138
+ return (0, import_resources2.toPaykitEvent)({ type: "subscription.updated", created: event.created, id: event.id, data: subscription });
139
+ } else if (event.type === "customer.subscription.deleted") {
140
+ const subscription = await this.retrieveSubscription(event.data.object.id);
141
+ return (0, import_resources2.toPaykitEvent)({ type: "subscription.canceled", created: event.created, id: event.id, data: subscription });
142
+ } else if (event.type === "customer.subscription.paused") {
143
+ const subscription = await this.retrieveSubscription(event.data.object.id);
144
+ return (0, import_resources2.toPaykitEvent)({ type: "subscription.updated", created: event.created, id: event.id, data: subscription });
145
+ }
146
+ throw new Error(`Unknown event type: ${event.type}`);
147
+ };
148
+ const { apiKey, ...rest } = config;
149
+ this.stripe = new import_stripe.default(apiKey, rest);
150
+ }
151
+ };
152
+
153
+ // src/index.ts
154
+ var createStripe = (config) => {
155
+ return new StripeProvider(config);
156
+ };
157
+ var stripe = () => {
158
+ const apiKey = process.env.STRIPE_API_KEY;
159
+ if (!apiKey) {
160
+ throw new Error("STRIPE_API_KEY is not set");
161
+ }
162
+ return createStripe({ apiKey, apiVersion: "2025-05-28.basil" });
163
+ };
164
+ // Annotate the CommonJS export names for ESM import in node:
165
+ 0 && (module.exports = {
166
+ stripe
167
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,132 @@
1
+ // src/stripe-provider.ts
2
+ import {
3
+ toPaykitEvent
4
+ } from "@paykit-sdk/core/src/resources";
5
+ import Stripe from "stripe";
6
+
7
+ // lib/mapper.ts
8
+ import { toPaykitSubscriptionStatus } from "@paykit-sdk/core/src/resources";
9
+ var toPaykitCheckout = (checkout) => {
10
+ return {
11
+ id: checkout.id,
12
+ customer_id: checkout.customer,
13
+ mode: checkout.mode,
14
+ success_url: checkout.success_url,
15
+ cancel_url: checkout.cancel_url,
16
+ products: checkout.line_items.data.map((item) => ({ id: item.price.id, quantity: item.quantity })),
17
+ url: checkout.url,
18
+ ...checkout.metadata && { metadata: checkout.metadata }
19
+ };
20
+ };
21
+ var toPaykitCustomer = (customer) => {
22
+ return { id: customer.id, email: customer.email ?? void 0, name: customer.name ?? void 0 };
23
+ };
24
+ var toPaykitSubscription = (subscription) => {
25
+ return {
26
+ id: subscription.id,
27
+ customer_id: subscription.customer,
28
+ status: toPaykitSubscriptionStatus(subscription.status),
29
+ current_period_start: new Date(subscription.start_date),
30
+ current_period_end: new Date(subscription.cancel_at)
31
+ };
32
+ };
33
+
34
+ // src/stripe-provider.ts
35
+ var StripeProvider = class {
36
+ constructor(config) {
37
+ /**
38
+ * Checkout management
39
+ */
40
+ this.createCheckout = async (params) => {
41
+ const { customer_id, price_id, quantity, ...rest } = params;
42
+ const checkout = await this.stripe.checkout.sessions.create({ customer: customer_id, line_items: [{ price: price_id, quantity }], ...rest });
43
+ return toPaykitCheckout(checkout);
44
+ };
45
+ this.retrieveCheckout = async (id) => {
46
+ const checkout = await this.stripe.checkout.sessions.retrieve(id);
47
+ return toPaykitCheckout(checkout);
48
+ };
49
+ /**
50
+ * Customer management
51
+ */
52
+ this.createCustomer = async (params) => {
53
+ const customer = await this.stripe.customers.create(params);
54
+ return toPaykitCustomer(customer);
55
+ };
56
+ this.updateCustomer = async (id, params) => {
57
+ const customer = await this.stripe.customers.update(id, params);
58
+ return toPaykitCustomer(customer);
59
+ };
60
+ this.deleteCustomer = async (id) => {
61
+ await this.stripe.customers.del(id);
62
+ };
63
+ this.retrieveCustomer = async (id) => {
64
+ const customer = await this.stripe.customers.retrieve(id);
65
+ if ("deleted" in customer) return null;
66
+ return toPaykitCustomer(customer);
67
+ };
68
+ /**
69
+ * Subscription management
70
+ */
71
+ this.cancelSubscription = async (id) => {
72
+ const subscription = await this.stripe.subscriptions.cancel(id);
73
+ return toPaykitSubscription(subscription);
74
+ };
75
+ this.updateSubscription = async (id, params) => {
76
+ const subscription = await this.stripe.subscriptions.update(id, { metadata: params.metadata });
77
+ return toPaykitSubscription(subscription);
78
+ };
79
+ this.retrieveSubscription = async (id) => {
80
+ const subscription = await this.stripe.subscriptions.retrieve(id);
81
+ return toPaykitSubscription(subscription);
82
+ };
83
+ /**
84
+ * Webhook management
85
+ */
86
+ this.handleWebhook = async (payload, signature, secret) => {
87
+ const event = this.stripe.webhooks.constructEvent(payload, signature, secret);
88
+ if (event.type === "checkout.session.completed") {
89
+ const checkout = await this.retrieveCheckout(event.data.object.id);
90
+ return toPaykitEvent({ type: "checkout.created", created: event.created, id: event.id, data: checkout });
91
+ } else if (event.type === "customer.created") {
92
+ const customer = await this.retrieveCustomer(event.data.object.id);
93
+ return toPaykitEvent({ type: "customer.created", created: event.created, id: event.id, data: customer });
94
+ } else if (event.type === "customer.updated") {
95
+ const customer = await this.retrieveCustomer(event.data.object.id);
96
+ return toPaykitEvent({ type: "customer.updated", created: event.created, id: event.id, data: customer });
97
+ } else if (event.type === "customer.deleted") {
98
+ return toPaykitEvent({ type: "customer.deleted", created: event.created, id: event.id, data: null });
99
+ } else if (event.type === "customer.subscription.created") {
100
+ const subscription = await this.retrieveSubscription(event.data.object.id);
101
+ return toPaykitEvent({ type: "subscription.created", created: event.created, id: event.id, data: subscription });
102
+ } else if (event.type === "customer.subscription.updated") {
103
+ const subscription = await this.retrieveSubscription(event.data.object.id);
104
+ return toPaykitEvent({ type: "subscription.updated", created: event.created, id: event.id, data: subscription });
105
+ } else if (event.type === "customer.subscription.deleted") {
106
+ const subscription = await this.retrieveSubscription(event.data.object.id);
107
+ return toPaykitEvent({ type: "subscription.canceled", created: event.created, id: event.id, data: subscription });
108
+ } else if (event.type === "customer.subscription.paused") {
109
+ const subscription = await this.retrieveSubscription(event.data.object.id);
110
+ return toPaykitEvent({ type: "subscription.updated", created: event.created, id: event.id, data: subscription });
111
+ }
112
+ throw new Error(`Unknown event type: ${event.type}`);
113
+ };
114
+ const { apiKey, ...rest } = config;
115
+ this.stripe = new Stripe(apiKey, rest);
116
+ }
117
+ };
118
+
119
+ // src/index.ts
120
+ var createStripe = (config) => {
121
+ return new StripeProvider(config);
122
+ };
123
+ var stripe = () => {
124
+ const apiKey = process.env.STRIPE_API_KEY;
125
+ if (!apiKey) {
126
+ throw new Error("STRIPE_API_KEY is not set");
127
+ }
128
+ return createStripe({ apiKey, apiVersion: "2025-05-28.basil" });
129
+ };
130
+ export {
131
+ stripe
132
+ };
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@paykit-sdk/stripe",
3
+ "version": "1.0.0",
4
+ "description": "Stripe provider for PayKit",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.esm.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsup src/index.ts --format cjs,esm --dts",
13
+ "prepublishOnly": "npm run build"
14
+ },
15
+ "keywords": [
16
+ "stripe",
17
+ "paykit",
18
+ "payment",
19
+ "typescript"
20
+ ],
21
+ "author": "Emmanuel Odii",
22
+ "license": "ISC",
23
+ "dependencies": {
24
+ "stripe": "^18.2.1"
25
+ },
26
+ "peerDependencies": {
27
+ "@paykit-sdk/core": "^1.0.0"
28
+ },
29
+ "devDependencies": {
30
+ "tsup": "^8.0.0",
31
+ "typescript": "^5.0.0",
32
+ "@paykit-sdk/core": "workspace:*"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "git+https://github.com/devodii/paykit.git"
40
+ },
41
+ "bugs": {
42
+ "url": "https://github.com/devodii/paykit/issues"
43
+ }
44
+ }