Spaces:
Sleeping
Sleeping
fix: add debug logging for payer extraction, fallback to response header
Browse files
server.js
CHANGED
|
@@ -96,14 +96,31 @@ function getPayerAddress(req) {
|
|
| 96 |
try {
|
| 97 |
const header =
|
| 98 |
req.get("payment-signature") || req.get("x-payment") || req.get("PAYMENT-SIGNATURE");
|
| 99 |
-
if (!header)
|
|
|
|
|
|
|
|
|
|
| 100 |
const decoded = JSON.parse(Buffer.from(header, "base64").toString());
|
| 101 |
-
// Decode the transaction XDR to get the source account (payer)
|
| 102 |
const txXdr = decoded?.payload?.transaction;
|
| 103 |
-
if (!txXdr)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
const tx = new Transaction(txXdr, Networks.TESTNET);
|
|
|
|
| 105 |
return tx.source;
|
| 106 |
-
} catch {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
return null;
|
| 108 |
}
|
| 109 |
}
|
|
|
|
| 96 |
try {
|
| 97 |
const header =
|
| 98 |
req.get("payment-signature") || req.get("x-payment") || req.get("PAYMENT-SIGNATURE");
|
| 99 |
+
if (!header) {
|
| 100 |
+
console.log("No payment header found");
|
| 101 |
+
return null;
|
| 102 |
+
}
|
| 103 |
const decoded = JSON.parse(Buffer.from(header, "base64").toString());
|
|
|
|
| 104 |
const txXdr = decoded?.payload?.transaction;
|
| 105 |
+
if (!txXdr) {
|
| 106 |
+
console.log("No transaction in payload");
|
| 107 |
+
return null;
|
| 108 |
+
}
|
| 109 |
+
// The transaction XDR source account is the payer
|
| 110 |
const tx = new Transaction(txXdr, Networks.TESTNET);
|
| 111 |
+
console.log(`Payer extracted: ${tx.source}`);
|
| 112 |
return tx.source;
|
| 113 |
+
} catch (err) {
|
| 114 |
+
console.error("Payer extraction failed:", err.message);
|
| 115 |
+
// Fallback: try extracting from the x402 settlement response on res
|
| 116 |
+
try {
|
| 117 |
+
const respHeader = req.res?.getHeader?.("PAYMENT-RESPONSE");
|
| 118 |
+
if (respHeader) {
|
| 119 |
+
const resp = JSON.parse(Buffer.from(respHeader, "base64").toString());
|
| 120 |
+
console.log(`Payer from response: ${resp.payer}`);
|
| 121 |
+
return resp.payer;
|
| 122 |
+
}
|
| 123 |
+
} catch { /* ignore */ }
|
| 124 |
return null;
|
| 125 |
}
|
| 126 |
}
|