尝试使用 stripe-react-native 中保存的卡进行付款时出错

Getting error while trying to do payment with saved card in stripe-react-native

我正在尝试使用 react-native-stripe 库中保存的卡进行支付,但出现以下错误:

提供的 payment_method 参数 pm_1J6uUAJe0mL5QUbWTxbO90MK 属于客户 cus_JkKJdvAXk19JMu。请在 PaymentIntent 的 customer 参数中包含客户。

代码如下:

export default function CheckoutScreen({route}) {
  const {
    vehicleType,
    packages: pkg,
    // price,
    addon,
  } = route.params;
  const auth = useSelector((state) => state.auth);
  const {displayName} = auth?.profile || {};
  const styles = useStyleSheet(themedStyles);
  const [cardToken, setCardToken] = useState('');

  const {confirmPayment, loading} = useConfirmPayment();

  const fetchPaymentIntentClientSecret = async () => {
    const response = await fetch(`${API_URL}/app/createPaymentIntent`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        items: {package: pkg, addon, vehicleType},
        discountCost: 0,
      }),
    });
    const {clientSecret} = await response.json();
    return {clientSecret};
  };

  const handlePayPress = async () => {
    try {
      const {clientSecret} = await fetchPaymentIntentClientSecret();

      const updatedObj = cardToken
        ? {
            type: 'Card',
            cvc: 424,
            paymentMethodId: cardToken              }
        : {
            type: 'Card',
            billingDetails: {
              name: displayName,
            },
            setupFutureUsage: 'OffSession',
          };

      console.log(`udpatedObj`, updatedObj);

      const {error, paymentIntent} = await confirmPayment(
        clientSecret,
        updatedObj,
      );
      if (error) {
        console.log(`error.message`, error.message);
        Alert.alert(`Error code: ${error.code}`, error.message);
      } else if (paymentIntent) {
        Alert.alert('Success', `Payment successfull: ${paymentIntent.id}`);
        await firestore()
          .collection('stripe_customers')
          .doc(auth.profile.uid)
          .collection('payment_methods')
          .add({id: paymentIntent.id});
      }
    } catch (error) {
      console.log(`error`, error);
    }
  };

  const updateSelectedItem = (item, cardDetails) => {
    setCardToken(item);
  };

  return (
    <>
      <SafeAreaView style={styles.safeAreaTop} />
      <SafeAreaView style={styles.safeArea}>
        <StatusBar barStyle="light-content" />
        {/* <Button
          variant="primary"
          disabled={!loading}
          title="Checkout"
          onPress={openPaymentSheet}
        /> */}
        <CardField
          onCardChange={(cardDetails) =>
            console.log('cardDetails', cardDetails)
          }
          style={styles.cardField}
        />
        <SavedCards
          selectedItem={cardToken}
          setSelectedItem={updateSelectedItem}
        />
        <Button
          variant="primary"
          disabled={loading}
          title="Pay"
          onPress={handlePayPress}
        />
      </SafeAreaView>
    </>
  );
}

我已将付款附加给客户

您可能需要在 fetchPaymentIntentClientSecret 函数中传递 customer_id 键。

更多详情:https://stripe.com/docs/api/payment_intents/create