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, and for payment providers a store has added itself.
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 GC Admin. 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. A request refused before it reaches the payment provider — an invalid body, an unknown order, or an amount that does not match the order — does not use up its Idempotency-Key, so the corrected request can be sent with the same key. While another request with the same key is still being initialized, the call returns 409 idempotency_in_progress; sending it again shortly with the same key returns the session once it exists. An order that is already paid returns 409 order_already_paid, and a cancelled order returns 409 order_not_payable. The payment is taken in the order’s own currency: a currency that differs from it returns 400 payment_currency_mismatch, and omitting currency uses the order’s.
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.

Taking a Stripe payment on your own page

initializePayment with mode: 'embedded' (Stripe only; any other provider answers 400 invalid_request) returns a client_secret and a publishable_key instead of a checkout_url, so the payment form is mounted in the storefront’s own page and takes cards, Apple Pay, Google Pay and Link. No success_url or cancel_url is needed. The default, mode: 'redirect', returns the hosted checkout_url.
The order settles from the payment confirmation; verifyPayment accepts the reference or the payment_intent_id. Repeating the request with the same Idempotency-Key while the payment is open returns the same client_secret with idempotent: true. A publishable key from a different mode than the secret key (test and live) is refused with 409 provider_misconfigured. Apple Pay appears on the hosts registered for the store in its payment settings.

Keeping a card for Subscribe & Save

save_payment_method: true keeps the shopper’s card so later Subscribe & Save deliveries can be charged without the shopper present. It applies only when order_id names an order that carries subscribe_save and was placed by a signed-in shopper, and only for stripe and paystack; it is ignored otherwise. The shopper sees the processor’s own page and agrees to the card being kept, and the card details never reach your site. The response’s renewal_mode is off_session when the card is kept and confirm when the shopper pays each later delivery from a link sent to them.

verifyPayment

Retrieve the absolute source of truth for a transaction. This is a read operation and does not require HMAC signing. A call made with a secret key returns X-Signature and X-Signature-Timestamp when the store has a signing secret, so a server can confirm that the payment status is the one Galactic Core produced. See Verify a response. Verification reads one store’s own payment providers, so a marketplace operator key receives 403. A marketplace checkout is settled by the processor’s notification; a signed-in shopper’s orders and their payment status are on GET /v1/customers/me.
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.
For Stripe, the reference returned by initializePayment and its session_id identify the same payment, and either may be passed; a PaymentIntent id (pi_...) or charge id (ch_...) taken from a Stripe event is accepted too. The response names the Checkout Session in session_id and the order the payment was initialized for in order_id, and returns reference exactly as it was passed.
Response

Response shape (per-provider oneOf)

The response shape depends on the provider field. The Stripe shape is shown above; the other providers return:
Status semantics by provider:
  • Stripe — success when the Checkout Session is paid, otherwise Stripe’s own payment_status (unpaid, no_payment_required, etc.). amount is in major currency units; order_id is null when the payment was initialized without an order.
  • 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.
  • PayPal — verify captures the buyer-approved order and returns success once the capture completes. The reference you pass is the PayPal order id returned at initialization; the response returns the payment reference the order was initialized with and the captured id in paypal_order_id.
  • A store’s own payment provider — provider is the identifier listed by getPaymentMethods, the same one initializePayment accepts. The response carries the reference, the status the provider reported (unknown when it reported none) and custom_provider: true.

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