@paykit-sdk/polar
Version:
Polar provider for PayKit
117 lines (113 loc) • 3.92 kB
JavaScript
// src/polar-provider.ts
import {
toPaykitEvent
} from "@paykit-sdk/core/src/resources";
import { Polar } from "@polar-sh/sdk";
// lib/mapper.ts
import {
toPaykitSubscriptionStatus
} from "@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: 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 toPaykitEvent({
data: response,
created: new Date(response.timestamp).getTime(),
id: response.id,
type: response.name
});
};
const { apiKey, environment = "test", ...rest } = config;
this.polar = new 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" });
};
export {
createPolar,
polar
};