Advertisement
ryonwhyte

Stripe React NAtive BUG

Feb 27th, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { StatusBar } from "expo-status-bar";
  2. import { StyleSheet, Text, View, TouchableOpacity } from "react-native";
  3. import { StripeProvider } from "@stripe/stripe-react-native";
  4. import React, { useEffect, useCallback } from "react";
  5. import { Linking } from "react-native";
  6. import { useStripe } from "@stripe/stripe-react-native";
  7.  
  8. export default function App() {
  9.   const { handleURLCallback, initPaymentSheet, presentPaymentSheet } =
  10.     useStripe();
  11.  
  12.   const handleDeepLink = useCallback(
  13.     async (url) => {
  14.       if (url) {
  15.         const stripeHandled = await handleURLCallback(url);
  16.         if (stripeHandled) {
  17.           // This was a Stripe URL - you can return or add extra handling here as you see fit
  18.         } else {
  19.           // This was NOT a Stripe URL – handle as you normally would
  20.         }
  21.       }
  22.     },
  23.     [handleURLCallback]
  24.   );
  25.  
  26.   useEffect(() => {
  27.     const getUrlAsync = async () => {
  28.       const initialUrl = await Linking.getInitialURL();
  29.       handleDeepLink(initialUrl);
  30.     };
  31.  
  32.     getUrlAsync();
  33.  
  34.     const deepLinkListener = Linking.addEventListener("url", (event) => {
  35.       handleDeepLink(event.url);
  36.     });
  37.  
  38.     return () => deepLinkListener.remove();
  39.   }, [handleDeepLink]);
  40.  
  41.   useEffect(() => {
  42.     console.log("Roooooo");
  43.     initializePaymentSheet();
  44.   }, []);
  45.  
  46.   const initializePaymentSheet = async () => {
  47.     console.log("Init sheet");
  48.     const { error } = await initPaymentSheet({
  49.       merchantDisplayName: "Example, Inc.",
  50.       returnURL: "duup://stripe-redirect",
  51.       intentConfiguration: {
  52.         mode: {
  53.           amount: 1099,
  54.           currencyCode: "USD",
  55.         },
  56.         confirmHandler: confirmHandler,
  57.       },
  58.     });
  59.     if (error) {
  60.       // handle error
  61.     }
  62.   };
  63.  
  64.   const confirmHandler = async (
  65.     paymentMethod,
  66.     shouldSavePaymentMethod,
  67.     intentCreationCallback
  68.   ) => {
  69.     // Make a request to your own server, passing paymentMethod.id and shouldSavePaymentMethod.
  70.     async function fetchData() {
  71.       try {
  72.         const response = await fetch(
  73.           "https://api.stripe.com/v1/payment_intents",
  74.           {
  75.             method: "POST",
  76.             headers: {
  77.               Authorization:
  78.                 "Bearer sk_test_51GrpvvJgUVsS8tQcO240WHBetBXusnWh7YI9ZGGSh4IfDRP90njfiVEnterYourOwnTestKey",
  79.               "Content-Type": "application/x-www-form-urlencoded",
  80.             },
  81.  
  82.             body: `amount=1099&payment_method=${paymentMethod.id}&currency=usd&automatic_payment_methods[enabled]=true&confirm=true&capture_method=manual&return_url=duup://stripe-redirect`,
  83.           }
  84.         );
  85.  
  86.         const data = await response.json();
  87.         console.log("Success:", data);
  88.         return data;
  89.       } catch (error) {
  90.         console.error("Error:", error);
  91.       }
  92.     }
  93.  
  94.     const { client_secret: clientSecret, error } = await fetchData();
  95.     console.log("Rooooola: ", clientSecret);
  96.  
  97.     // Call the `intentCreationCallback` with your server response's client secret or error
  98.     if (clientSecret) {
  99.       intentCreationCallback({ clientSecret });
  100.     } else {
  101.       intentCreationCallback({ error });
  102.     }
  103.   };
  104.  
  105.   const didTapCheckoutButton = async () => {
  106.     const { error: PaymentSheetError } = await presentPaymentSheet();
  107.  
  108.     if (PaymentSheetError) {
  109.       if (PaymentSheetError.code === PaymentSheetError.Canceled) {
  110.         console.log("Payment cancelled");
  111.       } else {
  112.         // PaymentSheet encountered an unrecoverable error. You can display the error to the user, log it, etc.
  113.       }
  114.     } else {
  115.       console.log("Payment completed");
  116.     }
  117.   };
  118.  
  119.   return (
  120.     <StripeProvider
  121.       publishableKey="pk_test_51GrpvvJgUVsS8tQcUBitbi3nZhSpsJ4P0t7xS6maCWqyrkc1LktncMhWL4CUW4OC4EnterYourOwn"
  122.       urlScheme="duug://"
  123.       merchantIdentifier="merchant.com.your.app"
  124.     >
  125.       <View style={styles.container}>
  126.         <Text>Open up App.js to start working on your app!</Text>
  127.         <TouchableOpacity
  128.           style={{
  129.             marginTop: 20,
  130.             borderColor: "#000000",
  131.             borderWidth: 2,
  132.             padding: 20,
  133.           }}
  134.           onPress={async () => {
  135.             await didTapCheckoutButton();
  136.           }}
  137.         >
  138.           <Text>Pay Now</Text>
  139.         </TouchableOpacity>
  140.         <StatusBar style="auto" />
  141.       </View>
  142.     </StripeProvider>
  143.   );
  144. }
  145.  
  146. const styles = StyleSheet.create({
  147.   container: {
  148.     flex: 1,
  149.     backgroundColor: "#fff",
  150.     alignItems: "center",
  151.     justifyContent: "center",
  152.   },
  153. });
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement