ifthenpay.js
ifthenpay unofficial javascript module
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 =
Usage
Using a standalone release build
HINT: Check out newest releases here!
JS ES-stage-0 a.k.a. ES-awesome
Generating multibanco's payment codes
const ifthenpay = entity: '99999' subentity: '999' // Generate a multibanco payment codesconsole/*> Object { "entity": "99999", "reference": "999000014", "value": 55.34} */
Connecting with a node http server
const server = httpconst preSharedKey = '99999999999999999999999999999' const ifthenpay = entity: '99999' subentity: '999' webhook: autosetup: true server preSharedKey { // Callback logic }
Connecting with an express server
const server = const preSharedKey = '99999999999999999999999999999' const ifthenpay = entity: '99999' subentity: '999' webhook: autosetup: true server preSharedKey { // Callback logic }
Callback logic
Callback allows you to do something whenever IfThenPay warns your server about a successful payment.
const ifthenpay = entity: '99999' subentity: '999' webhook: autosetup: true server preSharedKey { // fetch from database const payment = await database if paymentvalue !== value throw "Payment's value is different from the received value!?" // Warn dev team? if paymentcompleted throw "Payment was already completed" // Warn dev team? // Just because everything went fine, lets save await payment // 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 = var ifthenpay = entity: '99999' subentity: '999' // Generate a multibanco payment codesconsole/*> Object { "entity": "99999", "reference": "999000014", "value": 55.34} */
Connecting with an express server
var IfThenPay = var Express = var server = var preSharedKey = '99999999999999999999999999999' var ifthenpay = entity: '99999' subentity: '999' webhook: autosetup: true server: server preSharedKey: preSharedKey { // Callback logic }
Callback logic
var ifthenpay = entity: '99999' subentity: '999' webhook: autosetup: true server: server preSharedKey: preSharedKey { var id = contextid var value = contextvalue return Promise // 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.