Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
import test from 'node:test';
import assert from 'node:assert/strict';
import {mkdtemp,readFile,writeFile,mkdir,rm} from 'node:fs/promises';
import path from 'node:path';import os from 'node:os';import {DatabaseSync} from 'node:sqlite';
const temporary=await mkdtemp(path.join(os.tmpdir(),'payment-index-'));
process.env.LOADTEST_DATA_DIR=temporary;process.env.LOADTEST_CONSOLE_ROOT=path.resolve('.');process.env.LOADTEST_ROOT=path.resolve('..');
const config=JSON.parse(await readFile('../loadtest.config.example.json','utf8'));config.output=path.join(temporary,'artifacts');
process.env.LOADTEST_CONFIG_FILE=path.join(temporary,'seed.json');await writeFile(process.env.LOADTEST_CONFIG_FILE,JSON.stringify(config));
const state=await import('../server/state.mjs');state.initializeConfig();
const {createPaymentIndex,seedPaymentIndex,rebuildBuckets,outcomeWriter,paymentBars,paymentPage,paymentDetail,nextDiscrepancy}=await import('../server/reconciliation-index.mjs');
const directory=path.join(config.output,'single/runs/test');await mkdir(directory,{recursive:true});
const db=new DatabaseSync(path.join(directory,'reconciliation.sqlite'));
const n=Number(process.env.CONSOLE_SCALE_PAYMENTS||10000);
test(`exact bounded reconciliation queries over ${n.toLocaleString()} payments`,()=>{
const started=performance.now();createPaymentIndex(db);
db.exec('BEGIN');const chunk=100000;
for(let offset=0;offset<n;offset+=chunk)db.prepare(`WITH RECURSIVE sequence(x) AS (SELECT ? UNION ALL SELECT x+1 FROM sequence WHERE x<?) INSERT INTO reconciliation_payments(id,elapsed_ms,merchant_id,payment_id,recorded_status,database_status,outcome) SELECT x,CAST((x-1)/2 AS INTEGER),'merchant','p'||CAST(x AS INTEGER),'succeeded',CASE WHEN x%100003=0 THEN NULL WHEN x%17009=0 THEN 'failed' ELSE 'succeeded' END,CASE WHEN x%100003=0 THEN 'missing' WHEN x%17009=0 THEN 'mismatch' ELSE 'match' END FROM sequence`).run(offset+1,Math.min(n,offset+chunk));
rebuildBuckets(db);db.prepare('INSERT INTO reconciliation_meta VALUES(?,?)').run('duration',Math.ceil(n/2));db.exec('COMMIT');
const built=performance.now(),overview=paymentBars({id:'single:test',bars:300}),queried=performance.now();
assert.equal(overview.mode,'buckets');assert.ok(overview.rows.length<=302);
assert.equal(Object.values(overview.totals).reduce((a,b)=>a+b,0),n);
const exact=db.prepare("SELECT SUM(outcome='match') matched,SUM(outcome='missing') missing,SUM(outcome='mismatch') mismatch FROM reconciliation_payments").get();
assert.equal(overview.totals.match,exact.matched);assert.equal(overview.totals.missing,exact.missing);assert.equal(overview.totals.mismatch,exact.mismatch);
assert.equal(overview.rows.reduce((s,r)=>s+r.missing+r.mismatch,0),exact.missing+exact.mismatch);
assert.ok(Buffer.byteLength(JSON.stringify(overview))<180000);
const detail=paymentBars({id:'single:test',from:100,to:110,bars:100});assert.equal(detail.mode,'payments');assert.equal(detail.rows.length,20);
const stableDetail=paymentBars({id:'single:test',from:0,to:159,bars:300});assert.equal(stableDetail.rows.length,318);assert.equal(stableDetail.mode,'payments');
const edges=paymentBars({id:'single:test',from:123,to:4567,bars:30});assert.equal(edges.rows.reduce((s,r)=>s+r.match+r.missing+r.mismatch,0),(4567-123)*2);
const first=paymentPage({id:'single:test',filter:'all',limit:7}),second=paymentPage({id:'single:test',filter:'all',...first.next,limit:7});assert.equal(new Set([...first.rows,...second.rows].map(r=>r.id)).size,14);
assert.equal(paymentDetail({id:'single:test',merchant:'merchant',payment:'p1'}).id,1);
assert.ok(!nextDiscrepancy({id:'single:test',after:n}));
if(n>17009)assert.equal(nextDiscrepancy({id:'single:test'}).id,17009);
console.log(JSON.stringify({payments:n,buildMs:Math.round(built-started),overviewMs:Math.round(queried-built),bars:overview.rows.length,payloadBytes:Buffer.byteLength(JSON.stringify(overview)),discrepancies:exact.missing+exact.mismatch}));
});
test('pending bars update transactionally, first observation defines load time',()=>{
const source=path.join(directory,'results.sqlite'),input=new DatabaseSync(source);input.exec(`CREATE TABLE request_events(merchant_id TEXT,payment_id TEXT,observed_at INTEGER,run_id TEXT,role TEXT,operation TEXT,api_status_code INTEGER,event_id INTEGER,message TEXT,error_body TEXT);CREATE TABLE payments(run_id TEXT,merchant_id TEXT,payment_id TEXT,status TEXT,observed_at INTEGER,role TEXT);
INSERT INTO payments VALUES('run','m','a','succeeded',2000,'measured'),('run','m','b','requires_payment_method',3000,'measured');
INSERT INTO request_events(merchant_id,payment_id,observed_at) VALUES('m','a',1100),('m','a',2000),('m','b',1200);`);input.close();
seedPaymentIndex(db,source,{runId:'run',startedAt:1000,durationSeconds:3});
assert.equal(paymentBars({id:'single:test'}).totals.pending,2);
db.exec('BEGIN');outcomeWriter(db)([{merchantId:'m',paymentId:'a',databaseStatus:'succeeded',outcome:'match'},{merchantId:'m',paymentId:'b',databaseStatus:null,outcome:'missing'}]);db.exec('COMMIT');
const result=paymentBars({id:'single:test'});assert.deepEqual(result.totals,{match:1,missing:1,mismatch:0,pending:0,creation_unknown:0});assert.equal(result.rows[0].elapsed_ms,100);
assert.equal(paymentPage({id:'single:test',filter:'discrepancies'}).rows[0].payment_id,'b');
});
test.after(async()=>{db.close();await rm(temporary,{recursive:true,force:true});});