// CaMbaw - Durable.co Server Payment Listener
const express = require("express");
const cors = require("cors");
const fetch = require("node-fetch");
const app = express();
app.use(cors());
app.use(express.json());
// === CONFIGURATION ===
const TPW_SECRET_KEY = "c9077bd25ce61c5c5b5e8e730238f162e7b62e6307993f35b96e0b5671ce98e1";
const APPLICATION_ID = "fd675e6b-5ad5-4ba0-8be6-fecb95ec5700";
const TPW_BASE_URL = "https://mobilewallet.trustpayway.com/api";
const INITIATION_WEBHOOK = "https://hook.eu2.make.com/1xobr3t0bs1pdgnlgsnjyc79mab886k7";
const PAYMENT_STATUS_WEBHOOK = "https://hook.eu2.make.com/smwdcmflx1fulgqwwpv18vsjgd8f8dkp";
// === Function: Get TrustPayWay Bearer Token ===
async function getBearerToken() {
const res = await fetch(`${TPW_BASE_URL}/login`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${TPW_SECRET_KEY}`
},
body: JSON.stringify({ applicationId: APPLICATION_ID })
});
const data = await res.json();
if (!res.ok || !data.access_token) {
throw new Error(data.message || "Failed to obtain access token");
}
return data.access_token;
}
// === Endpoint: Handle Payment Request ===
app.post("/initiate-payment", async (req, res) => {
const { uuid, method, number } = req.body;
if (!uuid || !method || !number) {
return res.status(400).json({ error: "Missing uuid, method, or number" });
}
const amount = 500; // Fixed amount
const orderId = `ORD-${Date.now()}-${Math.floor(Math.random() * 10000)}`;
const network = method.toLowerCase().includes("mtn") ? "mtn" : "orange";
try {
// Step 1: Notify initiation webhook
await fetch(INITIATION_WEBHOOK, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ uuid, orderId })
});
// Step 2: Get bearer token
const token = await getBearerToken();
// Step 3: Process payment
const paymentRes = await fetch(`${TPW_BASE_URL}/${network}/process-payment`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`
},
body: JSON.stringify({
amount: amount.toString(),
currency: "XAF",
subscriberMsisdn: number,
description: "CaMbaw activation",
orderId,
notifUrl: PAYMENT_STATUS_WEBHOOK
})
});
const paymentData = await paymentRes.json();
if (!paymentRes.ok || !paymentData?.data?.transaction_id) {
return res.status(400).json({
error: "Payment initiation failed",
details: paymentData
});
}
// Step 4: Respond to client
res.json({
message: "Payment initiated. Please confirm on your phone.",
orderId,
transactionId: paymentData.data.transaction_id
});
} catch (error) {
console.error("Payment error:", error.message);
res.status(500).json({ error: "Server error", message: error.message });
}
});
// === Start Express Server ===
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`🚀 CaMbaw Payment Server running at http://localhost:${PORT}`);
});
💬 Ask Us
💬 Online Assistant