Guide

JavaScript (Node.js) guide for creating and paying a YKNPG transaction.

JavaScript (Node.js) guide for creating and paying a YKNPG transaction.

Prerequisites

  • Node.js 18+, npm.
  • Bearer token stored in .env as YKNPG_API_TOKEN.
  1. Init project
  • mkdir -p demo_js && cd demo_js
  • npm init -y
  1. Install dependencies
  • npm install axios dotenv
  1. Add environment file
  • File: .env
YKNPG_API_TOKEN=replace-with-your-token
YKNPG_BASE_URL=https://yknpg.ngoul.com/api/v1
  1. Create transaction script
  • File: transaction.js
import axios from "axios";
import dotenv from "dotenv";

dotenv.config();

const API_TOKEN = process.env.YKNPG_API_TOKEN;
const BASE_URL = process.env.YKNPG_BASE_URL || "https://yknpg.ngoul.com/api/v1";
const client = axios.create({
  baseURL: BASE_URL,
  headers: { Authorization: `Bearer ${API_TOKEN}` },
  timeout: 10000,
});

async function createTransaction(amount, phone) {
  const payload = {
    amount,
    provider: "ORANGE_MONEY",
    user_infos: { number: phone },
    callback_url: "http://example.com",
    callback_method: "POST",
    your_message: "pay this",
    your_order_ref: "demo-1",
    provider_fees_on_customer: true,
  };
  const { data } = await client.post("/transactions/", payload);
  return data;
}

async function payTransaction(uuid) {
  const { data } = await client.post(`/transactions/${uuid}/pay/`);
  return data;
}

async function pollStatus(uuid) {
  let status = "new";
  while (status === "new" || status === "paying") {
    await new Promise((r) => setTimeout(r, 5000));
    const { data } = await client.get(`/transactions/${uuid}/`);
    status = data.status;
    console.log("Current status:", status);
  }
  return status;
}

async function main() {
  const phone = process.argv[2];
  const amount = Number(process.argv[3]);
  if (!phone || !amount) {
    console.log("Usage: node transaction.js <phone> <amount>");
    process.exit(1);
  }

  const trx = await createTransaction(amount, phone);
  console.log("Gateway instructions:", trx.instructions);

  const pay = await payTransaction(trx.uuid);
  console.log("Post-pay instructions:", pay.instructions);

  const finalStatus = await pollStatus(trx.uuid);
  console.log("Final status:", finalStatus);
}

main().catch((err) => {
  console.error(err.response?.data || err.message);
  process.exit(1);
});
  1. Run
  • node transaction.js <phone> <amount>

Notes

  • Keep the token server-side; for browsers, call your own backend to add the Authorization header.