front/modules/payment.js

/* ============================================================================ *\
|| ########################################################################## ||
|| # Auction Software Marketplace          Release: 0.6   Build 0.7         # ||
|| # ---------------------------------------------------------------------- # ||
|| # License # 35YAHCNR9344X6O666C123AB                                     # ||
|| # ---------------------------------------------------------------------- # ||
|| # Copyright ©2014–2021 Develop Scripts LLC. All Rights Reserved          # ||
|| # This file may not be redistributed in whole or significant part.       # ||
|| # ------------- AUCTION SOFTWARE IS NOT FREE SOFTWARE ------------------ # ||
|| # http://www.auctionsoftwaremarketplace.com|support@auctionsoftware.com  # ||
|| # ---------------------------------------------------------------------- # ||
|| ########################################################################## ||
\* ============================================================================ */

const dateFormat = require('dateformat')
const md5 = require('md5')
const _ = require('underscore')

const mysqclass = require('./mysqli').default
/**
 * @class class to handle payment functions
 */
class paymentModule {
    /**
     * Mark cart as paid
     * @param {string} paymentType req object
     * @param {number} cartID req.body object
     * @param {number} paid count for the pagination
     * @returns {object} sql response
     */
    static async markCartPaid(paymentType, cartID, paid) {
        const mysql = {}
        const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
        const escapeData = [paid, paymentType, dateNow, cartID]
        const strQuery = await mysqclass.mysqli(mysql, 'update_payment_success_cart')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Mark Buy now cart as paid
     * @param {string} InvoiceID req object
     * @param {number} cartID req.body object
     * @param {number} paid count for the pagination
     * @returns {object} sql response
     */
    static async markBuyNowPaid(cartID, InvoiceID, paid) {
        const mysql = {}
        const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
        const escapeData = [paid, InvoiceID, dateNow, cartID]
        const strQuery = await mysqclass.mysqli(mysql, 'update_payment_success_buynow')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Mark Buy now cart as paid
     * @param {string} aptnID req object
     * @param {number} cartID req.body object
     * @returns {object} sql response
     */
    static async updateBuynowAppointment(cartID, aptnID) {
        const mysql = {}
        const escapeData = [aptnID, cartID]
        const strQuery = await mysqclass.mysqli(mysql, 'update_appointment_success_buynow')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Mark Buy now cart as paid
     * @param {string} pickup req object
     * @param {number} userID req.body object
     * @param {number} locationID req.body object
     * @returns {object} sql response
     */
    static async addAppointment(pickup, userID, locationID) {
        const mysql = {}
        const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
        const aptnDateTime = dateFormat(new Date(pickup), 'yyyy-mm-dd HH:MM:ss')
        const escapeData = [aptnDateTime, userID, locationID, dateNow, 1]
        const strQuery = await mysqclass.mysqli(mysql, 'insert_appointments_table')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Mark Buy now cart as paid
     * @param {string} cartID req object
     * @param {number} userID req.body object
     * @param {number} payType req.body object
     * @param {number} gateWay req.body object
     * @returns {object} sql response
     */
    static async insertPayLogRequest(cartID, userID, payType, gateWay) {
        const mysql = {}
        const escapeData = [userID, cartID, gateWay, payType]
        const strQuery = await mysqclass.mysqli(mysql, 'insert_payment_request_log')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Mark Buy now cart as paid
     * @param {string} data req object
     * @param {number} id req.body object
     * @param {number} type req.body object
     * @returns {object} sql response
     */
    static async updatePayLogRequest(data, id, type) {
        let postData = data
        const mysqli = {}
        const acceptedObjects = [
            'requested_at',
            'response_at',
            'request',
            'response',
            'transactionID',
        ]
        postData = _.omit(postData, (value, key) => {
            if (
                _.isNull(value) ||
                _.isUndefined(value) ||
                value === '' ||
                !_.contains(acceptedObjects, key)
            ) {
                return true
            }
            return false
        })
        let escapeData = []

        postData = _.omit(postData, (value, key, object) => {
            if (!_.contains(acceptedObjects, key)) {
                return true
            }
            return false
        })
        let defaultKeys = []
        if (type === 1) {
            defaultKeys = ['requested_at']
        } else {
            defaultKeys = ['response_at']
        }
        const defaultValues = [dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')]
        const keyswithescape = defaultKeys.concat(_.keys(postData))
        escapeData = defaultValues.concat(_.values(postData))
        mysqli.keys = _.map(keyswithescape, (val) => {
            return `${val} = ?`
        })
        mysqli.log_id = id
        const strQuery = await mysqclass.mysqli(mysqli, 'update_payment_log')
        const dataPromise = await global.mysql.query(strQuery, escapeData)
        return dataPromise
    }

    /**
     * Mark Buy now cart as paid
     * @param {string} req req object
     * @param {number} cardDetails req.body object
     * @param {number} cartValue req.body object
     * @param {number} data req.body object
     * @returns {object} sql response
     */
    static async authorizeInsertPayRecord(req, cardDetails, cartValue, data) {
        const mysql = {}
        const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
        const escapeData = [
            cartValue.id,
            cartValue.user_id,
            data.transId,
            dateNow,
            'authorize',
            req.body.no_of_cards,
            data.accountNumber,
            cardDetails.card_amount,
            data.accountType,
        ]
        const strQuery = await mysqclass.mysqli(mysql, 'insert_pay_record')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Mark Buy now cart as paid
     * @param {string} cardID req object
     * @returns {object} sql response
     */
    static async getMinimumInvoiceID(cardID) {
        const mysql = {}
        const escapeData = [cardID]
        const strQuery = await mysqclass.mysqli(mysql, 'get_minimum_invoiceid')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Mark Buy now cart as paid
     * @param {string} cardID req object
     * @returns {object} sql response
     */
    static async getCartDetails(cardID) {
        const mysql = {}
        const escapeData = [cardID]
        const strQuery = await mysqclass.mysqli(mysql, 'get_cart_details')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }
}

module.exports.default = paymentModule