SchedulePaymentPixDialog.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <template>
  2. <q-dialog ref="dialogRef" persistent maximized transition-show="slide-up" transition-hide="slide-down" @hide="onDialogHide">
  3. <div class="bg-page full-height column">
  4. <div class="row items-center q-px-md q-pt-md q-pb-sm bg-surface shadow-header">
  5. <q-btn class="header-back-btn" icon="mdi-chevron-left" flat round dense color="primary" @click="onDialogCancel" />
  6. <q-space />
  7. <span class="font16 fontbold gradient-diarista">
  8. {{ $t('payment.pix_title') }}
  9. </span>
  10. <q-space />
  11. <div style="width: 32px" />
  12. </div>
  13. <div v-if="success" class="col column items-center justify-center q-px-xl">
  14. <q-btn
  15. flat round icon="mdi-close" color="grey-5"
  16. class="self-end q-mb-md"
  17. @click="onDialogOK"
  18. />
  19. <div class="success-icon-wrapper q-mb-lg">
  20. <q-icon name="mdi-check-circle" size="100px" color="primary" />
  21. </div>
  22. <div class="success-title text-primary text-center q-mb-sm">
  23. {{ $t('payment.success_title') }}
  24. </div>
  25. <i18n-t keypath="payment.success_message" tag="div" class="success-message text-grey-6 text-center">
  26. <template #nextServices>
  27. <strong class="text-text">{{ $t('payment.success_next_services') }}</strong>
  28. </template>
  29. <template #agenda>
  30. <strong class="text-text">{{ $t('payment.success_agenda') }}</strong>
  31. </template>
  32. </i18n-t>
  33. </div>
  34. <div v-else-if="processing" class="col column items-center justify-center q-px-xl">
  35. <q-spinner-oval color="primary" size="72px" class="q-mb-lg" />
  36. <div class="processing-title text-primary text-center q-mb-sm">
  37. {{ $t('payment.processing_title') }}
  38. </div>
  39. <div class="processing-message text-grey-6 text-center">
  40. {{ $t('payment.processing_message') }}
  41. </div>
  42. </div>
  43. <div v-else class="pix-payment-content col scroll q-px-lg q-pt-lg q-pb-xl column">
  44. <div class="row items-center justify-between q-mb-sm">
  45. <span class="pix-label font14 fontbold">{{ $t('payment.pix_total') }}</span>
  46. <span class="text-primary font14 fontbold">{{ formatCurrency(total) }}</span>
  47. </div>
  48. <q-separator />
  49. <div class="row items-center justify-between q-mt-sm q-mb-lg">
  50. <span class="pix-label font14 fontbold">{{ $t('payment.pix_expires') }}</span>
  51. <span class="text-primary font14 fontbold">{{ countdown }}</span>
  52. </div>
  53. <div class="flex flex-center q-mb-md">
  54. <q-icon name="mdi-cash-fast" size="48px" color="teal" />
  55. </div>
  56. <div class="flex flex-center q-mb-md">
  57. <q-img
  58. v-if="pixQrCodeUrl"
  59. :src="pixQrCodeUrl"
  60. width="180px"
  61. height="180px"
  62. fit="contain"
  63. class="qrcode-wrapper"
  64. />
  65. <div v-else class="qrcode-wrapper column items-center justify-center">
  66. <q-icon name="mdi-qrcode" size="80px" color="grey-6" />
  67. </div>
  68. </div>
  69. <div class="pix-code-text q-mb-md">{{ pixCode || 'Código Pix indisponível.' }}</div>
  70. <q-btn
  71. unelevated
  72. rounded
  73. no-caps
  74. color="primary"
  75. padding="4px 12px"
  76. class="full-width q-mb-lg"
  77. :label="$t('payment.pix_copy_btn')"
  78. :loading="processing"
  79. @click="copyCode"
  80. />
  81. <i18n-t keypath="payment.pix_instructions" tag="p" class="pix-instructions-text font14 q-mb-sm">
  82. <template #copyCode>
  83. <span class="fontbold">{{ $t('payment.pix_copy_code') }}</span>
  84. </template>
  85. <template #pasteCode>
  86. <span class="fontbold">{{ $t('payment.pix_paste_code') }}</span>
  87. </template>
  88. <template #finalize>
  89. <span class="fontbold">{{ $t('payment.pix_finalize') }}</span>
  90. </template>
  91. </i18n-t>
  92. <p class="pix-instructions-text font14">{{ $t('payment.pix_email_note') }}</p>
  93. </div>
  94. </div>
  95. </q-dialog>
  96. </template>
  97. <script setup>
  98. import { computed, ref, onMounted, onUnmounted } from 'vue'
  99. import { useDialogPluginComponent, useQuasar, copyToClipboard } from 'quasar'
  100. import { formatCurrency } from 'src/helpers/utils'
  101. import { getServicePackagePix, payServicePackage, getScheduleProposalPix, payScheduleProposal } from 'src/api/payment'
  102. import { usePaymentStore } from 'src/stores/payment'
  103. const props = defineProps({
  104. servicePackage: { type: Object, required: true },
  105. total: { type: Number, required: true },
  106. targetType: { type: String, default: 'service_package' },
  107. })
  108. defineEmits([...useDialogPluginComponent.emits])
  109. const { dialogRef, onDialogHide, onDialogOK, onDialogCancel } = useDialogPluginComponent()
  110. const $q = useQuasar()
  111. const paymentStore = usePaymentStore()
  112. const pay = props.targetType === 'schedule_proposal' ? payScheduleProposal : payServicePackage
  113. const getPix = props.targetType === 'schedule_proposal' ? getScheduleProposalPix : getServicePackagePix
  114. const item = computed(() => props.servicePackage)
  115. const itemId = computed(() => item.value.id)
  116. const cacheKey = computed(() => `${props.targetType}:${itemId.value}`)
  117. const payment = ref(null)
  118. const success = ref(false)
  119. const processing = ref(true)
  120. const pixData = computed(() => payment.value?.pix ?? {})
  121. const pixCode = computed(() => pixData.value?.qr_code ?? '')
  122. const pixQrCodeUrl = computed(() => pixData.value?.qr_code_url ?? '')
  123. const pixExpiresAt = computed(() => pixData.value?.expires_at ?? payment.value?.expires_at ?? null)
  124. const copyCode = async () => {
  125. try {
  126. if (!pixCode.value) {
  127. $q.notify({ type: 'negative', message: 'Código Pix indisponível.' })
  128. return
  129. }
  130. await copyToClipboard(pixCode.value)
  131. $q.notify({ type: 'positive', message: 'Código copiado!' })
  132. } catch {
  133. $q.notify({ type: 'negative', message: 'Erro ao copiar.' })
  134. return
  135. }
  136. }
  137. const totalSeconds = ref(20 * 60)
  138. const countdown = ref('')
  139. let countdownTimer = null
  140. let pollingTimer = null
  141. let pollingInFlight = false
  142. const stopPolling = () => {
  143. clearInterval(pollingTimer)
  144. pollingTimer = null
  145. }
  146. const applyPaymentStatus = (nextPayment) => {
  147. payment.value = nextPayment
  148. if (nextPayment.status === 'paid') {
  149. processing.value = false
  150. $q.notify({
  151. type: 'positive',
  152. message: 'Pagamento confirmado!',
  153. position: 'top',
  154. })
  155. paymentStore.clearPixPaymentForServicePackage(cacheKey.value)
  156. stopPolling()
  157. onDialogOK()
  158. return
  159. }
  160. if (['failed', 'cancelled'].includes(nextPayment.status)) {
  161. processing.value = false
  162. paymentStore.clearPixPaymentForServicePackage(cacheKey.value)
  163. stopPolling()
  164. $q.notify({ type: 'negative', message: nextPayment.failure_message || 'Pagamento Pix não confirmado.' })
  165. onDialogCancel()
  166. return
  167. }
  168. paymentStore.setPixPaymentForServicePackage(cacheKey.value, nextPayment)
  169. }
  170. const checkPaymentStatus = async () => {
  171. if (pollingInFlight || success.value || totalSeconds.value <= 0) return
  172. pollingInFlight = true
  173. try {
  174. const result = await getPix(itemId.value)
  175. applyPaymentStatus(result)
  176. updateCountdown()
  177. } catch (e) {
  178. console.error('Erro ao verificar pagamento Pix:', e)
  179. } finally {
  180. pollingInFlight = false
  181. }
  182. }
  183. const startPolling = () => {
  184. stopPolling()
  185. if (success.value || ['failed', 'cancelled'].includes(payment.value?.status) || totalSeconds.value <= 0) return
  186. checkPaymentStatus()
  187. pollingTimer = setInterval(checkPaymentStatus, 5000)
  188. }
  189. const updateCountdown = () => {
  190. if (pixExpiresAt.value) {
  191. totalSeconds.value = Math.max(0, Math.floor((new Date(pixExpiresAt.value).getTime() - Date.now()) / 1000))
  192. }
  193. const m = Math.floor(totalSeconds.value / 60)
  194. const s = totalSeconds.value % 60
  195. countdown.value = `${m} min, ${String(s).padStart(2, '0')} seg`
  196. if (!pixExpiresAt.value && totalSeconds.value > 0) totalSeconds.value--
  197. if (pixExpiresAt.value && totalSeconds.value <= 0) {
  198. paymentStore.clearPixPaymentForServicePackage(cacheKey.value)
  199. stopPolling()
  200. }
  201. }
  202. onMounted(async () => {
  203. updateCountdown()
  204. countdownTimer = setInterval(updateCountdown, 1000)
  205. try {
  206. const cachedPayment = paymentStore.getValidPixPaymentForServicePackage(cacheKey.value)
  207. if (cachedPayment) {
  208. applyPaymentStatus(cachedPayment)
  209. updateCountdown()
  210. startPolling()
  211. return
  212. }
  213. const paymentResult = await pay(itemId.value, { payment_method: 'pix' })
  214. applyPaymentStatus(paymentResult)
  215. updateCountdown()
  216. startPolling()
  217. } catch (e) {
  218. console.error('Erro ao criar pagamento Pix:', e)
  219. if (e.response?.data?.error !== 'missing_payment_data') {
  220. $q.notify({ type: 'negative', message: e?.response?.data?.message ?? 'Erro ao criar pagamento Pix.' })
  221. }
  222. onDialogCancel()
  223. } finally {
  224. processing.value = false
  225. }
  226. })
  227. onUnmounted(() => {
  228. clearInterval(countdownTimer)
  229. stopPolling()
  230. })
  231. </script>
  232. <style scoped lang="scss">
  233. .shadow-header {
  234. box-shadow: 0px 1px 8px rgba(0, 0, 0, 0.1);
  235. }
  236. .pix-label {
  237. color: #3a3a4a;
  238. }
  239. .qrcode-wrapper {
  240. background: #fff;
  241. border: 1px solid #e0e0e0;
  242. border-radius: 12px;
  243. padding: 12px;
  244. }
  245. .pix-code-text {
  246. color: #5a5a6a;
  247. text-align: center;
  248. word-break: break-all;
  249. line-height: 1.5;
  250. background: #f5f5f8;
  251. border-radius: 8px;
  252. padding: 10px;
  253. }
  254. .pix-instructions-text {
  255. align-self: stretch;
  256. line-height: 1.5;
  257. color: #3a3a4a;
  258. margin-left: 0;
  259. margin-right: 0;
  260. max-width: 100%;
  261. overflow-wrap: anywhere;
  262. text-align: left;
  263. width: 100%;
  264. }
  265. .success-icon-wrapper {
  266. display: flex;
  267. align-items: center;
  268. justify-content: center;
  269. }
  270. .success-title {
  271. line-height: 1.3;
  272. max-width: 280px;
  273. }
  274. .success-message {
  275. line-height: 1.6;
  276. max-width: 280px;
  277. }
  278. </style>