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); | |
}; |
Hola!
El typescript me da problema sale "property readyState does not exist on type 'HTMLScriptElement'", expandiste el HtmlElement de alguna manera? gracias.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
//ES
La función
loadScript
sirve para cargar cualquier tipo de script, por lo tanto podría ser extraída de la funciónredirectToMercadoPago
. En este caso dejo la función adentro ya que me interesa documentarla en este gist.//EN
The function
loadScript
works for any script so it could be extracted fromredirectToMercadoPago
. In this case I left the function inside to be documented in this gist.