new(payment.gblib): #354 QRcode for payments.

This commit is contained in:
Rodrigo Rodriguez 2023-07-31 15:06:47 -03:00
parent b94942ce4f
commit 85b48c7a63
3 changed files with 100 additions and 26 deletions

View file

@ -1171,20 +1171,29 @@ export class DialogKeywords {
const { min, user } = await DialogKeywords.getProcessInfo(pid);
const element = filename._page ? filename._page : filename.screenshot ? filename : null;
let url;
let nameOnly;
// Web automation.
if (element) {
const gbaiName = DialogKeywords.getGBAIPath(min.botId);
const localName = Path.join('work', gbaiName, 'cache', `img${GBAdminService.getRndReadableIdentifier()}.jpg`);
nameOnly = Path.basename(localName);
await element.screenshot({ path: localName, fullPage: true });
url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', Path.basename(localName));
GBLog.info(`BASIC: WebAutomation: Sending the file ${url} to mobile ${mobile}.`);
} else if (filename.url) {
url = filename.url;
}
// GBFILE object.
else if (filename.url) {
url = filename.url;
nameOnly = Path.basename(filename.localName);
}
// Handles Markdown.
else if (filename.indexOf('.md') > -1) {
GBLog.info(`BASIC: Sending the contents of ${filename} markdown to mobile ${mobile}.`);
const md = await min.kbService.getAnswerTextByMediaName(min.instance.instanceId, filename);
@ -1203,6 +1212,8 @@ export class DialogKeywords {
} else {
url = filename
}
nameOnly = filename;
}
if (url) {
@ -1213,7 +1224,7 @@ export class DialogKeywords {
const contentType = mime.lookup(url);
reply['attachments'] = [];
reply['attachments'].push({
name: filename,
name: nameOnly,
contentType: contentType,
contentUrl: `data:${contentType};base64,${base64Image}`
});

View file

@ -253,6 +253,16 @@ export class KeywordsExpressions {
keywords[i++] = [/^\s*next *$/gim, '}'];
keywords[i++] = [
/^\s*((?:[a-z]+.?)(?:(?:\w+).)(?:\w+)*)\s*=\s*pay\s*(.*)/gim,
($0, $1, $2, $3) => {
const params = this.getParams($2, ['orderId', 'customerName', 'ammount']);
return `
${$1} = await sys.pay({pid: pid, ${params}})`;
}
];
keywords[i++] = [
/^\s*do while +(.*)/gim,
function (input, group1) {

View file

@ -181,13 +181,11 @@ export class SystemKeywords {
if (Array.isArray(data)) {
isObject = Object.keys(data[1]) !== null;
}
else {
} else {
isObject = true;
}
if (isObject || JSON.parse(data) !== null) {
// Copies data from JSON format into simple array.
if (!Array.isArray(data)) {
@ -210,7 +208,6 @@ export class SystemKeywords {
output.push({ gbarray: '0' });
}
for (let i = 0; i < data.length; i++) {
output[i + 1] = [];
for (let j = 0; j < keys.length; j++) {
@ -234,7 +231,6 @@ export class SystemKeywords {
* @see http://tabulator.info/examples/5.2
*/
private async renderTable(pid, data, renderPDF, renderImage) {
if (data.length && !data[1]) {
return null;
}
@ -661,7 +657,6 @@ export class SystemKeywords {
const filter = await DialogKeywords.getOption({ pid, name: 'filter' });
if (filter) {
// Creates id row.
body.values[0][0] = 'id';
@ -1121,7 +1116,7 @@ export class SystemKeywords {
}
}
const outputArray = await DialogKeywords.getOption({ pid, name:"output" });
const outputArray = await DialogKeywords.getOption({ pid, name: 'output' });
if (table.length === 1) {
GBLog.info(`BASIC: FIND returned no results (zero rows).`);
@ -1877,5 +1872,63 @@ export class SystemKeywords {
await client.v2.tweet(text);
GBLog.info(`Twitter Automation: ${text}.`);
}
}
public async pay({ pid, orderId, customerName, ammount }) {
const { min, user } = await DialogKeywords.getProcessInfo(pid);
const gbaiName = DialogKeywords.getGBAIPath(min.botId);
const merchantId = min.core.getParam(min.instance, 'Merchant ID', null);
const merchantKey = min.core.getParam(min.instance, 'Merchant Key', null);
if (!merchantId || !merchantKey) {
throw new Error('Payment not configured in .gbot.');
}
const apiUrl = 'https://apisandbox.cieloecommerce.cielo.com.br/1/sales/';
const requestId = GBAdminService.generateUuid();
GBLog.info(`GBPay: ${requestId}, ${orderId}, ${ammount}... `);
const requestData = {
MerchantOrderId: orderId,
Customer: {
Name: customerName
},
Payment: {
Type: 'qrcode',
Amount: ammount,
Installments: 1,
Capture: false,
Modality: 'Debit'
}
};
const response = await fetch(apiUrl, {
method: 'POST',
body: JSON.stringify(requestData),
headers: {
'Content-Type': 'application/json',
MerchantId: merchantId,
MerchantKey: merchantKey,
RequestId: requestId
}
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
// Prepare an image on cache and return the GBFILE information.
const buf = Buffer.from(data.Payment.QrCodeBase64Image, 'base64');
const localName = Path.join('work', gbaiName, 'cache', `qr${GBAdminService.getRndReadableIdentifier()}.png`);
Fs.writeFileSync(localName, buf, { encoding: null });
const url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', Path.basename(localName));
GBLog.info(`GBPay: ${data.MerchantOrderId} OK: ${url}.`);
return { localName: localName, url: url, data: buf, text: data.Payment.QrCodeString };
}
}