Guide

Dart CLI guide for creating and paying a YKNPG transaction.

Dart CLI guide for creating and paying a YKNPG transaction.

Prerequisites

  • Dart SDK 3+, bearer token in .env.
  1. Create project
  • dart create -t console-full demo_dart && cd demo_dart
  1. Add dependencies
  • dart pub add http dotenv
  1. Add environment file
  • File: .env
YKNPG_API_TOKEN=replace-with-your-token
YKNPG_BASE_URL=https://yknpg.ngoul.com/api/v1
  1. Update main script
  • File: bin/demo_dart.dart
import 'dart:convert';
import 'dart:io';
import 'package:dotenv/dotenv.dart' show load, env;
import 'package:http/http.dart' as http;

Future<Map<String, dynamic>> createTransaction(int amount, String phone) async {
  final payload = {
    'amount': 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,
  };

  final resp = await http.post(
    Uri.parse('${env['YKNPG_BASE_URL'] ?? 'https://yknpg.ngoul.com/api/v1'}/transactions/'),
    headers: {
      'Authorization': 'Bearer ${env['YKNPG_API_TOKEN']}',
      'Content-Type': 'application/json',
    },
    body: jsonEncode(payload),
  );
  if (resp.statusCode >= 400) throw resp.body;
  return jsonDecode(resp.body) as Map<String, dynamic>;
}

Future<Map<String, dynamic>> payTransaction(String uuid) async {
  final resp = await http.post(
    Uri.parse('${env['YKNPG_BASE_URL'] ?? 'https://yknpg.ngoul.com/api/v1'}/transactions/$uuid/pay/'),
    headers: {'Authorization': 'Bearer ${env['YKNPG_API_TOKEN']}'},
  );
  if (resp.statusCode >= 400) throw resp.body;
  return jsonDecode(resp.body) as Map<String, dynamic>;
}

Future<String> pollStatus(String uuid) async {
  String status = 'new';
  while (status == 'new' || status == 'paying') {
    await Future.delayed(const Duration(seconds: 5));
    final resp = await http.get(
      Uri.parse('${env['YKNPG_BASE_URL'] ?? 'https://yknpg.ngoul.com/api/v1'}/transactions/$uuid/'),
      headers: {'Authorization': 'Bearer ${env['YKNPG_API_TOKEN']}'},
    );
    if (resp.statusCode >= 400) throw resp.body;
    status = (jsonDecode(resp.body) as Map<String, dynamic>)['status'] as String;
    stdout.writeln('Current status: $status');
  }
  return status;
}

Future<void> main(List<String> args) async {
  load();
  if (env['YKNPG_API_TOKEN'] == null) {
    stderr.writeln('Set YKNPG_API_TOKEN in .env');
    exit(1);
  }
  if (args.length < 2) {
    stderr.writeln('Usage: dart run <phone> <amount>');
    exit(1);
  }
  final phone = args[0];
  final amount = int.parse(args[1]);

  final trx = await createTransaction(amount, phone);
  stdout.writeln('Gateway instructions: ${trx['instructions']}');

  final pay = await payTransaction(trx['uuid'] as String);
  stdout.writeln('Post-pay instructions: ${pay['instructions']}');

  final finalStatus = await pollStatus(trx['uuid'] as String);
  stdout.writeln('Final status: $finalStatus');
}
  1. Run
  • dart run <phone> <amount>

Notes

  • Flow: create → display instructions → pay → poll until not new/paying.