UNPKG

@paykit-sdk/stripe

Version:

Stripe provider for PayKit

171 lines (165 loc) 7.6 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { createStripe: () => createStripe, stripe: () => stripe }); module.exports = __toCommonJS(index_exports); // src/stripe-provider.ts var import_resources2 = require("@paykit-sdk/core/src/resources"); var import_stripe = __toESM(require("stripe")); // lib/mapper.ts var import_resources = require("@paykit-sdk/core/src/resources"); var toPaykitCheckout = (checkout) => { return { id: checkout.id, customer_id: checkout.customer, mode: checkout.mode, success_url: checkout.success_url, cancel_url: checkout.cancel_url, products: checkout.line_items.data.map((item) => ({ id: item.price.id, quantity: item.quantity })), url: checkout.url, ...checkout.metadata && { metadata: checkout.metadata } }; }; var toPaykitCustomer = (customer) => { return { id: customer.id, email: customer.email ?? void 0, name: customer.name ?? void 0 }; }; var toPaykitSubscription = (subscription) => { return { id: subscription.id, customer_id: subscription.customer, status: (0, import_resources.toPaykitSubscriptionStatus)(subscription.status), current_period_start: new Date(subscription.start_date), current_period_end: new Date(subscription.cancel_at) }; }; // src/stripe-provider.ts var StripeProvider = class { constructor(config) { /** * Checkout management */ this.createCheckout = async (params) => { const { customer_id, price_id, quantity, ...rest } = params; const checkout = await this.stripe.checkout.sessions.create({ customer: customer_id, line_items: [{ price: price_id, quantity }], ...rest }); return toPaykitCheckout(checkout); }; this.retrieveCheckout = async (id) => { const checkout = await this.stripe.checkout.sessions.retrieve(id); return toPaykitCheckout(checkout); }; /** * Customer management */ this.createCustomer = async (params) => { const customer = await this.stripe.customers.create(params); return toPaykitCustomer(customer); }; this.updateCustomer = async (id, params) => { const customer = await this.stripe.customers.update(id, params); return toPaykitCustomer(customer); }; this.deleteCustomer = async (id) => { await this.stripe.customers.del(id); }; this.retrieveCustomer = async (id) => { const customer = await this.stripe.customers.retrieve(id); if ("deleted" in customer) return null; return toPaykitCustomer(customer); }; /** * Subscription management */ this.cancelSubscription = async (id) => { const subscription = await this.stripe.subscriptions.cancel(id); return toPaykitSubscription(subscription); }; this.updateSubscription = async (id, params) => { const subscription = await this.stripe.subscriptions.update(id, { metadata: params.metadata }); return toPaykitSubscription(subscription); }; this.retrieveSubscription = async (id) => { const subscription = await this.stripe.subscriptions.retrieve(id); return toPaykitSubscription(subscription); }; /** * Webhook management */ this.handleWebhook = async (payload, signature, secret) => { const event = this.stripe.webhooks.constructEvent(payload, signature, secret); if (event.type === "checkout.session.completed") { const checkout = await this.retrieveCheckout(event.data.object.id); return (0, import_resources2.toPaykitEvent)({ type: "checkout.created", created: event.created, id: event.id, data: checkout }); } else if (event.type === "customer.created") { const customer = await this.retrieveCustomer(event.data.object.id); return (0, import_resources2.toPaykitEvent)({ type: "customer.created", created: event.created, id: event.id, data: customer }); } else if (event.type === "customer.updated") { const customer = await this.retrieveCustomer(event.data.object.id); return (0, import_resources2.toPaykitEvent)({ type: "customer.updated", created: event.created, id: event.id, data: customer }); } else if (event.type === "customer.deleted") { return (0, import_resources2.toPaykitEvent)({ type: "customer.deleted", created: event.created, id: event.id, data: null }); } else if (event.type === "customer.subscription.created") { const subscription = await this.retrieveSubscription(event.data.object.id); return (0, import_resources2.toPaykitEvent)({ type: "subscription.created", created: event.created, id: event.id, data: subscription }); } else if (event.type === "customer.subscription.updated") { const subscription = await this.retrieveSubscription(event.data.object.id); return (0, import_resources2.toPaykitEvent)({ type: "subscription.updated", created: event.created, id: event.id, data: subscription }); } else if (event.type === "customer.subscription.deleted") { const subscription = await this.retrieveSubscription(event.data.object.id); return (0, import_resources2.toPaykitEvent)({ type: "subscription.canceled", created: event.created, id: event.id, data: subscription }); } else if (event.type === "customer.subscription.paused") { const subscription = await this.retrieveSubscription(event.data.object.id); return (0, import_resources2.toPaykitEvent)({ type: "subscription.updated", created: event.created, id: event.id, data: subscription }); } throw new Error(`Unknown event type: ${event.type}`); }; const { apiKey, ...rest } = config; this.stripe = new import_stripe.default(apiKey, rest); } }; // src/index.ts var createStripe = (config) => { return new StripeProvider(config); }; var stripe = () => { const apiKey = process.env.STRIPE_API_KEY; const isDev = process.env.NODE_ENV === "development"; if (!apiKey) { throw new Error("STRIPE_API_KEY is not set"); } return createStripe({ apiKey, apiVersion: "2025-05-28.basil", environment: isDev ? "test" : "live" }); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createStripe, stripe });