import { usePaymentStore } from '#lib/stores/payment' import { useCancelPromotion, usePromotion } from '#lib/composables/promotion' import type { FastPayInfoResponse, GetFastPayRequest, GetFastPayTransferResponse, PriorityBankMethod, QRCodeRequest, } from '#lib/types' import { valueToNumber } from '#lib/utils' import { computed, ref } from 'vue' import { useDepositData } from '#lib/composables/deposit/use-deposit-data' import { useNadal } from '#lib/composables' /** * Provides methods and state related to FastPay, including fetching data and handling FastPay transactions. * @returns {Object} An object containing state and methods for managing FastPay functionality. * @namespace */ export function useFastPay() { const store = usePaymentStore() const { payment } = useNadal() const { isUsingPromotion, promotionPackages, fetchPromotionPackages } = usePromotion() const { openModalCancelPromotion } = useCancelPromotion() const { formatBankList, fetchFastpays } = useDepositData() const isLoading = ref(false) /** * Computes and returns a formatted list of FastPay banks. * @type {ComputedRef} */ const fastpayBanks = computed((): PriorityBankMethod[] => { return formatBankList(store.depositFastPays) }) /** * Computes and returns a list of FastPay banks with additional metadata. * @type {ComputedRef} */ const depositFastPays = computed((): PriorityBankMethod[] => { return fastpayBanks.value.map((item: PriorityBankMethod) => ({ ...item, value: item.bank_code ?? '', label: item.bankName ?? '', })) }) /** * Computes and returns the FastPay code from the store. * @type {ComputedRef} */ const fastpayCode = computed(() => { return store.fastpayCode }) /** * Requests a QR code for FastPay transactions. * @async * @param {QRCodeRequest} data - The request data for generating the QR code. * @returns {Promise} A promise that resolves to the QR code image URL. */ const fetchCodePayQR = async (data: QRCodeRequest): Promise => { return await payment.codePayQrcode(data) } /** * Retrieves transfer information for a FastPay transaction. * @async * @param {GetFastPayRequest} payload - The request payload for retrieving FastPay transfer information. * @returns {Promise} A promise that resolves to the transfer information and QR code, or undefined if the user is on a welcome type. */ const fetchFastPayTransferInfo = async ( payload: GetFastPayRequest, ): Promise => { if (isUsingPromotion.value) { openModalCancelPromotion() return } const amount = valueToNumber(payload.amount.toString(), 1000) const data = await payment.requestNicepayInvoice< GetFastPayRequest, FastPayInfoResponse >({ ...payload, amount, }) if (data) { const { bank_code, bank_account_no, content } = data const qrImage = await fetchCodePayQR({ bank_code, bank_no: bank_account_no, amount, memo: content, }) return { ...data, qrImage, amount, } as GetFastPayTransferResponse } } /** * Fetches FastPay data and promotion packages. * @async * @returns {Promise} A promise that resolves when the data has been fetched. */ const fetchData = async (): Promise => { await Promise.all([fetchFastpays(), fetchPromotionPackages()]) } return { isLoading, promotionPackages, fastpayCode, depositFastPays, fetchData, fetchFastPayTransferInfo, } }