Skip to content

Commit

Permalink
feat: Added order.payment_status event
Browse files Browse the repository at this point in the history
  • Loading branch information
icleitoncosta committed Aug 31, 2023
1 parent 75047b1 commit 8165a2a
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/chat/functions/sendOrderMessage.ts
Expand Up @@ -101,7 +101,6 @@ export async function sendOrderMessage(
undefined,
true
);
console.log(data);
if (typeof data === 'undefined')
throw new WPPError(
'product_not_found',
Expand Down
3 changes: 3 additions & 0 deletions src/eventEmitter/eventTypes.ts
Expand Up @@ -20,6 +20,7 @@ import { ChatEventTypes } from '../chat/events/eventTypes';
import { ConfigEventTypes } from '../config/eventTypes';
import { ConnEventTypes } from '../conn/events/eventTypes';
import { GroupEventTypes } from '../group/events/eventTypes';
import { OrderEventTypes } from '../order/events/eventTypes';
import { StatusEventTypes } from '../status/events/eventTypes';
import { WebpackEvents } from '../webpack/eventTypes';

Expand All @@ -29,6 +30,7 @@ export { ChatEventTypes } from '../chat/events/eventTypes';
export { ConfigEventTypes } from '../config/eventTypes';
export { ConnEventTypes } from '../conn/events/eventTypes';
export { GroupEventTypes } from '../group/events/eventTypes';
export { OrderEventTypes } from '../order/events/eventTypes';
export { StatusEventTypes } from '../status/events/eventTypes';
export { WebpackEvents } from '../webpack/eventTypes';

Expand All @@ -38,5 +40,6 @@ export type EventTypes = BlocklistEventTypes &
ConfigEventTypes &
ConnEventTypes &
GroupEventTypes &
OrderEventTypes &
StatusEventTypes &
WebpackEvents;
36 changes: 36 additions & 0 deletions src/order/events/eventTypes.ts
@@ -0,0 +1,36 @@
/*!
* Copyright 2023 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { MsgKey } from '../../whatsapp';

export interface OrderEventTypes {
/**
* Triggered when change the active chat
*
* @example
* ```javascript
* WPP.on('order.payment_status', (order) => {
* // Your code
* });
* ```
*/
'order.payment_status': {
method: string;
timestamp: number;
reference_id: string;
msgId: MsgKey;
};
}
17 changes: 17 additions & 0 deletions src/order/events/index.ts
@@ -0,0 +1,17 @@
/*!
* Copyright 2021 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import './registerUpdateOrderEvent';
45 changes: 45 additions & 0 deletions src/order/events/registerUpdateOrderEvent.ts
@@ -0,0 +1,45 @@
/*!
* Copyright 2021 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { internalEv } from '../../eventEmitter';
import * as webpack from '../../webpack';
import { MsgModel, MsgStore } from '../../whatsapp';

webpack.onInjected(() => registerUpdateOrderEvent());

function registerUpdateOrderEvent() {
MsgStore.on('add', (msg: MsgModel) => {
if (
msg.type !== 'interactive' ||
msg.interactivePayload?.buttons[0].name !== 'payment_method' ||
!msg.isNewMsg
) {
return;
}
const payload = JSON.parse(
msg.interactivePayload?.buttons[0].buttonParamsJson
);

queueMicrotask(() => {
internalEv.emit('order.payment_status', {
method: payload.payment_method,
timestamp: payload.payment_timestamp,
reference_id: payload.reference_id,
msgId: msg.id,
});
});
});
}
1 change: 1 addition & 0 deletions src/order/index.ts
Expand Up @@ -14,4 +14,5 @@
* limitations under the License.
*/

export * from './events';
export * from './functions';
2 changes: 2 additions & 0 deletions src/whatsapp/models/MsgModel.ts
Expand Up @@ -334,6 +334,8 @@ interface Derived {
pollOptions?: any;
pollSelectableOptionsCount?: number;
pollUpdateParentKey?: any;
nativeFlowName?: string;
interactivePayload?: any;
}

/** @whatsapp 17304 */
Expand Down

0 comments on commit 8165a2a

Please sign in to comment.