Email required on Nodejs mobile transaction payload


#1

Hello.

I am receiving an error where the email is being required on initiating mobile transaction. Is this correct?

Error is:
Error initiating Paynow transaction: Error: Invalid email. Please ensure that you pass a valid email address when initiating a mobile payment
at Paynow.fail (C:\projects\myproject\node_modules\paynow\dist\paynow.js:72:15)
at Paynow.initMobile (C:\projects\myproject\node_modules\paynow\dist\paynow.js:95:18)
at Paynow.sendMobile (C:\projects\myproject\node_modules\paynow\dist\paynow.js:66:21)
at initiateMobileTransaction (C:\projects\myproject\routes\paynow.js:61:37)
at C:\projects\myproject\routes\paynow.js:35:32
at Layer.handle [as handle_request] (C:\projects\myproject\node_modules\express\lib\router\layer.js:95:5)
at next (C:\projects\myproject\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (C:\projects\myproject\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (C:\projects\myproject\node_modules\express\lib\router\layer.js:95:5)
at C:\projects\myproject\node_modules\express\lib\router\index.js:284:15
Error initiating Paynow transaction: Error: Invalid email. Please ensure that you pass a valid email address when initiating a mobile payment
at Paynow.fail (C:\projects\myproject\node_modules\paynow\dist\paynow.js:72:15)
at Paynow.initMobile (C:\projects\myproject\node_modules\paynow\dist\paynow.js:95:18)
at Paynow.sendMobile (C:\projects\myproject\node_modules\paynow\dist\paynow.js:66:21)
at initiateMobileTransaction (C:\projects\myproject\routes\paynow.js:61:37)
at C:\projects\myproject\routes\paynow.js:35:32
at Layer.handle [as handle_request] (C:\projects\myproject\node_modules\express\lib\router\layer.js:95:5)
at next (C:\projects\myproject\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (C:\projects\myproject\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (C:\projects\myproject\node_modules\express\lib\router\layer.js:95:5)
at C:\projects\myproject\node_modules\express\lib\router\index.js:284:15
POST /paynow/transaction 500 9.701 ms - 33


#2

Hi Ngoni

Please use this guide for the Node Js package

and make sure you pass in a vaild email on paynow.createPayment as shown below

let payment = paynow.createPayment("Invoice 35", "validemail@example.com");

#3

Thank you. However. I keep getting An error occured while initiating transaction Error: Hashes do not match!

So far i checked , my integration keys are correct. I actually created another one. The email that I am using is the one on the account for managing key. The account is not live though.

Full error log:
createPayment called with invoiceId: your_invoice_id and items: [ { name: ‘Item 1’, price: 10.99 }, { name: ‘Item 2’, price: 5.99 } ]
Sending mobile payment with phoneNumber: 0771111111 and paymentMethod: ecocash
An error occured while initiating transaction Error: Hashes do not match!
at Paynow.parse (C:\projects\myproject\node_modules\paynow\dist\paynow.js:101:23)
at C:\projects\myproject\node_modules\paynow\dist\paynow.js:87:26
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async initiateMobileTransaction (C:\projects\myproject\routes\paynow.js:127:24)
at async C:\projects\myproject\routes\paynow.js:35:26
Response from paynow.send(): undefined
Error initiating Paynow transaction: TypeError: Cannot read properties of undefined (reading ‘success’)
at initiateMobileTransaction (C:\projects\myproject\routes\paynow.js:133:22)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async C:\projects\myproject\routes\paynow.js:35:26
Error initiating Paynow transaction: TypeError: Cannot read properties of undefined (reading ‘success’)
at initiateMobileTransaction (C:\projects\myproject\routes\paynow.js:133:22)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async C:\projects\myproject\routes\paynow.js:35:26
POST /paynow/transaction/mobile 500 46.686 ms - 33


#4

Hi Ngoni

Kindly send your full code where you are making the request to Paynow. Please remove your integration keys from the code before you send. Make sure you also the latest paynow Node Package.


#5

Sure… Here is the code:


#6

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;

    }

}


#7

I have sent it through @elphas


#8

Can you check if email you are passing in createPayment is correct? Log your email address.

 `let payment = paynow.createPayment(invoiceId, email);`
 log the email and make sure its correct format