Advertisement
hmunoz-stripe

PaymentSheet in Expo 0.1.4

Aug 13th, 2021
2,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState } from 'react';
  2.  
  3. import {
  4.     StyleSheet,
  5.     Button,
  6.     Text,
  7.   } from 'react-native';
  8.  
  9.   import {
  10.     useStripe,
  11.   } from '@stripe/stripe-react-native';
  12.  
  13.   const SetupIntentComponent = (props) => {
  14.       const { initPaymentSheet, presentPaymentSheet } = useStripe();
  15.  
  16.       async function createPaymentSheet() {
  17.         const response = await fetch(
  18.           "MY_BACKEND_URL/payment-sheet",
  19.           {
  20.             method: 'POST',
  21.             headers: {
  22.               Accept: 'application/json',
  23.               'Content-Type': 'application/json'
  24.             },    
  25.           }
  26.         );
  27.         const responseJson = await response.json()
  28.         console.log(responseJson);
  29.    
  30.         const {
  31.           paymentIntent,
  32.           ephemeralKey,
  33.           customer
  34.         } = responseJson;
  35.    
  36.         const { error } = await initPaymentSheet({
  37.           customerId: customer,
  38.           customerEphemeralKeySecret: ephemeralKey,
  39.           paymentIntentClientSecret: paymentIntent,
  40.           customFlow: false,
  41.           merchantDisplayName: "DineDen",
  42.           style: "alwaysDark",
  43.         });
  44.        
  45.         console.log(error);
  46.         if (!error) {
  47.           const { paymentError } = await presentPaymentSheet({
  48.             clientSecret: paymentIntent,
  49.           });
  50.    
  51.           if (paymentError) {
  52.             console.log("Payment Error");
  53.             console.log(paymentError);
  54.           }
  55.           else {
  56.             console.log("Successful payment")
  57.           }
  58.         }
  59.       }
  60.  
  61.       return (
  62.         <>
  63.         <Button
  64.             title="PaymentSheet!!"
  65.             onPress={() => {
  66.                 createPaymentSheet();
  67.             }}
  68.         />
  69.         </>
  70.       )
  71.   }
  72.  
  73.  
  74.  
  75. const SetupIntentComponentStyle = StyleSheet.create({
  76.     cardField: {
  77.       height: 50,
  78.       marginLeft: 15,
  79.       marginRight: 15,
  80.       marginVertical: 30,
  81.     },
  82. });
  83.  
  84. export default SetupIntentComponent;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement