0.0.4 • Published 2 years ago

mobilemoney.js v0.0.4

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

mobilemoney.js

About

mobilemoney.js is a powerful Node.js module that allows you to easily interact with the MTN MOMO API.

  • Object-oriented
  • Predictable abstractions
  • Performant
  • 100% coverage of the MTN MOMO API

Installation

Node.js 16.9.0 or newer is required.

npm install mobilemoney.js

Example usage

Interact with MTN OPEN API using function, easy & faster:

const { Client } = require('mobilemoney.js');

(async()=>{
    let user = new Client();
    user.isSandbox();
    let subscriptionKey = "COLLECTION_OR_DISBURSEMENT_CREDENTIAL_MAKE_SURE_TO_REPLACE_IT_CORRECTLY";
    // creating uuid version 4 from the library
    let uuid = user.getReferenceId();
    console.log(`UUID : ${uuid}`)

    // Creating user in sandbox env
    let [done, ] = await user.createApiUser(uuid, subscriptionKey);
    if (done){
        console.log('Create successfully');
        console.log(done);   
    }

    let [, apiUser] = await user.getApiUser(uuid, subscriptionKey)
    console.log(`apiUser : `)
    console.log(apiUser)

    let [, data] = await user.createApiKey(uuid, subscriptionKey)

    // Convert to basic token from apiuser & apikey, Library do it for you
    let basicToken = user.basicToken(uuid, data.apiKey)
    console.log(`Basic Token : ${basicToken}`)
    // Getting collection product with credential
    let collection = user.collection(subscriptionKey)
    // Creating access token for collection product
    let [,accessToken] = await collection.createAccessToken(basicToken)
    accessToken = accessToken.access_token
    console.log(`AccessToken : ${accessToken}`)
    // Convert to bearer token from access token
    let bearerToken = user.bearerToken(accessToken)
    console.log(`Bearer Token : ${bearerToken}`)

    //request to pay
    let body = {
        "amount": "5",
        "currency": "EUR",
        "externalId": "45464546454",
        "payer": {
            "partyIdType": "MSISDN",
            "partyId": "87937389"
        },
        "payerMessage": "BUY THING",
        "payeeNote": "THANKS"
    }

    let [reqToPay,] = await collection.requestToPay(bearerToken, 
        user.getReferenceId(), 
        apiUser.targetEnvironment,
        body)

    console.log(`Request to Pay : ${reqToPay}`)

   
})()

Links

Help

If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to join our official rewriteapi Server.