Transaction

Transaction module - Interacts with the M-Pesa API by exposing c2b, query and reverse methods. Throws errors if any of the methods receives incomplete or invalid parameters, including the class constructor
Parameters:
Name Type Description
options object
Properties
Name Type Attributes Default Description
api_host string <optional>
api.sandbox.vm.co.mz Hostname for the API
api_key string empty-string Used for creating authorize trasactions on the API
initiator_identifier string empty-string Provided by Vodacom MZ
origin string empty-string Used for identifying hostname which is sending transaction requests
public_key string empty-string Public Key for the M-Pesa API. Used for generating Authorization bearer tokens
security_credential string empty-string Provided by Vodacom MZ
service_provider_code number empty-string Provided by Vodacom MZ
Throws:
'Missing or invalid configuration parameters' Error if options object is incomplete or invalid
Returns:
Transaction
Type
Class
Example
Transaction = require('mpesa-mz-nodejs-lib')
 tx = new Transaction(options)

Members

(inner) _api_host :string

API Host - Required by the M-Pesa API
Type:
  • string

(inner) _api_key :string

API key - Required by the M-Pesa API
Type:
  • string

(inner) _initiator_identifier :string

Initiator Identifier - Required by the M-Pesa API
Type:
  • string

(inner) _origin :string

Origin - Required by the M-Pesa API
Type:
  • string

(inner) _public_key :string

Public key - Required by the M-Pesa API
Type:
  • string

(inner) _security_credential :string

Security Credential - Required by the M-Pesa API
Type:
  • string

(inner) _service_provider_code :number

Service Provider Code - Required by the M-Pesa API
Type:
  • number

(inner) _validMSISDN

MSISDN Validation

(inner) validation_errors

Validation buffer

Methods

(static) c2b(transaction_data) → {object}

Initiates a C2B (Client-to-Business) transaction on the M-Pesa API.
Parameters:
Name Type Description
transaction_data object
Properties
Name Type Description
amount float Value to transfer from Client to Business
msisdn string Client's phone number
reference string Transaction reference (unique)
third_party_reference string Third-party reference provided by Vodacom MZ
Throws:
'Missing or invalid C2B parameters' Error if params are missing or invalid
Returns:
Promise
Type
object
Example
Transaction = require('mpesa-mz-nodejs-lib')
// Instantiate Transaction object with valid options params
tx = new Transaction(options)

tx.c2b({
	amount: 1,
	msisdn: '821234567'
	reference: 'T001',
	third_party_reference: '12345'
}).then(function(data){
	console.log(data)
}).catch(function(error){
	console.log(error)
})

(static) query(query_data) → {object}

Initiates a C2B (Client-to-Business) transaction Query on the M-Pesa API.
Parameters:
Name Type Description
query_data object
Properties
Name Type Description
query_reference string TransactionID or ConversationID returned from the M-Pesa API
third_party_reference string Unique reference of the third-party system
Throws:
'Missing or invalid Query parameters' Error is params are missing or invalid
Returns:
Promise
Type
object
Example
Transaction = require('mpesa-mz-nodejs-lib')
// Instantiate Transaction object with valid params
tx = new Transaction(options)

tx.query({
	query_reference:'08y844du6gs',
	third_party_reference:'12345'
}).then(function(data){
	console.log(data)
}).catch(function(error){
	console.log(error)
})

(static) reverse(transaction_data) → {object}

Initiates a C2B (Client-to-Business) transaction Reversal on the M-Pesa API
Parameters:
Name Type Description
transaction_data object
Properties
Name Type Attributes Description
amount number <optional>
Amount of the transaction
transaction_id string TransactionID returned from the M-Pesa API
third_party_reference string Unique reference of the third-party system
Throws:
'Missing or invalid Reversal parameters' Error if params are missing or invalid
Returns:
Promise
Type
object
Example
Transaction = require('mpesa-mz-nodejs-lib')
// Instantiate Transaction object with valid params
tx = new Transaction(options)

tx.reversal({
	amount:1,
	transaction_id: 'tvfs2503x1d'
	third_party_reference:'12345'
}).then(function(data){
console.log(data)
}).catch(function(error){
console.log(error)
})

(inner) _getBearerToken() → {string}

Generates a Bearer Token
Throws:
'Missing or invalid configuration parameters' Error if _public_key or _api_key are missing or invalid from object instantiation
Returns:
bearer_token
Type
string

(inner) _isValidated(type, data) → {boolean}

Validates all configuration parameters
Parameters:
Name Type Description
type string
data object
Returns:
Type
boolean

(inner) _isValidMSISDN(msisdn) → {boolean}

Validates a customer's MSISDN (Phone number)
Parameters:
Name Type Description
msisdn string
Returns:
isValid
Type
boolean

(inner) _request_headers(_origin, _public_key, _api_key) → {object}

Holds the request headers for each API request
Parameters:
Name Type Description
_origin string origin value from initialization
_public_key string public_key value from initialization
_api_key string api_key value from initialization
Throws:
'Missing or invalid configuration parameters' Error if _api_key, _origin or _public_key are missing or invalid from object instantiation
Returns:
Type
object