SchedulePaymentPixDialog.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 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 { getSchedulePixPayment, paySchedule } from 'src/api/payment'
  102. import { usePaymentStore } from 'src/stores/payment'
  103. const props = defineProps({
  104. schedule: { type: Object, required: true },
  105. total: { type: Number, required: true },
  106. })
  107. defineEmits([...useDialogPluginComponent.emits])
  108. const { dialogRef, onDialogHide, onDialogOK, onDialogCancel } = useDialogPluginComponent()
  109. const $q = useQuasar()
  110. const paymentStore = usePaymentStore()
  111. const payment = ref(null)
  112. const success = ref(false)
  113. const processing = ref(true)
  114. const pixData = computed(() => payment.value?.pix ?? {})
  115. const pixCode = computed(() => pixData.value?.qr_code ?? '')
  116. const pixQrCodeUrl = computed(() => pixData.value?.qr_code_url ?? '')
  117. const pixExpiresAt = computed(() => pixData.value?.expires_at ?? payment.value?.expires_at ?? null)
  118. const copyCode = async () => {
  119. try {
  120. if (!pixCode.value) {
  121. $q.notify({ type: 'negative', message: 'Código Pix indisponível.' })
  122. return
  123. }
  124. await copyToClipboard(pixCode.value)
  125. $q.notify({ type: 'positive', message: 'Código copiado!' })
  126. } catch {
  127. $q.notify({ type: 'negative', message: 'Erro ao copiar.' })
  128. return
  129. }
  130. }
  131. const totalSeconds = ref(20 * 60)
  132. const countdown = ref('')
  133. let countdownTimer = null
  134. let pollingTimer = null
  135. let pollingInFlight = false
  136. const stopPolling = () => {
  137. clearInterval(pollingTimer)
  138. pollingTimer = null
  139. }
  140. const applyPaymentStatus = (nextPayment) => {
  141. if (!nextPayment) return
  142. payment.value = nextPayment
  143. if (nextPayment.status === 'paid') {
  144. success.value = true
  145. processing.value = false
  146. paymentStore.clearPixPayment(props.schedule.id)
  147. stopPolling()
  148. return
  149. }
  150. if (['failed', 'cancelled'].includes(nextPayment.status)) {
  151. processing.value = false
  152. paymentStore.clearPixPayment(props.schedule.id)
  153. stopPolling()
  154. $q.notify({ type: 'negative', message: nextPayment.failure_message || 'Pagamento Pix não confirmado.' })
  155. onDialogCancel()
  156. return
  157. }
  158. paymentStore.setPixPayment(props.schedule.id, nextPayment)
  159. }
  160. const checkPaymentStatus = async () => {
  161. if (pollingInFlight || success.value || totalSeconds.value <= 0) return
  162. pollingInFlight = true
  163. try {
  164. applyPaymentStatus(await getSchedulePixPayment(props.schedule.id))
  165. updateCountdown()
  166. } catch (e) {
  167. console.error('Erro ao verificar pagamento Pix:', e)
  168. } finally {
  169. pollingInFlight = false
  170. }
  171. }
  172. const startPolling = () => {
  173. stopPolling()
  174. if (success.value || ['failed', 'cancelled'].includes(payment.value?.status) || totalSeconds.value <= 0) return
  175. checkPaymentStatus()
  176. pollingTimer = setInterval(checkPaymentStatus, 5000)
  177. }
  178. const updateCountdown = () => {
  179. if (pixExpiresAt.value) {
  180. totalSeconds.value = Math.max(0, Math.floor((new Date(pixExpiresAt.value).getTime() - Date.now()) / 1000))
  181. }
  182. const m = Math.floor(totalSeconds.value / 60)
  183. const s = totalSeconds.value % 60
  184. countdown.value = `${m} min, ${String(s).padStart(2, '0')} seg`
  185. if (!pixExpiresAt.value && totalSeconds.value > 0) totalSeconds.value--
  186. if (pixExpiresAt.value && totalSeconds.value <= 0) {
  187. paymentStore.clearPixPayment(props.schedule.id)
  188. stopPolling()
  189. }
  190. }
  191. onMounted(async () => {
  192. updateCountdown()
  193. countdownTimer = setInterval(updateCountdown, 1000)
  194. try {
  195. const cachedPayment = paymentStore.getValidPixPayment(props.schedule.id)
  196. if (cachedPayment) {
  197. applyPaymentStatus(cachedPayment)
  198. updateCountdown()
  199. startPolling()
  200. return
  201. }
  202. applyPaymentStatus(await paySchedule(props.schedule.id, { payment_method: 'pix' }))
  203. updateCountdown()
  204. startPolling()
  205. } catch (e) {
  206. console.error('Erro ao criar pagamento Pix:', e)
  207. $q.notify({ type: 'negative', message: e?.response?.data?.message ?? 'Erro ao criar pagamento Pix.' })
  208. onDialogCancel()
  209. } finally {
  210. processing.value = false
  211. }
  212. })
  213. onUnmounted(() => {
  214. clearInterval(countdownTimer)
  215. stopPolling()
  216. })
  217. </script>
  218. <style scoped lang="scss">
  219. .shadow-header {
  220. box-shadow: 0px 1px 8px rgba(0, 0, 0, 0.1);
  221. }
  222. .pix-label {
  223. color: #3a3a4a;
  224. }
  225. .qrcode-wrapper {
  226. background: #fff;
  227. border: 1px solid #e0e0e0;
  228. border-radius: 12px;
  229. padding: 12px;
  230. }
  231. .pix-code-text {
  232. color: #5a5a6a;
  233. text-align: center;
  234. word-break: break-all;
  235. line-height: 1.5;
  236. background: #f5f5f8;
  237. border-radius: 8px;
  238. padding: 10px;
  239. }
  240. .pix-instructions-text {
  241. align-self: stretch;
  242. line-height: 1.5;
  243. color: #3a3a4a;
  244. margin-left: 0;
  245. margin-right: 0;
  246. max-width: 100%;
  247. overflow-wrap: anywhere;
  248. text-align: left;
  249. width: 100%;
  250. }
  251. .success-icon-wrapper {
  252. display: flex;
  253. align-items: center;
  254. justify-content: center;
  255. }
  256. .success-title {
  257. line-height: 1.3;
  258. max-width: 280px;
  259. }
  260. .success-message {
  261. line-height: 1.6;
  262. max-width: 280px;
  263. }
  264. </style>