API Reference
Complete reference for the @squaredr/paykit public API — classes, interfaces, types, and utility functions.
PayKit
The main entry point. Wraps a provider adapter and exposes unified payment operations.
import { PayKit } from '@squaredr/paykit'
const paykit = new PayKit({
adapter: stripeAdapter,
})
paykit.charges // ChargeOperations
paykit.refunds // RefundOperations
paykit.customers // CustomerOperations
paykit.webhooks // WebhookOperations
paykit.provider // string — adapter name
paykit.capabilities // ProviderCapabilities
ChargeOperations
interface ChargeOperations {
create(params: CreateChargeParams): Promise<UnifiedCharge>
retrieve(id: string): Promise<UnifiedCharge>
capture(id: string, params?: CaptureParams): Promise<UnifiedCharge>
cancel(id: string, options?: RequestOptions): Promise<UnifiedCharge>
list(params?: ListParams): Promise<PaginatedList<UnifiedCharge>>
}
CreateChargeParams
interface CreateChargeParams {
amount: number // Smallest currency unit (e.g. cents)
currency: string // ISO 4217 (e.g. "usd", "inr")
source?: string // Payment source ID
customer?: string // Customer ID
description?: string
metadata?: Record<string, string>
capture?: boolean // Auto-capture (default: true)
_provider?: string // Force a specific provider (router)
idempotencyKey?: string
timeout?: number
}
UnifiedCharge
interface UnifiedCharge {
id: string
providerId: string
provider: string
amount: number
currency: string
status: ChargeStatus
description?: string
clientSecret?: string
customer?: { id: string; email?: string }
paymentMethod?: PaymentMethodSummary
metadata?: Record<string, string>
redirectUrl?: string
createdAt: Date
updatedAt: Date
_raw: unknown // Provider-specific raw response
}
type ChargeStatus =
| 'pending'
| 'requires_action'
| 'processing'
| 'succeeded'
| 'failed'
| 'canceled'
| 'refunded'
| 'partially_refunded'
RefundOperations
interface RefundOperations {
create(params: CreateRefundParams): Promise<UnifiedRefund>
retrieve(id: string): Promise<UnifiedRefund>
list(params?: ListParams): Promise<PaginatedList<UnifiedRefund>>
}
interface CreateRefundParams {
chargeId: string
amount?: number // Partial refund amount
reason?: string
metadata?: Record<string, string>
}
interface UnifiedRefund {
id: string
providerId: string
provider: string
chargeId: string
amount: number
currency: string
status: 'pending' | 'succeeded' | 'failed' | 'canceled'
reason?: string
metadata?: Record<string, string>
createdAt: Date
_raw: unknown
}
CustomerOperations
interface CustomerOperations {
create(params: CreateCustomerParams): Promise<UnifiedCustomer>
retrieve(id: string): Promise<UnifiedCustomer>
update(id: string, params: UpdateCustomerParams): Promise<UnifiedCustomer>
delete(id: string): Promise<void>
list(params?: ListParams): Promise<PaginatedList<UnifiedCustomer>>
}
interface UnifiedCustomer {
id: string
providerId: string
provider: string
email?: string
name?: string
phone?: string
metadata?: Record<string, string>
createdAt: Date
_raw: unknown
}
WebhookOperations
interface WebhookOperations {
verify(payload: string | Buffer, headers: Record<string, string>, secret: string): boolean
parse(payload: string | Buffer, headers: Record<string, string>, secret: string): UnifiedWebhookEvent
}
interface UnifiedWebhookEvent {
id: string
provider: string
type: WebhookEventType
providerType: string // Original provider event name
data: unknown
createdAt: Date
_raw: unknown
}
type WebhookEventType =
| 'charge.succeeded'
| 'charge.failed'
| 'charge.refunded'
| 'charge.disputed'
| 'refund.created'
| 'refund.completed'
| 'refund.failed'
| 'subscription.created'
| 'subscription.renewed'
| 'subscription.canceled'
| 'subscription.payment_failed'
| 'customer.created'
| 'customer.updated'
| 'payout.completed'
| 'payout.failed'
SubscriptionOperations
interface SubscriptionOperations {
create(params: CreateSubscriptionParams): Promise<UnifiedSubscription>
retrieve(id: string): Promise<UnifiedSubscription>
update(id: string, params: UpdateSubscriptionParams): Promise<UnifiedSubscription>
cancel(id: string, params?: CancelSubscriptionParams): Promise<UnifiedSubscription>
list(params?: ListParams): Promise<PaginatedList<UnifiedSubscription>>
}
interface UnifiedSubscription {
id: string
providerId: string
provider: string
customerId: string
status: SubscriptionStatus
amount: number
currency: string
interval: BillingInterval
intervalCount: number
currentPeriodStart: Date
currentPeriodEnd: Date
cancelAtPeriodEnd: boolean
metadata?: Record<string, string>
createdAt: Date
_raw: unknown
}
type SubscriptionStatus =
| 'active' | 'past_due' | 'canceled'
| 'paused' | 'trialing' | 'unpaid' | 'incomplete'
type BillingInterval = 'day' | 'week' | 'month' | 'year'
PaymentAdapter
The interface that all provider adapters implement. Use this to build custom adapters.
interface PaymentAdapter {
readonly name: string
readonly capabilities: ProviderCapabilities
initialize(credentials: ProviderCredentials): Promise<void>
healthCheck(): Promise<HealthStatus>
charges: ChargeOperations
refunds: RefundOperations
customers: CustomerOperations
paymentMethods: PaymentMethodOperations
webhooks: WebhookOperations
subscriptions?: SubscriptionOperations
payouts?: PayoutOperations
}
interface ProviderCapabilities {
charges: boolean
authAndCapture: boolean
refunds: boolean
partialRefunds: boolean
subscriptions: boolean
savedPaymentMethods: boolean
hostedCheckout: boolean
embeddableUI: boolean
payouts: boolean
multiCurrency: boolean
directDebit: boolean
webhooks: boolean
threeDS: boolean
}
Error Handling
class PaymentError extends Error {
readonly code: PaymentErrorCode
readonly provider: string
readonly providerCode?: string
readonly providerMessage?: string
readonly isRetryable: boolean
readonly httpStatus?: number
readonly _raw?: unknown
}
class NotSupportedError extends PaymentError {
// Thrown when an adapter doesn't support an operation
}
type PaymentErrorCode =
| 'card_declined'
| 'insufficient_funds'
| 'invalid_card'
| 'expired_card'
| 'processing_error'
| 'authentication_required'
| 'rate_limit'
| 'invalid_request'
| 'provider_unavailable'
| 'not_supported'
| 'duplicate_transaction'
| 'fraud_detected'
| 'currency_not_supported'
| 'amount_too_small'
| 'amount_too_large'
Currency Utilities
import { toSmallestUnit, fromSmallestUnit } from '@squaredr/paykit'
// Convert human-readable amounts to smallest unit
toSmallestUnit(10, 'USD') // 1000 (cents)
toSmallestUnit(500, 'INR') // 50000 (paise)
toSmallestUnit(1000, 'JPY') // 1000 (yen — zero-decimal)
// Convert back
fromSmallestUnit(1000, 'USD') // 10
fromSmallestUnit(50000, 'INR') // 500
Common Types
interface PaginatedList<T> {
data: T[]
hasMore: boolean
totalCount?: number
}
interface ListParams {
limit?: number
startingAfter?: string
endingBefore?: string
}
interface RequestOptions {
idempotencyKey?: string
timeout?: number
}