Git Product home page Git Product logo

node-masterpass-api's Introduction

Simple Node.js library to interact with the masterpass apis

bitHound Overall Score bitHound Dependencies bitHound Code

Based on Test Merchant Server

Install

npm install github:perusworld/node-masterpass-api --save

Usage (v7)

The v7 version is much simpler with just one call to get the lightbox and another to get payment data post lightbox.

To start the standard checkout in v7 is very simple, create a unique cartId for this purchase and associate it with this flow.

Start the lightbox on the client browser using the following include (depending upon stage or production) and the code

The callback url is something that you need to configure in you mastercard developer account settings, since the cartId needs to be associated to a specific flow you could do it statefull by storing it in your session or stateless by including it as a parameter in the callbackUrl below as the callback url needs to only match with that of the one configured at the top domain level. In this example case it could be somehting like http://localhost/requestTokenCallbackv7?cartId=some-unique-id

<!-- Sandbox -->
<script type="text/javascript" src="https://sandbox.masterpass.com/integration/merchant.js"></script>

<!-- Production -->
<script type="text/javascript" src="https://masterpass.com/integration/merchant.js"></script>
//sample for more options see https://developer.mastercard.com/documentation/masterpass-merchant-integration-v7#js-library
function beginCheckout(cartId, amount, callBackUrl, merchantCheckoutId) {
  masterpass.checkout({
    "cartId": cartId,
    "amount": "" + Math.round(amount * 100),
    "currency": 'USD',
    "callbackUrl": callBackUrl,
    "checkoutId": merchantCheckoutId,
    "allowedCardTypes": ["master,amex,discover,visa"]
  });
}

Once the user goes though the lightbox and chooses the payment info, masterpass will redirect back to the callbackUrl, you can retrieve the user's payment credentials using

var async = require('async');
var masterpassapi = require('node-masterpass-api').masterpass();

var masterpass = new masterpassapi.Masterpass({
    privateKey: process.env.MP_PRIVATE_KEY,
    consumerKey: process.env.MP_CONSUMER_KEY,
    callBackUrl: process.env.MP_CALLBACK_URL,
    merchantCheckoutId: process.env.MP_CHECKOUT_ID,
    originUrl: process.env.MP_ORIGIN_URL,
    env: process.env.MP_ENV,
    v7: true
});

    masterpass.paymentData({
        oauthVerifier: req.query.oauth_verifier,
        cartId: req.query.cartId //cart id from session or query param
    }, (err, resp) => {
        if (err) {
            res.json(err);
        } else {
            res.json(resp); //user payment credentials to use with your payment processor/gateway
        }
    });

Usage (v6) might be deprecated

var async = require('async');
var masterpassapi = require('node-masterpass-api').masterpass();

var masterpass = new masterpassapi.Masterpass({
    privateKey: process.env.MP_PRIVATE_KEY,
    consumerKey: process.env.MP_CONSUMER_KEY,
    callBackUrl: process.env.MP_CALLBACK_URL,
    originUrl: process.env.MP_ORIGIN_URL
});

var ctx = {};
async.waterfall([
    function (callback) {
        masterpass.requestToken(callback);
    },
    function (tokenReq, callback) {
        ctx.tokenReq = tokenReq;
        masterpass.createShoppingCart({
            token: tokenReq.oauth_token,
            subTotal: 74996,
            currencyCode: 'USD',
            items: [
                { desc: 'One', qty: 1, value: 29999 },
                { desc: 'Two', qty: 5, value: 4999 }
            ]
        }, callback);
    },
    function (resp, callback) {
        masterpass.merchantInit({
            token: ctx.tokenReq.oauth_token
        }, callback);
    }
], (err, resp) => {
    if (err) {
        console.log(err);
    } else {
        console.log(resp);
    }
});
var async = require('async');
var masterpassapi = require('node-masterpass-api').masterpass();

var masterpass = new masterpassapi.Masterpass({
    privateKey: process.env.MP_PRIVATE_KEY,
    consumerKey: process.env.MP_CONSUMER_KEY,
    callBackUrl: process.env.MP_CALLBACK_URL,
    originUrl: process.env.MP_ORIGIN_URL
});

var ctx = {};
var req = {
    oauth_token: '----oauth token from lightbox response----',
    oauth_verifier: '----oauth verifier from lightbox response----',
    checkout_resource_url: '---- checkout resource url from lightbox response----'
};
async.waterfall([
    function (callback) {
        masterpass.accessToken({
            token: req.oauth_token,
            verifier: req.oauth_verifier,
        }, callback);
    },
    function (resp, callback) {
        masterpass.checkout({
            token: resp.oauth_token,
            checkoutId: req.checkout_resource_url.substring(req.checkout_resource_url.lastIndexOf('/') + 1),
        }, callback);
    }
], (err, resp) => {
    if (err) {
        console.log(err);
    } else {
        console.log('card info', resp);
    }
});
var masterpassapi = require('node-masterpass-api').masterpass();

var masterpass = new masterpassapi.Masterpass({
    privateKey: process.env.MP_PRIVATE_KEY,
    consumerKey: process.env.MP_CONSUMER_KEY,
    callBackUrl: process.env.MP_CALLBACK_URL
});

masterpass.requestToken((err, resp) => {
    if (err) {
        console.log(err);
    } else {
        console.log(resp);
    }
});
var masterpassapi = require('node-masterpass-api').masterpass();
var async = require('async');

var masterpass = new masterpassapi.Masterpass({
    privateKey: process.env.MP_PRIVATE_KEY,
    consumerKey: process.env.MP_CONSUMER_KEY,
    callBackUrl: process.env.MP_CALLBACK_URL,
    appVersion: process.env.MP_APP_VERSION,
    appId: process.env.MP_APP_ID,
});

async.waterfall([
    function (callback) {
        masterpass.initSession(callback);
    },
    function (ctx, callback) {
        masterpass.sessionKeySign(ctx, callback);
    }
], (err, resp) => {
    if (err) {
        console.log(err);
    } else {
        console.log(resp);
    }
});

Example App

node-masterpass-api - Masterpass Standard Checkout Lightbox Demo Video (v6)

node-masterpass-api - Masterpass Standard Checkout Lightbox Demo Video

Example App

Example App

There is a sample app in the example folder that shows the available operations, set the following env variables before running the app.

    $env:MP_PRIVATE_KEY = [IO.File]::ReadAllText("---your-private-key---")
    $env:MP_CONSUMER_KEY = "---your-consumer-key---"
    $env:MP_CALLBACK_URL = "http://localhost:3000/requestTokenCallbackv7"
    $env:MP_ORIGIN_URL = "http://localhost:3000"
    $env:MP_CHECKOUT_ID = "--your-merchant-checkout-id--"
    $env:MP_ENV = "stage"

Run the app

    node server.js

node-masterpass-api's People

Contributors

perusworld avatar

Watchers

James Cloos avatar Fabio R. D. de Barros avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.