@henrylabs-interview/payments
v0.2.27
Published
A lightweight payments SDK for creating and confirming checkouts, with built-in webhook support and an embeddable checkout UI.
Readme
Henry Labs – Interview: Payment Processor
A lightweight payments SDK for creating and confirming checkouts, with built-in webhook support and an embeddable checkout UI.
This SDK simulates a real-world payment processing system, including:
- Fraud detection
- Transient system errors
- Retry scenarios
- Asynchronous authorization flows
It is designed to test robustness, error handling, and webhook integration.
Installation
bun installBuild:
bun run buildOverview
The SDK provides:
- Checkout creation
- Checkout confirmation
- Webhook endpoint registration
- Embeddable checkout UI (frontend)
- API key validation
The system performs internal validation, fraud screening, and simulated load conditions.
Operations may:
- Succeed immediately
- Resolve asynchronously
- Require retries
- Fail due to fraud controls
- Fail due to temporary system overload
Usage
Initialize (Backend)
import { PaymentProcessor } from '@henrylabs-interview/payments';
const processor = new PaymentProcessor({
apiKey: ...,
});Checkout API
Available under:
processor.checkout;Create a Checkout
const response = await processor.checkout.create({
amount: 1000,
currency: 'USD',
customerId: 'cust_123',
});Confirm a Checkout (Backend)
const response = await processor.checkout.confirm({
checkoutId: ...,
type: 'raw-card',
data: {
number: ...,
expMonth: XX,
expYear: XXXX,
cvc: XXX,
},
});Embedded Checkout (Frontend)
The SDK provides an optional embeddable checkout UI for collecting card details in the browser.
Initialize Embedded Checkout
import { EmbeddedCheckout } from '@henrylabs-interview/payments';
const embedded = new EmbeddedCheckout({
checkoutId: 'chk_123',
});Render Embedded Checkout
embedded.render('#checkout-container', (paymentToken) => {
console.log('Received payment token:', paymentToken);
// Send token to backend for confirmation
});Parameters
| Parameter | Description |
| -------------------- | ------------------------------------- |
| containerElementId | ID of DOM element to render into |
| callbackFn | Called with a generated payment token |
Behavior
- Renders a secure card collection UI
- Collects card number, expiry, and CVC
- Generates a payment token
- Returns the token via callback
- Token should be sent to your backend for confirmation
The embedded checkout is intended for browser environments only.
Webhooks
Available under:
processor.webhooks;Register Webhook Endpoint
processor.webhooks.createEndpoint({
url: 'https://example.com/webhooks',
event: 'checkout.confirm',
secret: 'whsec_...',
});Webhook Events
Events may be emitted for:
checkout.createcheckout.confirmcheckout.successcheckout.failure
Events are triggered for:
- Immediate outcomes
- Asynchronous resolutions
- Retry scenarios
Webhook Security
If a secret is provided:
- Deliveries are signed
- Consumers must verify signatures
- Handlers must be idempotent
Retries may occur if delivery fails.
Disclaimer
This SDK is intended for evaluation and sandbox purposes only.
It does not process real payments and should not be used in production.
