import Env from 'react-native-config';
import React, { useState } from 'react';
import { StyleSheet, View, Text, TouchableOpacity, Alert } from 'react-native';
import MercadoPagoCheckout from '@blackbox-vision/react-native-mercadopago-px';
import * as MercadoPagoService from './mercadopago-service';
import styles from './styles';
export default function App() {
const [paymentResult, setPaymentResult] = useState(null);
const startCheckout = async () => {
try {
const preferenceId = await MercadoPagoService.getPreferenceId('payer@email.com', {
title: 'Dummy Item Title',
description: 'Dummy Item Description',
quantity: 1,
currency_id: 'ARS',
unit_price: 10.0,
});
const payment = await MercadoPagoCheckout.createPayment({
publicKey: Env.MP_PUBLIC_KEY,
preferenceId,
});
setPaymentResult(payment);
} catch (err) {
Alert.alert('Something went wrong', err.message);
}
};
return (
<View style={styles.container}>
<TouchableOpacity onPress={startCheckout}>
<Text style={styles.text}>Start Payment</Text>
</TouchableOpacity>
<Text style={styles.text}>Payment: {JSON.stringify(paymentResult)}</Text>
</View>
);
}