This is still in development. To connect to the current kopokopo's api check out it's documentation on https://app.kopokopo.com/push_api
This is a module to assist developers in consuming Kopokopo's API
To install run the following command on your project's directory:
npm install --save k2-connect-node
The package should be configured with your client id and client secret which you can get from your account on the kopokopo's app
//Having stored your client id and client secret as environment variables
const options = {
clientId: process.env.K2_CLIENT_ID,
clientSecret: process.env.K2_CLIENT_SECRET,
baseUrl: process.env.K2_BASE_URL
}
//Including the kopokopo module
var K2 = require("k2-connect-node")(options)
Note: The baseUrl
can be custom for testing purposes but we recommend using the sandbox base url during development.
- Tokens :
var TokenService = K2.TokenService
- Webhooks :
var Webhooks = K2.Webhooks
- STK PUSH :
var StkService = K2.StkService
- Pay :
var PayService = K2.PayService
- Transfer :
var TransferService = K2.TransferService
The package needs to be configured with your kopokopo's clientId and Secret Key, which you can get from the kopokopo application.
To send any requests to Kopokopo's API you'll need an access token
const TokenService = K2.TokenService
TokenService
.getTokens()
.then(response => {
//Developer can decide to store the token_details and track expiry
console.log(response)
})
.catch( error => {
console.log(error)
})
- Consuming
const Webhooks = K2.Webhooks
const webhookSecret = 'my_webhook_secret'
//Router or whatever server you are using
router.post('/customercreated', function(req, res, next){
// Send message and capture the response or error
Webhooks
.webhookHandler(req, res, webhookSecret)
.then( response => {
console.log(response)
})
.catch( error => {
console.log(error)
})
})
- Subscription
const subscribeOptions = {
eventType: 'buy_goods_received',
url: 'https://my-valid-url.com/endpoint',
webhookSecret: 'my_webhook_secret',
accessToken: 'my_access_token',
scope: 'Till',
scopeReference: '555555'
}
Webhooks
.subscribe(subscribeOptions)
.then(response => { console.log(response) })
.catch(error => { console.log(error) })
- Checking the webhook subscription
statusOptions = {
location: 'https://my-valid-url.com/endpoint',
accessToken: 'my_access_token'
}
Webhooks
.webhookSubscriptionStatus(statusOptions)
.then(response => { console.log(response) })
.catch(error => { console.log(error) })
const StkService = K2.StkService
var stkOptions = {
tillNumber: 36546,
firstName: 'Jane'
lastName: 'Doe',
phone: '+254712345678',
email: '[email protected]',
currency: 'KES',
amount: 20,
callbackUrl: 'https://my-valid-url.com/endpoint',
accessToken: 'my_access_token',
//A maximum of 5 key value pairs
metadata: {
customerId: '123456789',
reference: '123456',
notes: 'Payment for invoice 123456'
}
}
StkService
.paymentRequest(stkOptions)
.then( response => {
console.log(response)
})
.catch( error => {
console.log(error)
})
For other usage examples check out the example app.
The methods are asynchronous.
The only supported ISO currency code at the moment is: KES
-
TokenService.getToken()
to get an access token.- The response will contain:
token type
,expires_in
andaccess_token
- The response will contain:
NB: The access token is required to send subsequent requests
-
StkService.paymentRequest({ stkOptions })
:stkOptions
: A hash of objects containing the following keys:firstName
: Customer's first nameREQUIRED
lastName
: Customer's last nameREQUIRED
phone
: Phone number to pull money from.REQUIRED
email
: Amount to charge.currency
: 3-digit ISO format currency code.REQUIRED
amount
: Amount to charge.REQUIRED
callbackUrl
: Url that the result will be posted toREQUIRED
metadata
: It is a hash containing a maximum of 5 key value pairsaccessToken
: Gotten from theTokenService
responseREQUIRED
-
StkService.paymentRequestStatus({ location: 'location', accessToken: 'my_access_token' })
:location
: The request location you get when you send a requestREQUIRED
accessToken
: Gotten from theTokenService
responseREQUIRED
For more information, please read https://api-docs.kopokopo.com/#receive-payments-from-m-pesa-users-via-stk-push
-
PayService.addPayRecipient({ payRecipientOptions })
:payRecipientOptions
: A hash of objects containing the following keys:type
: Pay recipient typeREQUIRED
firstName
: Pay recipient's first nameREQUIRED
lastName
: Pay recipient's last nameREQUIRED
phone
: Pay recipient's phone numberREQUIRED
email
: Pay recipient's email numbernetwork
: Pay recipient's networkREQUIRED
for mobile wallet recipientbankId
: Pay recipient's bank IDREQUIRED
for bank account recipientbankBranchId
: Pay recipient's bank branch IDREQUIRED
for bank account recipientaccountNumber
: Pay recipient's account numberREQUIRED
for bank account recipientaccountName
: Pay recipient's account nameREQUIRED
for bank account recipientaccessToken
: Gotten from theTokenService
responseREQUIRED
-
PayService.sendPay({ payOptions })
:payOptions
: A hash of objects containing the following keys:destination
: The destinationREQUIRED
currency
: 3-digit ISO format currency code.REQUIRED
amount
: Amount to charge.REQUIRED
callbackUrl
: Url that the result will be posted toREQUIRED
metadata
: It is a hash containing a maximum of 5 key value pairsaccessToken
: Gotten from theTokenService
responseREQUIRED
-
PayService.payStatus({ location: 'location', accessToken: 'my_access_token' })
:location
: The request location you get when you send a requestREQUIRED
accessToken
: Gotten from theTokenService
responseREQUIRED
For more information, please read https://api-docs.kopokopo.com/#send-money-pay
-
TransferService.createMerchantBankAccount({ accountOpts })
:accountOpts
: A hash of objects containing the following keys:accountName
: Settlement Account NameREQUIRED
bankId
: Settlement Bank IDREQUIRED
bankBranchId
: Settlement Bank Branch IDREQUIRED
accountNumber
: Settlement account numberREQUIRED
accessToken
: Gotten from theTokenService
responseREQUIRED
-
TransferService.createMerchantWallet({ accountOpts })
:accountOpts
: A hash of objects containing the following keys:network
: The mobile wallet's accountREQUIRED
msisdn
: Msisdn(Phone number with the country code)REQUIRED
accessToken
: Gotten from theTokenService
responseREQUIRED
-
TransferService.settleFunds({ settleOpts })
:settleOpts
: A hash of objects containing the following keys:destinationType
: Thepletion - Transfers Simulation Spec Tests for Settlement Account Simulation 3 Assignee: Nicollet Njora destinationREQUIRED FOR A TARGETED TRANSFER
destinationReference
: The destinationREQUIRED FOR A TARGETED TRANSFER
currency
: 3-digit ISO format currency code.REQUIRED
amount
: Amount to charge.REQUIRED
callbackUrl
: Url that the result will be posted to REQUIREDaccessToken
: Gotten from theTokenService
responseREQUIRED
-
TransferService.settlementStatus({ location: 'location', accessToken: 'my_access_token' })
:location
: The request location you get when you send a requestREQUIRED
accessToken
: Gotten from theTokenService
responseREQUIRED
For more information, please read https://api-docs.kopokopo.com/#transfer-to-your-account-s
- All the post requests are asynchronous apart from
TokenService
. This means that the result will be posted to your custom callback url when the request is complete. The immediate response of the post requests contain thelocation
url of the request you have sent which you can use to query the status.
Note: The asynchronous results are processed like webhooks but use the application secret instead of the webhook secret.
We welcome contributions with open arms just make a pull request and we will review.
Run all tests:
$ npm install
$ npm test
If you find a bug, please file an issue on our issue tracker on GitHub.
k2-connect-node is MIT licensed. See LICENSE for details.