/* ============================================================================ *\
|| ########################################################################## ||
|| # 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 # ||
|| # ---------------------------------------------------------------------- # ||
|| ########################################################################## ||
\* ============================================================================ */
/* eslint-disable prefer-destructuring */
/* eslint-disable no-param-reassign */
const axios = require('axios')
const refundModule = require('../../modules/refund').default
const returnCtrl = require('../return')
const schemaModule = require('./schema').default
const { jsonResponse } = require('../logger')
const kountCtrl = require('../kount')
const shortDescribeSCH = async (items) => {
function changingdata(item) {
const payItems = {
itemId: item.item_id,
name: item.title.substring(0, 31),
description: item.desc_proc,
quantity: item.qty,
unitPrice: item.buynowamount,
taxable: 'true',
}
return payItems
}
const promises = items.map(changingdata)
items = await Promise.all(promises)
return items
}
const generateRequestData = async (type, cardDetails, lineItem, cartValues, refTransId) => {
const request = {}
request.createTransactionRequest = {}
request.createTransactionRequest.merchantAuthentication = {
name: global.authorize.api_login_id,
transactionKey: global.authorize.transaction_key,
}
const results = await Promise.all([
refundModule.insertPayLogRequest(cartValues.id, cartValues.user_id, type, 'authorize'),
])
request.createTransactionRequest.refId = results[0].insertId
request.createTransactionRequest.transactionRequest = {}
if (type === 'priorAuthCaptureTransaction') {
request.createTransactionRequest.transactionRequest.transactionType = type
request.createTransactionRequest.transactionRequest.amount = cardDetails.card_amount
request.createTransactionRequest.transactionRequest.refTransId = refTransId
} else {
request.createTransactionRequest.transactionRequest.transactionType = type
request.createTransactionRequest.transactionRequest.amount = cardDetails.card_amount
request.createTransactionRequest.transactionRequest.payment = {
creditCard: {
cardNumber: cardDetails.card_number,
expirationDate: cardDetails.card_expiry,
cardCode: cardDetails.card_cvv,
},
}
request.createTransactionRequest.transactionRequest.lineItems = {
lineItem,
}
request.createTransactionRequest.transactionRequest.tax = {
amount: cartValues.total_tax,
name: 'Sales Tax',
}
request.createTransactionRequest.transactionRequest.customer = {
id: cartValues.user_id,
}
request.createTransactionRequest.transactionRequest.billTo = {
firstName: cartValues.bill_first_name,
lastName: cartValues.bill_first_name,
address: cartValues.bill_address1,
city: cartValues.bill_city,
state: cartValues.bill_state,
zip: cartValues.bill_zipcode,
country: 'USA',
}
request.createTransactionRequest.transactionRequest.customerIP = '192.168.1.1'
request.createTransactionRequest.transactionRequest.userFields = {
userField: [
{
name: 'refId',
value: results[0].insertId,
},
],
}
}
return request
}
const makeApiCall = async (data) => {
let responseData = {}
const url = global.authorize.endpoint
const options = {
method: 'POST',
headers: { 'content-type': 'application/json' },
data,
url,
}
// refundModule.updatePayLogRequest(
// { request: JSON.stringify(data) },
// data.createTransactionRequest.refId,
// 1,
// )
const response = await axios(options)
const paymentResponse = response.data.transactionResponse
if (paymentResponse) {
refundModule.updatePayLogRequest(
{ response: JSON.stringify(paymentResponse), transactionID: paymentResponse.transId },
paymentResponse.userFields[0].value,
0,
)
}
if (
paymentResponse &&
(parseInt(paymentResponse.responseCode, 10) === 1 ||
parseInt(paymentResponse.responseCode, 10) === 4)
) {
responseData = {
status: 'success',
responseType: 1,
message: 'Payment succesfully completed!',
responseData: { paymentResponse },
}
} else if (paymentResponse && paymentResponse.errors) {
responseData = {
status: 'error',
responseType: 2,
message: paymentResponse.errors[0].errorText,
}
} else if (response.data.messages.message.length) {
responseData = {
status: 'error',
responseType: 3,
message: response.data.messages.message[0].text,
}
} else {
responseData = {
status: 'error',
responseType: 4,
message: 'Payment Gateway Error.',
}
}
return responseData
}
/**
* Payment Success Handler
*
* @memberOf frontend.refund
* @param {refundModule.getCartDetails} modules
* @param {refundModule.getReturnCommonInvoiceNo} modules
* @param {refundModule.markCartRefund} modules
* @param {refundModule.markBuyNowRefund} modules
* @param {refundModule.addAppointment} modules
* @param {refundModule.updateBuynowAppointment} modules
*/
const paymentSuccess = async (cartID, userID, req) => {
const [cartDetails, results] = await Promise.all([
refundModule.getCartDetails(cartID),
refundModule.getReturnCommonInvoiceNo(),
])
let cinvoiceno = 10000
if (results.length > 0) {
cinvoiceno =
results[0].return_invoice === 0 ||
results[0].return_invoice === '' ||
!results[0].return_invoice
? 10000
: parseInt(results[0].return_invoice, 10) + 1
}
await Promise.all([
refundModule.markCartRefund(req, cartID, 0),
refundModule.markBuyNowRefund(cartID, cinvoiceno, 0),
])
let apnmtIDUpdate = req.body.appointmentID
if (!apnmtIDUpdate) {
const resultsAppoint = await Promise.all([
refundModule.addAppointment(req.body.pickup, userID, cartDetails[0].cartlocation_id),
])
apnmtIDUpdate = resultsAppoint[0].insertId
}
await Promise.all([refundModule.updateBuynowAppointment(cartID, apnmtIDUpdate)])
const responseData = {}
return responseData
}
/**
* Return Product Handler
*
* @memberOf frontend.refund
* @param {frontend.return.relistProduct} modules
*/
const returnProduct = async (items) => {
async function relist(item) {
await Promise.all([returnCtrl.relistProduct(item.id, 'returned')])
return true
}
const promises = items.map(relist)
items = await Promise.all(promises)
return items
}
module.exports = {
/**
* Authorize Refund
*
* @memberOf frontend.refund
* @param {frontend.refund.paymentSuccess} modules
* @param {frontend.return.getSingleCartDetails} modules
* @param {frontend.refund.returnProduct} modules
*/
authorizePayment: async (req, res) => {
try {
const [cartDetails] = await Promise.all([
returnCtrl.getSingleCartDetails(req, req.body.cart_id),
])
await returnProduct(cartDetails.cartItems)
await Promise.all([
paymentSuccess(cartDetails.cartValues.id, cartDetails.cartValues.user_id, req),
])
jsonResponse(res, 'success', {
responseType: 1,
message: 'Return Successfully completed!',
})
} catch (e) {
console.error(e)
jsonResponse(res, 'error', {
responseType: 5,
message: 'Internal Server error!',
})
}
},
}