I logged the keys and the env variables are as sent on email
const { Paynow } = require(“paynow”);
var express = require(‘express’);
var router = express.Router();
// Initialize Paynow instance with integration ID and integration key from environment variables
const paynow = new Paynow(process.env.PAYNOW_INTEGRATION_ID, process.env.PAYNOW_INTEGRATION_KEY);
// Set the result and return URLs
paynow.resultUrl = process.env.PAYNOW_RESULT_URL;
paynow.returnUrl = process.env.PAYNOW_RETURN_URL;
// Route to initiate a Paynow transaction
router.post(’/transaction/mobile’, async (req, res) => {
console.log(“Initiating transaction yemobile:”, req.body);
try {
const { invoiceId, items, phoneNumber, paymentMethod, email } = req.body;
const response = await initiateMobileTransaction(invoiceId, items, phoneNumber, paymentMethod, email);
res.json(response);
} catch (error) {
console.error(“Error initiating Paynow transaction:”, error);
res.status(500).json({ error: “Internal Server Error” });
}
});
router.post(’/transaction/web’, async (req, res) => {
console.log(“Initiating transaction yeweb:”, req.body);
try {
// const { invoiceId, items, phoneNumber, paymentMethod, email } = req.body;
const { invoiceId, items, email } = req.body;
const response = await initiateWebTransaction(invoiceId, items, email);
res.json(response);
} catch (error) {
console.error(“Error initiating Paynow transaction:”, error);
res.status(500).json({ error: “Internal Server Error” });
}
});
async function initiateWebTransaction(invoiceId, items, email) {
try {
console.log(“Initiating web transaction:”, invoiceId, items, email);
// Create a new payment with the correct email field
let payment = paynow.createPayment(invoiceId, email);
// Add items to the payment
items.forEach(item => {
payment.add(item.name, item.price);
});
console.log(“createPayment called with invoiceId:”, invoiceId, “and items:”, items);
// Log the data being sent to Paynow
let requestData = paynow.build(payment);
console.log(“Data being sent to Paynow:”, requestData);
// Send the payment to Paynow
let response = await paynow.send(payment);
console.log(“Response from paynow.send():”, response);
if (response && response.success) {
// Handle success case
let link = response.redirectUrl;
let transactionDetails = {
invoice_id: invoiceId,
amount: payment.total(),
items: JSON.stringify(items),
paynow_reference: response.reference,
status: ‘initiated’,
poll_url: response.pollUrl
};
await logTransactionIntoDatabase(transactionDetails);
return {
success: true,
redirectUrl: link
};
} else {
// Handle failure case
console.log(“Transaction initiation failed:”, response);
throw new Error(“Transaction initiation failed”);
}
} catch (error) {
console.error(“Error initiating Paynow transaction:”, error);
throw error;
}
}
// Function to initiate a Paynow transaction
async function initiateMobileTransaction(invoiceId, items, phoneNumber, paymentMethod, email) {
console.log(“Initiating mobile transaction:”, invoiceId, items, phoneNumber, paymentMethod, email);
console.log(“Environment variables::”, process.env.PAYNOW_INTEGRATION_ID, process.env.PAYNOW_INTEGRATION_KEY);
try {
let payment = paynow.createPayment(invoiceId, email);
items.forEach(item => {
payment.add(item.name, item.price);
});
console.log(“createPayment called with invoiceId:”, invoiceId, “and items:”, items);
// Send the payment to Paynow
let response;
if (phoneNumber && paymentMethod) {
console.log(“Sending mobile payment with phoneNumber:”, phoneNumber, “and paymentMethod:”, paymentMethod);
response = await paynow.sendMobile(payment, phoneNumber, paymentMethod);
} else {
response = await paynow.send(payment);
}
console.log(“Response from paynow.send():”, response);
if (response.success) {
let transactionDetails = {
invoice_id: invoiceId,
amount: payment.total(),
items: JSON.stringify(items),
paynow_reference: response.reference,
status: ‘initiated’
};
await logTransactionIntoDatabase(transactionDetails);
if (response.pollUrl) {
await savePollUrlToDatabase(response.reference, response.pollUrl);
}
return {
success: true,
instructions: response.instructions,
pollUrl: response.pollUrl
};
} else {
console.log(“Transaction initiation failed:”, response);
throw new Error(“Transaction initiation failed”);
}
} catch (error) {
console.error(“Error initiating Paynow transaction:”, error);
throw error;
}
}