Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@payark/sdk",
"version": "0.1.1",
"version": "0.1.6",
"description": "Official TypeScript SDK for the PayArk payment gateway API.",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down Expand Up @@ -40,7 +40,8 @@
"@types/node": "^20.0.0"
},
"dependencies": {
"@effect/platform": "0.94.2",
"effect": "3.19.15"
"@effect/platform": "^0.94.5",
"@payark/sdk-effect": "^0.1.5",
"effect": "^3.19.19"
}
}
28 changes: 28 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { WebhooksResource } from "./resources/webhooks";
import { Projects } from "./resources/projects";
import { CustomersResource } from "./resources/customers";
import { SubscriptionsResource } from "./resources/subscriptions";
import { AutomationResource } from "./resources/automation";
import { TokensResource } from "./resources/tokens";
import type { PayArkConfig } from "./types";

/**
Expand Down Expand Up @@ -76,6 +78,8 @@ export class PayArk {
private _projects?: Projects;
private _customers?: CustomersResource;
private _subscriptions?: SubscriptionsResource;
private _automation?: AutomationResource;
private _tokens?: TokensResource;

/**
* Create a new PayArk client.
Expand Down Expand Up @@ -159,4 +163,28 @@ export class PayArk {
}
return this._subscriptions;
}

/**
* Automation resource.
*
* Use this to trigger reminders and reapers.
*/
get automation(): AutomationResource {
if (!this._automation) {
this._automation = new AutomationResource(this.http);
}
return this._automation;
}

/**
* Tokens resource.
*
* Use this to manage Personal Access Tokens (PAT).
*/
get tokens(): TokensResource {
if (!this._tokens) {
this._tokens = new TokensResource(this.http);
}
return this._tokens;
}
}
16 changes: 13 additions & 3 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
import { Cause, Exit, Duration, Option } from "effect";

/** SDK version – injected at build time for User-Agent header. */
const SDK_VERSION = "0.1.0";
const SDK_VERSION = "0.1.4";

/** Supported HTTP methods for the PayArk API. */
export type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
Expand Down Expand Up @@ -218,14 +218,24 @@ export class HttpClient {
err._tag === "ResponseError"
) {
const responseError = err as any;
// 1. Try to parse JSON problem body (RFC 7807) or standard JSON error
const errorBody = yield* responseError.response.json.pipe(
Effect.catchAll(() => Effect.succeed(undefined)),
// 2. Fall back to raw text if it's not JSON (e.g., 502 Bad Gateway from Nginx)
Effect.catchAll(() =>
responseError.response.text.pipe(
Effect.map((text: string) => ({
error: text.length > 0 ? text.slice(0, 512) : undefined,
})),
Effect.catchAll(() => Effect.succeed(undefined)),
),
),
);

return yield* Effect.fail(
PayArkError.generate(
responseError.response.status,
errorBody,
errorBody?.error,
errorBody?.detail || errorBody?.error,
),
);
}
Expand Down
36 changes: 30 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
// ── Main client ────────────────────────────────────────────────────────────
export { PayArk } from "./client";

// ── Functional Resources ───────────────────────────────────────────────────
export * from "./resources/checkout";
export * from "./resources/payments";
export * from "./resources/customers";
export * from "./resources/subscriptions";
export * from "./resources/automation";
export * from "./resources/tokens";
export * from "./resources/projects";
export * from "./resources/webhooks";

// ── Error classes ──────────────────────────────────────────────────────────
export {
PayArkError,
Expand All @@ -26,19 +36,33 @@ export type { PayArkErrorCode } from "./errors";
export type {
// Config
PayArkConfig,
// Models
Customer,
Payment,
Subscription,
Project,
Token,
Metadata,
// Primitives
Provider,
PaymentStatus,
SubscriptionStatus,
SubscriptionInterval,
// Checkout
CreateCheckoutParams,
CheckoutSession,
// Customers
CreateCustomerParams,
UpdateCustomerParams,
ListCustomersParams,
// Payments
Payment,
PaymentStatus,
ListPaymentsParams,
// Subscriptions
CreateSubscriptionParams,
ListSubscriptionsParams,
// Pagination
PaginatedResponse,
PaginationMeta,
// Project
Project,
// Provider
Provider,
// Webhooks
WebhookEvent,
WebhookEventType,
Expand Down
62 changes: 62 additions & 0 deletions src/resources/automation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// ---------------------------------------------------------------------------
// PayArk SDK – Automation Resource
// ---------------------------------------------------------------------------
// Management of automated billing tasks (reminders, reapers).
// ---------------------------------------------------------------------------

import type { HttpClient } from "../http";
import type { PayArkClient } from "./customers";

/**
* Trigger subscription reminders for upcoming renewals.
*/
export async function dispatchReminders(
client: PayArkClient,
cronSecret: string,
): Promise<{ message: string; count: number }> {
return client.http.request<{ message: string; count: number }>(
"POST",
"/v1/automation/reminders",
{
headers: { "x-cron-secret": cronSecret },
},
);
}

/**
* Trigger the subscription reaper to handle expired subscriptions.
*/
export async function runReaper(
client: PayArkClient,
cronSecret: string,
): Promise<{ message: string; count: number }> {
return client.http.request<{ message: string; count: number }>(
"POST",
"/v1/automation/reaper",
{
headers: { "x-cron-secret": cronSecret },
},
);
}

// ── Legacy Resource Class ──────────────────────────────────────────────────

/**
* Resource class for PayArk Automation.
* @deprecated Use functional exports instead.
*/
export class AutomationResource {
constructor(public readonly http: HttpClient) {}

async dispatchReminders(
cronSecret: string,
): Promise<{ message: string; count: number }> {
return dispatchReminders(this, cronSecret);
}

async runReaper(
cronSecret: string,
): Promise<{ message: string; count: number }> {
return runReaper(this, cronSecret);
}
}
30 changes: 30 additions & 0 deletions src/resources/customers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,32 @@ export async function deleteCustomer(
);
}

/**
* High-level helper to either create a new customer or update an existing one
* based on the `merchant_customer_id`.
*/
export async function createOrUpdateCustomer(
client: PayArkClient,
params: CreateCustomerParams,
): Promise<Customer> {
const existingList = await listCustomers(client, {
merchant_customer_id: params.merchant_customer_id,
limit: 1,
});

if (existingList.data.length > 0) {
const existing = existingList.data[0];
return updateCustomer(client, existing.id, {
email: params.email,
name: params.name,
phone: params.phone,
metadata: params.metadata,
});
}

return createCustomer(client, params);
}

// ── Legacy Resource Class ──────────────────────────────────────────────────

/**
Expand Down Expand Up @@ -132,4 +158,8 @@ export class CustomersResource {
async delete(id: string): Promise<void> {
return deleteCustomer(this, id);
}

async createOrUpdate(params: CreateCustomerParams): Promise<Customer> {
return createOrUpdateCustomer(this, params);
}
}
82 changes: 82 additions & 0 deletions src/resources/tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// ---------------------------------------------------------------------------
// PayArk SDK – Tokens Resource
// ---------------------------------------------------------------------------
// Management of Personal Access Tokens (PAT).
// ---------------------------------------------------------------------------

import type { HttpClient } from "../http";
import type { Token } from "../types";
import type { PayArkClient } from "./customers";

/**
* Create a new personal access token.
* Requires User Session (Supabase JWT).
*/
export async function createToken(
client: PayArkClient,
params: {
name: string;
scopes?: string[];
expires_in_days?: number;
},
userToken: string,
): Promise<Token & { token: string }> {
return client.http.request<Token & { token: string }>("POST", "/v1/tokens", {
body: params,
headers: { Authorization: `Bearer ${userToken}` },
});
}

/**
* List all tokens belonging to the authenticated user.
*/
export async function listTokens(
client: PayArkClient,
userToken: string,
): Promise<Token[]> {
return client.http.request<Token[]>("GET", "/v1/tokens", {
headers: { Authorization: `Bearer ${userToken}` },
});
}

/**
* Delete a token by its ID.
*/
export async function deleteToken(
client: PayArkClient,
id: string,
userToken: string,
): Promise<void> {
return client.http.request<void>(
"DELETE",
`/v1/tokens/${encodeURIComponent(id)}`,
{
headers: { Authorization: `Bearer ${userToken}` },
},
);
}

// ── Legacy Resource Class ──────────────────────────────────────────────────

/**
* Resource class for PayArk Tokens.
* @deprecated Use functional exports instead.
*/
export class TokensResource {
constructor(public readonly http: HttpClient) {}

async create(
params: { name: string; scopes?: string[]; expires_in_days?: number },
userToken: string,
): Promise<Token & { token: string }> {
return createToken(this, params, userToken);
}

async list(userToken: string): Promise<Token[]> {
return listTokens(this, userToken);
}

async delete(id: string, userToken: string): Promise<void> {
return deleteToken(this, id, userToken);
}
}
10 changes: 10 additions & 0 deletions src/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as S from "@payark/sdk-effect/schemas";

/**
* Unified Schemas for the PayArk SDK.
*
* These schemas are derived from the Effect-based industrial API
* to ensure 100% type-parity across all SDK variants.
*/

export * from "@payark/sdk-effect/schemas";
Loading