Merge branch 'feature/intellipay' into release/2023-08-25
This commit is contained in:
@@ -12,6 +12,39 @@ query FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID(
|
||||
}
|
||||
`;
|
||||
|
||||
exports.GET_JOB_BY_RO_NUMBER = `
|
||||
query GET_JOB_BY_RO_NUMBER($ro_number: String!) {
|
||||
jobs(where:{ro_number:{_eq:$ro_number}}) {
|
||||
id
|
||||
bodyshop {
|
||||
id
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
exports.INSERT_NEW_PAYMENT = `
|
||||
mutation INSERT_NEW_PAYMENT($paymentInput: [payments_insert_input!]!) {
|
||||
insert_payments(objects: $paymentInput) {
|
||||
returning {
|
||||
id
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
exports.INSERT_PAYMENT_RESPONSE = `
|
||||
mutation INSERT_PAYMENT_RESPONSE(
|
||||
$paymentResponse: [payment_response_insert_input!]!
|
||||
) {
|
||||
insert_payment_response(objects: $paymentResponse) {
|
||||
returning {
|
||||
id
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
exports.UNARCHIVE_CONVERSATION = `
|
||||
mutation UNARCHIVE_CONVERSATION($id: uuid!) {
|
||||
update_conversations_by_pk(pk_columns: {id: $id}, _set: {archived: false}) {
|
||||
@@ -955,6 +988,7 @@ exports.GET_JOB_BY_PK = `query GET_JOB_BY_PK($id: uuid!) {
|
||||
ins_ph1
|
||||
est_co_nm
|
||||
est_ct_fn
|
||||
shopid
|
||||
est_ct_ln
|
||||
vehicle{
|
||||
id
|
||||
|
||||
46
server/intellipay/aws-secrets-manager.js
Normal file
46
server/intellipay/aws-secrets-manager.js
Normal file
@@ -0,0 +1,46 @@
|
||||
"use strict";
|
||||
|
||||
const AWS = require("aws-sdk");
|
||||
|
||||
class SecretsManager {
|
||||
/**
|
||||
* Uses AWS Secrets Manager to retrieve a secret
|
||||
*/
|
||||
static async getSecret(secretName, region) {
|
||||
const config = { region: region };
|
||||
let secretsManager = new AWS.SecretsManager(config);
|
||||
try {
|
||||
let secretValue = await secretsManager
|
||||
.getSecretValue({ SecretId: secretName })
|
||||
.promise();
|
||||
if ("SecretString" in secretValue) {
|
||||
return secretValue.SecretString;
|
||||
} else {
|
||||
let buff = new Buffer(secretValue.SecretBinary, "base64");
|
||||
return buff.toString("ascii");
|
||||
}
|
||||
} catch (err) {
|
||||
if (err.code === "DecryptionFailureException")
|
||||
// Secrets Manager can't decrypt the protected secret text using the provided KMS key.
|
||||
// Deal with the exception here, and/or rethrow at your discretion.
|
||||
throw err;
|
||||
else if (err.code === "InternalServiceErrorException")
|
||||
// An error occurred on the server side.
|
||||
// Deal with the exception here, and/or rethrow at your discretion.
|
||||
throw err;
|
||||
else if (err.code === "InvalidParameterException")
|
||||
// You provided an invalid value for a parameter.
|
||||
// Deal with the exception here, and/or rethrow at your discretion.
|
||||
throw err;
|
||||
else if (err.code === "InvalidRequestException")
|
||||
// You provided a parameter value that is not valid for the current state of the resource.
|
||||
// Deal with the exception here, and/or rethrow at your discretion.
|
||||
throw err;
|
||||
else if (err.code === "ResourceNotFoundException")
|
||||
// We can't find the resource that you asked for.
|
||||
// Deal with the exception here, and/or rethrow at your discretion.
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
module.exports = SecretsManager;
|
||||
208
server/intellipay/intellipay.js
Normal file
208
server/intellipay/intellipay.js
Normal file
@@ -0,0 +1,208 @@
|
||||
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");
|
||||
const moment = require("moment");
|
||||
const logger = require("../utils/logger");
|
||||
|
||||
require("dotenv").config({
|
||||
path: path.resolve(
|
||||
process.cwd(),
|
||||
`.env.${process.env.NODE_ENV || "development"}`
|
||||
),
|
||||
});
|
||||
|
||||
const domain = process.env.NODE_ENV ? "secure" : "test";
|
||||
|
||||
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) {
|
||||
return {
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
exports.lightbox_credentials = async (req, res) => {
|
||||
logger.log(
|
||||
"intellipay-lightbox-credentials",
|
||||
"DEBUG",
|
||||
req.user?.email,
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
const shopCredentials = await getShopCredentials(req.body.bodyshop);
|
||||
|
||||
if (shopCredentials.error) {
|
||||
res.json(shopCredentials);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const options = {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/x-www-form-urlencoded" },
|
||||
data: qs.stringify({
|
||||
...shopCredentials,
|
||||
operatingenv: "businessattended",
|
||||
}),
|
||||
url: `https://${domain}.cpteller.com/api/custapi.cfc?method=autoterminal${
|
||||
req.body.refresh ? "_refresh" : ""
|
||||
}`, //autoterminal_refresh
|
||||
};
|
||||
|
||||
const response = await axios(options);
|
||||
|
||||
res.send(response.data);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
logger.log(
|
||||
"intellipay-lightbox-credentials-error",
|
||||
"ERROR",
|
||||
req.user?.email,
|
||||
null,
|
||||
{ error: JSON.stringify(error) }
|
||||
);
|
||||
res.json({ error });
|
||||
}
|
||||
};
|
||||
|
||||
exports.payment_refund = async (req, res) => {
|
||||
logger.log("intellipay-refund", "DEBUG", req.user?.email, null, null);
|
||||
|
||||
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);
|
||||
logger.log("intellipay-refund-error", "ERROR", req.user?.email, null, {
|
||||
error: JSON.stringify(error),
|
||||
});
|
||||
res.json({ error });
|
||||
}
|
||||
};
|
||||
|
||||
exports.generate_payment_url = async (req, res) => {
|
||||
logger.log("intellipay-payment-url", "DEBUG", req.user?.email, null, null);
|
||||
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,
|
||||
//The postback URL is set at the CP teller global terminal settings page.
|
||||
}),
|
||||
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);
|
||||
logger.log("intellipay-payment-url-error", "ERROR", req.user?.email, null, {
|
||||
error: JSON.stringify(error),
|
||||
});
|
||||
res.json({ error });
|
||||
}
|
||||
};
|
||||
|
||||
exports.postback = async (req, res) => {
|
||||
logger.log("intellipay-postback", "ERROR", req.user?.email, null, req.body);
|
||||
const { body: values } = req;
|
||||
|
||||
if (!values.invoice) {
|
||||
res.sendStatus(200);
|
||||
return;
|
||||
}
|
||||
// TODO query job by account name
|
||||
const job = await gqlClient.request(queries.GET_JOB_BY_PK, {
|
||||
id: values.invoice,
|
||||
});
|
||||
// TODO add mutation to database
|
||||
|
||||
try {
|
||||
const paymentResult = await gqlClient.request(queries.INSERT_NEW_PAYMENT, {
|
||||
paymentInput: {
|
||||
amount: values.total,
|
||||
transactionid: `C00 ${values.authcode}`,
|
||||
payer: "Customer",
|
||||
type: values.cardtype,
|
||||
jobid: values.invoice,
|
||||
date: moment(Date.now()),
|
||||
},
|
||||
});
|
||||
|
||||
await gqlClient.request(queries.INSERT_PAYMENT_RESPONSE, {
|
||||
paymentResponse: {
|
||||
amount: values.total,
|
||||
bodyshopid: job.jobs_by_pk.shopid,
|
||||
paymentid: paymentResult.id,
|
||||
jobid: values.invoice,
|
||||
declinereason: "Approved",
|
||||
ext_paymentid: values.paymentid,
|
||||
successful: true,
|
||||
response: values,
|
||||
},
|
||||
});
|
||||
|
||||
res.send({ message: "Postback Successful" });
|
||||
} catch (error) {
|
||||
logger.log("intellipay-postback-error", "ERROR", req.user?.email, null, {
|
||||
error: JSON.stringify(error),
|
||||
body: req.body,
|
||||
});
|
||||
res.status(400).json({ succesful: false, error: error.message });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user