@paykit-sdk/polar
Version:
Polar provider for PayKit
141 lines (135 loc) • 5.02 kB
JavaScript
"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);
// src/polar-provider.ts
var import_resources2 = require("@paykit-sdk/core/src/resources");
var import_sdk = require("@polar-sh/sdk");
// lib/mapper.ts
var import_resources = require("@paykit-sdk/core/src/resources/");
var toPaykitCheckout = (checkout) => {
return {
id: checkout.id,
url: checkout.url,
cancel_url: void 0,
customer_id: checkout.customerId,
mode: "payment",
success_url: checkout.successUrl,
products: checkout.products.map((product) => ({ id: product.id, quantity: 1 })),
metadata: checkout.metadata
};
};
var toPaykitCustomer = (customer) => {
return { id: customer.id, email: customer.email, name: customer.name ?? void 0 };
};
var toPaykitSubscription = (subscription) => {
return {
id: subscription.id,
customer_id: subscription.customerId,
status: (0, import_resources.toPaykitSubscriptionStatus)(subscription.status),
current_period_start: new Date(subscription.currentPeriodStart),
current_period_end: new Date(subscription.currentPeriodEnd)
};
};
// src/polar-provider.ts
var PolarProvider = class {
constructor(config) {
this.config = config;
this.productionURL = "https://api.polar.sh";
this.sandboxURL = "https://api.sandbox.polar.sh";
/**
* Checkout management
*/
this.createCheckout = async (params) => {
const { metadata, success_url, price_id } = params;
const response = await this.polar.checkouts.create({ ...metadata && { metadata }, successUrl: success_url, products: [price_id] });
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, 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) => {
const response = await this.polar.subscriptions.revoke({ id });
return toPaykitSubscription(response);
};
this.retrieveSubscription = async (id) => {
const response = await this.polar.subscriptions.get({ id });
return toPaykitSubscription(response);
};
this.updateSubscription = async (id, params) => {
const subscription = await this.retrieveSubscription(id);
return subscription;
};
/**
* Webhook management
*/
this.handleWebhook = async (payload, signature, secret) => {
const response = await this.polar.events.get({ id: payload });
return (0, import_resources2.toPaykitEvent)({
data: response,
created: new Date(response.timestamp).getTime(),
id: response.id,
type: response.name
});
};
const { apiKey, environment = "test", ...rest } = config;
this.polar = new import_sdk.Polar({ ...rest, accessToken: apiKey, serverURL: environment === "test" ? this.sandboxURL : this.productionURL });
}
};
// src/index.ts
var createPolar = (config) => {
return new PolarProvider(config);
};
var polar = () => {
const apiKey = process.env.POLAR_ACCESS_TOKEN;
const isDev = process.env.NODE_ENV === "development";
if (!apiKey) throw new Error("POLAR_ACCESS_TOKEN is not set");
return createPolar({ apiKey, environment: isDev ? "test" : "live" });
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createPolar,
polar
});