Skip to content

API Reference: Functions

Dispatches initiation based on input.method.

function initiatePayment(
input: UnifiedInitiateInput
): Promise<{ url: string; fields: Record<string, string> } | InitiateKhaltiResult>
  • input: A discriminated union on method:
    • method: 'esewa': Requires config: EsewaConfig and order: { orderId, amountPaisa, taxPaisa?, servicePaisa?, deliveryPaisa? }.
    • method: 'khalti': Requires config: KhaltiConfig and order: { purchaseOrderId (or orderId), purchaseOrderName, amountPaisa, customerInfo?, amountBreakdown?, extra? }.
  • For eSewa: { url: string, fields: Record<string, string> } (target form URL and hidden input dictionary).
  • For Khalti: { pidx: string, payment_url: string, expires_at: string, expires_in: number }.

Dispatches verification based on input.method and returns a normalized PaymentResult.

function verifyPayment(
input: UnifiedVerifyInput
): Promise<PaymentResult>
  • input:
    • method: 'esewa': Requires config, data (base64 string), expectedOrderId, expectedAmountPaisa.
    • method: 'khalti': Requires config, pidx, expectedAmountPaisa, expectedOrderId?.

A promise resolving to a normalized PaymentResult:

interface PaymentResult {
status: "completed" | "pending" | "failed" | "expired" | "canceled";
amountPaisa: number;
orderId: string;
traceId: string | null; // eSewa ref_id or Khalti transaction_id
raw: unknown; // Raw provider responses for audit logging
}

function buildEsewaFields(opts: EsewaFieldOpts): {
url: string;
fields: Record<string, string>;
}

Constructs the HMAC-SHA256 signature and returns the form action URL and required fields.

function verifyEsewaPayment(opts: VerifyEsewaPaymentOpts): Promise<PaymentResult>

Decodes data, checks HMAC with timingSafeEqual, queries status API, and normalizes output.

function signEpayV2(
p: { total_amount: string; transaction_uuid: string; product_code: string },
secret: string
): string

Calculates ePay v2 Base64 digest of total_amount=...,transaction_uuid=...,product_code=....

function checkEpayStatus(opts: EpayStatusOpts): Promise<EpayStatusResult>

Queries eSewa transaction status endpoint directly via HTTP GET.


function initiateKhalti(opts: InitiateKhaltiOpts): Promise<InitiateKhaltiResult>

Calls /epayment/initiate/ with Authorization: Key <secret> and returns pidx and payment_url.

function lookupKhalti(opts: LookupKhaltiOpts): Promise<LookupKhaltiResult>

Calls /epayment/lookup/ with pidx. Correctly parses HTTP 400 error bodies into status strings.

function verifyKhaltiPayment(opts: VerifyKhaltiPaymentOpts): Promise<PaymentResult>

Queries lookup and normalizes payment status to PaymentResult.


Converts rupees to integer paisa: Math.round(npr * 100).

paisaToRupeesString(paisa: number): string

Section titled “paisaToRupeesString(paisa: number): string”

Converts integer paisa to string decimal representation (e.g. 12345 $\rightarrow$ "123.45").

parseAmountToPaisa(raw: string | number): number

Section titled “parseAmountToPaisa(raw: string | number): number”

Parses formatted or comma-separated amount strings (e.g. "1,000.0") to integer paisa (100000).