English 中文(简体)
2. 土著加沙地带——网络申请失败
原标题:React Native Stripe - Network request failed

For an app I am making, I want to make the user pay 10EUR via a paymentscreen in React Native with the help of Stripe. Unfortunately I keep stumbling upon errors when I try to test it with the 4242 card of stripe. It only occurs when I click on the pay button.

我用错误的话说:

长度=0;指数=0

我也发出警告,称网络要求失败,而且我很相信与网络有关。

你们能否帮助我?

我的守则是:

import React, { useEffect, useState } from  react ;
import { Alert, View, Text, Button } from  react-native ;
import { CardField, useStripe } from  @stripe/stripe-react-native ;

const PaymentScreen = () => {
  const { confirmPayment } = useStripe();
  const [key, setKey] = useState(  );

useEffect(() => {

    fetch( http://localhost:3000/create-payment-intent , {
        method:  POST 
    }).then(res => res.json()).then(res => {
        Alert.alert(res.clientSecret);
        const intent = res as {clientSecret: string};
        setKey(intent.clientSecret);
    });
}, []);

const handlePayment = async () => {
  try {
      const { error } = await confirmPayment(key, {
          paymentMethodType:  Card ,
      });

      if (error) {
          Alert.alert( Error , error.message);
      } else {
          Alert.alert( Payment successful! );
      }
  } catch (error) {
      console.error( Network request failed: , error);
      Alert.alert( Error ,  Network request failed. Please check your internet connection. );
  }
};

  return (
    
    <View style={{ flex: 1, justifyContent:  center , alignItems:  center  }}>
      <Text>Enter Card Details:</Text>
      <CardField
        style={{
            height: 100,
            width:  100% ,
        }}
        postalCodeEnabled={false} // Adjust based on your requirements
        placeholders={{
          number:  Card Number ,
          expiration:  MM/YY ,
          cvc:  CVC ,
        }}
        onCardChange={(cardDetails) => {
          // Handle card details change if needed
        }}
      />
      <Button title="Pay" onPress={handlePayment} />
    </View>
  );
};

export default PaymentScreen;

这是后端守则的一部分:

import express from  express 
import Stripe from  stripe 

const stripe = new Stripe( sk_test_XXX , {
    apiVersion:  2023-10-16 ,
    typescript: true,
});

const app = express();
app.use(express.json());

app.post( /create-payment-intent , async (req, res) => {
    const paymentIntent = await stripe.paymentIntents.create({
        amount: 1000,
        currency:  eur ,
    });

    res.send({
        clientSecret: paymentIntent.client_secret,
    })
});

app.listen(3000, () => console.log( Running ));

here是该错误的印刷屏幕:

我在身体和虚拟装置上都发现了错误。

Could you help me understand the problem and show me a way out? Many thanks in advance!!

问题回答




相关问题
HandleClick not being recognized as a prop

I m trying to get my menu to show when I click the sidebar, but I m getting an error "react-dom.development.js:86 Warning: React does not recognize the handleClick prop on a DOM " import { ...

Is there an equivalent to localStorage in React Native?

I d like to implement the equivalent to the javascript localStorage in my React Native app. But I m unsure how to set. To function how I want it to, I would like the localStorage to be stored every ...

Video Calling in Expo React Native Application

I am building a React Native Application using Expo and I want to integrate a 1:1 video calling functionality in the app. From what I have researched so far on the topic is that I can use SDKs of ...

热门标签