TypeError: status.paid is not a function


#1

const { Paynow } = require(“paynow”);

const intId = “XXXXX”;
const intKey = “XXXXXXXXXXXXXXXXXXXXXXXXXXXXX”;

// Create instance of Paynow class
const paynow = new Paynow(intId, intKey);

module.exports = {
createPayment: async (req, res, next) => {
try {

  const eventId = req.params.id;
  const myevent = await Event.findById(eventId);

  const userId = req.session.passport.user;
  const user = await User.findById(userId);

  // Set return and result urls
  paynow.resultUrl = "http://example.com/gateways/paynow/update";
  paynow.returnUrl = "http://example.com/return?gateway=paynow&merchantReference=1234";


  const {name, email, phone, type} = req.body;

  var num = Math.random().toExponential().split('e-');
  var num2 = num[0].split('.');
  var ticketId = num2[1];

  const ticket = new Ticket({
    name: name,
    email: email,
    phone: phone,
    type: type,
    title: myevent.title,
    image: myevent.image,
    ticketId: ticketId,
    price: myevent.price,
    userId: userId,
    eventId: eventId,
    date: myevent.date,
    time: myevent.time
  });

  var pay_item = myevent.title;
  var pay_email = email;
  var pay_price = myevent.price;
  var pay_number = phone;

// Create a new payment
let payment = paynow.createPayment(`Invoice ${ticketId}`, pay_email);

// Add items to the payment list passing in the name of the item and it's price
payment.add(pay_item, pay_price);

// Send off the payment to Paynow
var response = await paynow.sendMobile(payment, '0771111111', 'ecocash');


// Check if request was successful
if(response && response.success) {

    let instructions = response.instructions
    let pollUrl = response.pollUrl;

    // Check the status of the transaction with the specified pollUrl
    // Now you see why you need to save that url ;-)
    console.log(response);
    console.log('PollUrl', pollUrl);
    console.log('Instructions', instructions)

    let status = await paynow.pollTransaction(pollUrl);

    console.log('Status', status);
    if (status.paid()) {

      // Yay! Transaction was paid for
      const successTrans = new PaynowStatus({
        name: name,
        email: email,
        phone: phone,
        date: new Date(),
        show: pay_item,
        price: pay_price,
        pollUrl: pollUrl
      });

      await successTrans.save();

      await ticket.save();
      user.tickets.push(ticket);

      user.my_events.push(myevent);
      await user.save();

      console.log("Payment successful");

      return res.redirect('/user/tickets');

    } else {
      console.log("Why you no pay?");
      return res.render('payfail');
    }
}

else {
    console.log(response.error)
    return res.render('payfail');
}

} catch (err) {
  next(err)
}

},

}


#2

Don’t know if you still need this or not, but for future readers that encounter this issue. To get it working, I changed the part:

if (status.paid()) {

to

if (status.status) {

then compared that expected results such as “Sent”, “paid”, “cancelled” etc

or you could do something like:

if (Object.values(status).includes(“paid”))