Muhammad Saqib Noor
Muhammad Saqib Noor

Reputation: 358

Apple Pay using @stripe/stripe-react-native is not working showing some hook call error

When i am trying to implement Apple Pay using @stripe/stripe-react-native then is not working showing some hook call , Code & Error showing below:

    import { StripeProvider, useApplePay} from '@stripe/stripe-react-native';
    const { presentApplePay, confirmApplePayPayment } = useApplePay();
   
export default class App (){
    handlePayPress = async () => {
          
        const {error, paymentMethod} = await presentApplePay({
          cartItems: [
            {
              label: 'payment label',
              amount: '50', // amount as string
              type: 'final',
            },
          ],
          country: 'US', // enter any country code supported by stripe,
          currency: 'USD', // enter any currency supported by stripe,
        });
        if (error) {
          Alert.alert(error.code, error.message);
        } else {
          const {error: confirmApplePayError} = await confirmApplePayPayment(
            clientSecret,
          );
          confirmApplePayPayment(clientSecret);
          if (confirmApplePayError) {
            Alert.alert(confirmApplePayError.code, confirmApplePayError.message);
          } else {
            Alert.alert('Success', 'The payment was confirmed      successfully!');
          }
        }
      };
...
...
}

Error Screenshot

Upvotes: 0

Views: 1306

Answers (2)

Sharif Noor
Sharif Noor

Reputation: 322

You can create a seperate functional component for that and call in your class component like this

export default class App (){
    render() {
        return(
            <ApplePayFunction data={'your data here'}/>
        );
    }
}
export default function ApplePayFunction(props) {
  const { presentApplePay, confirmApplePayPayment } = useApplePay();
  const handlePayPress = async () => {
    const { error, paymentMethod } = await presentApplePay({
      cartItems: [
        {
          label: "payment label",
          amount: "50", // amount as string
          type: "final",
        },
      ],
      country: "US", // enter any country code supported by stripe,
      currency: "USD", // enter any currency supported by stripe,
    });
    if (error) {
      Alert.alert(error.code, error.message);
    } else {
      const { error: confirmApplePayError } = await confirmApplePayPayment(
        clientSecret
      );
      confirmApplePayPayment(clientSecret);
      if (confirmApplePayError) {
        Alert.alert(confirmApplePayError.code, confirmApplePayError.message);
      } else {
        Alert.alert("Success", "The payment was confirmed      successfully!");
      }
    }
  };

  // Return UI Views below
  return null;
}

Upvotes: 1

Fiston Emmanuel
Fiston Emmanuel

Reputation: 4859

Hooks must always be called inside functional components. Code refactor for reference.

import { StripeProvider, useApplePay } from "@stripe/stripe-react-native";

export default function App() {
  const { presentApplePay, confirmApplePayPayment } = useApplePay();
  const handlePayPress = async () => {
    const { error, paymentMethod } = await presentApplePay({
      cartItems: [
        {
          label: "payment label",
          amount: "50", // amount as string
          type: "final",
        },
      ],
      country: "US", // enter any country code supported by stripe,
      currency: "USD", // enter any currency supported by stripe,
    });
    if (error) {
      Alert.alert(error.code, error.message);
    } else {
      const { error: confirmApplePayError } = await confirmApplePayPayment(
        clientSecret
      );
      confirmApplePayPayment(clientSecret);
      if (confirmApplePayError) {
        Alert.alert(confirmApplePayError.code, confirmApplePayError.message);
      } else {
        Alert.alert("Success", "The payment was confirmed      successfully!");
      }
    }
  };

  // Return UI Views below
  return null;
}


Upvotes: 0

Related Questions