MPower
The unofficial Node.JS library for MPower Payments (mpowerpayments.com).
Built on the MPower Payments HTTP API (beta).
Notice
- This is documentation for the current
0.2.x
release.0.1.x
documentation is available here. 0.2.x
introduced breaking changes incompatible with0.1.x
.0.2.x
is incompatible with Node.js0.8.x
and below.
Installation
$ npm install --save mpower
API configuration
Setup MPower API keys.
var setup = masterKey: 'dd6f2c90-f075-012f-5b69-00155d864600' privateKey: 'test_private_oDLVlm1eNyh0IsetdhdJvcl0ygA' publicKey: 'test_public_zzF3ywvX9DE-OSDNhUqKoaTI4wc' token: 'ca03737cf942cf644f36' mode: 'test' // optional. use in sandbox mode.;
It might usually be suitable to put your API configuration in environment variables. In that case you can initialize mpower.Setup
without passing configuration parameters.
The library will automatically detect the environment variables and use them.
Auto-detected environment variables: MP_MASTER_KEY
, MP_PRIVATE_KEY
, MP_PUBLIC_KEY
, MP_TOKEN
Checkout Store Configuration
var store = name: 'Awesome Store' // only name is required tagline: 'Easy shopping' phoneNumber: '0261234567' postalAddress: 'P.0. Box MP555, Accra' logoURL: 'http://www.awesomestore.com.gh/logo.png';
Initialize Checkout Invoice
var invoice = setup store;
Initialize Onsite Invoice
var invoice = setup store;
Add Invoice Items & Description
invoice; // name, quantity, unit price, total priceinvoicedescription = 'Apple product'
Setting Total Amount Chargeable
invoicetotalAmount = 500;
Redirecting to MPower Checkout
After setting total amount and adding items to your invoice you can create a checkout and redirect the customer. It takes a callback which gets passed the updated invoice object containing the url.
invoice ;
Onsite Payment Request (OPR): Step 1 - Token request
After setting total amount and adding items to your invoice get the MPower customer's username or phone number and start an OPR request.
invoice ;
Onsite Payment Request (OPR): Step 2 - Charge
To successfully complete an OPR charge, you need both your OPR Token & the Confirmation code sent to the customer. After a successfull charge you can programatically access the receipt url, customer information and more.
invoice ;
Extra API methods
Adding Tax Information
You may include tax information on on the checkout page. This information will be available on the invoice & receipt printouts and PDF downloads.
invoice;invoice;
Adding Custom Data
There are times when you may need to add an extra load of data with the checkout information for later use. MPower allows saving of custom data on their servers which are persisted even after successful payment. Note: Custom data is not displayed anywhere on the checkout page, invoice/receipt download & printouts.
invoice;invoice;
Setting a Cancel URL
You can optionally set the URL where your customers will be redirected to after canceling a checkout. Note: There are two options as to how the cancel URL is set, one is to set it globally using the checkout setup information and the other is set it as per checkout invoice. Setting the cancel URL directly on the invoice instance will overwrite the global settings if already set.
// Globallyvar store = name: 'Awesome Store' cancelURL: 'http://www.awesomestore.com'; // Per CheckoutinvoicecancelURL;
Setting a Return URL
MPower does a good job of managing receipt downloads and printouts after your customer successfuly makes payment. However there may be cases where you may descide to redirect your customers to another URL after successfully making payment. Return URL guarantees this action.
Note: MPower will append ?token=INVOICE_TOKEN
to your URL.
// Globallyvar store = name: 'Awesome Store' returnURL: 'http://www.awesomestore.com'; // Per CheckoutinvoicereturnURL;
Confirming a Checkout Programatically
The API allows you to check on the status of any checkout using the checkout token key. You have access to all the data including the receipt download link & customer information in cases where the checkout has been confirmed as completed.
var token = 'aodaff0023'; var invoice = setup store;invoice ;
DirectPay
You can pay any MPower account directly via your third party apps. This is particularly excellent for implementing your own Adaptive payment solutions on top of MPower.
var directPay = setup;directPay ;
TODO
Add DirectCard functionality.
Running Tests
To run tests just setup the API configuration environment variables. An internet connection is required for some of the tests to pass.
npm install -g mocha
Then
npm test
or mocha
License
MIT