Skip to content

Commit

Permalink
feat: Add PayDTO for payment information
Browse files Browse the repository at this point in the history
  • Loading branch information
suk-6 committed Sep 18, 2024
1 parent 38dfb3f commit 6cb80ff
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 18 deletions.
17 changes: 17 additions & 0 deletions src/modules/user/dto/pay.user.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { IsNotEmpty, IsNumber, IsString, Length, MaxLength } from 'class-validator';

export class PayDTO {
@IsString()
@MaxLength(200)
@IsNotEmpty()
paymentKey: string;

@IsString()
@Length(6, 64)
@IsNotEmpty()
orderId: string;

@IsNumber()
@IsNotEmpty()
amount: number;
}
12 changes: 1 addition & 11 deletions src/modules/user/dto/update.user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsNotEmpty, IsNumber, IsString } from 'class-validator';
import { IsNotEmpty, IsString } from 'class-validator';

import { ApiProperty } from '@nestjs/swagger';

Expand All @@ -11,13 +11,3 @@ export class UpdateWalletDTO {
})
walletAddress: string;
}

export class UpdateCreditDTO {
@IsNotEmpty()
@IsNumber()
@ApiProperty({
example: 100,
description: '크레딧',
})
credit: number;
}
16 changes: 9 additions & 7 deletions src/modules/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Body, Controller, Delete, Get, Put, UseGuards } from '@nestjs/common';
import { Body, Controller, Delete, Get, Post, Put, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import {
ApiBadRequestResponse,
ApiBearerAuth,
ApiInternalServerErrorResponse,
ApiNotFoundResponse,
ApiOkResponse,
ApiOperation,
Expand All @@ -14,7 +15,8 @@ import { JwtPayload } from 'src/auth/model/payload.jwt.model';
import { CurrentUser } from 'src/common';

import { GetUserDTO } from './dto/get.user.dto';
import { UpdateCreditDTO, UpdateWalletDTO } from './dto/update.user.dto';
import { PayDTO } from './dto/pay.user.dto';
import { UpdateWalletDTO } from './dto/update.user.dto';
import { UserService } from './user.service';

@Controller('user')
Expand Down Expand Up @@ -65,15 +67,15 @@ export class UserController {
await this.userService.deleteUser(id);
}

@Put('credit')
@Post('pay')
@UseGuards(AuthGuard('access'))
@ApiBearerAuth()
@ApiOperation({ summary: '유저 크레딧 변경' })
@ApiOperation({ summary: '크레딧 충전' })
@ApiOkResponse({ description: 'Success' })
@ApiBadRequestResponse({ description: '응답 참조' })
@ApiNotFoundResponse({ description: 'User not found' })
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
async updateCredit(@CurrentUser() { id }: JwtPayload, @Body() body: UpdateCreditDTO) {
await this.userService.updateCredit(id, body.credit);
@ApiInternalServerErrorResponse({ description: 'Payment confirmation failed' })
async pay(@CurrentUser() { id }: JwtPayload, @Body() dto: PayDTO) {
await this.userService.pay(id, dto);
}
}
44 changes: 44 additions & 0 deletions src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {

import { PrismaService } from 'src/common/modules/prisma/prisma.service';

import { PayDTO } from './dto/pay.user.dto';

@Injectable()
export class UserService {
constructor(private readonly prisma: PrismaService) {}
Expand All @@ -28,6 +30,48 @@ export class UserService {
return user;
}

async pay(userId: string, dto: PayDTO) {
const url = 'https://api.tosspayments.com/v1/payments/confirm';
const options = {
method: 'POST',
headers: {
Authorization: 'Basic dGVzdF9za196WExrS0V5cE5BcldtbzUwblgzbG1lYXhZRzVSOg==',
'Content-Type': 'application/json',
},
body: JSON.stringify(dto),
};

try {
const response = await fetch(url, options);
const data = await response.json();
const { totalAmount } = data as { totalAmount: number };
if (!response.ok)
throw new InternalServerErrorException(data.message || 'Payment confirmation failed');

await this.prisma.userAccount
.update({
where: {
id: userId,
},
data: {
credit: {
increment: totalAmount,
},
},
})
.catch((error) => {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
if (error.code === 'P2025') {
throw new NotFoundException('사용자를 찾을 수 없습니다.');
}
}
});
} catch (error) {
console.error(error);
throw new InternalServerErrorException('Payment confirmation failed');
}
}

async updateWalletAddress(id: string, walletAddress: string) {
await this.prisma.userAccount.update({
where: {
Expand Down

0 comments on commit 6cb80ff

Please sign in to comment.