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

ifthenpay

v0.3.4

Published

ifthenpay unofficial javascript module

Downloads

7

Readme

ifthenpay.js

ifthenpay unofficial javascript module

mbifthenpay

What is this?

Portugal has a payment service called Referências Multibanco, which means "multi bank references" in English.

Multibanco allows portuguese people to get way more from an ATM service.

Portuguese people can, over a Smartphone or an ATM:

  • Pre-charge their phones
  • Pay bills (electric, ISP and other contract bills)
  • Pay for one-shot services
  • Send money to others just by knowing their number
  • Pay taxes
  • much more, I can't remember all cases.

Those services are offered by a all-bank-shared company called SIBS.

For being able to generate Referências Multibanco, you must engage with your bank's Account Manager and pay a Setup + Anual fee, but it is only worth if you have a noticeable business flow going.

To bypass this scenario, some products emerged by allowing the small companies to enter into the Multibanco scene with a pay-as-you-go business model as you may find on Credit Cards.

IfThenPay is one of them. I've choosed to work with them because they've created an algorithm to allow their customers to create their custom Multibanco references on the go.

Note: At them time, I have not any kind of engagement with IfThenPay, and I'm the owner of this Software.

Why should I use it?

  • Does NOT require API connections for generating Multibanco references.
  • IfThenPay connects to your server's endpoint (webhook) when a payment is made
  • This project does not require any module on production
  • It is Isomorphic/Universal, can run over both browser or server engines.
  • Server-side webhook can work with node's http interface or server frameworks such as express or restify.
  • Has a test framework harassing hardly for bugs.
  • Standalone compressed released is about 5KB!!!

Installation

npm i --save ifthenpay

NOTE: In case you're including this in a bundle, and need to cut down the server part to decrease bundle's size, please include the browser implementation instead.

var IfThenPay = require( 'ifthenpay/browser' )

Usage

Using a standalone release build

HINT: Check out newest releases here!

<script type="text/javascript" src="/path/to/ifthenpay.min.js"></script>
<script type="text/javascript">

  window.onload = function () {
    var ifthenpay = IfThenPay({ /* ... */ })
  }

</script>

JS ES-stage-0 a.k.a. ES-awesome

Generating multibanco's payment codes

import IfThenPay from 'ifthenpay'

const ifthenpay = new IfThenPay({
  entity: '99999'
  subentity: '999'
})

// Generate a multibanco payment codes
console.log( ifthenpay.generate( 55.34 ) )
/*> Object {
  "entity": "99999",
  "reference": "999000014",
  "value": 55.34
} */

Connecting with a node http server

import http from 'http'

const server = http.createServer().listen(80)
const preSharedKey = '99999999999999999999999999999'

const ifthenpay = new IfThenPay({
  entity: '99999'
  subentity: '999',
  webhook: {
    autosetup: true,
    server, preSharedKey,
    callback: async function ({ id, value }) {
      // Callback logic
    }
  }
})

Connecting with an express server

import IfThenPay from 'ifthenpay'
import Express from 'express'

const server = Express().listen(80)
const preSharedKey = '99999999999999999999999999999'

const ifthenpay = new IfThenPay({
  entity: '99999'
  subentity: '999',
  webhook: {
    autosetup: true,
    server, preSharedKey,
    callback: async function ({ id, value }) {
      // Callback logic
    }
  }
})

Callback logic

Callback allows you to do something whenever IfThenPay warns your server about a successful payment.

const ifthenpay = new IfThenPay({
  entity: '99999'
  subentity: '999',
  webhook: {
    autosetup: true,
    server, preSharedKey,
    callback: async function ({ id, value }) {

      // fetch from database
      const payment = await database.fetch( id )

      if ( payment.value !== value ) {
        throw new Error( "Payment's value is different from the received value!?" )
        // Warn dev team?
      }

      if ( payment.completed ) {
        throw new Error( "Payment was already completed" )
        // Warn dev team?
      }

      // Just because everything went fine, lets save
      await payment.set( 'completed', true ).save()

      // We don't need to return nothing fancy, if callback runs without errors
      // this module will assume everything is fine and will respond with a 200
      // http status to the IfThenPay webhook request.
    }
  }
})

JS ES5 / CommonJS

Generating multibanco's payment codes

var IfThenPay = require( 'ifthenpay' )

var ifthenpay = new IfThenPay({
  entity: '99999'
  subentity: '999'
})

// Generate a multibanco payment codes
console.log( ifthenpay.generate( 55.34 ) )
/*> Object {
  "entity": "99999",
  "reference": "999000014",
  "value": 55.34
} */

Connecting with an express server

var IfThenPay = require( 'ifthenpay' )
var Express = require( 'express' )

var server = Express().listen(80)
var preSharedKey = '99999999999999999999999999999'

var ifthenpay = new IfThenPay({
  entity: '99999'
  subentity: '999',
  webhook: {
    autosetup: true,
    server: server, preSharedKey: preSharedKey,
    callback: function (context) {
      // Callback logic
    }
  }
})

Callback logic

var ifthenpay = new IfThenPay({
  entity: '99999'
  subentity: '999',
  webhook: {
    autosetup: true,
    server: server, preSharedKey: preSharedKey,
    callback: function (context) {
      var id = context.id
      var value = context.value

      return Promise.try(function () {
        // fetch from database
        return database.fetch( id )
      })
      .then(function ( payment ) {
        if ( payment.value !== value ) {
          throw new Error( "Payment's value is different from the received value!?" )
          // Warn dev team?
        }

        if ( payment.completed ) {
          throw new Error( "Payment was already completed" )
          // Warn dev team?
        }

        return payment.set( 'completed', true ).save()
      })

      // We don't need to return nothing fancy, if callback runs without errors
      // this module will assume everything is fine and will respond with a 200
      // http status to the IfThenPay webhook request.
    }
  }
})

API

IfThenPay

options.entity

Number [0-9]{5} Option provided by IfThen.

options.subentity

Number [0-9]{1,3} Option provided by IfThen.

options.webhook

Object | false Object with options for webhook handling.

options.webhook.autosetup

Boolean | false Defines if webhook should auto mount the middleware handler on the server. Needed for those who are using other routers, such as Restify.

options.webhook.url

String | false

URL to strict responses. If not defined, it will respond to all requests.

options.webhook.server

http.Server / Express / Restify

Will be used to set up a route handler

options.webhook.preSharedKey

String .{1,50} Pre shared key provided to IfThen.

options.webhook.callback

Function ( Object: { entity, subentity, id, reference, value, terminal, date } )

NOTE: all parameters are mandatory, except for terminal and date.

Method for handling validation on your side, such database payments comparison.

Method will receive most needed variables, already parsed and validated.

The callback won't be called in case:

  • There isn't a match between pre-shared keys
  • Entity does not match
  • Reference contains a different subentity
  • Entity/Reference/Value has a bad Checksum

The callback should return a promise in case it contains async operations. In case you wan't to delay the payment webhook for later, you must throw up an error or a rejected promise.

LICENSE - POL-v1

Private-Open License v1