// MIDDLEWARES
async function settleWithFlow402({ nonce, txHash, feeTxHash, intentId }) {
const url = "https://api.flow402.net/settle"
const payload = {
nonce,
txHash,
feeTxHash,
intentId
}
const res = await axios.post(url, payload, {
headers: { 'Content-Type': 'application/json' }
})
return res.data
}
async function requirePayment(req, res, next) {
const nonce = req.body["nonce"]
const intentId = req.body["intentId"]
const txHash = req.body["txHash"]
const feeTxHash = req.body["feeTxHash"] || null
// Return 402 status code "Payment required"
if (!nonce || !intentId || !txHash) {
return res.status(402).json({
error: 'payment_required',
message: 'Missing Flow402 payment body.',
expectedBody: [
"nonce",
"intentId",
"txHash"
],
optionalBody: "feeTxHash"
})
}
const result = await settleWithFlow402({
nonce,
txHash,
feeTxHash,
intentId
})
// Return 402 status code "Payment required"
if (!result.settled) {
return res.status(402).json({
error: 'payment_not_settled',
message: 'Payment not settled on-chain.',
facilitatorResponse: result
})
}
// Attach receipt to request to be handled
req.flow402 = {
txHash: result.txHash,
receipt: result.receipt
}
return next()
}