0.3.4 • Published 4 years ago

flwjs v0.3.4

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Unofficial Flutterwave JavaScript SDK

Still In Active Development

Base API

The sdk returns a function that could be used to initialize the FW function to return a new instance of the prototype.

const flwjs = require('fwjs');

const options = {};

const flw = fwjs(options);

Afterwards, we can use the fw to call any public function or access any property of the FW class.

Configuration

The sdk experts that the API keys should be set as an env keys. Below are the supported names

  • FLW_ENCRYPTION_KEY Encryption key from the dashboard
  • FLW_PUBLIC_KEY Public key from the dashboard
  • FLW_SECRET_KEY Secret key from the dashboard

Regardless, we can pass in our keys into the options of the flwjs function.

Available configurations are:

  • publicKey string : The public key
  • secretKey string : The secrte key
  • encryptKey string : The encryption key
  • version string : The version to use, default to "v2"
  • live booklean : Live or sandbox? defaults to false (sandbox)
const flwjs = require('flwjs');
// Builds up the options
const options = {
  publicKey: process.env.FLW_ENCRYPTION_KEY,
  encryptKey: process.env.FLW_PUBLIC_KEY,
  secretKey: process.env.FLW_SECRET_KEY,
  live: false,
  version: 'v2'
};
// Initialzes the SDK
const flw = flwjs(options);

Functions

encrypt (text): string

Encrypts a text usind 3DES algorithm arguments:

  • text string: The string to be encrypted

Returns encrypted hash string

makePayload (payload, encryptPayload = true): string

Generates a payload that can be used to make request.

arguments:

  • payload object: The object for a request
  • encryptPayload boolean: Encrypts the payload before returning it

Returns

  • object That can be used to make valid request to Flutterwave

charge(payload, type, country): Promise

This method loads the supported charge type for each version.

const flwjs = require('./dist/flwjs');

const flw = flwjs({ version: 'v2' });
flw
  .charge({
    cardno: '5438898014560229',
    cvv: '890',
    expirymonth: '08',
    expiryyear: '32',
    currency: 'NGN',
    pin: '3310',
    country: 'NG',
    amount: '10',
    email: 'desola.ade1@gmail.com',
    suggested_auth: 'PIN',
    phonenumber: '0902620185',
    firstname: 'temi',
    lastname: 'desola',
    IP: '355426087298442',
    txRef: 'MC-' + Date.now(),
    redirect_url: 'https://rave-webhook.herokuapp.com/receivepayment',
    meta: [{ metaname: 'flightID', metavalue: '123949494DC' }],
    device_fingerprint: '69e6b7f0b72037aa8428b70fbe03986c'
  })
  .then((res) => {
    console.log(res);
  })
  .catch((e) => console.log(e));

Supported charge type across versions

charge(payload, 'card') : Promise

Charges a card. The payload and response are standard to what is expected by the v2 charge endpoint.

flw
  .charge({
    // ...
  })
  .then((res) => {
    console.log(res);
  })
  .catch((e) => console.log(e));

charge(payload, 'nigerianAccount') : Promise

Charges a nigerian account. The payload and response are standard to what is expected by the v2 charge endpoint.

flw
  .charge(
    {
      // ...
    },
    'nigerianAccount'
  )
  .then((res) => {
    console.log(res);
  })
  .catch((e) => console.log(e));

charge(payload, 'ukAccount') : Promise

Charges a nigerian account. The payload and response are standard to what is expected by the v2 charge endpoint.

flw
  .charge(
    {
      // ...
    },
    'ukAccount'
  )
  .then((res) => {
    console.log(res);
  })
  .catch((e) => console.log(e));

charge(payload, 'mobileMoney', country = "ghana") : Promise

Charging through mobile money. The payload and response are standard to what is expected by the v2 charge endpoint.

Country could be one of: ghana, rwanda, uganda, zambia, francophone

V2 API Reference

V3 API Reference

flw
  .charge(
    {
      // ...
    },
    'mobileMoney',
    'zambia'
  )
  .then((res) => {
    console.log(res);
  })
  .catch((e) => console.log(e));

charge.mpesa(payload) : Promise

Charge using mpesa. The payload and response are standard to what is expected by the v2 charge endpoint.

flw
  .charge(
    {
      // ...
    },
    'mpesa'
  )
  .then((res) => {
    console.log(res);
  })
  .catch((e) => console.log(e));

charge(payload, "qr") : Promise

Charge using QR Code. The payload and response are standard to what is expected by the v2 charge endpoint.

flw
  .charge(
    {
      // ...
    },
    'qr'
  )
  .then((res) => {
    console.log(res);
  })
  .catch((e) => console.log(e));

charge(payload, "ach") : Promise

Charge using ACH. The payload and response are standard to what is expected by the v2 charge endpoint.

flw
  .charge(
    {
      // ...
    },
    'ach'
  )
  .then((res) => {
    console.log(res);
  })
  .catch((e) => console.log(e));

charge(payload, "bankTransfer") : Promise

Charge using ACH. The payload and response are standard to what is expected by the v2 charge endpoint.

flw
  .charge(
    {
      // ...
    },
    'bankTransfer'
  )
  .then((res) => {
    console.log(res);
  })
  .catch((e) => console.log(e));

charge(payload, "voucher") : Promise

Charge using ZAR Voucher. The payload and response are standard to what is expected by the v2 charge endpoint.

flw
  .charge(
    {
      // ...
    },
    'bankTransfer'
  )
  .then((res) => {
    console.log(res);
  })
  .catch((e) => console.log(e));

charge(payload, "ussd") : Promisev

Charge using USSD. The payload and response are standard to what is expected by the v2 charge endpoint.

flw
  .charge(
    {
      // ...
    },
    'ussd'
  )
  .then((res) => {
    console.log(res);
  })
  .catch((e) => console.log(e));

flw.transactions(actionType, payload): Promise

Actions that can be carried out on transaction(s)

Supported transactions action across versions

flw.transactions("getAll", payload) : Promise

Get all transactions by the merchant

The payload and response are standard to what is expected by the v2 charge endpoint.

try {
  const res = await flw.transactions('getAll', {});
  console.log(res);
} catch (error) {
  console.log(e);
}

flw.transactions("getFee", payload) : Promise

Calculates the fee for a transaction amount

The payload and response are standard to what is expected by the v2 charge endpoint.

try {
  const res = await flw.transactions('getFee', {});
  console.log(res);
} catch (error) {
  console.log(e);
}

flw.transactions("refund", payload) : Promise

Refunds a transaction.

Payload is transactionId for V3 and object for v2

The payload and response are standard to what is expected by the v2 charge endpoint.

try {
  const res = await flw.transactions('refund', {} | 1234);
  console.log(res);
} catch (error) {
  console.log(e);
}

flw.transactions("resendWebhook", payload) : Promise

Resends a failed transaction webhook.

Payload is transactionId for V3 and object for v2

The payload and response are standard to what is expected by the v2 charge endpoint.

// Resend webhook transaction
try {
  const res = await flw.transactions('resendWebhook', 1472126);
  console.log(res);
} catch (e) {
  console.log(e);
}

flw.transactions("timeline", payload) : Promise

Get the timeline of a transaction.

Payload is transactionId for V3 and object for v2

The payload and response are standard to what is expected by the v2 charge endpoint.

// Transaction timeline
try {
  const res = await flw.transactions('timeline', 1472126);
  console.log(res);
} catch (e) {
  console.log(e);
}

flw.transactions("verify", payload) : Promise

Verify the status of a transaction.

Payload is transactionId for V3 and object for v2

The payload and response are standard to what is expected by the v2 charge endpoint.

// Verify transaction
try {
  const res = await flw.transactions('verify', 1472126);
  console.log(res);
} catch (e) {
  console.log(e);
}

flw.validateCharge(payload): Promis

Validates a charge after calling the charge function.

The payload and response are standard to what is expected by the v2 charge endpoint.

For V2 Requests

  • PBFPubKey: Required This is your merchant public key.

  • transaction_reference: Required This is the flwRef returned in the Initiate payment response.

  • otp: Required This is the one time pin inputed by the customer.

API Reference

// Verify transaction
try {
  const payload = { transaction_reference: '', otp: '12345' };
  const res = await flw.validateCharge(payload, 1472126);
  console.log(res);
} catch (e) {
  console.log(e);
}

For V3 Requests

  • type: This recognises the type of payment you want to validate. Set to account if you want to validate an account transaction and set to card for card transactions. Defaults to 'card'

  • flw_ref: Required This is the reference returned in the initiate charge call as flw_ref

  • otp: Required This is the one time pin inputed by the customer.

API Reference

// Verify transaction
try {
  const res = await flw.transactions('verify', 1472126);
  console.log(res);
} catch (e) {
  console.log(e);
}
0.3.7

4 years ago

0.3.2

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.0

4 years ago

0.3.1

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago

0.0.2

4 years ago