Skip to content

Commit

Permalink
chore: Add discord noti payment webhook payos
Browse files Browse the repository at this point in the history
  • Loading branch information
MinhhTien committed Jun 16, 2024
1 parent 4d8a98c commit e8873b9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/common/services/discord.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class DiscordService {
try {
await this.webhookClient.send({
content,
username: 'Furnique Bot',
username: `${this.configService.get('NODE_ENV')} Furnique Bot`,
avatarURL:
'https://nftcalendar.io/storage/uploads/2021/11/30/webp_net-gifmaker__1__1130202114500961a63a2147d4d.gif',
embeds: [fields ? this.embed.setFields(fields) : this.embed]
Expand Down
1 change: 1 addition & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default () => ({
endpoint: process.env.EDEN_AI_ENDPOINT,
apiKey: process.env.EDEN_AI_API_KEY
},
NODE_ENV: process.env.NODE_ENV,
JWT_ACCESS_SECRET: process.env.JWT_ACCESS_SECRET || 'accessSecret',
JWT_ACCESS_EXPIRATION: process.env.JWT_ACCESS_EXPIRATION || 864000, // seconds
JWT_REFRESH_SECRET: process.env.JWT_REFRESH_SECRET || 'refreshSecret',
Expand Down
5 changes: 3 additions & 2 deletions src/customer/customer.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module, OnModuleInit } from '@nestjs/common'
import { Logger, Module, OnModuleInit } from '@nestjs/common'
import { MongooseModule } from '@nestjs/mongoose'
import { Customer, CustomerSchema } from '@customer/schemas/customer.schema'
import { CustomerRepository } from '@customer/repositories/customer.repository'
Expand All @@ -13,10 +13,11 @@ import { DEFAULT_CREDITS } from '@ai-generation/contracts/constant'
exports: [CustomerService, CustomerRepository]
})
export class CustomerModule implements OnModuleInit {
private readonly logger = new Logger(CustomerModule.name)
constructor(private readonly customerRepository: CustomerRepository) {}

async onModuleInit() {
console.log(`CustomerModule.OnModuleInit: Set default credits for customers`)
this.logger.log(`CustomerModule.OnModuleInit: Set default credits for customers`)
await this.customerRepository.updateMany(
{ credits: { $exists: false } },
{
Expand Down
52 changes: 50 additions & 2 deletions src/payment/strategies/payos.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CartService } from '@cart/services/cart.service'
import { OrderStatus, TransactionStatus, UserRole } from '@common/contracts/constant'
import { Errors } from '@common/contracts/error'
import { AppException } from '@common/exceptions/app.exception'
import { DiscordService } from '@common/services/discord.service'
import { CustomerRepository } from '@customer/repositories/customer.repository'
import { MailerService } from '@nestjs-modules/mailer'
import { Injectable, Logger, OnModuleInit } from '@nestjs/common'
Expand Down Expand Up @@ -39,7 +40,8 @@ export class PayOSPaymentStrategy implements IPaymentStrategy, OnModuleInit {
private readonly productRepository: ProductRepository,
private readonly paymentRepository: PaymentRepository,
private readonly customerRepository: CustomerRepository,
private readonly mailerService: MailerService
private readonly mailerService: MailerService,
private readonly discordService: DiscordService
) {
this.config = this.configService.get('payment.payos')
this.payOS = new PayOS(this.config.clientId, this.config.apiKey, this.config.checksumKey)
Expand Down Expand Up @@ -264,6 +266,30 @@ export class PayOSPaymentStrategy implements IPaymentStrategy, OnModuleInit {
}
})
// 10. Send notification to staff

// 4. Send notification to staff => Discord Bot
try {
this.discordService.sendMessage({
fields: [
{
name: 'PayOSPaymentStrategy',
value: 'processWebhookOrder'
},
{
name: 'Payment For Order',
value: 'SUCCESS'
},
{
name: 'orderId',
value: `${orderId}`
},
{
name: 'Total Amount',
value: `${order.totalAmount}`
}
]
})
} catch (err) {}
} else {
// Payment failed
this.logger.log('processWebhook: payment FAILED')
Expand Down Expand Up @@ -355,7 +381,29 @@ export class PayOSPaymentStrategy implements IPaymentStrategy, OnModuleInit {
)

// 3. Send email/notification to customer
// 4. Send notification to staff
// 4. Send notification to staff => Discord Bot
try {
this.discordService.sendMessage({
fields: [
{
name: 'PayOSPaymentStrategy',
value: 'processWebhookAI'
},
{
name: 'Payment For AI',
value: 'SUCCESS'
},
{
name: 'orderId',
value: `${orderId}`
},
{
name: 'credits',
value: `${credits}`
}
]
})
} catch (err) {}
} else {
// Payment failed
this.logger.log('processWebhook: payment FAILED')
Expand Down

0 comments on commit e8873b9

Please sign in to comment.