Skip to content

Commit

Permalink
feat: fill contents
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwiyou committed Nov 21, 2024
1 parent 0ffea79 commit 518ca8e
Show file tree
Hide file tree
Showing 10 changed files with 409 additions and 82 deletions.
4 changes: 2 additions & 2 deletions src/components/interactive-docs/PgSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kpnLogo from "~/assets/pg-circle/kpn.png";
import ksnetLogo from "~/assets/pg-circle/ksnet.png";
import naverLogo from "~/assets/pg-circle/naver.png";
import niceLogo from "~/assets/pg-circle/nice.png";
import smatroLogo from "~/assets/pg-circle/smartro.png";
import smartro from "~/assets/pg-circle/smartro.png";
import tossLogo from "~/assets/pg-circle/toss.png";
import { type Pg, useInteractiveDocs } from "~/state/interactive-docs";

Expand All @@ -21,7 +21,7 @@ export type PgSelectOption = {

const PgOptions = {
nice: { label: "나이스페이먼츠", icon: niceLogo },
smatro: { label: "스마트로", icon: smatroLogo },
smartro: { label: "스마트로", icon: smartro },
toss: { label: "토스페이먼츠", icon: tossLogo },
kpn: { label: "한국결제네트웍스", icon: kpnLogo },
inicis: { label: "이니시스", icon: inicisLogo },
Expand Down
222 changes: 209 additions & 13 deletions src/routes/(root)/opi/ko/quick-guide/_preview/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,19 @@ export function Preview() {

const requestPayment = async () => {
if (paymentStatus().status === "PENDING") return undefined;
const paymentId = crypto
.getRandomValues(new Uint32Array(1))[0]!
.toString(16)
.padStart(8, "0");
const request = match(untrack(() => params))
.with(
{ pg: { name: "toss", payMethods: "card" } },
() =>
({
storeId: "store-e4038486-8d83-41a5-acf1-844a009e0d94",
paymentId: crypto.randomUUID(),
orderName: "테스트 결제",
totalAmount: 100,
paymentId,
orderName: "나이키 멘즈 조이라이드 플라이니트",
totalAmount: 1000,
currency: "CURRENCY_KRW",
channelKey: "channel-key-ebe7daa6-4fe4-41bd-b17d-3495264399b5",
payMethod: "CARD",
Expand All @@ -64,9 +68,9 @@ export function Preview() {
() =>
({
storeId: "store-e4038486-8d83-41a5-acf1-844a009e0d94",
paymentId: crypto.randomUUID(),
orderName: "테스트 결제",
totalAmount: 100,
paymentId,
orderName: "나이키 멘즈 조이라이드 플라이니트",
totalAmount: 1000,
currency: "CURRENCY_KRW",
channelKey: "channel-key-ebe7daa6-4fe4-41bd-b17d-3495264399b5",
payMethod: "VIRTUAL_ACCOUNT",
Expand All @@ -82,9 +86,9 @@ export function Preview() {
() =>
({
storeId: "store-e4038486-8d83-41a5-acf1-844a009e0d94",
paymentId: crypto.randomUUID(),
orderName: "테스트 결제",
totalAmount: 100,
paymentId,
orderName: "나이키 멘즈 조이라이드 플라이니트",
totalAmount: 1000,
currency: "CURRENCY_KRW",
channelKey: "channel-key-4ca6a942-3ee0-48fb-93ef-f4294b876d28",
payMethod: "CARD",
Expand All @@ -97,9 +101,9 @@ export function Preview() {
() =>
({
storeId: "store-e4038486-8d83-41a5-acf1-844a009e0d94",
paymentId: crypto.randomUUID(),
orderName: "테스트 결제",
totalAmount: 100,
paymentId,
orderName: "나이키 멘즈 조이라이드 플라이니트",
totalAmount: 1000,
currency: "CURRENCY_KRW",
channelKey: "channel-key-e6c31df1-5559-4b4a-9b2c-a35793d14db2",
payMethod: "VIRTUAL_ACCOUNT",
Expand All @@ -111,6 +115,198 @@ export function Preview() {
redirectUrl: "https://sdk-playground.portone.io/",
}) satisfies PortOne.PaymentRequest,
)
.with(
{ pg: { name: "smartro", payMethods: "card" } },
() =>
({
storeId: "store-e4038486-8d83-41a5-acf1-844a009e0d94",
paymentId,
orderName: "나이키 멘즈 조이라이드 플라이니트",
totalAmount: 1000,
currency: "CURRENCY_KRW",
channelKey: "channel-key-c4a4b281-a1e5-40c9-8140-f055262bcefd",
payMethod: "CARD",
card: {},
redirectUrl: "https://sdk-playground.portone.io/",
customer: {
phoneNumber: "01012341234",
},
}) satisfies PortOne.PaymentRequest,
)
.with(
{ pg: { name: "smartro", payMethods: "virtualAccount" } },
() =>
({
storeId: "store-e4038486-8d83-41a5-acf1-844a009e0d94",
paymentId,
orderName: "나이키 멘즈 조이라이드 플라이니트",
totalAmount: 1000,
currency: "CURRENCY_KRW",
channelKey: "channel-key-c4a4b281-a1e5-40c9-8140-f055262bcefd",
payMethod: "VIRTUAL_ACCOUNT",
virtualAccount: {
accountExpiry: {
validHours: 1,
},
},
customer: {
phoneNumber: "01012341234",
},
redirectUrl: "https://sdk-playground.portone.io/",
}) satisfies PortOne.PaymentRequest,
)
.with(
{ pg: { name: "inicis", payMethods: "card" } },
() =>
({
storeId: "store-e4038486-8d83-41a5-acf1-844a009e0d94",
paymentId,
orderName: "나이키 멘즈 조이라이드 플라이니트",
totalAmount: 1000,
currency: "CURRENCY_KRW",
channelKey: "channel-key-fc5f33bb-c51e-4ac7-a0df-4dc40330046d",
payMethod: "CARD",
card: {},
customer: {
fullName: "포트원",
email: "[email protected]",
phoneNumber: "01012341234",
},
redirectUrl: "https://sdk-playground.portone.io/",
}) satisfies PortOne.PaymentRequest,
)
.with(
{ pg: { name: "inicis", payMethods: "virtualAccount" } },
() =>
({
storeId: "store-e4038486-8d83-41a5-acf1-844a009e0d94",
paymentId,
orderName: "나이키 멘즈 조이라이드 플라이니트",
totalAmount: 1000,
currency: "CURRENCY_KRW",
channelKey: "channel-key-fc5f33bb-c51e-4ac7-a0df-4dc40330046d",
payMethod: "VIRTUAL_ACCOUNT",
virtualAccount: {
accountExpiry: {
validHours: 1,
},
},
customer: {
fullName: "포트원",
email: "[email protected]",
phoneNumber: "01012341234",
},
redirectUrl: "https://sdk-playground.portone.io/",
}) satisfies PortOne.PaymentRequest,
)
.with(
{ pg: { name: "kcp", payMethods: "card" } },
() =>
({
storeId: "store-e4038486-8d83-41a5-acf1-844a009e0d94",
paymentId,
orderName: "나이키 멘즈 조이라이드 플라이니트",
totalAmount: 1000,
currency: "CURRENCY_KRW",
channelKey: "channel-key-a79920e0-a898-49f0-aab7-50aa6834848f",
payMethod: "CARD",
card: {},
redirectUrl: "https://sdk-playground.portone.io/",
}) satisfies PortOne.PaymentRequest,
)
.with(
{ pg: { name: "kcp", payMethods: "virtualAccount" } },
() =>
({
storeId: "store-e4038486-8d83-41a5-acf1-844a009e0d94",
paymentId,
orderName: "나이키 멘즈 조이라이드 플라이니트",
totalAmount: 1000,
currency: "CURRENCY_KRW",
channelKey: "channel-key-a79920e0-a898-49f0-aab7-50aa6834848f",
payMethod: "VIRTUAL_ACCOUNT",
virtualAccount: {
accountExpiry: {
validHours: 1,
},
},
redirectUrl: "https://sdk-playground.portone.io/",
}) satisfies PortOne.PaymentRequest,
)
.with(
{ pg: { name: "kpn", payMethods: "card" } },
() =>
({
storeId: "store-e4038486-8d83-41a5-acf1-844a009e0d94",
paymentId,
orderName: "나이키 멘즈 조이라이드 플라이니트",
totalAmount: 1000,
currency: "CURRENCY_KRW",
channelKey: "channel-key-bcbb1622-ff80-49d5-adef-49191fda8ede",
payMethod: "CARD",
card: {},
redirectUrl: "https://sdk-playground.portone.io/",
}) satisfies PortOne.PaymentRequest,
)
.with(
{ pg: { name: "kpn", payMethods: "virtualAccount" } },
() =>
({
storeId: "store-e4038486-8d83-41a5-acf1-844a009e0d94",
paymentId,
orderName: "나이키 멘즈 조이라이드 플라이니트",
totalAmount: 1000,
currency: "CURRENCY_KRW",
channelKey: "channel-key-bcbb1622-ff80-49d5-adef-49191fda8ede",
payMethod: "VIRTUAL_ACCOUNT",
virtualAccount: {
accountExpiry: {
validHours: 1,
},
},
redirectUrl: "https://sdk-playground.portone.io/",
}) satisfies PortOne.PaymentRequest,
)
.with(
{ pg: { name: "ksnet", payMethods: "card" } },
() =>
({
storeId: "store-e4038486-8d83-41a5-acf1-844a009e0d94",
paymentId,
orderName: "나이키 멘즈 조이라이드 플라이니트",
totalAmount: 1000,
currency: "CURRENCY_KRW",
channelKey: "channel-key-4a5daa34-aecb-44af-aaad-e42384acfb6e",
payMethod: "CARD",
card: {},
customer: {
fullName: "포트원",
},
redirectUrl: "https://sdk-playground.portone.io/",
}) satisfies PortOne.PaymentRequest,
)
.with(
{ pg: { name: "ksnet", payMethods: "virtualAccount" } },
() =>
({
storeId: "store-e4038486-8d83-41a5-acf1-844a009e0d94",
paymentId,
orderName: "나이키 멘즈 조이라이드 플라이니트",
totalAmount: 1000,
currency: "CURRENCY_KRW",
channelKey: "channel-key-4a5daa34-aecb-44af-aaad-e42384acfb6e",
payMethod: "VIRTUAL_ACCOUNT",
virtualAccount: {
accountExpiry: {
validHours: 1,
},
},
customer: {
fullName: "포트원",
},
redirectUrl: "https://sdk-playground.portone.io/",
}) satisfies PortOne.PaymentRequest,
)
.exhaustive();

setPaymentStatus({ status: "PENDING" });
Expand All @@ -123,7 +319,7 @@ export function Preview() {
.with({ code: P.nonNullable }, () => {
setPaymentStatus({ status: "FAILED" });
})
.with({ code: P.nullish }, () => {
.with(P.not({ code: P.nonNullable }), () => {
setPaymentStatus({ status: "PAID" });
})
.exhaustive(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ ${({ section }) => section("server:import-portone-sdk")`
const PortOne = require("@portone/server-sdk")
`}
const portOne = PortOne.PortOneClient(process.env.V2_API_SECRET)
const portone = PortOne.PortOneClient(process.env.V2_API_SECRET)
${({ section }) => section("server:complete-payment:verify-payment")`
function verifyPayment(payment) {
if (payment.customData == null) return false
const customData = JSON.parse(payment.customData)
Expand All @@ -25,6 +26,7 @@ function verifyPayment(payment) {
payment.currency === item.currency
)
}
`}
const paymentStore = new Map()
async function syncPayment(paymentId) {
Expand All @@ -34,24 +36,28 @@ async function syncPayment(paymentId) {
})
}
const payment = paymentStore.get(paymentId)
${({ section }) => section("server:complete-payment:get-payment")`
let actualPayment
try {
actualPayment = await portOne.payment.getPayment(paymentId)
actualPayment = await portone.payment.getPayment(paymentId)
} catch (e) {
if (e instanceof PortOne.Errors.PortOneError) return false
throw e
}
if (actualPayment == null) return false
`}
switch (actualPayment.status) {
case "PAID":
if (!verifyPayment(actualPayment)) return false
if (payment.status === "PAID") return payment
payment.status = "PAID"
console.info("결제 성공", actualPayment)
break
${({ when }) => when(({ pg }) => pg.payMethods === "virtualAccount")`
case "VIRTUAL_ACCOUNT_ISSUED":
payment.status = "VIRTUAL_ACCOUNT_ISSUED"
break
`}
default:
return false
}
Expand All @@ -60,12 +66,14 @@ async function syncPayment(paymentId) {
const app = express()
${({ section }) => section("server:webhook:raw-body")`
app.use(
"/api/payment/webhook",
bodyParser.text({
type: "application/json",
}),
)
`}
app.use(bodyParser.json())
const items = new Map([
Expand All @@ -87,6 +95,7 @@ app.get("/api/item", (req, res) => {
})
})
${({ section }) => section("server:complete-payment")`
app.post("/api/payment/complete", async (req, res, next) => {
try {
const { paymentId } = req.body
Expand All @@ -101,11 +110,15 @@ app.post("/api/payment/complete", async (req, res, next) => {
next(e)
}
})
`}
${({ section }) => section("server:webhook")`
app.post("/api/payment/webhook", async (req, res, next) => {
try {
${({ section }) => section("server:webhook:verify")`
let webhook
try {
await PortOne.Webhook.verify(
webhook = await PortOne.Webhook.verify(
process.env.V2_WEBHOOK_SECRET,
req.body,
req.headers,
Expand All @@ -115,16 +128,17 @@ app.post("/api/payment/webhook", async (req, res, next) => {
return res.status(400).end()
throw e
}
const {
type,
data: { paymentId },
} = JSON.parse(req.body)
if (type.startsWith("Transaction.")) await syncPayment(paymentId)
`}
${({ section }) => section("server:webhook:complete-payment")`
if ('data' in webhook && 'paymentId' in webhook.data)
await syncPayment(webhook.data.paymentId)
`}
res.status(200).end()
} catch (e) {
next(e)
}
})
`}
const server = app.listen(8080, "localhost", () => {
console.log("server is running on", server.address())
Expand Down
Loading

0 comments on commit 518ca8e

Please sign in to comment.