Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { StatusBar } from "expo-status-bar";
- import { StyleSheet, Text, View, TouchableOpacity } from "react-native";
- import { StripeProvider } from "@stripe/stripe-react-native";
- import React, { useEffect, useCallback } from "react";
- import { Linking } from "react-native";
- import { useStripe } from "@stripe/stripe-react-native";
- export default function App() {
- const { handleURLCallback, initPaymentSheet, presentPaymentSheet } =
- useStripe();
- const handleDeepLink = useCallback(
- async (url) => {
- if (url) {
- const stripeHandled = await handleURLCallback(url);
- if (stripeHandled) {
- // This was a Stripe URL - you can return or add extra handling here as you see fit
- } else {
- // This was NOT a Stripe URL – handle as you normally would
- }
- }
- },
- [handleURLCallback]
- );
- useEffect(() => {
- const getUrlAsync = async () => {
- const initialUrl = await Linking.getInitialURL();
- handleDeepLink(initialUrl);
- };
- getUrlAsync();
- const deepLinkListener = Linking.addEventListener("url", (event) => {
- handleDeepLink(event.url);
- });
- return () => deepLinkListener.remove();
- }, [handleDeepLink]);
- useEffect(() => {
- console.log("Roooooo");
- initializePaymentSheet();
- }, []);
- const initializePaymentSheet = async () => {
- console.log("Init sheet");
- const { error } = await initPaymentSheet({
- merchantDisplayName: "Example, Inc.",
- returnURL: "duup://stripe-redirect",
- intentConfiguration: {
- mode: {
- amount: 1099,
- currencyCode: "USD",
- },
- confirmHandler: confirmHandler,
- },
- });
- if (error) {
- // handle error
- }
- };
- const confirmHandler = async (
- paymentMethod,
- shouldSavePaymentMethod,
- intentCreationCallback
- ) => {
- // Make a request to your own server, passing paymentMethod.id and shouldSavePaymentMethod.
- async function fetchData() {
- try {
- const response = await fetch(
- "https://api.stripe.com/v1/payment_intents",
- {
- method: "POST",
- headers: {
- Authorization:
- "Bearer sk_test_51GrpvvJgUVsS8tQcO240WHBetBXusnWh7YI9ZGGSh4IfDRP90njfiVEnterYourOwnTestKey",
- "Content-Type": "application/x-www-form-urlencoded",
- },
- body: `amount=1099&payment_method=${paymentMethod.id}¤cy=usd&automatic_payment_methods[enabled]=true&confirm=true&capture_method=manual&return_url=duup://stripe-redirect`,
- }
- );
- const data = await response.json();
- console.log("Success:", data);
- return data;
- } catch (error) {
- console.error("Error:", error);
- }
- }
- const { client_secret: clientSecret, error } = await fetchData();
- console.log("Rooooola: ", clientSecret);
- // Call the `intentCreationCallback` with your server response's client secret or error
- if (clientSecret) {
- intentCreationCallback({ clientSecret });
- } else {
- intentCreationCallback({ error });
- }
- };
- const didTapCheckoutButton = async () => {
- const { error: PaymentSheetError } = await presentPaymentSheet();
- if (PaymentSheetError) {
- if (PaymentSheetError.code === PaymentSheetError.Canceled) {
- console.log("Payment cancelled");
- } else {
- // PaymentSheet encountered an unrecoverable error. You can display the error to the user, log it, etc.
- }
- } else {
- console.log("Payment completed");
- }
- };
- return (
- <StripeProvider
- publishableKey="pk_test_51GrpvvJgUVsS8tQcUBitbi3nZhSpsJ4P0t7xS6maCWqyrkc1LktncMhWL4CUW4OC4EnterYourOwn"
- urlScheme="duug://"
- merchantIdentifier="merchant.com.your.app"
- >
- <View style={styles.container}>
- <Text>Open up App.js to start working on your app!</Text>
- <TouchableOpacity
- style={{
- marginTop: 20,
- borderColor: "#000000",
- borderWidth: 2,
- padding: 20,
- }}
- onPress={async () => {
- await didTapCheckoutButton();
- }}
- >
- <Text>Pay Now</Text>
- </TouchableOpacity>
- <StatusBar style="auto" />
- </View>
- </StripeProvider>
- );
- }
- const styles = StyleSheet.create({
- container: {
- flex: 1,
- backgroundColor: "#fff",
- alignItems: "center",
- justifyContent: "center",
- },
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement