punch-library
v0.1.4
Published
Framework-agnostic payment orchestration library for Node.js with Punch CLI
Maintainers
Readme
punch-library
Framework-agnostic payment orchestration library for Node.js with a punch CLI.
Documentation is powered by Docsify.
Package
- npm:
https://www.npmjs.com/package/punch-library - GitHub:
https://github.com/oguzhan18/punch-library
Install
# npm
npm install punch-library
# yarn
yarn add punch-library
# pnpm
pnpm add punch-libraryMaintainer
- Name: Oguzhan Cart
- Website:
https://oguzhancart.dev - Email:
[email protected] - LinkedIn:
https://www.linkedin.com/in/o%C4%9Fuzhan-%C3%A7art-b73405199/ - GitHub:
http://github.com/oguzhan18 - Medium:
https://medium.com/@oguzhancart1 - CodePen:
https://codepen.io/oguzhan1881
Features
- Unified payment API across providers (
create/capture/cancel/refund) - Provider-level
custom-operationAPI to access advanced provider endpoints beyond unified contract - Provider catalog with payment/webhook documentation links
- Programmatic webhook parsing API (
parseWebhook) - Idempotency key support (
x-idempotency-keyoridempotencyKey) - Correlation ID support (
x-correlation-id) + structured HTTP logs - Phase-3 platform controls:
- Provider rate limiting
- Provider circuit breaker
- Webhook idempotency handling
- Webhook dead-letter queue (DLQ)
- Outbox event store (memory or file)
- Stripe support:
- Payment intent creation
- Capture, cancel, refund
- Webhook signature verification (
stripe-signature)
- PayPal support:
- OAuth2 token flow
- Order create/capture + capture refund
- Webhook signature verification (verify-webhook-signature API)
- Adyen support:
- Payment create/capture/cancel/refund
- Webhook signature verification (HMAC)
- Square support:
- Payment create/capture/cancel/refund
- Webhook signature verification
Supported Providers
stripe, paypal, adyen, square, braintree, checkout, worldpay, razorpay, paddle, twocheckout, iyzico, paytr, parampos, sipay, hepsipay
Current Status
Full live API + webhook support is not complete for all providers yet.
- Implemented:
stripe,paypal,adyen,square - Stub adapters: all other providers (visible in catalog)
Setup
npm install
npm run buildRun Docs (Docsify)
npm run docs:serveThen open:
http://localhost:3004
Vercel Deployment (Docsify)
Vercel is configured to deploy Docsify static docs (not NestJS runtime) via vercel.json.
Library API
Instantiate the library:
import { Punch } from 'punch-api';
const punch = new Punch({
exposeUnconfiguredProviders: false,
stateStoreMode: 'memory'
});Main methods:
listProviders()listCatalog()createIntent()/createPayment()capturePayment()cancelPayment()refundPayment()executeCustomOperation()parseWebhook()listOutbox()/clearOutbox()listWebhookDlq()/replayWebhookDlqItem()/clearWebhookDlq()resilienceSnapshot()
Example intent payload:
{
"provider": "stripe",
"amount": 1000,
"currency": "TRY",
"idempotencyKey": "pay-create-ord-42",
"customerId": "cust_1",
"metadata": {
"orderId": "ORD-42"
}
}Example custom operation payload:
{
"endpoint": "/payment_intents",
"method": "POST",
"bodyType": "form",
"idempotencyKey": "custom-op-42",
"body": {
"amount": 1000,
"currency": "usd",
"automatic_payment_methods[enabled]": true
}
}Environment Variables
PUNCH_STRIPE_API_KEYPUNCH_STRIPE_WEBHOOK_SECRET(required for Stripe webhook verification)PUNCH_PAYPAL_CLIENT_ID,PUNCH_PAYPAL_CLIENT_SECRET,PUNCH_PAYPAL_WEBHOOK_IDPUNCH_ADYEN_API_KEY,PUNCH_ADYEN_MERCHANT_ACCOUNTPUNCH_ADYEN_HMAC_KEY(required for Adyen webhook verification)PUNCH_SQUARE_API_KEY,PUNCH_SQUARE_LOCATION_IDPUNCH_SQUARE_WEBHOOK_SIGNATURE_KEY,PUNCH_SQUARE_WEBHOOK_NOTIFICATION_URLPUNCH_<PROVIDER>_API_KEYfor other providers- Stub provider live mode (for
braintree,checkout,worldpay,razorpay,paddle,twocheckout,iyzico,paytr,parampos,sipay,hepsipay):- If API key includes
_mock_, mock mode is used. - If API key does not include
_mock_, these are required:PUNCH_<PROVIDER>_CREATE_ENDPOINTPUNCH_<PROVIDER>_CAPTURE_ENDPOINTPUNCH_<PROVIDER>_CANCEL_ENDPOINTPUNCH_<PROVIDER>_REFUND_ENDPOINT
- Optional:
PUNCH_<PROVIDER>_AUTH_MODE(bearer|basic|api_key)PUNCH_<PROVIDER>_BASIC_USERNAME,PUNCH_<PROVIDER>_BASIC_PASSWORDPUNCH_<PROVIDER>_API_KEY_HEADERPUNCH_<PROVIDER>_IDEMPOTENCY_HEADERPUNCH_<PROVIDER>_WEBHOOK_SIGNATURE_REQUIREDPUNCH_<PROVIDER>_WEBHOOK_SIGNATURE_HEADERPUNCH_<PROVIDER>_WEBHOOK_SIGNATURE_SECRETPUNCH_<PROVIDER>_WEBHOOK_SIGNATURE_ALGORITHM
- If API key includes
- Optional base URLs for custom operation mode:
PUNCH_STRIPE_BASE_URLPUNCH_PAYPAL_BASE_URLPUNCH_ADYEN_BASE_URLPUNCH_SQUARE_BASE_URLPUNCH_CHECKOUT_BASE_URLPUNCH_WORLDPAY_BASE_URLPUNCH_RAZORPAY_BASE_URLPUNCH_PADDLE_BASE_URLPUNCH_IYZICO_BASE_URLPUNCH_PAYTR_BASE_URLPUNCH_BRAINTREE_BASE_URLPUNCH_TWOCHECKOUT_BASE_URLPUNCH_PARAMPOS_BASE_URLPUNCH_SIPAY_BASE_URLPUNCH_HEPSIPAY_BASE_URL
PUNCH_EXPOSE_UNCONFIGURED_PROVIDERS=trueto expose unconfigured providers in API responsesPUNCH_RATE_LIMIT_WINDOW_MS,PUNCH_RATE_LIMIT_MAXPUNCH_CIRCUIT_FAILURE_THRESHOLD,PUNCH_CIRCUIT_OPEN_MSPUNCH_WEBHOOK_IDEMPOTENCY_TTL_MSPUNCH_WEBHOOK_MAX_ATTEMPTS,PUNCH_WEBHOOK_RETRY_DELAY_MSPUNCH_STATE_STORE_MODE(memoryorfile)PUNCH_STATE_STORE_DIR(default.punch-state)
CLI (Direct punch ... Usage)
Link the CLI once:
npm run cli:linkThen use it directly:
punch list
punch status
punch new stripe
punch new paypal --key=live_or_sandbox_key
punch remove stripe
punch doctor
punch doctor --fix
punch template stripeDisable CLI banner if needed:
PUNCH_NO_BANNER=true punch statusCLI commands:
punch new <provider>- Creates/updates provider API key in
.env - Adds provider-specific extra keys for implemented providers (Stripe/PayPal/Adyen/Square)
- Updates
PUNCH_ENABLED_PROVIDERS - Creates/updates
punch.providers.json --key=...to provide a custom API key--no-enableto skip enabling provider inPUNCH_ENABLED_PROVIDERS
- Creates/updates provider API key in
punch status [--json]- Shows provider configuration status from
.envandpunch.providers.json
- Shows provider configuration status from
punch list [--json]- Lists provider, env key, and docs URL
punch remove <provider>- Removes provider key and enablement from
.env - Removes provider from
punch.providers.json
- Removes provider key and enablement from
punch doctor [--json]- Validates
.envandpunch.providers.jsonconsistency - Returns exit code
1on critical errors
- Validates
punch doctor --fix [--json]- Auto-creates missing
.env - Backfills
PUNCH_ENABLED_PROVIDERSfrompunch.providers.jsonwhen missing - Adds mock API keys for enabled providers that are missing keys
- Synchronizes
punch.providers.jsonwithPUNCH_ENABLED_PROVIDERS
- Auto-creates missing
punch template <provider> [--print]- Generates provider-specific custom operation templates
- Writes template to
punch.templates/<provider>.json --printreturns JSON template to stdout
Unlink when needed:
npm run cli:unlinkLibrary Usage
import { PunchClient } from 'punch-api';
const client = new PunchClient();
const payment = await client.createPayment({
provider: 'stripe',
amount: 1200,
currency: 'TRY'
});
await client.refundPayment({
provider: 'stripe',
paymentId: payment.id,
amount: 1200,
currency: 'TRY'
});
await client.executeCustomOperation({
provider: 'stripe',
endpoint: '/customers',
method: 'POST',
bodyType: 'form',
body: {
email: '[email protected]'
}
});
import { Punch } from 'punch-api';
const punch = new Punch({ stateStoreMode: 'file' });
const providers = punch.listProviders();
console.log(providers);Tests
test/provider-registry.spec.ts: configured provider filteringtest/stripe-webhook.spec.ts: Stripe webhook signature verificationtest/cli.spec.ts: CLI integration checks (new/remove/doctor/template)test/payment-provider-resilience.spec.ts: rate limit + circuit breaker checkstest/payment-webhook-idempotency.spec.ts: webhook duplicate detectiontest/payments.service.phase3.spec.ts: outbox + DLQ behaviortest/provider-definition-of-done.spec.ts: per-provider Definition of Done contract checkstest/persistent-storage.spec.ts: file-backed outbox + DLQ persistence checkstest/stub-provider-standardization.spec.ts: standardized lifecycle on stub providerstest/payments.e2e.ts: e2e API flow checks
Quality gates:
- GitHub Actions CI:
.github/workflows/ci.yml(build + unit/integration + e2e on Node 20/22)
Release
Before publish:
npm run release:preflightPublish to npm:
npm login
npm run publish:npmIf your account requires publish 2FA:
NPM_OTP=123456 npm run publish:npmPublish with Yarn Berry:
yarn npm login
npm run publish:yarnPublish with Yarn Classic:
npm run publish:yarn:classicDocumentation Files
- Docsify app entry:
docs/index.html - Sidebar:
docs/_sidebar.md - Navbar:
docs/_navbar.md - Home page:
docs/README.md - Technical reference:
docs/technical-documentation.md
