199 lines
5.0 KiB
JavaScript
199 lines
5.0 KiB
JavaScript
const GraphQLClient = require("graphql-request").GraphQLClient;
|
|
const path = require("path");
|
|
const queries = require("../graphql-client/queries");
|
|
const Dinero = require("dinero.js");
|
|
const qs = require("query-string");
|
|
const axios = require("axios");
|
|
require("dotenv").config({
|
|
path: path.resolve(
|
|
process.cwd(),
|
|
`.env.${process.env.NODE_ENV || "development"}`
|
|
),
|
|
});
|
|
|
|
const domain = process.env.NODE_ENV ? "secure" : "test";
|
|
const SecretsManager = require("./aws-secrets-manager");
|
|
const {
|
|
SecretsManagerClient,
|
|
GetSecretValueCommand,
|
|
} = require("@aws-sdk/client-secrets-manager");
|
|
|
|
const client = new SecretsManagerClient({
|
|
region: "ca-central-1",
|
|
});
|
|
|
|
const gqlClient = require("../graphql-client/graphql-client").client;
|
|
|
|
const getShopCredentials = async (bodyshop) => {
|
|
// Development only
|
|
if (process.env.NODE_ENV === undefined) {
|
|
return {
|
|
merchantkey: process.env.INTELLIPAY_MERCHANTKEY,
|
|
apikey: process.env.INTELLIPAY_APIKEY,
|
|
};
|
|
}
|
|
|
|
// Production code
|
|
if (bodyshop?.imexshopid) {
|
|
try {
|
|
const secret = await client.send(
|
|
new GetSecretValueCommand({
|
|
SecretId: `intellipay-credentials-${bodyshop.imexshopid}`,
|
|
VersionStage: "AWSCURRENT", // VersionStage defaults to AWSCURRENT if unspecified
|
|
})
|
|
);
|
|
return JSON.parse(secret.SecretString);
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
}
|
|
};
|
|
|
|
exports.lightbox_credentials = async (req, res) => {
|
|
const shopCredentials = await getShopCredentials(req.body.bodyshop);
|
|
|
|
try {
|
|
const options = {
|
|
method: "POST",
|
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
//TODO: Move these to environment variables/database.
|
|
data: qs.stringify({
|
|
...shopCredentials,
|
|
operatingenv:
|
|
// process.env.NODE_ENV === undefined
|
|
// ? process.env.NODE_ENV
|
|
// :
|
|
"businessattended",
|
|
}),
|
|
url: `https://${domain}.cpteller.com/api/custapi.cfc?method=autoterminal`,
|
|
};
|
|
|
|
const response = await axios(options);
|
|
|
|
res.send(response.data);
|
|
} catch (error) {
|
|
console.log(error);
|
|
res.json({ error });
|
|
}
|
|
};
|
|
|
|
exports.payment_refund = async (req, res) => {
|
|
const shopCredentials = await getShopCredentials(req.body.bodyshop);
|
|
|
|
try {
|
|
const options = {
|
|
method: "POST",
|
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
|
|
data: qs.stringify({
|
|
method: "payment_refund",
|
|
...shopCredentials,
|
|
paymentid: req.body.paymentid,
|
|
amount: req.body.amount,
|
|
}),
|
|
url: `https://${domain}.cpteller.com/api/26/webapi.cfc?method=payment_refund`,
|
|
};
|
|
|
|
const response = await axios(options);
|
|
|
|
res.send(response.data);
|
|
} catch (error) {
|
|
console.log(error);
|
|
res.json({ error });
|
|
}
|
|
};
|
|
|
|
exports.generate_payment_url = async (req, res) => {
|
|
const shopCredentials = await getShopCredentials(req.body.bodyshop);
|
|
try {
|
|
const options = {
|
|
method: "POST",
|
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
//TODO: Move these to environment variables/database.
|
|
data: qs.stringify({
|
|
...shopCredentials,
|
|
...req.body,
|
|
createshorturl: true,
|
|
}),
|
|
url: `https://${domain}.cpteller.com/api/custapi.cfc?method=generate_lightbox_url`,
|
|
};
|
|
|
|
const response = await axios(options);
|
|
|
|
res.send(response.data);
|
|
} catch (error) {
|
|
console.log(error);
|
|
res.json({ error });
|
|
}
|
|
};
|
|
|
|
exports.postback = async (req, res) => {
|
|
console.log("postback as", req.body);
|
|
|
|
const { body: values } = req;
|
|
|
|
// TODO query job by account name
|
|
const job = await gqlClient.request(queries.GET_JOB_BY_RO_NUMBER, {
|
|
ro_number: values.account,
|
|
});
|
|
// TODO add mutation to database
|
|
|
|
const paymentResult = await gqlClient.request(queries.INSERT_NEW_PAYMENT, {
|
|
paymentInput: {
|
|
amount: values.total,
|
|
transactionid: `C00 ${values.authcode}`,
|
|
payer: "Customer",
|
|
type: values.cardtype,
|
|
jobid: job.jobs[0].id,
|
|
date: moment(Date.now()),
|
|
},
|
|
});
|
|
|
|
await gqlClient.request(queries.INSERT_PAYMENT_RESPONSE, {
|
|
paymentResponse: {
|
|
amount: values.total,
|
|
bodyshopid: job.jobs[0].bodyshop.id,
|
|
paymentid: paymentResult.id,
|
|
jobid: job.jobs[0].id,
|
|
declinereason: "Approved",
|
|
ext_paymentid: values.paymentid,
|
|
successful: true,
|
|
response: values,
|
|
},
|
|
});
|
|
|
|
res.send({ message: "Postback Successful" });
|
|
};
|
|
|
|
`{
|
|
ipaddress: '136.158.34.242',
|
|
firstname: 'JC',
|
|
notes: '',
|
|
city: '',
|
|
fee: ' 0.00',
|
|
origin: 'OneLink',
|
|
total: '5061.36',
|
|
avsdata: 'N',
|
|
arglist: '""',
|
|
state: ' ',
|
|
cardtype: 'Visa',
|
|
department: '',
|
|
email: '',
|
|
timestamp: "{ts '2023-03-23 09:52:23'}",
|
|
op: 'Kh6Pa6AT9keg',
|
|
amount: '5061.36',
|
|
method: 'CARD',
|
|
address2: '',
|
|
address1: '',
|
|
lastname: 'Tolentino',
|
|
zipcode: '1742 ',
|
|
authcode: '367885',
|
|
phone: '',
|
|
merchantid: '7114',
|
|
paymentid: '24205435',
|
|
customerid: '19610104',
|
|
comment: '',
|
|
invoice: '',
|
|
account: 'QBD241'
|
|
}`;
|