npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@droplinked_inc/payment-intent

v0.1.0

Published

Hardened rebuild of [email protected]. Server-side PaymentIntent state machine + idempotency + cross-PSP defence. Pairs with @droplinked_inc/payment-hub.

Downloads

63

Readme

@droplinked_inc/payment-intent

Server-side PaymentIntent state machine, idempotency, and refund accounting for the droplinked platform. Pairs with @droplinked_inc/payment-hub (PSP orchestration).

This package is the durable record of a payment's lifecycle. It does not call out to PSPs and it does not verify webhook signatures — payment-hub and its adapters own that. What it owns is the legal state graph, idempotent creation, refund accounting under bigint arithmetic, optimistic-concurrency-protected updates, and a tamper- evident audit history.

See THREAT_MODEL.md for the eight P0 threats this package mitigates and how.

State machine

   requires_payment_method ─► requires_confirmation ─► processing
            │                          │                     │
            │                          │                     ├─► succeeded
            │                          │                     │       │
            │                          │                     │       ├─► partially_refunded ──► refunded
            │                          │                     │       └─► refunded
            │                          │                     │
            │                          │                     └─► failed
            │                          │
            └────────── canceled ◄─────┘

Terminal states (refunded, failed, canceled) have no outgoing transitions. Every transition is gated by the allowlist in state-machine.ts and goes through one private codepath (commitTransition).

Install

pnpm add @droplinked_inc/payment-intent

Quick start

import {
  PaymentIntentService,
  MemoryIdempotencyStore,
  MemoryPaymentIntentRepository,
} from '@droplinked_inc/payment-intent';

const svc = new PaymentIntentService({
  // production: inject Mongo/Redis-backed implementations
  repository: new MemoryPaymentIntentRepository(),
  idempotencyStore: new MemoryIdempotencyStore(),
});

// 1. Create
const intent = await svc.create({
  orderId: 'ord_abc',
  provider: 'stripe',
  intentType: 'payment',
  amountMinorUnits: 1999n,  // $19.99 in cents
  currency: 'USD',
  idempotencyNonce: 'checkout-button-click-uuid',
});

// 2. Walk it through the machine
await svc.confirm(intent.id);
await svc.markProcessing(intent.id);
await svc.markSucceeded(intent.id);

// 3. Refund (partial-first, then top-up)
await svc.refund({
  intentId: intent.id,
  provider: 'stripe',
  amountMinorUnits: 999n,
  currency: 'USD',
  refundEventId: 're_xyz',
  reason: 'partial-refund',
});
// state is now `partially_refunded`

await svc.refund({
  intentId: intent.id,
  provider: 'stripe',
  amountMinorUnits: 1000n,
  currency: 'USD',
  refundEventId: 're_xyz_2',
  reason: 'complete-refund',
});
// state is now `refunded` — terminal

Webhook integration

The caller — usually a route handler in the droplinked backend — is responsible for cryptographic signature verification (use @droplinked_inc/payment-hub adapters). Once verified, hand the structured event to applyWebhookEvent:

const updated = await svc.applyWebhookEvent({
  intentId: 'pi_…',
  provider: 'stripe',
  eventId: 'evt_…',       // provider-side id (used for replay defence)
  targetState: 'succeeded',
  reason: 'payment_intent.succeeded',
});

The package will:

  1. Reject if the event's provider does not match the intent's (cross-PSP defence — ProviderMismatchError).
  2. Reject if eventId was already applied (WebhookEventReplayError).
  3. Reject if the implied state is unreachable from the current state (InvalidStateTransitionError).
  4. Append the event to the immutable history array, bump version, and OCC-update the record.

Idempotency

Two derivation strategies:

  • Deterministic (idempotencyNonce or full input set): SHA-256(length-prefix(orderId, provider, amount, currency, nonce)). Re-submitting the same logical operation returns the same intent.
  • Random (no nonce supplied): 256 bits of CSPRNG entropy, hex.

Implementations of IdempotencyStore for Redis/Mongo must enforce atomic put-if-absent semantics. See src/idempotency.ts.

Errors

Every typed error extends PaymentIntentError, which extends Error. Error messages are passed through redactSecrets() so credential- shaped tokens never leak through the surface.

| Class | Code | |--------------------------------|----------------------------| | InvalidStateTransitionError | INVALID_STATE_TRANSITION | | ProviderMismatchError | PROVIDER_MISMATCH | | CurrencyMismatchError | CURRENCY_MISMATCH | | IdempotencyConflictError | IDEMPOTENCY_CONFLICT | | RefundExceedsChargeError | REFUND_EXCEEDS_CHARGE | | ConcurrentUpdateError | CONCURRENT_UPDATE | | WebhookEventReplayError | WEBHOOK_REPLAY | | WebhookSignatureError | WEBHOOK_SIGNATURE | | PaymentIntentNotFoundError | NOT_FOUND | | PaymentIntentValidationError | VALIDATION |

Test coverage

90% lines / 95% branches / 100% funcs across the package. Property-based tests (via fast-check) cover state-machine invariants and refund-sum accumulation.

Development

pnpm typecheck
pnpm lint
pnpm test
pnpm test:coverage
pnpm build

License

MIT — see monorepo root.