forked from unimatrix/web
Settle recipient-only Rihla transfers with funded tax journal
This commit is contained in:
parent
8601f3a120
commit
ed1637299e
2 changed files with 143 additions and 4 deletions
28
website/node/rihla-postbank.mjs
Normal file
28
website/node/rihla-postbank.mjs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import {createHash} from 'node:crypto';
|
||||
import {PostbankPayments} from './postbank-payments.mjs';
|
||||
import {creditID} from './postbank-credit-ledger.mjs';
|
||||
|
||||
const taxTreasury='sys_tax_treasury';
|
||||
const systemActor=creditID('rihla:internal-fee-actor');
|
||||
|
||||
export function createRihlaFeePoster({backingSystemKey,clock=Date.now}){
|
||||
if(typeof backingSystemKey!=='string'||!/^sys_rihla_[a-z0-9_]{1,24}$/.test(backingSystemKey))
|
||||
throw Error('A dedicated Rihla backing system account is required.');
|
||||
const payments=new PostbankPayments(null,null,{clock});
|
||||
return async (tx,{transferId,taxMinor})=>{
|
||||
const rows=await tx.query('SELECT * FROM pb_accounts WHERE system_key IN (?,?) ORDER BY id FOR UPDATE',
|
||||
[backingSystemKey,taxTreasury]);
|
||||
const source=rows.find(row=>row.system_key===backingSystemKey);
|
||||
const target=rows.find(row=>row.system_key===taxTreasury);
|
||||
if(!source||!target||source.state!=='active'||target.state!=='active'||
|
||||
source.client_number!==null||source.package_id!==null||source.type_code!=='999')
|
||||
throw Error('Rihla backing and tax treasury must be active.');
|
||||
if(taxMinor<=0n||BigInt(source.balance_minor)-BigInt(source.reserved_minor)<taxMinor)
|
||||
throw Error('Rihla backing does not have enough available funds.');
|
||||
const id=creditID('rihla:fee:'+transferId);
|
||||
const fingerprint=createHash('sha256').update(JSON.stringify([id,String(source.id),String(target.id),String(taxMinor)])).digest('hex');
|
||||
await tx.query(`INSERT INTO pb_orders(id,actor_id,source_id,target_id,fingerprint,amount_minor,memo,mode,state,created_at)
|
||||
VALUES(?,?,?,?,?,?,'Rihla settlement fee','rihla','queued',?)`,[id,systemActor,source.id,target.id,fingerprint,String(taxMinor),clock()]);
|
||||
await payments.post(tx,{id,actor_id:systemActor,amount_minor:String(taxMinor)},{...source,entity_id:null},target,false);
|
||||
};
|
||||
}
|
||||
|
|
@ -49,7 +49,7 @@ export function createGameRihlaAPI(central,bank,settings={},deps={}){
|
|||
const fields={
|
||||
read:['action','uuid'],quote:['action','uuid','amount','recipientUuid'],
|
||||
create:['action','uuid','amount','recipientUuid','quoteReference','requestId'],
|
||||
cancel:['action','uuid','number']
|
||||
cancel:['action','uuid','number'],collect:['action','uuid','number']
|
||||
};
|
||||
if(!Object.hasOwn(fields,input.action)||Object.keys(input).some(key=>!fields[input.action].includes(key)))
|
||||
return json({message:'Invalid Rihla request.'},400);
|
||||
|
|
@ -57,7 +57,7 @@ export function createGameRihlaAPI(central,bank,settings={},deps={}){
|
|||
const actor=await resolve(input.uuid);
|
||||
if(!actor)return json({message:'Link your active personal website account first.'},403);
|
||||
if(!bank)return json({message:'Rihla records are temporarily unavailable.'},503);
|
||||
if(input.action==='create'&&deps.reservationsEnabled!==true)
|
||||
if(input.action==='create'&&deps.reservationsEnabled!==true&&typeof deps.postFee!=='function')
|
||||
return json({message:'Rihla transfer settlement is unavailable.'},503);
|
||||
if(input.action==='quote'||input.action==='create'){
|
||||
if(!validUuid(input.recipientUuid)||input.recipientUuid===input.uuid)
|
||||
|
|
@ -91,6 +91,39 @@ export function createGameRihlaAPI(central,bank,settings={},deps={}){
|
|||
return json({number,state:'created',amountMinor:quote.amountMinor,taxMinor:quote.taxMinor},201);
|
||||
});
|
||||
}
|
||||
if(input.action==='collect'){
|
||||
if(typeof input.number!=='string'||!/^RIA-[A-Z0-9-]{1,36}$/.test(input.number))
|
||||
return json({message:'Invalid RIA reference.'},400);
|
||||
if(typeof deps.postFee!=='function')return json({message:'Rihla transfer settlement is unavailable.'},503);
|
||||
return await bank.transaction(async tx=>{
|
||||
const reference=await tx.first('SELECT sender_id FROM ria_transfers WHERE transaction_number=? AND recipient_id=?',
|
||||
[input.number,actor.id]);
|
||||
if(!reference)return json({message:'Transfer unavailable.'},404);
|
||||
if(reference.sender_id===actor.id)throw Error('Rihla transfer must have two parties.');
|
||||
for(const id of [reference.sender_id,actor.id].sort())await ensureRihlaAccount(tx,id,{clock});
|
||||
const accounts=await tx.query('SELECT owner_id,balance_minor,reserved_minor FROM ria_accounts WHERE owner_id IN (?,?) ORDER BY owner_id FOR UPDATE',
|
||||
[reference.sender_id,actor.id]);
|
||||
const row=await tx.first('SELECT * FROM ria_transfers WHERE transaction_number=? AND recipient_id=? FOR UPDATE',
|
||||
[input.number,actor.id]);
|
||||
if(!row||row.sender_id!==reference.sender_id)return json({message:'Transfer unavailable.'},404);
|
||||
if(row.state==='paid')return json(transferView(row));
|
||||
if(row.state!=='created')return json({message:'This transfer cannot be collected.'},409);
|
||||
const sender=accounts.find(account=>account.owner_id===row.sender_id);
|
||||
const recipient=accounts.find(account=>account.owner_id===actor.id);
|
||||
if(!sender||!recipient)throw Error('Rihla accounts are unavailable.');
|
||||
const amount=BigInt(row.amount_minor),tax=BigInt(row.tax_minor),total=amount+tax;
|
||||
const held=BigInt(sender.reserved_minor),balance=BigInt(sender.balance_minor);
|
||||
const received=BigInt(recipient.balance_minor)+amount;
|
||||
if(amount<=0n||tax<=0n||held<total||balance<held||received>9223372036854775807n)
|
||||
throw Error('Rihla reservation cannot be settled safely.');
|
||||
await deps.postFee(tx,{transferId:row.id,taxMinor:tax});
|
||||
await tx.query('UPDATE ria_accounts SET balance_minor=?,reserved_minor=? WHERE owner_id=?',
|
||||
[String(balance-total),String(held-total),row.sender_id]);
|
||||
await tx.query('UPDATE ria_accounts SET balance_minor=? WHERE owner_id=?',[String(received),actor.id]);
|
||||
await tx.query("UPDATE ria_transfers SET state='paid' WHERE id=?",[row.id]);
|
||||
return json(transferView({...row,state:'paid'}));
|
||||
});
|
||||
}
|
||||
if(input.action==='cancel'){
|
||||
if(typeof input.number!=='string'||!/^RIA-[A-Z0-9-]{1,36}$/.test(input.number))
|
||||
return json({message:'Invalid RIA reference.'},400);
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff