Web SDK (TypeScript)

Full contract reference: see clients/CONTRACT.md in the backend repo.

Installation

npm install @onemyposmate/sdk

Quick Start

const client = new OneMyPosMateClient({
  baseUrl: 'https://payus.co.nz',
  system: 'pos',
});

const resp = await client.payments.payNow({
  grandTotal: '10.00',
  referenceId: 'REF-001',
  branchId: 17,
  configId: 123,
  channel: 'WECHAT',
});

Authentication Flow

// 1. Activate (one-time)
await client.auth.activate('ABC123');

// 2. Token is cached in TokenStore (localStorage by default)
// Every subsequent call auto-refreshes if within 60s of expiry
const token = await client.auth.currentToken();

// 3. Single-flight Promise cache prevents concurrent refreshes

Error Handling

try {
  await client.payments.payNow(request);
} catch (e) {
  if (e instanceof OneMyPosMateError) {
    switch (e.errorCode) {
      case 'ERR_GATEWAY_NOT_FOUND': // Terminal not configured
      case 'ERR_DUPLICATE_TRANSACTION': // referenceId reused
      case 'ERR_UNAUTHORIZED': // Token expired, SDK auto-retried
    }
  }
}

Common Error Codes

CodeHTTPMeaning
ERR_GATEWAY_NOT_FOUND422Terminal gateway not configured
ERR_DUPLICATE_TRANSACTION409referenceId already used
ERR_REFUND_EXCEEDED400Refund amount > remaining balance
ERR_UNAUTHORIZED401Token expired (SDK auto-retries once)
ERR_FORBIDDEN403Tenant isolation violation
ERR_RATE_LIMITED429Too many requests
ERR_SYSTEM_ERROR500Server error (SDK retries 3x)