From ab415ff9bec8cd922df7e613eef76e8591581779 Mon Sep 17 00:00:00 2001 From: tuyuribr <45042245+tuyuribr@users.noreply.github.com> Date: Mon, 13 Sep 2021 17:58:39 -0300 Subject: [PATCH] feat: Include payment info (#684) --- index.d.ts | 52 ++++++++++++++++++++++++++ src/structures/Message.js | 16 ++++++++ src/structures/Payment.js | 78 +++++++++++++++++++++++++++++++++++++++ src/structures/index.js | 3 +- src/util/Constants.js | 3 +- 5 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 src/structures/Payment.js diff --git a/index.d.ts b/index.d.ts index b38135d..78c5b2c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -428,6 +428,7 @@ declare namespace WAWebJS { REVOKED = 'revoked', ORDER = 'order', PRODUCT = 'product', + PAYMENT = 'payment', UNKNOWN = 'unknown', GROUP_INVITE = 'groups_v4_invite', } @@ -600,6 +601,10 @@ declare namespace WAWebJS { * Gets the order associated with a given message */ getOrder: () => Order, + /** + * Gets the payment details associated with a given message + */ + getPayment: () => Payment, } /** ID that represents a message */ @@ -1060,6 +1065,53 @@ declare namespace WAWebJS { /** Order Created At*/ createdAt: number; } + + /** + * Represents a Payment on WhatsApp + * + * @example + * { + * id: { + * fromMe: true, + * remote: { + * server: 'c.us', + * user: '5511999999999', + * _serialized: '5511999999999@c.us' + * }, + * id: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', + * _serialized: 'true_5511999999999@c.us_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' + * }, + * paymentCurrency: 'BRL', + * paymentAmount1000: 1000, + * paymentMessageReceiverJid: { + * server: 'c.us', + * user: '5511999999999', + * _serialized: '5511999999999@c.us' + * }, + * paymentTransactionTimestamp: 1623463058, + * paymentStatus: 4, + * paymentTxnStatus: 4, + * paymentNote: 'note' + * } + */ + export interface Payment { + /** Payment Id*/ + id: object, + /** Payment currency */ + paymentCurrency: string, + /** Payment ammount */ + paymentAmount1000 : number, + /** Payment receiver */ + paymentMessageReceiverJid : object, + /** Payment transaction timestamp */ + paymentTransactionTimestamp : number, + /** Payment paymentStatus */ + paymentStatus : number, + /** Integer that represents the payment Text */ + paymentTxnStatus : number, + /** The note sent with the payment */ + paymentNote : string; + } /** * Represents a Call on WhatsApp diff --git a/src/structures/Message.js b/src/structures/Message.js index 1930ff1..c10a675 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -4,6 +4,7 @@ const Base = require('./Base'); const MessageMedia = require('./MessageMedia'); const Location = require('./Location'); const Order = require('./Order'); +const Payment = require('./Payment'); const { MessageTypes } = require('../util/Constants'); /** @@ -438,6 +439,21 @@ class Message extends Base { } return undefined; } + /** + * Gets the payment details associated with a given message + * @return {Promise} + */ + async getPayment() { + if (this.type === MessageTypes.PAYMENT) { + const msg = await this.client.pupPage.evaluate(async (msgId) => { + const msg = window.Store.Msg.get(msgId); + if(!msg) return null; + return msg.serialize(); + }, this.id._serialized); + return new Payment(this.client, msg); + } + return undefined; + } } module.exports = Message; diff --git a/src/structures/Payment.js b/src/structures/Payment.js new file mode 100644 index 0000000..b3ff7dc --- /dev/null +++ b/src/structures/Payment.js @@ -0,0 +1,78 @@ +const Base = require('./Base'); + +class Payment extends Base { + constructor(client, data) { + super(client); + + if (data) this._patch(data); + } + + _patch(data) { + /** + * The payment Id + * @type {object} + */ + this.id = data.id; + + /** + * The payment currency + * @type {string} + */ + this.paymentCurrency = data.paymentCurrency; + + /** + * The payment ammount ( R$ 1.00 = 1000 ) + * @type {number} + */ + this.paymentAmount1000 = data.paymentAmount1000; + + /** + * The payment receiver + * @type {object} + */ + this.paymentMessageReceiverJid = data.paymentMessageReceiverJid; + + /** + * The payment transaction timestamp + * @type {number} + */ + this.paymentTransactionTimestamp = data.paymentTransactionTimestamp; + + /** + * The paymentStatus + * @type {number} + * + * Possible Status + * 0:UNKNOWN_STATUS + * 1:PROCESSING + * 2:SENT + * 3:NEED_TO_ACCEPT + * 4:COMPLETE + * 5:COULD_NOT_COMPLETE + * 6:REFUNDED + * 7:EXPIRED + * 8:REJECTED + * 9:CANCELLED + * 10:WAITING_FOR_PAYER + * 11:WAITING + */ + this.paymentStatus = data.paymentStatus; + + /** + * Integer that represents the payment Text + * @type {number} + */ + this.paymentTxnStatus = data.paymentTxnStatus; + + /** + * The note sent with the payment + * @type {string} + */ + this.paymentNote = !data.paymentNoteMsg ? undefined : data.paymentNoteMsg.body ? data.paymentNoteMsg.body : undefined ; + + return super._patch(data); + } + +} + +module.exports = Payment; diff --git a/src/structures/index.js b/src/structures/index.js index bdb393e..025db1f 100644 --- a/src/structures/index.js +++ b/src/structures/index.js @@ -14,5 +14,6 @@ module.exports = { Label: require('./Label.js'), Order: require('./Order'), Product: require('./Product'), - Call: require('./Call') + Call: require('./Call'), + Payment: require('./Payment') }; \ No newline at end of file diff --git a/src/util/Constants.js b/src/util/Constants.js index d973290..30a4138 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -74,7 +74,8 @@ exports.MessageTypes = { REVOKED: 'revoked', PRODUCT: 'product', UNKNOWN: 'unknown', - GROUP_INVITE: 'groups_v4_invite' + GROUP_INVITE: 'groups_v4_invite', + PAYMENT: 'payment' }; /**