Last active
October 2, 2021 23:59
-
-
Save mattgle/c14ad7ccd6bea85ddd7e30512dfd0faa to your computer and use it in GitHub Desktop.
Integracion de Mercadopago CheckoutPro en ReactJS [Sin usar FunctionComponents o ClassComponents]
This file contains hidden or 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
export const redirectToMercadoPago = (transactionId: string) => { | |
const loadScript = (url: string, callback: () => void) => { | |
let script = document.createElement('script'); | |
script.type = 'text/javascript'; | |
if (script.readyState) { | |
script.onreadystatechange = () => { | |
if ( | |
script.readyState === 'loaded' || | |
script.readyState === 'complete' | |
) { | |
script.onreadystatechange = null; | |
callback(); | |
} | |
}; | |
} else { | |
script.onload = () => callback(); | |
} | |
script.src = url; | |
document.getElementsByTagName('head')[0].appendChild(script); | |
}; | |
const handleScriptLoad = () => { | |
const mp = new window.MercadoPago(process.env.REACT_APP_MERCADOPAGO_KEY, { | |
locale: 'es-AR' | |
}); | |
mp.checkout({ | |
preference: { | |
id: transactionId | |
}, | |
autoOpen: true | |
}); | |
}; | |
loadScript('https://sdk.mercadopago.com/js/v2', handleScriptLoad); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hola!
El typescript me da problema sale "property readyState does not exist on type 'HTMLScriptElement'", expandiste el HtmlElement de alguna manera? gracias.