@uappi/public-sdk 1.4.3 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
import { ref, computed, onMounted, onUnmounted } from 'vue';
|
|
3
3
|
|
|
4
4
|
import api from '../../modules/Api';
|
|
5
|
+
import { purchaseAction } from '../../modules/Tracking/events/actions/purchase';
|
|
5
6
|
|
|
6
7
|
import type { Props, Emits, PaymentSuccessData, PaymentRedirectData } from './types';
|
|
8
|
+
import type { CartResponse } from '../../modules/Api/types';
|
|
7
9
|
|
|
8
10
|
// `PaymentHosted` renderiza o formulário de pagamento via iframe isolado,
|
|
9
11
|
// servido pelo serviço `render.uappi`. Todos os dados sensíveis (número do cartão,
|
|
@@ -19,6 +21,13 @@ const loadingToken = ref(true);
|
|
|
19
21
|
const tokenError = ref<string | null>(null);
|
|
20
22
|
const iframeHeight = ref<number | null>(null);
|
|
21
23
|
|
|
24
|
+
// Snapshot do carrinho anterior à compra, usado apenas para disparar o evento
|
|
25
|
+
// `purchase` do GTM quando o `UAPPI_PAYMENT_SUCCESS` chegar. É necessário buscar
|
|
26
|
+
// esse dado antes do pagamento porque, após o sucesso, o carrinho já foi
|
|
27
|
+
// consumido pelo `/cart/buy` (chamado internamente pelo iframe) e não reflete
|
|
28
|
+
// mais os itens/frete/descontos da compra que acabou de ser feita.
|
|
29
|
+
const cartSnapshot = ref<CartResponse | null>(null);
|
|
30
|
+
|
|
22
31
|
const iframeSrc = computed(() => {
|
|
23
32
|
if (!paymentToken.value) return '';
|
|
24
33
|
|
|
@@ -59,6 +68,14 @@ async function fetchPaymentToken(): Promise<void> {
|
|
|
59
68
|
}
|
|
60
69
|
}
|
|
61
70
|
|
|
71
|
+
async function fetchCartSnapshot(): Promise<void> {
|
|
72
|
+
try {
|
|
73
|
+
cartSnapshot.value = await api.cart.listDetail();
|
|
74
|
+
} catch (err) {
|
|
75
|
+
console.error('[PaymentHosted] Erro ao buscar carrinho para tracking de compra:', err);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
62
79
|
/**
|
|
63
80
|
* Só processa mensagens cuja origem seja a própria janela do iframe renderizado
|
|
64
81
|
* por este componente, evitando que outra janela/frame na página finja ser o
|
|
@@ -82,6 +99,11 @@ function handleIframeMessage(event: MessageEvent): void {
|
|
|
82
99
|
break;
|
|
83
100
|
|
|
84
101
|
case 'UAPPI_PAYMENT_SUCCESS':
|
|
102
|
+
if (cartSnapshot.value) {
|
|
103
|
+
purchaseAction(props.option.paymentType.code, cartSnapshot.value);
|
|
104
|
+
} else {
|
|
105
|
+
console.error('[PaymentHosted] Carrinho indisponível: evento de tracking de compra (purchase) não foi disparado.');
|
|
106
|
+
}
|
|
85
107
|
emit('payment-success', data);
|
|
86
108
|
break;
|
|
87
109
|
|
|
@@ -101,7 +123,7 @@ function handleIframeMessage(event: MessageEvent): void {
|
|
|
101
123
|
|
|
102
124
|
onMounted(async () => {
|
|
103
125
|
window.addEventListener('message', handleIframeMessage);
|
|
104
|
-
await fetchPaymentToken();
|
|
126
|
+
await Promise.all([fetchPaymentToken(), fetchCartSnapshot()]);
|
|
105
127
|
});
|
|
106
128
|
|
|
107
129
|
onUnmounted(() => {
|