Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fechamento de compra com iFrame
function handleEvents(event) {
switch(event) {
// ...
case 'sendPayments.vtex':
let checkoutPayments = event.data.arguments[0];
let transactionResponse = event.data.arguments[1];
let deviceFingerprint = event.data.arguments[2];
sendPayments(checkoutPayments, transactionResponse, deviceFingerprint);
break;
}
}
function sendPayments(checkoutPayments, transactionResponse, deviceFingerprint) {
// Recupero os dados do pagamento que estao dentro do iFrame
let iFramePayments = PaymentStore.get('payments');
let payments = map(checkoutPayments, (checkoutPayment) => {
if (checkoutPayment.originalPaymentIndex == null) {
return checkoutPayment;
}
let payment = iFramePayments.get(checkoutPayment.originalPaymentIndex);
let fields = payment.get('fields');
// Adiciono fingePrint caso o pagamento precise
if (deviceFingerprint) {
fields.deviceFingerprint = deviceFingerprint;
}
// Preencho o array de pagamentos com os dados sensiveis
// Todos dentro do objeto "fieds"
return {
...checkoutPayment,
groupName: paymentGroup,
fields: fields
};
});
// Monto a URL que o VTEX PCI receberá os pagamentos
let orderId = transactionResponse.gatewayCallbackTemplatePath.split('/')[3];
let tid = transactionResponse.id;
const transactionUrl = `https://${accountName}.vtexpayments.com.br/api/pub/transactions/${tid}/payments?orderId=${orderId}`;
request({
url: transactionUrl,
method: 'post',
data: payments
}).then(() => {
// Mando um evento do iFrame para o Checkout com a resposta
Checkout.paymentSuccess(transactionResponse.gatewayCallbackTemplatePath.replace('{messageCode}', 'Success'));
}).catch(() => {
Checkout.paymentFail(transactionResponse.gatewayCallbackTemplatePath.replace('{messageCode}', 'SystemInconsistency'));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.