Nylon PayNylon Pay

Collect Payment

Initiate a payment collection with event-driven status tracking

collectPayment(request)

Initiates a payment collection and returns a PaymentInstance immediately. Use the event listeners to track status changes in real time.

const payment = await nylonpay.collectPayment(request);

Request Fields

FieldTypeRequiredDescription
amountnumberyesPayment amount in smallest currency unit. Minimum 500 UGX
currencyCurrencyyesISO 4217 currency code (e.g., "UGX")
descriptionstringyesPayment description shown to customer
customer.namestringyesCustomer full name
customer.phoneNumberstringyesCustomer phone number. Any common format accepted, auto-normalized to 256XXXXXXXXX. See Phone Number Format
customer.emailstringnoCustomer email address
methodPaymentMethodnoSpecific payment method to use ("mobileMoney" or "bank")
referencestringnoUnique reference (any valid UUID). Auto-generated as UUID v4 if omitted
metadataRecord<string, string>noCustom key-value pairs attached to the transaction

Example

import { createNylonPay } from '@nile-squad/nylonpay-ts';

const nylonpay = createNylonPay({
  apiKey: 'npk_test_...',
  apiSecret: 'nps_test_...',
});

const payment = await nylonpay.collectPayment({
  amount: 50000,
  currency: 'UGX',
  description: 'Order #12345',
  customer: {
    name: 'John Doe',
    phoneNumber: '+256700000000',
    email: 'customer@example.com',
  },
  reference: '550e8400-e29b-41d4-a716-446655440000', // UUID (any version accepted)
  metadata: {
    orderId: '12345',
    items: '3',
  },
});

payment.on('success', ({ transaction }) => {
  console.log('Payment completed:', transaction.id);
});

Payment Flow

Your Server → Nylon Pay Backend → Payment Provider → Customer Phone

    ← ← ← ← ← ← ← ← ← ← Status Updates ← ← ← ← ← ← ← ← ← ←
         ↓               ↓               ↓
    processing      success/failed     cancelled
  1. You call collectPayment() with customer details and amount
  2. SDK sends request to Nylon Pay backend
  3. Backend initiates payment with provider (e.g., Mobile Money)
  4. Customer receives payment prompt on their phone
  5. SDK tracks the payment status automatically
  6. Events fire as status changes (processing, success/failed)

Idempotency

The reference field prevents duplicate charges. If a request fails and you retry with the same reference, the SDK returns the existing payment instance instead of creating a new one.

A supplied reference must be a valid UUID of any version. The SDK generates a UUID v4 when you omit reference. Never reuse the same reference for different payments.

Return Value

collectPayment() returns a PaymentInstance with:

MethodDescription
.on(event, handler)Listen for events (processing, success, failed, cancelled, error)
.once(event, handler)Listen once, then auto-remove
.off(event, handler)Remove a handler
.wait()Promise that resolves Transaction on success, null on failure/cancel/error. Never rejects.
.referenceThe transaction reference
.statusCurrent transaction status

On this page