How junaidshahzad3/pk-pay works
1. Repository shape
This is a TypeScript monorepo with two real runtime surfaces: the core SDK in packages/pk-pay and a Next.js playground in apps/playground. The rest of the repo is documentation, examples, CI, and release automation.
2. Primary product boundary: pk-pay SDK
packages/pk-pay is the production library. It exposes a single public entrypoint in src/index.ts and is the main abstraction layer for payment integrations. The SDK’s purpose is to normalize multiple gateway-specific implementations behind one API shape.
3. Provider adapter layer
The SDK contains separate provider modules for JazzCash, EasyPaisa, and Stripe under src/providers/*. These are the gateway-specific adapters that translate the SDK’s unified operations into provider-specific request/response formats, signatures, and workflow details.
4. Framework integration layer
The src/middleware directory provides thin adapters for Express, Fastify, and Next.js. These are integration boundaries, not business logic: they make the SDK usable inside different server runtimes without forcing a single web framework.
5. Shared utility layer
Cross-cutting concerns live in src/utils:
- crypto.ts for security-sensitive signing/verification logic
- date.ts for time handling
- idempotency.ts for preventing duplicate operations
- retry.ts for transient failure handling
These utilities support provider consistency and reduce duplicated gateway logic.
6. Types and contract surface
src/types defines the shared domain contracts used across providers and middleware. Architecturally, this is the glue that keeps the SDK’s unified API stable while allowing each provider implementation to vary internally.
7. Playground application
apps/playground is a separate Next.js 15 app used to exercise the SDK and payment flows in a live, interactive environment. It is not the core library; it is a testing and demonstration surface for real integrations.
8. Playground request flow
Inside the playground, src/lib holds UI and server-action helpers, while src/app/api/events/[id]/route.ts and src/app/api/webhook/[id]/route.ts expose backend endpoints. The intended flow is: user inputs credentials/config in the UI, server-side actions initiate a payment or test handshake, and webhook/event endpoints capture asynchronous gateway callbacks.
9. Realtime/event delivery
The README states the playground has a real-time webhook/event feed backed by Upstash Redis. That means the playground includes an external event distribution layer for observing payment callbacks and transaction progress during live testing.
10. Examples as integration references
examples/full-checkout-demo and the docs examples in packages/pk-pay/docs/examples act as reference implementations. They show how consumers wire the SDK into plain browser code, Express, or Next.js, which helps define the SDK’s integration boundary.
11. Testing strategy
The SDK has a focused test suite under packages/pk-pay/tests covering provider behavior, middleware integrations, and utility functions. This indicates the architecture is centered on contract verification across adapters rather than only unit testing isolated helpers.
12. Build, publish, and deployment
The repo includes GitHub Actions for CI and publishing, plus vercel.json for deployment configuration of the playground. So the operational model is: library built and published from the monorepo, while the playground is deployed as a separate app for hands-on validation.
13. How to draw the architecture
A good diagram would show:
- consumers/frameworks on the left
- middleware adapters as the integration boundary
- the pk-pay core in the center
- provider adapters branching to JazzCash/EasyPaisa/Stripe on the right
- shared utilities underneath as cross-cutting dependencies
- the playground as a parallel app that calls into the SDK and receives webhooks/events through its own API routes and Redis-backed feed.
Main entry points: src/index.ts SDK entry · actions.ts Server actions · types/index.ts Types · jazzcash/index.ts JazzCash
Components 24
pk-pay SDK
Production payment library and its adapters
- SDK entry · Single export surface for the payment SDK
packages/pk-pay/src/index.ts - Types · Shared domain contracts and API shapes
packages/pk-pay/src/types/index.ts - Crypto · Signing and verification helpers
packages/pk-pay/src/utils/crypto.ts - Dates · Time and date handling helpers
packages/pk-pay/src/utils/date.ts - Idempotency · Duplicate-operation protection
packages/pk-pay/src/utils/idempotency.ts - Retry · Transient-failure retry helpers
packages/pk-pay/src/utils/retry.ts - Express · Express integration boundary
packages/pk-pay/src/middleware/express/index.ts - Fastify · Fastify integration boundary
packages/pk-pay/src/middleware/fastify/index.ts - Next.js · Next.js integration boundary
packages/pk-pay/src/middleware/nextjs/index.ts - JazzCash · Provider-specific payment implementation
packages/pk-pay/src/providers/jazzcash/index.ts - EasyPaisa · Provider-specific payment implementation
packages/pk-pay/src/providers/easypaisa/index.ts - Stripe · Provider-specific payment implementation
packages/pk-pay/src/providers/stripe/index.ts
Playground
Next.js app for live integration testing and demos
- Playground UI · Interactive UI for configuring and exercising flows
apps/playground/src/lib/PlaygroundUI.tsx - Server actions · Server-side helpers that initiate playground flows
apps/playground/src/lib/actions.ts - Schemas · Shared input and payload schemas
apps/playground/src/lib/schemas.ts - Events API · Event ingestion endpoint for callback tracking
apps/playground/src/app/api/events/[id]/route.ts - Webhook API · Webhook endpoint for asynchronous gateway callbacks
apps/playground/src/app/api/webhook/[id]/route.ts - Upstash Redis · Realtime event distribution backing the playground feed
References
Examples and docs that show intended usage
- Checkout demo · Standalone browser checkout reference implementation
examples/full-checkout-demo/index.js - Express example · SDK usage example for Express integrations
packages/pk-pay/docs/examples/express.ts - Next.js example · SDK usage example for Next.js integrations
packages/pk-pay/docs/examples/nextjs.ts
Ops
CI, publishing, and deployment configuration
- CI · Continuous integration checks
.github/workflows/ci.yml - Publish · Library publishing pipeline
.github/workflows/publish.yml - Vercel config · Deployment configuration for the playground
vercel.json
Connections 32
- SDK entry → Types: uses · Public API is shaped by shared contracts
- SDK entry → Crypto: depends on · Core API relies on signing and verification helpers
- SDK entry → Dates: depends on · Core API relies on time handling helpers
- SDK entry → Idempotency: depends on · Core API relies on duplicate-operation protection
- SDK entry → Retry: depends on · Core API relies on retry helpers
- SDK entry → Express: exposes · Public SDK exports Express integration
- SDK entry → Fastify: exposes · Public SDK exports Fastify integration
- SDK entry → Next.js: exposes · Public SDK exports Next.js integration
- SDK entry → JazzCash: routes to · Unified API dispatches to JazzCash adapter
- SDK entry → EasyPaisa: routes to · Unified API dispatches to EasyPaisa adapter
- SDK entry → Stripe: routes to · Unified API dispatches to Stripe adapter
- Express → Types: uses · Middleware shares SDK contracts
- Fastify → Types: uses · Middleware shares SDK contracts
- Next.js → Types: uses · Middleware shares SDK contracts
- JazzCash → Crypto: uses · Provider-specific signatures and verification
- JazzCash → Retry: uses · Provider calls are retried when needed
- EasyPaisa → Crypto: uses · Provider-specific signatures and verification
- Stripe → Idempotency: uses · Provider operations are deduplicated
- Playground UI → Server actions: invokes · User input triggers server-side actions
- Server actions → SDK entry: calls · Playground actions exercise the SDK
- Server actions → Schemas: validates · Actions use shared request schemas
- Events API → Upstash Redis: publishes · Events endpoint feeds the realtime stream
- Webhook API → Upstash Redis: publishes · Webhook callbacks feed the realtime stream
- Server actions → Events API: observes · Actions are reflected in the event feed
- Server actions → Webhook API: observes · Actions are reflected in webhook handling
- Checkout demo → SDK entry: consumes · Browser demo integrates the SDK directly
- Express example → Express: demonstrates · Docs show Express integration
- Next.js example → Next.js: demonstrates · Docs show Next.js integration
- CI → SDK entry: builds · CI validates the SDK surface
- CI → Playground UI: checks · CI validates the playground app
- Publish → SDK entry: publishes · Release workflow publishes the library
- Vercel config → Playground UI: deploys · Vercel config targets the playground app