hmunoz-stripe

ReactNative example with PaymentSheet 0.1.5

Jul 27th, 2021 (edited)
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import { StripeProvider, useStripe } from '@stripe/stripe-react-native';
  3. import type {Node} from 'react';
  4. import {
  5.   SafeAreaView,
  6.   StatusBar,
  7.   Button,
  8. } from 'react-native';
  9.  
  10. const App: () => Node = () => {
  11.   const { initPaymentSheet, presentPaymentSheet } = useStripe();
  12.  
  13.   async function createPaymentSheet() {
  14.     const response = await fetch(
  15.       "https://my-redacted-backend-url/payment-sheet",
  16.       {
  17.         method: 'POST',
  18.         headers: {
  19.           Accept: 'application/json',
  20.           'Content-Type': 'application/json'
  21.         },    
  22.       }
  23.     );
  24.     const responseJson = await response.json()
  25.     console.log(responseJson);
  26.  
  27.     const {
  28.       paymentIntent,
  29.       ephemeralKey,
  30.       customer
  31.     } = responseJson;
  32.  
  33.     const { error } = await initPaymentSheet({
  34.       customerId: customer,
  35.       customerEphemeralKeySecret: ephemeralKey,
  36.       paymentIntentClientSecret: paymentIntent,
  37.       googlePay: true,
  38.       merchantCountryCode: "US",
  39.       testEnv: true,
  40.     });
  41.     if (!error) {
  42.       const { paymentError } = await presentPaymentSheet({
  43.         clientSecret: paymentIntent,
  44.       });
  45.  
  46.       if (paymentError) {
  47.         console.log("Payment Error");
  48.         console.log(paymentError);
  49.       }
  50.       else {
  51.         console.log("Successful payment")
  52.       }
  53.     }
  54.   }
  55.  
  56.  
  57.   return (
  58.     <StripeProvider
  59.           publishableKey="pk_test_mypublishablekey"
  60.           merchantIdentifier="merchant.identifier">
  61.       <SafeAreaView>
  62.         <StatusBar />
  63.         <Button
  64.               title="Set up and present PaymentSheet"
  65.               onPress={ () => {
  66.                 createPaymentSheet();
  67.         }} />
  68.       </SafeAreaView>
  69.     </StripeProvider>
  70.   );
  71. };
  72.  
  73. export default App;
  74.  
Add Comment
Please, Sign In to add comment