interface MintingToTosspayments {
cardIdx: string;
orderIdx: string;
price: number;
orderName: string;
}
const useTossPaymentsWrappingFunctions = () => {
const { account } = useMetamask();
const profile = useCachedProfile(); // 고객명, 고객이메일
const router = useRouter();
const { locale, team } = router.query;
const { mutate: savePaymentOrderId } = useSavePaymentOrderId();
const requestPayment = useCallback(
async ({ orderIdx, cardIdx, price, orderName }: MintingToTosspayments) => {
const tossPayments = await loadTossPayments(process.env.TOSS_CLIENT_KEY as string);
const paymentOrderId = `orderIdx-${orderIdx}_${new Date().getTime()}`;
savePaymentOrderId({ paymentOrderId }); // 서버에 paymentOrderId 저장
try {
tossPayments.requestPayment('계좌이체', {
amount: price,
orderId: paymentOrderId,
orderName,
successUrl: `${window.location.origin}/${locale}/club/${team}/success?orderIdx=${orderIdx}&cardIdx=${cardIdx}`,
failUrl: `${window.location.origin}/${locale}/club/${team}/fail`,
customerName: account as string,
customerEmail: profile?.email,
cashReceipt: { type: '소득공제' },
});
} catch (error) {
if (error.code === 'USER_CANCEL') {
// 결제 고객이 결제창을 닫았을 때 에러 처리
...
} else if (error.code === 'INVALID_CARD_COMPANY') {
// 유효하지 않은 카드 코드에 대한 에러 처리
...
}
}
},
[account, profile?.email, locale, team, savePaymentOrderId],
);
const mintingToTossPayments = useCallback(
async ({ orderIdx, cardIdx, price, orderName }: MintingToTosspayments) => {
if (!account) {
return;
}
requestPayment({ orderIdx, cardIdx, price, orderName });
},
[account, requestPayment],
);
return { mintingToTossPayments };
};
export default useTossPaymentsWrappingFunctions;