0.1.0 • Published 3 years ago

paylense-sdk v0.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

Paylense API NodeJS Client

Power your ECMA apps Paylense API

Build Status

npm package npm downloads GitHub issues npm bundle size (minified + gzip) GitHub Dependency Status Dev Dependency Status Known Vulnerabilities Coverage Status Join the community on Spectrum

Usage

Installation

Add the library to your project

npm install paylense-sdk --save-dev

User Credentials

You get the api_key and api_secret from https://dashboard.paylense.com/signup.

The credentials for the sandbox environment can be used straight away. In production, the credentials are provided for you after KYC requirements are met.

Configuration

Before we can fully utilize the library, we need to specify global configurations. The global configuration must contain the following:

  • environment: Optional environment, either "sandbox" or "production". Default is 'sandbox'
  • version: The API version being accessed. This is mandatory.

As an example, you might configure the library like this:

const paylense = require("paylense-sdk");

const defaultGlobalConfig: paylense.GlobalConfig = {
  environment: paylense.Environment.SANDBOX,
  version: "v1"
};

const userConfig: paylense.UserConfig = {
  api_key: "11011",
  api_secret: "sdk@2020"
};

const { Collections, Disbursements } = paylense.create(defaultGlobalConfig, userConfig);

Collections

Used for receiving money

You can create a collections client with the following:

const collections = Collections();

Methods

  1. requestToPay(request: PaymentRequest): Promise<string>: This operation is used to request a payment from a consumer (Payer). The payer will be asked to authorize the payment. The transaction is executed once the payer has authorized the payment. The transaction will be in status PENDING until it is authorized or declined by the payer or it is timed out by the system. Status of the transaction can be validated by using getTransaction

  2. getTransaction(transactionId: string): Promise<Payment>: Retrieve transaction information using the transactionId returned by requestToPay. You can invoke it at intervals until the transaction fails or succeeds. If the transaction has failed, it will throw an appropriate error. The error will be a subclass of PaylenseError. Check src/error.ts for the various errors that can be thrown

Sample Code

const paylense = require("paylense-sdk");

const defaultGlobalConfig: paylense.GlobalConfig = {
  environment: paylense.Environment.SANDBOX,
  version: "v1"
};

const userConfig: paylense.UserConfig = {
  api_key: "11011",
  api_secret: "sdk@2020"
};

const { Collections } = paylense.create(defaultGlobalConfig, userConfig);

const collections = Collections();

// Request to pay
collections
  .requestToPay({
    amount: "50",
    merchant_reference: "123456",
    account_number: "256774290781",
    narration: "testing"
  })
  .then(transactionId => {
    console.log({ transactionId });

    // Get transaction status
    return collections.getTransaction(transactionId);
  })
  .then(transaction => {
    console.log({ transaction });
  })
  .catch(error => {
    console.log(error);
  });

Disbursements

Used for sending money to users

You can create a disbursements client with the following

const disbursements = Disbursements();

Methods

  1. transfer(request: TransferRequest): Promise<string>

Used to transfer an amount from the owner’s account to a payee account. It returns a transaction id which can use to check the transaction status with the getTransaction function

  1. getTransaction(transactionId: string): Promise<Transfer>: Retrieve transaction information using the transactionId returned by transfer. You can invoke it at intervals until the transaction fails or succeeds. If the transaction has failed, it will throw an appropriate error. The error will be a subclass of PaylenseError. Check src/error.ts for the various errors that can be thrown

Sample Code

const paylense = require("paylense-sdk");

const defaultGlobalConfig: paylense.GlobalConfig = {
  environment: paylense.Environment.SANDBOX,
  version: "v1"
};

const userConfig: paylense.UserConfig = {
  api_key: "11011",
  api_secret: "sdk",
  password: "sdk@2020"
};
// initialise paylense library
const { Disbursements } = paylense.create(defaultGlobalConfig, userConfig);

// initialise disbursements
const disbursements = Disbursements();

// Transfer
disbursements
  .transfer({
    amount: "50",
    merchant_reference: "123456",
    account_number: "256774290781",
    narration: "testing"
  })
  .then(transactionId => {
    console.log({ transactionId });

    // Get transaction status
    return disbursements.getTransaction(transactionId);
  })
  .then(transaction => {
    console.log({ transaction });
  })
  .catch(error => {
    console.log(error);
  });