new(payment.gblib): #354 QRcode for payments.
This commit is contained in:
parent
b94942ce4f
commit
85b48c7a63
3 changed files with 100 additions and 26 deletions
|
@ -1171,20 +1171,29 @@ export class DialogKeywords {
|
||||||
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
||||||
const element = filename._page ? filename._page : filename.screenshot ? filename : null;
|
const element = filename._page ? filename._page : filename.screenshot ? filename : null;
|
||||||
let url;
|
let url;
|
||||||
|
let nameOnly;
|
||||||
|
|
||||||
|
// Web automation.
|
||||||
|
|
||||||
if (element) {
|
if (element) {
|
||||||
const gbaiName = DialogKeywords.getGBAIPath(min.botId);
|
const gbaiName = DialogKeywords.getGBAIPath(min.botId);
|
||||||
const localName = Path.join('work', gbaiName, 'cache', `img${GBAdminService.getRndReadableIdentifier()}.jpg`);
|
const localName = Path.join('work', gbaiName, 'cache', `img${GBAdminService.getRndReadableIdentifier()}.jpg`);
|
||||||
|
nameOnly = Path.basename(localName);
|
||||||
await element.screenshot({ path: localName, fullPage: true });
|
await element.screenshot({ path: localName, fullPage: true });
|
||||||
|
|
||||||
url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', Path.basename(localName));
|
url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', Path.basename(localName));
|
||||||
|
|
||||||
GBLog.info(`BASIC: WebAutomation: Sending the file ${url} to mobile ${mobile}.`);
|
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.
|
// Handles Markdown.
|
||||||
|
|
||||||
else if (filename.indexOf('.md') > -1) {
|
else if (filename.indexOf('.md') > -1) {
|
||||||
GBLog.info(`BASIC: Sending the contents of ${filename} markdown to mobile ${mobile}.`);
|
GBLog.info(`BASIC: Sending the contents of ${filename} markdown to mobile ${mobile}.`);
|
||||||
const md = await min.kbService.getAnswerTextByMediaName(min.instance.instanceId, filename);
|
const md = await min.kbService.getAnswerTextByMediaName(min.instance.instanceId, filename);
|
||||||
|
@ -1203,6 +1212,8 @@ export class DialogKeywords {
|
||||||
} else {
|
} else {
|
||||||
url = filename
|
url = filename
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nameOnly = filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (url) {
|
if (url) {
|
||||||
|
@ -1213,7 +1224,7 @@ export class DialogKeywords {
|
||||||
const contentType = mime.lookup(url);
|
const contentType = mime.lookup(url);
|
||||||
reply['attachments'] = [];
|
reply['attachments'] = [];
|
||||||
reply['attachments'].push({
|
reply['attachments'].push({
|
||||||
name: filename,
|
name: nameOnly,
|
||||||
contentType: contentType,
|
contentType: contentType,
|
||||||
contentUrl: `data:${contentType};base64,${base64Image}`
|
contentUrl: `data:${contentType};base64,${base64Image}`
|
||||||
});
|
});
|
||||||
|
|
|
@ -253,6 +253,16 @@ export class KeywordsExpressions {
|
||||||
|
|
||||||
keywords[i++] = [/^\s*next *$/gim, '}'];
|
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++] = [
|
keywords[i++] = [
|
||||||
/^\s*do while +(.*)/gim,
|
/^\s*do while +(.*)/gim,
|
||||||
function (input, group1) {
|
function (input, group1) {
|
||||||
|
|
|
@ -154,22 +154,22 @@ export class SystemKeywords {
|
||||||
if (date) {
|
if (date) {
|
||||||
return array
|
return array
|
||||||
? array.sort((a, b) => {
|
? array.sort((a, b) => {
|
||||||
const c = new Date(a[memberName]);
|
const c = new Date(a[memberName]);
|
||||||
const d = new Date(b[memberName]);
|
const d = new Date(b[memberName]);
|
||||||
return c.getTime() - d.getTime();
|
return c.getTime() - d.getTime();
|
||||||
})
|
})
|
||||||
: null;
|
: null;
|
||||||
} else {
|
} else {
|
||||||
return array
|
return array
|
||||||
? array.sort((a, b) => {
|
? array.sort((a, b) => {
|
||||||
if (a[memberName] < b[memberName]) {
|
if (a[memberName] < b[memberName]) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (a[memberName] > b[memberName]) {
|
if (a[memberName] > b[memberName]) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
})
|
})
|
||||||
: array;
|
: array;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -181,23 +181,21 @@ export class SystemKeywords {
|
||||||
|
|
||||||
if (Array.isArray(data)) {
|
if (Array.isArray(data)) {
|
||||||
isObject = Object.keys(data[1]) !== null;
|
isObject = Object.keys(data[1]) !== null;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
isObject = true;
|
isObject = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isObject || JSON.parse(data) !== null) {
|
if (isObject || JSON.parse(data) !== null) {
|
||||||
|
|
||||||
// Copies data from JSON format into simple array.
|
// Copies data from JSON format into simple array.
|
||||||
|
|
||||||
if (!Array.isArray(data)) {
|
if (!Array.isArray(data)) {
|
||||||
// If data is a single object, wrap it in an array
|
// If data is a single object, wrap it in an array
|
||||||
data = [data];
|
data = [data];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure that keys is an array of strings representing the object keys
|
// Ensure that keys is an array of strings representing the object keys
|
||||||
const keys = Object.keys(data[0]);
|
const keys = Object.keys(data[0]);
|
||||||
|
|
||||||
if (headers) {
|
if (headers) {
|
||||||
output[0] = [];
|
output[0] = [];
|
||||||
|
|
||||||
|
@ -210,7 +208,6 @@ export class SystemKeywords {
|
||||||
output.push({ gbarray: '0' });
|
output.push({ gbarray: '0' });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for (let i = 0; i < data.length; i++) {
|
for (let i = 0; i < data.length; i++) {
|
||||||
output[i + 1] = [];
|
output[i + 1] = [];
|
||||||
for (let j = 0; j < keys.length; j++) {
|
for (let j = 0; j < keys.length; j++) {
|
||||||
|
@ -234,7 +231,6 @@ export class SystemKeywords {
|
||||||
* @see http://tabulator.info/examples/5.2
|
* @see http://tabulator.info/examples/5.2
|
||||||
*/
|
*/
|
||||||
private async renderTable(pid, data, renderPDF, renderImage) {
|
private async renderTable(pid, data, renderPDF, renderImage) {
|
||||||
|
|
||||||
if (data.length && !data[1]) {
|
if (data.length && !data[1]) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -661,7 +657,6 @@ export class SystemKeywords {
|
||||||
|
|
||||||
const filter = await DialogKeywords.getOption({ pid, name: 'filter' });
|
const filter = await DialogKeywords.getOption({ pid, name: 'filter' });
|
||||||
if (filter) {
|
if (filter) {
|
||||||
|
|
||||||
// Creates id row.
|
// Creates id row.
|
||||||
|
|
||||||
body.values[0][0] = 'id';
|
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) {
|
if (table.length === 1) {
|
||||||
GBLog.info(`BASIC: FIND returned no results (zero rows).`);
|
GBLog.info(`BASIC: FIND returned no results (zero rows).`);
|
||||||
|
@ -1877,5 +1872,63 @@ export class SystemKeywords {
|
||||||
await client.v2.tweet(text);
|
await client.v2.tweet(text);
|
||||||
GBLog.info(`Twitter Automation: ${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 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue