8Pay
Search…
JavaScript SDK
The SDK provides a way to programmatically interact with 8Pay's smart contracts.
It allows you to manage the subscriptions to your plans (e.g. trigger billings or terminate them).

Installation

This is a Node.js module available through the npm registry.
Before installing, download and install Node.js.
Installation is done using the npm install command:
1
$ npm install @8pay/sdk
Copied!

Usage

This module uses web3 under the hood, which is the most popular library used to interact with the Web 3.0.
To keep things familiar with web3, it follows the same strucure when sending transactions but adds additional functionalities.
To get started, import the module and provide it with an instance of web3:
1
const Web3 = require('web3');
2
const EightPaySDK = require('@8pay/sdk');
3
4
const web3 = new Web3('<provider-url>');
5
const eightPay = new EightPaySDK(web3, 'bsc-mainnet');
Copied!

Sending transactions

There are two ways to sign a transaction before broadcasting it to the blockchain:
    Using an unlocked account (e.g. when using Metamask)
1
eightPay.fixedRecurring.bill(planId, subscriptionIds)
2
.send({ from: account })
Copied!
    With privateKey
1
eightPay.fixedRecurring.bill(planId, subscriptionIds)
2
.send({ privateKey: '0x32df7......' })
Copied!

Estimate gas

The gas consumed by a transaction can be estimated using the estimateGas function which takes and optionsobject and returns the amount of gas as number:
1
eightPay.fixedRecurring.bill(planId, subscriptionIds)
2
.estimateGas(options)
Copied!

Options

The send method accepts the same options available in web3 like gas, gasPrice, nonce and so on.

Events

The following events are emitted when sending a transaction: transactionHash, receipt, confirmation and error.
1
const receipt = await eightPay.fixedRecurring.bill(planId, subscriptionIds)
2
.send({ from: account })
3
.on('transactionHash', hash => {})
4
.on('receipt', receipt => {})
5
.on('confirmation', confirmation => {})
6
.on('error', error => {})
Copied!

Methods

Here is the list of all the available methods for each billing model.

Fixed Recurring

Bill

Bills subscriptions of a plan.
Parameters:
    planId - id of the plan
    subscriptionIds - array of subscription ids
1
const planId = '0x57b2059e526841b3dfd964144513359c9fcfd6d91040b6c47f589c1e032b6bf4';
2
const subscriptionIds = ['0xe63ba761797e289076f80a7c0916a31740684806aaf507da85f81ee785fec6ba'];
3
4
const receipt = await eightPay.fixedRecurring.bill(planId, subscriptionIds)
5
.send({ from: account })
Copied!

Terminate

Forcefully cancels subscriptions of a plan.
The sender account must be the plan's admin or an operational account with terminate permission.
Parameters:
    planId - id of the plan
    subscriptionIds - array of subscription ids
1
const planId = '0x57b2059e526841b3dfd964144513359c9fcfd6d91040b6c47f589c1e032b6bf4';
2
const subscriptionIds = ['0xe63ba761797e289076f80a7c0916a31740684806aaf507da85f81ee785fec6ba'];
3
4
const receipt = await eightPay.fixedRecurring.terminate(planId, subscriptionIds)
5
.send({ from: account })
Copied!

Variable Recurring

Bill

Bills subscriptions of a plan.
The sender account must be the plan's admin or an operational account with bill permission.
Parameters:
    planId - id of the plan
    subscriptionIds - array of subscription ids
    amounts - array of amounts to charge for each subscription
1
const planId = '0x57b2059e526841b3dfd964144513359c9fcfd6d91040b6c47f589c1e032b6bf4';
2
const subscriptionIds = ['0xe63ba761797e289076f80a7c0916a31740684806aaf507da85f81ee785fec6ba'];
3
const amounts = [eightPay.utils.parseAmount('10', '8PAY')];
4
5
const receipt = await eightPay.variableRecurring.bill(planId, subscriptionIds, amounts)
6
.send({ from: account })
Copied!

Terminate

Forcefully cancels subscriptions of a plan.
The sender account must be the plan's admin or an operational account with terminate permission.
Parameters:
    planId - id of the plan
    subscriptionIds - array of subscription on-chain ids
1
const planId = '0x57b2059e526841b3dfd964144513359c9fcfd6d91040b6c47f589c1e032b6bf4';
2
const subscriptionIds = ['0xe63ba761797e289076f80a7c0916a31740684806aaf507da85f81ee785fec6ba'];
3
4
const receipt = await eightPay.variableRecurring.terminate(planId, subscriptionIds)
5
.send({ from: account })
Copied!

On Demand

Bill

Bills subscriptions of a plan.
The sender account must be the plan's admin or an operational account with bill permission.
Parameters:
    planId - id of the plan
    subscriptionIds - array of subscription ids
    amounts - array of amounts to charge for each subscription
1
const planId = '0x57b2059e526841b3dfd964144513359c9fcfd6d91040b6c47f589c1e032b6bf4';
2
const subscriptionIds = ['0xe63ba761797e289076f80a7c0916a31740684806aaf507da85f81ee785fec6ba'];
3
const amounts = [eightPay.utils.parseAmount('10', '8PAY')];
4
5
const receipt = await eightPay.onDemand.bill(planId, subscriptionIds, amounts)
6
.send({ from: account })
Copied!

Terminate

Forcefully cancels subscriptions of a plan.
The sender account must be the plan's admin or an operational account with terminate permission.
Parameters:
    planId - id of the plan
    subscriptionIds - array of subscription ids
1
const planId = '0x57b2059e526841b3dfd964144513359c9fcfd6d91040b6c47f589c1e032b6bf4';
2
const subscriptionIds = ['0xe63ba761797e289076f80a7c0916a31740684806aaf507da85f81ee785fec6ba'];
3
4
const receipt = await eightPay.onDemand.terminate(planId, subscriptionIds)
5
.send({ from: account })
Copied!

Accounts

The accounts utility can be used to obtain an account object from a private key or a mnemonic. The account object has two properties: address and privateKey.
1
const privateKey = '<private-key>';
2
const account = eightPay.accounts.fromPrivateKey(privateKey);
3
4
// OR
5
6
const mnemonic = '<mnemonic>';
7
const mnemonicIndex = 0;
8
const account = eightPay.accounts.fromMnemonic(mnemonic, mnemonicIndex);
9
10
console.log(account);
11
12
/*
13
{
14
address: '0x2F2....',
15
privateKey: '0x1Ee...'
16
}
17
*/
Copied!

Utils

To help in parsing amount from and to ethereum decimals, you can use addDecimals and parseDecimals methods.
1
const parsedAmount = eightPay.utils.parseAmount('1', '8PAY') // 1000000000000000000
2
const amount = eightPay.utils.formatAmount(parsedAmount, '8PAY') // 1
Copied!
Last modified 4mo ago