Created
August 15, 2016 20:07
-
-
Save klzns/6dbfef1daab8168d75a380efa196c050 to your computer and use it in GitHub Desktop.
Fechamento de compra com iFrame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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