-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature:FUR-33 [BE][Web] API payment for upgrading GenAI Premium Plan (…
- Loading branch information
Showing
15 changed files
with
779 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Body, Controller, Post, Req, UseGuards } from '@nestjs/common' | ||
import { ApiBearerAuth, ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger' | ||
import * as _ from 'lodash' | ||
import { Roles } from '@auth/decorators/roles.decorator' | ||
import { UserRole } from '@common/contracts/constant' | ||
import { RolesGuard } from '@auth/guards/roles.guard' | ||
import { JwtAuthGuard } from '@auth/guards/jwt-auth.guard' | ||
import { GenerateTextToImageDto, TextToImageResponseDto } from '@ai-generation/dtos/text-to-image.dto' | ||
import { AIGenerationPricingService } from '@ai-generation/services/pricing.service' | ||
import { CreateCreditPurchaseDto, CreditPurchaseResponseDto } from '@ai-generation/dtos/pricing.dto' | ||
|
||
@ApiTags('AIGeneration - Pricing') | ||
@ApiBearerAuth() | ||
@Roles(UserRole.CUSTOMER) | ||
@UseGuards(JwtAuthGuard.ACCESS_TOKEN, RolesGuard) | ||
@Controller('pricing') | ||
export class AIGenerationPricingController { | ||
constructor(private readonly aiGenerationPricingService: AIGenerationPricingService) {} | ||
|
||
@ApiOperation({ | ||
summary: 'Create payment for credits purchase' | ||
}) | ||
@ApiOkResponse({ type: CreditPurchaseResponseDto }) | ||
@Post() | ||
createPaymentForCreditsPurchase(@Req() req, @Body() createCreditPurchaseDto: CreateCreditPurchaseDto) { | ||
createCreditPurchaseDto.customerId = _.get(req, 'user._id') | ||
return this.aiGenerationPricingService.createPayment(createCreditPurchaseDto) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { ApiProperty } from '@nestjs/swagger' | ||
import { DataResponse } from '@common/contracts/openapi-builder' | ||
import { IsEnum, IsNotEmpty, MaxLength } from 'class-validator' | ||
import { AIPricingPlan } from '@ai-generation/contracts/constant' | ||
import { PayOSStatus, PaymentMethod } from '@payment/contracts/constant' | ||
|
||
export class CreateCreditPurchaseDto { | ||
@ApiProperty({ | ||
enum: AIPricingPlan, | ||
example: 'PERSONAL | PREMIUM' | ||
}) | ||
@IsNotEmpty() | ||
@IsEnum(AIPricingPlan) | ||
plan: string | ||
|
||
@ApiProperty({ enum: PaymentMethod, example: 'PAY_OS | MOMO' }) | ||
@IsNotEmpty() | ||
@IsEnum(PaymentMethod) | ||
paymentMethod?: PaymentMethod | ||
|
||
customerId?: string | ||
} | ||
|
||
export class CreditPurchaseDto { | ||
@ApiProperty() | ||
bin: string | ||
@ApiProperty() | ||
accountNumber: string | ||
@ApiProperty() | ||
accountName: string | ||
@ApiProperty() | ||
amount: number | ||
@ApiProperty() | ||
description: string | ||
@ApiProperty() | ||
orderCode: number | ||
@ApiProperty() | ||
currency: string | ||
@ApiProperty() | ||
paymentLinkId: string | ||
@ApiProperty({ | ||
enum: PayOSStatus | ||
}) | ||
status: PayOSStatus | ||
@ApiProperty() | ||
checkoutUrl: string | ||
@ApiProperty() | ||
qrCode: string | ||
} | ||
|
||
export class CreditPurchaseResponseDto extends DataResponse(CreditPurchaseDto) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { ConfigService } from '@nestjs/config' | ||
import { AIPricingPlanCost } from '@ai-generation/contracts/constant' | ||
import { TransactionStatus } from '@common/contracts/constant' | ||
import { CreateCreditPurchaseDto } from '@ai-generation/dtos/pricing.dto' | ||
import { Connection } from 'mongoose' | ||
import { InjectConnection } from '@nestjs/mongoose' | ||
import { CheckoutRequestType, CheckoutResponseDataType, PaymentLinkDataType } from '@payos/node/lib/type' | ||
import { CreateMomoPaymentResponse } from '@payment/dto/momo-payment.dto' | ||
import { PaymentMethod, PaymentType } from '@payment/contracts/constant' | ||
import { PaymentService } from '@payment/services/payment.service' | ||
import { PaymentRepository } from '@payment/repositories/payment.repository' | ||
|
||
@Injectable() | ||
export class AIGenerationPricingService { | ||
constructor( | ||
@InjectConnection() readonly connection: Connection, | ||
private readonly configService: ConfigService, | ||
private readonly paymentService: PaymentService, | ||
private readonly paymentRepository: PaymentRepository | ||
) {} | ||
|
||
async createPayment(createCreditPurchaseDto: CreateCreditPurchaseDto) { | ||
const { customerId, plan } = createCreditPurchaseDto | ||
try { | ||
// 1. Calculate plan cost | ||
const totalAmount = AIPricingPlanCost[plan] | ||
|
||
// 2. Process transaction | ||
let paymentResponseData: CreateMomoPaymentResponse | PaymentLinkDataType | ||
let checkoutData: CreateMomoPaymentResponse | CheckoutResponseDataType | ||
const MAX_VALUE = 9_007_199_254_740_991 | ||
const MIM_VALUE = 1_000_000_000_000_000 | ||
const orderCode = Math.floor(MIM_VALUE + Math.random() * (MAX_VALUE - MIM_VALUE)) | ||
createCreditPurchaseDto['paymentMethod'] = PaymentMethod.PAY_OS | ||
switch (createCreditPurchaseDto.paymentMethod) { | ||
case PaymentMethod.PAY_OS: | ||
default: | ||
this.paymentService.setStrategy(PaymentMethod.PAY_OS) | ||
const checkoutRequestType: CheckoutRequestType = { | ||
orderCode: orderCode, | ||
amount: totalAmount, | ||
description: `FUR - Purchase credits`, | ||
items: [ | ||
{ | ||
name: plan, | ||
quantity: 1, | ||
price: totalAmount | ||
} | ||
], | ||
cancelUrl: `${this.configService.get('WEB_URL')}/pricing`, | ||
returnUrl: `${this.configService.get('WEB_URL')}/ai` | ||
} | ||
checkoutData = await this.paymentService.createTransaction(checkoutRequestType) | ||
paymentResponseData = await this.paymentService.getTransaction(checkoutData['orderCode']) | ||
break | ||
} | ||
|
||
// 3. Create payment | ||
await this.paymentRepository.create({ | ||
customerId, | ||
transactionStatus: TransactionStatus.DRAFT, | ||
transaction: paymentResponseData, | ||
transactionHistory: [paymentResponseData], | ||
paymentMethod: createCreditPurchaseDto.paymentMethod, | ||
amount: totalAmount, | ||
paymentType: PaymentType.AI | ||
}) | ||
return checkoutData | ||
} catch (error) { | ||
console.error(error) | ||
throw error | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.