FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

402 middleware: fall back to chain-specific balances by melvincarvalho · Pull Request #204 · JavaScriptSolidServer/JavaScriptSolidServer · GitHub

Repository navigation

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .js  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
32 changes: 26 additions & 6 deletions src/handlers/pay.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -988,21 +988,41 @@ export function createPayHandler(options = {}) {

const didUri = pubkeyToDidNostr(pubkey);
const ledger = await readLedger();
const { success, balance } = debit(ledger, didUri, cost);

if (!success) {
return reply.code(402).send({
// Try generic sat balance first, then fall back to chain balances
const currency = request.headers['x-pay-currency'] || null;
let payUnit = currency && payChains && payChains.includes(currency) ? currency : null;
let result = debit(ledger, didUri, cost, payUnit);

// If generic sat failed and no explicit currency, try each chain balance
if (!result.success && !payUnit && payChains) {
for (const chainId of payChains) {
result = debit(ledger, didUri, cost, chainId);
if (result.success) { payUnit = chainId; break; }
}
}

if (!result.success) {
const response = {
error: 'Payment Required',
balance,
balance: result.balance,
cost,
unit: 'sat',
deposit: '/pay/.deposit'
});
};
if (payChains) {
response.balances = {};
for (const chainId of payChains) {
response.balances[chainId] = getBalance(ledger, didUri, chainId);
}
}
return reply.code(402).send(response);
}

await writeLedger(ledger);
reply.header('X-Balance', String(balance));
reply.header('X-Balance', String(result.balance));
reply.header('X-Cost', String(cost));
if (payUnit) reply.header('X-Pay-Currency', payUnit);
return; // continue to normal resource handler
}

Expand Down

Back | FazBrowse Home | New Git URL