Skip to main content
The PaymentsService class (accessed via client.payments) handles the core initialization and status verification for diverse payment providers including Stripe, PayPal, Paystack, and M-Pesa.
Sandbox test mode: When you use a tybrite_sk_test_* key, all payment providers are automatically switched to their test/sandbox credentials — regardless of what your store’s payment settings show. Stripe uses test mode, PayPal uses its sandbox, Paystack uses test keys, and M-Pesa targets the Safaricom sandbox. No code changes are needed between sandbox and production; swap the key and the right environment follows. Every response includes a Tybrite-Environment: sandbox | production header confirming which environment resolved.

🔐 Security & Signing (HMAC & Idempotency)

To prevent unauthorized payment initiation and duplicate charges, initializePayment requires both an HMAC-SHA256 signature and a mandatory Idempotency Key.

Signing & Idempotency Process

  1. Generate ID: Create a unique key for the transaction (e.g., payment-{order_id}-{timestamp}).
  2. Generate Timestamp: Get the current Unix timestamp in seconds.
  3. Prepare Payload: Concatenate timestamp and JSON body: timestamp + "." + JSON_body.
  4. Generate Signature: Compute HMAC-SHA256 signature using your HMAC Secret.
  5. Base64 Encode: The signature must be Base64 encoded.

Code Implementation (TypeScript)

Financial Integrity: Payment initialization triggers money movement. The Idempotency-Key prevents duplicate sessions and charges. Requests missing this header will return 400 missing_idempotency_key.

Methods

getPaymentMethods

Retrieve all active payment methods configured in your Tybrite Admin Panel. This endpoint accepts both publishable (pk_*) and secret (sk_*) API keys — storefronts can safely call it client-side to discover available providers before checkout.
Response
Each method in the returned list includes:

initializePayment

Create a payment session with your chosen provider. Requires a Secret Key, a valid HMAC signature, and a mandatory Idempotency-Key. On the first successful call this returns 201 Created; subsequent calls with the same idempotency key and identical payload return 200 OK with idempotent: true and the original session.
The mandatory Idempotency-Key also makes this call eligible for the SDK’s automatic retries: a transient failure is retried safely and returns the original session rather than starting a second charge.
Secret Key required. initializePayment will reject publishable keys with 403. Never call this from untrusted client environments — keep your sk_* key and HMAC secret server-side.

verifyPayment

Retrieve the absolute source of truth for a transaction. This is a read operation and does not require HMAC signing.
Secret Key required. verifyPayment will reject publishable keys with 403. Verification responses include charge details (amounts, customer email, payment intents) and must not be exposed to untrusted clients.

Response shape (per-provider oneOf)

The response shape depends on the provider field — one of four variants is returned:
Status semantics by provider:
  • Stripesuccess when the session/intent is paid, otherwise the raw Stripe payment_status (unpaid, no_payment_required, etc.).
  • Paystack — Mirrors Paystack’s status field (success, failed, abandoned).
  • M-Pesa — Enum: pending | success | cancelled | failed. Result code 1032 (user cancelled on phone) maps to cancelled.
  • PayPalverify captures the buyer-approved order and returns success once the capture completes. The reference you pass is the PayPal order id returned at initialization.

Provider Support Matrix


Formatting Tip: For M-Pesa, always provide the phone number in international format without the + prefix (e.g., 2547...).

Response Codes

initializePayment

verifyPayment

getPaymentMethods