1.0.3 • Published 4 years ago
mpesa-package v1.0.3
MPESA PACKAGE - NODE.JS
Instantiate
Instantiate the payment package with default setting from daraja
const Payment = require('payment')
const payment = new Payment({
consumerKey: '<Get from daraja api>',
consumerSecret: '<Get from daraja api>',
businessShortCode: '174379', // From daraja api for testing
passKey: 'bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919' // Also from daraja api ,
uri: 'https://sandbox.safaricom.co.ke'
})
Authentication
- After instantiating, the next step should be to set authentication
payment.setAuthentication()
- The above sets you a token,
payment.token
that will be used for authenticating mpesa api requests going forward
Stk
- Processes stk push for a transaction
- We'll still be using the created payment object above
- processStk function params
Field | Required | Description |
---|---|---|
amount | true | Amount to be paid |
phoneNumber | true | Phone number of paying user |
callbackUrl | true | Callback url on success or failure |
accountReference | true | Account reference for payment |
transactionDescription | true | Transaction description |
processStk function
await payment.setAuthentication()
await payment.processStk({
amount,
phoneNumber,
callbackUrl,
accountReference,
transactionDescription
})
Stk callback
- Once an stk push has been processed, then mpesa api will need a way to inform your application that your payment has been processed or rejected
- To do this, set up webhooks on your app that will receive this information