Node.js Client
Code example for Node.js
const crypto = require('crypto');
class PaymentEngineClient {
constructor(apiKey, secretKey, baseUrl = 'https://api.2settle.io') {
this.apiKey = apiKey;
this.secretKey = secretKey;
this.baseUrl = baseUrl;
}
sign(method, path, body = {}) {
const timestamp = Date.now().toString();
const bodyStr = JSON.stringify(body);
const bodyHash = crypto.createHash('sha256').update(bodyStr).digest('hex');
const payload = `${timestamp}|${method}|${path}|${bodyHash}`;
const hmacKey = crypto.createHash('sha256').update(this.secretKey).digest('hex');
const signature = crypto.createHmac('sha256', hmacKey).update(payload).digest('hex');
return { timestamp, signature, bodyStr };
}
async request(method, path, body) {
const { timestamp, signature, bodyStr } = this.sign(method, path, body);
const response = await fetch(`${this.baseUrl}${path}`, {
method,
headers: {
'Content-Type': 'application/json',
'X-API-Key': this.apiKey,
'X-Timestamp': timestamp,
'X-Signature': signature,
},
body: method !== 'GET' ? bodyStr : undefined,
});
return response.json();
}
// Create any payment type
async createPayment(options) {
return this.request('POST', '/v1/payments', options);
}
// Get payment by reference
async getPayment(reference) {
return this.request('GET', `/v1/payments/${reference}`, {});
}
// Claim a gift
async claimGift(reference, receiver) {
return this.request('POST', `/v1/payments/gifts/${reference}/claim/confirm`, { receiver });
}
// Fulfill a request
async fulfillRequest(reference, options) {
return this.request('POST', `/v1/payments/requests/${reference}/fulfill`, options);
}
}
// Usage
const client = new PaymentEngineClient('pk_xxx', 'sk_xxx');
// Create a transfer
const transfer = await client.createPayment({
type: 'transfer',
fiatAmount: 10000,
fiatCurrency: 'NGN',
crypto: 'USDT',
network: 'trc20',
payer: { chatId: '7389201648' },
receiver: {
bankCode: '090405',
accountNumber: '8012345678',
},
});
console.log(`Deposit ${transfer.payment.cryptoAmount} USDT to:`);
console.log(transfer.payment.depositAddress);