Last active
January 14, 2023 21:31
-
-
Save AlexHenkel/01e9bc5b0480c273f01815aef9a46776 to your computer and use it in GitHub Desktop.
Render props component to use Conekta with Expo in React-native
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
import React, { Component } from 'react'; | |
import PropTypes from 'prop-types'; | |
import { Constants } from 'expo'; | |
import { WebView } from 'react-native'; | |
import { CONEKTA_KEY } from 'react-native-dotenv'; | |
class Conekta extends Component { | |
deviceId = Constants.deviceId.replace(/-/g, ''); | |
tokenizeCard = card => | |
new Promise(async (resolve, reject) => { | |
try { | |
const tokenRes = await fetch('https://api.conekta.io/tokens', { | |
method: 'POST', | |
body: JSON.stringify({ card }), | |
headers: { | |
Accept: 'application/vnd.conekta-v0.3.0+json', | |
'Content-Type': 'application/json', | |
Authorization: `Basic ${window.btoa(`${CONEKTA_KEY}:`)}`, | |
'Accept-Language': 'en', // TODO: Change this to get language from translation | |
'Conekta-Client-User-Agent': JSON.stringify({ | |
agent: 'Conekta iOS SDK', | |
}), | |
}, | |
}); | |
const token = await tokenRes.json(); | |
resolve(token); | |
} catch (error) { | |
reject(error); | |
} | |
}); | |
render() { | |
const html = ` | |
<html style="background: blue;"> | |
<head></head> | |
<body> | |
<script type="text/javascript" src="https://conektaapi.s3.amazonaws.com/v0.5.0/js/conekta.js" data-conekta-public-key="${CONEKTA_KEY}" data-conekta-session-id="${ | |
this.deviceId | |
}"></script> | |
</body> | |
</html> | |
`; | |
const { children } = this.props; | |
children({ tokenizeCard: this.tokenizeCard }); | |
return ( | |
<WebView | |
source={{ html, baseUrl: 'web/' }} | |
style={{ height: 0, width: 0 }} | |
mixedContentMode="always" | |
/> | |
); | |
} | |
} | |
Conekta.propTypes = { children: PropTypes.func.isRequired }; | |
export default Conekta; |
Hola Alex!, sabes si el endpoint https://api.conekta.io//tokens sigue funcionando o solo es posible utilizar ahora Conekta.js?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hola amigo tienes algun ejemplo de como implementarlo? soy nuevo en esto