Skip to content

Commit

Permalink
feat: Added WPP.chat.sendPixKeyMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
icleitoncosta committed Jun 21, 2024
1 parent 3aeca62 commit b3526f2
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/chat/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export {
OrderMessageOptions,
sendOrderMessage,
} from './sendOrderMessage';
export { sendPixKeyMessage } from './sendPixKeyMessage';
export { sendRawMessage } from './sendRawMessage';
export { sendReactionToMessage } from './sendReactionToMessage';
export {
Expand Down
147 changes: 147 additions & 0 deletions src/chat/functions/sendPixKeyMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*!
* Copyright 2024 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 { generateOrderUniqueId, WPPError } from '../../util';
import {
defaultSendMessageOptions,
RawMessage,
SendMessageOptions,
SendMessageReturn,
} from '..';
import { sendRawMessage } from '.';

export interface OrderItems {
type: 'product' | 'custom';
id?: number[] | string[];
name?: string;
price?: number;
qnt?: number;
}

export interface OrderMessageOptions extends SendMessageOptions {
notes?: string;
discount?: number;
tax?: number;
shipping?: number;
offset?: number;
}

/**
* Send a invoice message
* To send (prices, tax, shipping or discount), for example: USD 12.90, send them without dots or commas, like: 12900
*
* @example
* ```javascript
* // Send PIX Key Message (Brazil Pix Key)
* WPP.chat.sendPixKeyMessage('[number]@c.us', {
* keyType: 'CNPJ',
* name: 'WPPCONNECT-TEAM',
* key: '33460516000178',
* instructions: 'Pay text for instructions here',
* });
*
* ```
* @category Message
*/
export async function sendPixKeyMessage(
chatId: any,
params: {
keyType: 'CNPJ' | 'CPF' | 'PHONE' | 'EMAIL' | 'EVP';
name: string;
key: string;
instructions?: string;
},
options?: SendMessageOptions
): Promise<SendMessageReturn> {
if (!chatId || !params.keyType || !params.name || !params.key)
throw new WPPError(
'parameter_not_fount',
'Please, send all the required parameters'
);
options = {
...defaultSendMessageOptions,
...options,
};

const buttonParamsJson = {
order: {
items: [
{
name: '',
retailer_id: `custom-item-${generateOrderUniqueId}`,
amount: {
offset: 1,
value: 0,
},
quantity: 0,
},
],
order_type: 'ORDER_WITHOUT_AMOUNT',
status: 'payment_requested',
subtotal: {
value: 0,
offset: 1,
},
},
total_amount: {
value: 0,
offset: 1,
},
reference_id: generateOrderUniqueId(),
payment_settings: [
{
type: 'pix_static_code',
pix_static_code: {
key_type: params.keyType,
merchant_name: params.name,
key: params.key,
},
},
{
type: 'cards',
cards: {
enabled: false,
},
},
],
external_payment_configurations: [
{
payment_instruction: params.instructions,
type: 'payment_instruction',
},
],
additional_note: params.instructions,
currency: 'BRL',
type: 'physical-goods',
};

const message: RawMessage = {
type: 'interactive',
caption: '',
nativeFlowName: 'payment_info',
interactiveType: 'native_flow',
interactivePayload: {
buttons: [
{
buttonParamsJson: JSON.stringify(buttonParamsJson),
name: 'payment_info',
},
],
messageVersion: 1,
},
};
return await sendRawMessage(chatId, message, options);
}

0 comments on commit b3526f2

Please sign in to comment.