使用@stripe/stripe-react-native的Apple Pay无法正常工作,显示一些钩子调用错误

eit6fx6z  于 2022-12-30  发布在  React
关注(0)|答案(1)|浏览(270)

当我尝试使用@stripe/stripe-react-native实现Apple Pay时,无法正常工作,显示一些钩子调用,代码和错误如下所示:

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!');
          }
        }
      };
...
...
}

eeq64g8w

eeq64g8w1#

钩子必须总是在函数组件内部调用。代码重构供参考。

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;
}

相关问题