UNPKG

@paykit-sdk/polar

Version:
223 lines (217 loc) 8.83 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { createPolar: () => createPolar, polar: () => polar }); module.exports = __toCommonJS(index_exports); var import_core3 = require("@paykit-sdk/core"); // src/polar-provider.ts var import_core2 = require("@paykit-sdk/core"); var import_sdk = require("@polar-sh/sdk"); var import_webhooks = require("@polar-sh/sdk/webhooks"); // lib/mapper.ts var import_core = require("@paykit-sdk/core"); var toPaykitCheckout = (checkout) => { return { id: checkout.id, payment_url: checkout.url, customer_id: checkout.customerId, session_type: checkout.subscriptionId ? "recurring" : "one_time", products: checkout.products.map((product) => ({ id: product.id, quantity: 1 })), metadata: checkout.metadata ?? null, currency: checkout.currency, amount: checkout.amount }; }; var toPaykitCustomer = (customer) => { return { id: customer.id, email: customer.email, name: customer.name ?? void 0 }; }; var toPaykitSubscriptionStatus = (status) => { if (status === "active") return "active"; if (status === "past_due" || status === "incomplete") return "past_due"; if (status === "canceled" || status === "unpaid") return "canceled"; if (status === "incomplete_expired") return "expired"; throw new Error(`Unhandled status: ${status}`); }; var toPaykitSubscription = (subscription) => { return { id: subscription.id, customer_id: subscription.customerId, status: toPaykitSubscriptionStatus(subscription.status), current_period_start: new Date(subscription.currentPeriodStart), current_period_end: new Date(subscription.currentPeriodEnd), metadata: (0, import_core.stringifyObjectValues)({ ...subscription.metadata ?? {}, $customFieldData: subscription.customFieldData }) }; }; var toPaykitInvoice = (invoice) => { return { id: invoice.id, amount: invoice.totalAmount, currency: invoice.currency, metadata: (0, import_core.stringifyObjectValues)({ ...invoice.metadata ?? {}, $customFieldData: invoice.customFieldData }), customer_id: invoice.customerId, billing_mode: invoice.billingMode }; }; // src/polar-provider.ts var PolarProvider = class { constructor(config) { this.config = config; this.productionURL = import_sdk.ServerList["production"]; this.sandboxURL = import_sdk.ServerList["sandbox"]; /** * Checkout management */ this.createCheckout = async (params) => { const { metadata, item_id, provider_metadata } = params; const response = await this.polar.checkouts.create({ metadata, products: [item_id], ...provider_metadata }); return toPaykitCheckout(response); }; this.retrieveCheckout = async (id) => { const response = await this.polar.checkouts.get({ id }); return toPaykitCheckout(response); }; /** * Customer management */ this.createCustomer = async (params) => { const { email, name, metadata } = params; const response = await this.polar.customers.create({ email, name, ...metadata && { metadata } }); return toPaykitCustomer(response); }; this.updateCustomer = async (id, params) => { const { email, name, metadata } = params; const response = await this.polar.customers.update({ id, customerUpdate: { ...email && { email }, ...name && { name }, ...metadata && { metadata } } }); return toPaykitCustomer(response); }; this.retrieveCustomer = async (id) => { const response = await this.polar.customers.get({ id }); return toPaykitCustomer(response); }; /** * Subscription management */ this.cancelSubscription = async (id) => { await this.polar.subscriptions.revoke({ id }); return null; }; this.retrieveSubscription = async (id) => { const response = await this.polar.subscriptions.get({ id }); return toPaykitSubscription(response); }; this.updateSubscription = async (id, params) => { const response = await this.polar.subscriptions.update({ id, subscriptionUpdate: { ...params.metadata } }); return toPaykitSubscription(response); }; /** * Webhook management */ this.handleWebhook = async (params) => { const { body, headers, webhookSecret } = params; const webhookHeaders = (0, import_core2.headersExtractor)(headers, ["webhook-id", "webhook-timestamp", "webhook-signature"]).reduce( (acc, kv) => { acc[kv.key] = Array.isArray(kv.value) ? kv.value.join(",") : kv.value; return acc; }, {} ); const { data, type } = (0, import_webhooks.validateEvent)(body, webhookHeaders, webhookSecret); const id = webhookHeaders["webhook-id"]; const timestamp = webhookHeaders["webhook-timestamp"]; const webhookHandlers = { /** * Invoice */ "order.paid": (data2) => { const { status, metadata } = data2; if (status !== "paid") return null; return (0, import_core2.toPaykitEvent)({ type: "$invoicePaid", created: parseInt(timestamp), id, data: toPaykitInvoice({ ...data2, metadata: { ...metadata ?? {} }, billingMode: "one_time" }) }); }, "order.created": (data2) => { const { billingReason, metadata, status } = data2; if (["subscription_create", "subscription_cycle"].includes(billingReason)) { return (0, import_core2.toPaykitEvent)({ type: "$invoicePaid", created: parseInt(timestamp), id, data: toPaykitInvoice({ ...data2, metadata: { ...metadata ?? {} }, billingMode: "recurring" }) }); } return null; }, /** * Customer */ "customer.created": (data2) => { return (0, import_core2.toPaykitEvent)({ type: "$customerCreated", created: parseInt(timestamp), id, data: toPaykitCustomer(data2) }); }, "customer.updated": (data2) => { return (0, import_core2.toPaykitEvent)({ type: "$customerUpdated", created: parseInt(timestamp), id, data: toPaykitCustomer(data2) }); }, "customer.deleted": () => { return (0, import_core2.toPaykitEvent)({ type: "$customerDeleted", created: parseInt(timestamp), id, data: null }); }, /** * Subscription */ "subscription.updated": (data2) => { return (0, import_core2.toPaykitEvent)({ type: "$subscriptionUpdated", created: parseInt(timestamp), id, data: toPaykitSubscription(data2) }); }, "subscription.created": (data2) => { return (0, import_core2.toPaykitEvent)({ type: "$subscriptionCreated", created: parseInt(timestamp), id, data: toPaykitSubscription(data2) }); }, "subscription.revoked": (data2) => { return (0, import_core2.toPaykitEvent)({ type: "$subscriptionCancelled", created: parseInt(timestamp), id, data: toPaykitSubscription(data2) }); } }; const handler = webhookHandlers[type]; if (!handler) throw new Error(`Unhandled event type: ${type}`); const result = handler(data); if (!result) throw new Error(`Unhandled event type: ${type}`); return result; }; const { accessToken, server, ...rest } = config; this.polar = new import_sdk.Polar({ accessToken, serverURL: server === "sandbox" ? this.sandboxURL : this.productionURL, ...rest }); } }; // src/index.ts var createPolar = (config) => { return new PolarProvider(config); }; var polar = () => { const envVars = (0, import_core3.validateEnvVars)(["POLAR_ACCESS_TOKEN"], process.env); const isDev = process.env.NODE_ENV === "development"; return createPolar({ debug: true, accessToken: envVars.POLAR_ACCESS_TOKEN, server: isDev ? "sandbox" : "production" }); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createPolar, polar });