Skip to content

Commit 9c78d3b

Browse files
committed
feat: server auth payment
1 parent 44628df commit 9c78d3b

File tree

6 files changed

+84
-24
lines changed

6 files changed

+84
-24
lines changed

prisma/schema.prisma

+8-8
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ model Card {
3636

3737
model Products {
3838
id String @id @default(cuid())
39-
name String
39+
name String @unique
4040
price Int
4141
detail String?
4242
createdAt DateTime @default(now())
@@ -45,17 +45,17 @@ model Products {
4545
}
4646

4747
model Transaction {
48-
id String @unique
48+
id String @unique
4949
userId String
50-
productId String
50+
productId String?
5151
productName String
5252
price Int
53-
createdAt DateTime @default(now())
54-
updatedAt DateTime @updatedAt
55-
canceled Boolean @default(false)
53+
createdAt DateTime @default(now())
54+
updatedAt DateTime @updatedAt
55+
canceled Boolean @default(false)
5656
tid String
57-
PRODUCT Products @relation(fields: [productId], references: [id])
58-
USER User @relation(fields: [userId], references: [id])
57+
PRODUCT Products? @relation(fields: [productId], references: [id])
58+
USER User @relation(fields: [userId], references: [id])
5959
6060
@@unique([id, userId])
6161
}

src/interface/body.ts

+5
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ export type PaymentRequest = RequestBody<{
3131
export type CancelRequest = RequestBody<{
3232
transactionId: string
3333
}>
34+
35+
export type ServerAuthRequest = RequestBody<{
36+
tid: string
37+
amount: number
38+
}>

src/lib/createTransaction.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import prisma from 'lib/prisma'
2+
3+
export default async (
4+
data: Record<'tid' | 'orderId' | 'goodsName' | 'userId', string> & {
5+
amount: number
6+
productId?: string
7+
}
8+
) => {
9+
return await prisma.transaction.create({
10+
data: {
11+
tid: data.tid,
12+
id: data.orderId,
13+
price: data.amount,
14+
productName: data.goodsName,
15+
USER: { connect: { id: data.userId } },
16+
...(data.productId
17+
? { PRODUCT: { connect: { id: data.productId } } }
18+
: {}),
19+
},
20+
select: {
21+
id: true,
22+
price: true,
23+
createdAt: true,
24+
productName: true,
25+
},
26+
})
27+
}

src/routers/payment/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import key from './key'
22
import pay from './pay'
33
import cancel from './cancel'
44
import history from './history'
5+
import serverAuth from './serverAuth'
56

67
import Joi from 'joi'
78
import checkRouter from 'lib/checkRouter'
@@ -74,5 +75,11 @@ export default checkRouter({
7475
needAuth: true,
7576
handler: history,
7677
},
78+
{
79+
path: '/serverAuth',
80+
method: 'post',
81+
needAuth: false,
82+
handler: serverAuth,
83+
}
7784
],
7885
})

src/routers/payment/pay/index.ts

+9-16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import random from 'lib/random'
88
import { sha256 } from 'lib/token'
99
import { HttpError } from 'lib/error'
1010
import { getCard, getProduct } from './get'
11+
import createTransaction from 'lib/createTransaction'
1112

1213
import type { Response } from 'express'
1314
import type { AxiosPromise } from 'axios'
@@ -29,29 +30,21 @@ export default async (req: PaymentRequest, res: Response) => {
2930
cardQuota: 0, // 일시불
3031
amount: product.price,
3132
goodsName: product.name,
32-
useShopInterest: false // false만 사용가능
33+
useShopInterest: false, // false만 사용가능
3334
},
3435
}))
3536

3637
if (response.resultCode !== '0000') {
3738
throw new HttpError(500, response.resultMsg, response.resultCode)
3839
}
3940

40-
const transaction = await prisma.transaction.create({
41-
data: {
42-
tid: response.tid,
43-
id: response.orderId,
44-
price: response.amount,
45-
productName: response.goodsName,
46-
USER: { connect: { id: req.user.id } },
47-
PRODUCT: { connect: { id: productId } },
48-
},
49-
select: {
50-
id: true,
51-
price: true,
52-
createdAt: true,
53-
productName: true,
54-
},
41+
const transaction = await createTransaction({
42+
productId,
43+
tid: response.tid,
44+
userId: req.user.id,
45+
amount: response.amount,
46+
orderId: response.orderId,
47+
goodsName: response.goodsName,
5548
})
5649

5750
res.json(transaction)
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import axios from 'lib/axios'
2+
import { HttpError } from 'lib/error'
3+
import createTransaction from 'lib/createTransaction'
4+
5+
import type { Response } from 'express'
6+
import type { ServerAuthRequest } from 'interface/body'
7+
8+
export default async (req: ServerAuthRequest, res: Response) => {
9+
const { data: response } = await axios({
10+
url: `/v1/payments/${req.body.tid}`,
11+
method: 'POST',
12+
data: { amount: req.body.amount },
13+
})
14+
15+
if (response.resultCode !== '0000') {
16+
throw new HttpError(500, response.resultMsg, response.resultCode)
17+
}
18+
19+
const transaction = await createTransaction({
20+
tid: response.tid,
21+
orderId: response.orderId,
22+
amount: response.amount,
23+
goodsName: response.goodsName,
24+
userId: req.user.id,
25+
})
26+
27+
res.json(transaction)
28+
}

0 commit comments

Comments
 (0)