I am getting "error": "Invalid+Hash.++Hash+should+start+with:+0395E9" }


#1

async function initiateTransaction(req, res) {
try {
const {
method,
phone,
token,
merchanttrace = TRACE-${Date.now()},
reference,
amount,
additionalinfo,
authemail,
authphone,
authname,
tokenize = false
} = req.body;

    console.log('Request body received:', req.body);

    // Enhanced validation
    if (!method || !reference || !amount) {
        console.log('Validation Error: Required fields missing');
        return res.status(400).json({ 
            success: false, 
            error: 'Required fields missing: method, reference, and amount are mandatory' 
        });
    }

    console.log('Validation passed: method, reference, and amount');

    // Validate payment method
    const validMethods = ['zimswitch', 'vmc', 'ecocash', 'onemoney', 'innbucks', 'paygo'];
    if (!validMethods.includes(method)) {
        console.log(`Validation Error: Invalid payment method - ${method}`);
        return res.status(400).json({ 
            success: false, 
            error: `Invalid payment method. Must be one of: ${validMethods.join(', ')}` 
        });
    }

    console.log('Payment method validated:', method);

    // Validate phone number for mobile money methods
    if (['ecocash', 'onemoney'].includes(method) && (!phone || !/^263[7|8]\d{8}$/.test(phone))) {
        console.log('Validation Error: Valid phone number required for mobile money payments');
        return res.status(400).json({ 
            success: false, 
            error: 'Valid phone number required for mobile money payments' 
        });
    }

    console.log('Phone number validated:', phone);

    // Validate token for card payments
    if (['vmc', 'zimswitch'].includes(method) && token && token.length < 32) {
        console.log('Validation Error: Invalid card token');
        return res.status(400).json({ 
            success: false, 
            error: 'Invalid card token' 
        });
    }

    console.log('Token validated:', token);

    const fields = {
        id: 19806, 
        reference,                               
        amount,         
        additionalinfo: additionalinfo || reference, 
        returnurl: "http://www.google.com/search?q=returnurl", 
        resulturl: "http://www.google.com/search?q=resulturl",
        authemail: authemail || '',
        authphone: authphone || phone || '',
        authname: authname || '',
        method,
        phone: phone || '',
         status: 'Message'                        
    };

    console.log('Fields prepared for hash generation:', fields);

    // Generate hash
    fields.hash = generateHash(fields, 'f23a9773-9a0d-42b4-8c08-c708ee92135e');
    console.log('Hash generated:', fields.hash);
    console.log('Request sent to Paynow:', fields);
   
    const response = await axios.post(
        `https://www.paynow.co.zw/interface/initiatetransaction`,
        fields,
        {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }
    );

    console.log('Response from Paynow:', response.data);

    
    const responseData = parsePaynowResponse(response.data);
    console.log('Parsed Paynow response:', responseData);
    
    if (responseData.status.toLowerCase() === 'error') {
        console.log('Error in Paynow response:', responseData.error);
        return res.status(400).json({
            success: false,
            error: responseData.error
        });
    }

    // Format response based on payment method
    const formattedResponse = formatPaymentResponse(method, responseData);
    return res.status(200).json(formattedResponse);

} catch (error) {
    console.error('Transaction initiation error:', error);
    return res.status(500).json({ 
        success: false, 
        error: 'Payment initiation failed. Please try again.' 
    });
}

}

this is my node.js controller

function generateHash(fields, integrationKey) {

const concatenatedValues = 
  `${fields.id}${fields.reference}${fields.amount}${fields.additionalinfo}${fields.returnurl}${fields.resulturl}${fields.status}`;
  console.log('>>>>>>>>>>>>>>>>>'+concatenatedValues);

const stringToHash = concatenatedValues + integrationKey;
console.log('>>>>>>adding_key>>>>>>>>>>>'+stringToHash);

const hash = crypto.createHash('sha512').update(stringToHash).digest('hex').toUpperCase();

return hash;

}

this is my generate hash i am still getting the error .My credentials are correct


#2

Hi @Anjali

I encourage you to use the Paynow NodeJs SDK for integration. Please find the Get Started guide below