@todaqmicro/payment-node
v0.4.0
Published
TODAQ Micro's Node.js SDK
Readme
Payment Node.JS
Server-side Node.js SDK for TODAQ Micro micropayments.
Installation
npm install @todaq/payment-nodeUsage
Initialize the SDK
import { Micro } from '@todaq/payment-node';
const micro = new Micro(
'your-client-id',
'your-client-secret',
{ apiVersion: 'v2' }
);Create a Commodity
const commodity = await micro.Commodity.createCommodity(
accessToken,
twinId,
{
cost: 100,
descriptor: 'Premium Article Access',
dq: 'usd'
}
);
console.log('Commodity hash:', commodity.hash);Validate a Payment
const isValid = await micro.Payment.validPayment(
accessToken,
commodityHash,
nonce,
timestamp
);
if (isValid) {
console.log('Payment is valid!');
}Session-Based Payments (Paywall)
The Paywall feature allows platforms to make payments on behalf of users using session tokens. This is useful for implementing paywalls, subscriptions, or any scenario where you want to process payments without requiring users to interact with the payment UI each time.
1. Create a Session
First, create a session for the authenticated user:
// After user has authenticated and you have their access token
const { session } = await micro.Session.createSession(userAccessToken);
// Store this session token securely (e.g., in a cookie or session storage)
// The session expires after 1 hour2. Make Payments Using the Session
Once you have a session token, you can make payments on behalf of the user:
try {
const result = await micro.Paywall.createPayment(
sessionToken,
'payment-type-hash', // The commodity hash
100, // Amount
'https://destination.example.com/callback' // Destination URL
);
console.log('Payment successful!', result);
} catch (error) {
console.error('Payment failed:', error);
}Example: Paywall Middleware
import express from 'express';
import { Micro } from '@todaq/payment-node';
const app = express();
const micro = new Micro(clientId, clientSecret, { apiVersion: 'v2' });
// Middleware to check for valid session
async function requirePayment(req, res, next) {
const sessionToken = req.cookies.session_id;
if (!sessionToken) {
return res.redirect('/auth');
}
try {
// Attempt to make payment for accessing this resource
await micro.Paywall.createPayment(
sessionToken,
req.paywallHash, // Set this based on the resource
req.paywallAmount,
req.originalUrl
);
next();
} catch (error) {
res.status(402).json({ error: 'Payment required' });
}
}
// Protected route
app.get('/premium-content', requirePayment, (req, res) => {
res.json({ content: 'This is premium content!' });
});Personas
// Create a persona
const persona = await micro.Persona.createPersona(
accessToken,
twinId,
{
descriptor: 'Digital Collectible',
// ... other persona properties
}
);API Reference
Commodity
createCommodity(accessToken, twinId, { cost, descriptor, dq })- Create a new commodity
Payment
validPayment(accessToken, hash, nonce, timestamp)- Validate a payment transaction
Paywall
createPayment(session, paymentTypeHash, amount, destinationUrl)- Make a payment on behalf of a user using their session token
Session
createSession(accessToken)- Create a new session token for a user (expires in 1 hour)
Persona
createPersona(accessToken, twinId, personaData)- Create a new persona
Twin
- Twin management utilities
Security Notes
- Session Tokens: Session tokens expire after 1 hour. Store them securely and never expose them in client-side code.
- Credentials: Never commit your client ID and secret to version control.
- HTTPS Only: Always use HTTPS in production to protect session tokens in transit.
License
See LICENSE file.
