Initial Node.js SDK v1.0.0
This commit is contained in:
54
examples/basic.ts
Normal file
54
examples/basic.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* FetcherPay Node.js SDK - Basic Example
|
||||
*/
|
||||
|
||||
import { FetcherPay } from '../src';
|
||||
|
||||
// Initialize client
|
||||
const client = new FetcherPay({
|
||||
apiKey: process.env.FETCHERPAY_API_KEY || 'sandbox',
|
||||
environment: 'sandbox',
|
||||
});
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
// Create a payment
|
||||
console.log('Creating payment...');
|
||||
const payment = await client.payments.create({
|
||||
amount: 10000, // $100.00 in cents
|
||||
currency: 'USD',
|
||||
source: {
|
||||
payment_method_id: 'pm_bank_123',
|
||||
},
|
||||
destination: {
|
||||
payment_method_id: 'pm_merchant_456',
|
||||
},
|
||||
rail: 'auto', // Let FetcherPay choose optimal rail
|
||||
});
|
||||
console.log('Payment created:', payment.id, 'Status:', payment.status);
|
||||
|
||||
// Retrieve the payment
|
||||
console.log('\nRetrieving payment...');
|
||||
const retrieved = await client.payments.retrieve(payment.id);
|
||||
console.log('Retrieved:', retrieved.id, 'Timeline:', retrieved.timeline.length, 'events');
|
||||
|
||||
// List payments
|
||||
console.log('\nListing payments...');
|
||||
const payments = await client.payments.list({ limit: 5 });
|
||||
console.log(`Found ${payments.data.length} payments`);
|
||||
|
||||
// List ledger accounts
|
||||
console.log('\nListing ledger accounts...');
|
||||
const accounts = await client.ledger.listAccounts();
|
||||
console.log(`Found ${accounts.data.length} accounts`);
|
||||
accounts.data.forEach(acc => {
|
||||
console.log(` - ${acc.name}: $${acc.balance.available / 100} available`);
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
40
examples/webhook-verification.ts
Normal file
40
examples/webhook-verification.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* FetcherPay Node.js SDK - Webhook Verification Example
|
||||
*/
|
||||
|
||||
import { FetcherPay } from '../src';
|
||||
|
||||
const client = new FetcherPay({
|
||||
apiKey: 'sandbox',
|
||||
environment: 'sandbox',
|
||||
});
|
||||
|
||||
// Example webhook payload
|
||||
const payload = JSON.stringify({
|
||||
id: 'evt_123',
|
||||
type: 'payment.settled',
|
||||
created_at: '2026-02-18T20:00:00Z',
|
||||
data: {
|
||||
id: 'pay_456',
|
||||
status: 'settled',
|
||||
amount: 10000,
|
||||
},
|
||||
});
|
||||
|
||||
// Your webhook secret from the dashboard
|
||||
const webhookSecret = 'whsec_your_secret_here';
|
||||
|
||||
// Simulate receiving a webhook
|
||||
const signature = 'sha256=...'; // From X-FetcherPay-Signature header
|
||||
|
||||
// Verify the signature
|
||||
const isValid = client.verifyWebhookSignature(payload, signature, webhookSecret);
|
||||
|
||||
if (isValid) {
|
||||
console.log('✅ Webhook signature verified');
|
||||
// Process the webhook event
|
||||
const event = JSON.parse(payload);
|
||||
console.log('Event type:', event.type);
|
||||
} else {
|
||||
console.log('❌ Invalid webhook signature');
|
||||
}
|
||||
Reference in New Issue
Block a user