app.post('/your-backend/charge', async (req, res) => {
const { paymentMethodId, amount, stripeCustomerId } = req.body;
let declineCode;
// 1. Try charging through YOUR primary gateway using YOUR token
// (Your normal checkout — Revtain not involved yet)
try {
const charge = await stripe.paymentIntents.create({
amount,
currency: 'usd',
customer: stripeCustomerId,
payment_method: 'pm_xxx',
confirm: true
});
// Log the organic success so Revtain can calculate Lift
await fetch(`${REVTAIN_BASE_URL}/api/recovery/pulse`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-KEY': process.env.REVTAIN_API_KEY
},
body: JSON.stringify({ amount, currency: 'USD', transactionId: charge.id })
});
return res.json({ success: true, chargeId: charge.id });
} catch (stripeError) {
declineCode = stripeError.decline_code;
}
// 2. Primary failed — hand off to Revtain
const recovery = await fetch(`${REVTAIN_BASE_URL}/api/recovery/execute`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-KEY': process.env.REVTAIN_API_KEY
},
body: JSON.stringify({
paymentMethodToken: paymentMethodId,
amount,
currency: 'USD',
originalDeclineCode: declineCode,
cardOrigin: 'US',
idempotencyKey: `order_${orderId}_recovery`
})
});
const result = await recovery.json();
if (recovery.status === 200 && result.success) {
return res.json({ success: true, recovered: true, transactionId: result.transactionId });
} else if (recovery.status === 202) {
return res.json({ success: false, queued: true, message: result.message });
} else if (recovery.status === 403) {
return res.status(403).json({ success: false, blocked: true, error: result.error });
} else if (recovery.status === 409) {
return res.json({ success: false, duplicate: true, existing: result.existingTransactionId });
}
return res.json({ success: false, error: result.error || result.message });
});