Skip to content

Commit e8873b9

Browse files
committed
chore: Add discord noti payment webhook payos
1 parent 4d8a98c commit e8873b9

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

src/common/services/discord.service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class DiscordService {
1717
try {
1818
await this.webhookClient.send({
1919
content,
20-
username: 'Furnique Bot',
20+
username: `${this.configService.get('NODE_ENV')} Furnique Bot`,
2121
avatarURL:
2222
'https://nftcalendar.io/storage/uploads/2021/11/30/webp_net-gifmaker__1__1130202114500961a63a2147d4d.gif',
2323
embeds: [fields ? this.embed.setFields(fields) : this.embed]

src/config/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export default () => ({
4343
endpoint: process.env.EDEN_AI_ENDPOINT,
4444
apiKey: process.env.EDEN_AI_API_KEY
4545
},
46+
NODE_ENV: process.env.NODE_ENV,
4647
JWT_ACCESS_SECRET: process.env.JWT_ACCESS_SECRET || 'accessSecret',
4748
JWT_ACCESS_EXPIRATION: process.env.JWT_ACCESS_EXPIRATION || 864000, // seconds
4849
JWT_REFRESH_SECRET: process.env.JWT_REFRESH_SECRET || 'refreshSecret',

src/customer/customer.module.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Module, OnModuleInit } from '@nestjs/common'
1+
import { Logger, Module, OnModuleInit } from '@nestjs/common'
22
import { MongooseModule } from '@nestjs/mongoose'
33
import { Customer, CustomerSchema } from '@customer/schemas/customer.schema'
44
import { CustomerRepository } from '@customer/repositories/customer.repository'
@@ -13,10 +13,11 @@ import { DEFAULT_CREDITS } from '@ai-generation/contracts/constant'
1313
exports: [CustomerService, CustomerRepository]
1414
})
1515
export class CustomerModule implements OnModuleInit {
16+
private readonly logger = new Logger(CustomerModule.name)
1617
constructor(private readonly customerRepository: CustomerRepository) {}
1718

1819
async onModuleInit() {
19-
console.log(`CustomerModule.OnModuleInit: Set default credits for customers`)
20+
this.logger.log(`CustomerModule.OnModuleInit: Set default credits for customers`)
2021
await this.customerRepository.updateMany(
2122
{ credits: { $exists: false } },
2223
{

src/payment/strategies/payos.strategy.ts

+50-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CartService } from '@cart/services/cart.service'
33
import { OrderStatus, TransactionStatus, UserRole } from '@common/contracts/constant'
44
import { Errors } from '@common/contracts/error'
55
import { AppException } from '@common/exceptions/app.exception'
6+
import { DiscordService } from '@common/services/discord.service'
67
import { CustomerRepository } from '@customer/repositories/customer.repository'
78
import { MailerService } from '@nestjs-modules/mailer'
89
import { Injectable, Logger, OnModuleInit } from '@nestjs/common'
@@ -39,7 +40,8 @@ export class PayOSPaymentStrategy implements IPaymentStrategy, OnModuleInit {
3940
private readonly productRepository: ProductRepository,
4041
private readonly paymentRepository: PaymentRepository,
4142
private readonly customerRepository: CustomerRepository,
42-
private readonly mailerService: MailerService
43+
private readonly mailerService: MailerService,
44+
private readonly discordService: DiscordService
4345
) {
4446
this.config = this.configService.get('payment.payos')
4547
this.payOS = new PayOS(this.config.clientId, this.config.apiKey, this.config.checksumKey)
@@ -264,6 +266,30 @@ export class PayOSPaymentStrategy implements IPaymentStrategy, OnModuleInit {
264266
}
265267
})
266268
// 10. Send notification to staff
269+
270+
// 4. Send notification to staff => Discord Bot
271+
try {
272+
this.discordService.sendMessage({
273+
fields: [
274+
{
275+
name: 'PayOSPaymentStrategy',
276+
value: 'processWebhookOrder'
277+
},
278+
{
279+
name: 'Payment For Order',
280+
value: 'SUCCESS'
281+
},
282+
{
283+
name: 'orderId',
284+
value: `${orderId}`
285+
},
286+
{
287+
name: 'Total Amount',
288+
value: `${order.totalAmount}`
289+
}
290+
]
291+
})
292+
} catch (err) {}
267293
} else {
268294
// Payment failed
269295
this.logger.log('processWebhook: payment FAILED')
@@ -355,7 +381,29 @@ export class PayOSPaymentStrategy implements IPaymentStrategy, OnModuleInit {
355381
)
356382

357383
// 3. Send email/notification to customer
358-
// 4. Send notification to staff
384+
// 4. Send notification to staff => Discord Bot
385+
try {
386+
this.discordService.sendMessage({
387+
fields: [
388+
{
389+
name: 'PayOSPaymentStrategy',
390+
value: 'processWebhookAI'
391+
},
392+
{
393+
name: 'Payment For AI',
394+
value: 'SUCCESS'
395+
},
396+
{
397+
name: 'orderId',
398+
value: `${orderId}`
399+
},
400+
{
401+
name: 'credits',
402+
value: `${credits}`
403+
}
404+
]
405+
})
406+
} catch (err) {}
359407
} else {
360408
// Payment failed
361409
this.logger.log('processWebhook: payment FAILED')

0 commit comments

Comments
 (0)