Nile PayNile Pay

Collect Payment

Initiate a payment collection with event-driven status tracking

collectPayment(request)

Initiates a payment collection and returns a PaymentInstance immediately. The instance is not a promise, it provides event listeners for real-time status updates.

const payment = nile.collectPayment(request);

Request Fields

FieldTypeRequiredDescription
amountnumberyesPayment amount in smallest currency unit (e.g., UGX cents)
currencystringnoISO 4217 currency code. Default: "UGX"
methodstringnoSpecific payment method to use
descriptionstringnoPayment description shown to customer
customer.phonestringyesCustomer phone number (E.164 format recommended)
customer.emailstringnoCustomer email address
referencestringyesUUID v4 for idempotency. Same reference = same payment
invoiceNumberstringnoYour invoice/bill number
notifyMerchantbooleannoSend notification to merchant on completion. Default: true
metadataRecord<string, unknown>noCustom data attached to the transaction

Example

import { createNilePay } from "@nilepay/sdk";

const nile = createNilePay({
  environment: "sandbox",
  apiKey: "npk_test_...",
  apiSecret: "nps_test_...",
});

const payment = nile.collectPayment({
  amount: 50000, // 500 UGX
  currency: "UGX",
  description: "Order #12345",
  customer: {
    phone: "+256700000000",
    email: "customer@example.com",
  },
  reference: crypto.randomUUID(), // Required for idempotency
  invoiceNumber: "INV-2024-001",
  metadata: {
    orderId: "12345",
    items: 3,
  },
});

payment.on("success", (tx) => {
  console.log("Payment completed:", tx.transactionId);
});

Payment Flow

Your Server → Nile 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 Nile Pay backend
  3. Backend initiates payment with provider (e.g., Mobile Money)
  4. Customer receives payment prompt on their phone
  5. SDK polls getStatus() internally until terminal state
  6. Events fire as status changes

Idempotency

The reference field is critical for idempotency. If a request fails mid-flight and you retry with the same reference, the SDK recognizes it as a duplicate and returns the existing payment instance.

Always generate a fresh UUID v4 for each new payment:

const reference = crypto.randomUUID(); // Node.js 19+

Return Value

collectPayment() returns a PaymentInstance immediately (synchronously). The instance is event-driven, use .on() to listen for status changes or .wait() to await completion.

See Payment Events for event handling patterns.

On this page