import { usePaymentStore } from '#lib/stores/payment' import { useCancelPromotion, usePromotion } from '#lib/composables/promotion' import type { TPayInvoiceDataResponse, GetTPayRequest } from '#lib/types' import { valueToNumber } from '#lib/utils' import { computed, ref } from 'vue' import { useNadal } from '#lib/composables' import { useApiFetch } from '../service/use-api-fetch' import { DEPOSIT_WITHDRAW_API } from '#lib/configs/api' /** * Provides methods and state related to TPay, including fetching data and handling TPay transactions. * @returns {Object} An object containing state and methods for managing TPay functionality. * @namespace */ export function useTPay() { const { request } = useApiFetch() const store = usePaymentStore() const { payment } = useNadal() const { isUsingPromotion, promotionPackages, fetchPromotionPackages } = usePromotion() const { openModalCancelPromotion } = useCancelPromotion() const tpayInvoiceData = ref({}) /** * Computes and returns the TPay code from the store. * @type {ComputedRef} */ const tpayCode = computed(() => { return store.tpayCode }) /** * Resets the invoice data after expiration */ const resetInvoiceData = () => { return tpayInvoiceData.value = {} } /** * Retrieves transfer information for a TPay transaction. * @async * @param {GetTPayRequest} payload - The request payload for retrieving TPay transfer information. * @returns {Promise} A promise that resolves to the transfer information and QR code. */ const fetchTPayTransferInfo = async ( payload: GetTPayRequest, ): Promise => { if (isUsingPromotion.value) { openModalCancelPromotion() return } const amount = valueToNumber(payload.amount.toString(), 1000) const data = await payment.tpayInvoice< GetTPayRequest, TPayInvoiceDataResponse >({ ...payload, amount, }) store.setTpayCode(data.qr_code ?? '') tpayInvoiceData.value = data return data } /** * Checks if TPay is under maintenance * @async * @returns {Promise} A promise that resolves when the maintenance status has been checked. */ const checkTpayMaintenance = async () => { const { data } = await request( DEPOSIT_WITHDRAW_API.CHECK_TPAY_MAINTENANCE, ) store.setMaintainedTpayDeposit(data.is_maintain) } const isMaintainedTpay = computed(() => { return store.isMaintainedTpayDeposit }) return { tpayInvoiceData, fetchTPayTransferInfo, promotionPackages, tpayCode, fetchPromotionPackages, resetInvoiceData, checkTpayMaintenance, isMaintainedTpay, } }