const { ethers } = require('ethers');
require('dotenv').config();
class RisePayments {
constructor(baseUrl, jwtToken) {
this.baseUrl = baseUrl;
this.headers = {
'Authorization': `Bearer ${jwtToken}`,
'Content-Type': 'application/json'
};
}
async createPayment(
teamNanoid,
recipients,
walletAddress,
privateKey,
payNow = true
) {
// Step 1: Create payment draft
const createResponse = await fetch(`${this.baseUrl}/v2/payments`, {
method: 'POST',
headers: this.headers,
body: JSON.stringify({
from: teamNanoid,
to: recipients,
pay_now: payNow,
network: 'arbitrum',
}),
});
if (!createResponse.ok) {
const errorText = await createResponse.text();
throw new Error(
`Failed to create payment draft: ${createResponse.status} - ${errorText}`
);
}
const { data: typedData } = await createResponse.json();
console.log('✅ Payment draft created:', typedData);
// Step 2: Sign typed data
const wallet = new ethers.Wallet(privateKey);
const signature = await wallet.signTypedData(
typedData.domain,
typedData.types,
typedData.typed_data
);
// Step 3: Execute payment
const executeResponse = await fetch(`${this.baseUrl}/v2/payments`, {
method: 'PUT',
headers: this.headers,
body: JSON.stringify({
signer: walletAddress,
from: teamNanoid,
to: recipients,
pay_now: payNow,
typed_data: typedData.typed_data,
signature: signature,
}),
});
const response = await executeResponse.json();
console.log('✅ Payment executed:', response.data);
return response.data;
}
async queryPayments(teamNanoid, options = {}) {
const params = new URLSearchParams({
team_nanoid: teamNanoid,
state: options.state || 'all',
query_type: options.queryType || 'payable',
...(options.startDate && { start_date: options.startDate }),
...(options.endDate && { end_date: options.endDate }),
...(options.recipient && { recipient: options.recipient })
});
const response = await fetch(`${this.baseUrl}/v2/payments?${params}`, {
headers: this.headers
});
return response.json();
}
}
// Usage example
const payments = new RisePayments('your-base-url', jwtToken); // Configure for your environment
const to = [
{
to: 'us-jRxg2LRL54DJ',
amount_cents: 300,
currency_symbol: 'USD',
invoice_description: 'Papa Sent you',
},
{
to: 'us-d6JHBF2kuZjE',
amount_cents: 700,
currency_symbol: 'USD',
invoice_description: 'Papa Sent you',
},
];
const payment = await payments.createPayment(
'te-bXy7gjb_Iga-',
to,
process.env.WALLET_ADDRESS,
process.env.WALLET_PRIVATE_KEY,
true // true = pay immediately, false = pay intent (pay later)
);