npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mastercard-hosted-session

v3.0.1

Published

Wrapper around Mastercard hosted session, allowing you to treat it as a modern Promise-based Javascript API.

Downloads

131

Readme

Mastercard Hosted Session Facade

You're probably here because you went to implement the Mastercard (TNS) session.js API in your app and encountered the...odd...design of their API. "Why," you ask, "would they think a callback registered on a config object being called by a completely unrelated global method is a good idea? Surely there's a better way?"

Perhaps you wish that you could ask for a card session, and get a Promise back that might resolve to a valid session, or reject on an invalid session.

Ssh. It's ok. Just use this facade. Don't fret. Don't look into the lib folder, because this is an ugly, ugly hack. But it works reliably. And you can keep writing your modern javascript code without twisting your brain around a tortured API.

Supported Environments and Dependencies

Mastercard's session.js is a browser API that works via injected iframes. So this facade will also only work in a browser. It's also a javascript module, so you will need a module builder. I've tested this with webpack, but since the distribution is transpiled you should be able to use it with your packer of choice.

No matter what module environment you choose, your destination will need to support ES6 Promises, since this facade relies on them heavily. Many browsers already natively support Promises, but if a target browser you support does not you'll need to include a polyfill.

Prerequisites

Unfortunately, Mastercard's session.js does not return Access-Control-Allow-Origin headers when you request the script for your merchant ID. This means this library is unable to dynamically append the Mastercard script for you, for reasons of CORS restrictions.

You must append a script tag containing your own Mastercard session.js script, e.g.

<script src="https://ap-gateway.mastercard.com/form/version/52/merchant/<MERCHANT_ID>/session.js" />
<script src="myCodeBundle.js" />

Installation

npm i mastercard-hosted-session --save-dev

Usage

Import the library, and initialise the module with the merchant ID supplied to you by Mastercard, and the config you'd normally pass to the PaymentSession.configure method. Do not pass the callbacks methods on your config object, since the facade configures those for you.

The initialisation function returns a Promise that will resolve a Hosted Session facade, initialised against the form specified by the config fields.

The example below uses Promises directly, but since a Promise is a Promise you can happily use the async function style if your environment or transpilation tool support them.

const getHostedSession = require('mastercard-hosted-session')

getHostedSession({
  fields: {
    card: {
      number: '#card-number',
      securityCode: '#security-code',
      expiryMonth: '#expiry-month',
      expiryYear: '#expiry-year'
    }
  },
  frameEmbeddingMitigation: ['csp'],
  interaction: {
    displayControl: {
      formatCard: 'EMBOSSED',
      invalidFieldCharacters: 'ALLOW'
    }
  }
}).then(hostedSession => {
  // hostedSession is a facade that has one public function: `sezzionize`
  return hostedSession.sessionize()
}).then((result) => {
  console.log(result.status) // e.g. 'ok' or 'fields_in_error'
  console.log(result.session.id) // e.g. SESSION001212312123123123
  console.log(result.sourceOfFunds.provided.card.brand) // e.g 'MASTERCARD'
}).catch(error => {
  // The facade rejects with HostedSessionError
  // if the script fails to append, or fails to sessionise.
  console.error(error)
})