MoneySpace NODE JS SDK
Requirements
- Minimal NodeJS version
v10.x
Installation
yarn add @moneyspace.net/moneyspace-node-js-sdk
Application templates
Check out example/typescript/index.ts or example/nodejs/index.js
templates for typescript/javascript application which uses minimal required configuration to create transaction requests.
Check out example/typescript/template-qrnone.ts or example/nodejs/template-qrnone.js
templates for typescript/javascript application for creation transaction QR Payments with minimalistic web interface.
API
Get MoneySpace API handle
import { createMoneySpace, createSimpleCredentialsProvider } from '@moneyspace.net/moneyspace-node-js-sdk';
const api = createMoneySpace({
credentials: {'secretId': '<secret id>', 'secretKey': '<secret key>'},
successUrl: 'https://www.moneyspace.net/merchantapi/paycardsuccess',
cancelUrl: 'https://www.moneyspace.net?status=cancel',
failUrl: 'https://www.moneyspace.net?status=fail',
baseUrl:
currency:
gatewayType:
feeType:
});
Notes
-
credentials
:
object
: {'secretId': '<secret id>', 'secretKey': '<secret key>'}
function
: Credentials provider.
For example JSON file credentials provider:
createFileCredentialsProvider('moneyspace_credentials.json');
-
feeType
:
include
Merchant pays transaction feesexclude
Shopper pays transaction fees
-
amount
: Order amount. It must be string with two decimal places
after point and no comma in number.
-
customerOrderId
: Internal merchant order id.
Order id must be unique over all merchant orders.
Length can't be over 20 symbols. Can contain english and numbers only.
-
gatewayType
:
card
: Payment by cardqrnone
: Payment by qrcode. This mode can be in use with include
feeType mode and non-empty productDescription
.
Create payment transaction
const resp = await api.merchantApi.sendPaymentTransaction({
amount: '<order amount>',
customerOrderId: '<order id>',
firstName: '<shopper first name>',
lastName: '<shopper last name>',
currency:
gatewayType:
feeType:
message:
email:
phone:
address:
productDescription:
successUrl:
failUrl:
cancelUrl:
});
Notes
Response of calling payment sendPaymentTransaction
:
{
orderId: '<merchant order od>',
transactionId: '<moneyspace transaction id>',
timeHash: '<transacton time hash>'
}
Check payment transaction status
const resp = await api.checkPaymentTransaction({
transactionId: '<transaction id>',
orderId: '<order id>'
});
This function accepts sendPaymentTransaction
response object or object where
you can specify transactionId
or orderId
. If transactionId
not specified merchant
orderId
will be used to check transaction status.
const tx = await api.merchantApi.sendPaymentTransaction({...});
const status = await api.checkPaymentTransaction(tx);
Returned value
{
amount: '<order amount>',
statusPayment: '<Transaction status>'
transactionId:
orderId:
description:
}
Get payment link for Shopper
const tx = await api.merchantApi.sendPaymentTransaction({...});
const linkUrl = await api.merchantApi.getPaymentLink(tx.transactionId);
Accepts tranasctionId
and returns payment link for the shopper for payment.
If gatewayType
is qrnone
, then payment link shows QR code. QR code expires within 15 minutes.
Get Merchant profile data
const profileData = await api.merchantApi.getMerchantProfile();
Profile data:
{
stores: [
{
name: '<store name>',
logo: '<optional link to merchant store image>',
phone: '<optional merchant phone number>'
},
...
]
}
WebHook integration
Merchant SDK provided WebHook integration in order to be notified about payment events.
Merchant SDK can be used in context of existing application webserver like ExpressJS
as well in dedicated nodejs http/http webserver.
WebHook in standalone NodeJS HTTP/HTTPS server
import * as http from 'http';
import { createMoneySpace } from '@moneyspace.net/moneyspace-node-js-sdk';
const api = createMoneySpace({....});
http
.createServer(api.createWebHookRequestHandler())
.on('error', function(err) {
console.log(err);
})
.listen(
{
host: 'localhost',
port: 8080
},
() => console.log('HTTP WebHook server running')
);
WebHook in ExpressJS server
import * as express from 'express';
import { createMoneySpace } from '@moneyspace.net/moneyspace-node-js-sdk';
const api = createMoneySpace({....});
const app = express();
app.post('/mywebhook', api.createWebHookRequestHandler());
app.listen(8080, function () {
console.log('HTTP WebHook server running')
});
Receiving payment transactions events
Once you have installed MoneySpace SDK WebHook
you can receive payment events for your store.
const ref = api.registerApiEventListener('*', (type, event) => {
console.log('Got WebHook event: ', event);
if (type == 'PaymentSuccess') {
console.log(`Order: ${event.orderId} paid successfully`);
}
});
api.unregisterApiEventListener(ref);
Event types
*
: Listener will be subscribed to all API eventsPaymentSent
: Payment transaction sent successfullyPaymentReceived
: MoneySpace server acknowledged what payment transaction receivedPaymentPending
: Payment in progressPaymentSuccess
: Payment completed successfullyPaymentFailed
: Payment failedPaymentCancelled
: Payment cancelled.
Payment listener receives event object with the following structure:
{
transactionId: '<transaction id>';
orderId: '<order id>';
amount: '<order amount>';
timeHash: '<optional timestamp string: yyyyMMddHHmmss>';
}
Development
Run local tests
yarn install
yarn run test
Run tests using real MoneySpace API
Before run
Create json credentials configuration file: ${HOME}/.moneyspace-test-creds.json
with the following
content:
{
"secretId": "...",
"secretKey": "..."
}
yarn install
yarn run test-manual