需要帮助修复在 Stripe(React-Native)上确认付款的错误

问题描述 投票:0回答:1

我需要帮助来解决我面临的这个错误,它是:

付款确认错误:,对象{ "代码": "失败", “拒绝代码”:空, "localizedMessage": "卡详细信息不完整", "message": "卡详细信息不完整", “stripeErrorCode”:空, “类型”:空, }

我的API代码是:

app.post('/create-payment-intent', async (req, res) => {
    const { amount, stripeCustomerId } = req.body;

    try {
        // Retrieve the customer to get the default payment method
        const customer = await stripe.customers.retrieve(stripeCustomerId);
        const defaultPaymentMethodId = customer.invoice_settings.default_payment_method;

        if (!defaultPaymentMethodId) {
            return res.status(400).send({ error: 'No default payment method found for the customer' });
        }

        const paymentIntent = await stripe.paymentIntents.create({
            amount: amount,
            currency: 'usd',
            customer: stripeCustomerId,
            payment_method: defaultPaymentMethodId,
            confirm: true,
            off_session: true, // Set to true if the customer is not present
        });

        res.send({ clientSecret: paymentIntent.client_secret });
    } catch (error) {
        console.error('Error creating payment intent:', error);
        res.status(500).send({ error: error.message });
    }
});

我的函数正在 React 组件中的 TouchabileOpacity 上调用:

const handleConfirmPayment = async () => {
        console.log('Starting payment confirmation...');
    
        if (!selectedPaymentMethodId) {
            console.log('No payment method selected.');
            Alert.alert('Error', 'No payment method selected');
            return;
        }
    
        console.log(`Selected payment method ID: ${selectedPaymentMethodId}`);
    
        try {
            console.log('Creating payment intent...');
            const response = await axios.post('/create-payment-intent', {
                amount: 3006, // Amount in cents ($30.00)
                stripeCustomerId: "id", // Ensure this is dynamically retrieved
                paymentMethodId: selectedPaymentMethodId,
            });
    
            console.log('Payment intent response:', response.data);
        
            const { clientSecret } = response.data;
        
            if (!clientSecret) {
                console.error('Failed to get client secret from the backend');
                Alert.alert('Payment failed', 'Unable to process the payment.');
                return;
            }
    
            console.log(`Confirming payment with client secret: ${clientSecret}`);
            const { error } = await confirmPayment(clientSecret, {
                paymentMethodType: 'Card', 
                paymentMethodId: selectedPaymentMethodId,
            });
    
            if (error) {
                console.error('Payment confirmation error:', error);
                Alert.alert('Payment failed', error.message);
            } else {
                console.log('Payment successful!');
                Alert.alert('Payment successful', 'Your payment was successful!');
            }
        } catch (error) {
            console.error('Payment error:', error);
            Alert.alert('Payment failed', 'There was an issue with your payment.');
        }
    };

但是,当我单击 TouchableOpacity 时,它会在控制台中打印:

Starting payment confirmation...
Selected payment method ID: "actualID"
Creating payment intent...
Payment intent response: Object {
  "clientSecret": "actualsecret",
}
Confirming payment with client secret: "actualsecret"

Payment confirmation error:, Object {
  "code": "Failed",
  "declineCode": null,
  "localizedMessage": "Card details not complete",
  "message": "Card details not complete",
  "stripeErrorCode": null,
  "type": null,
}

奇怪的是,在 Stripe 仪表板中,我确实在状态中看到付款成功,而且看起来好像已经完成了。但是点击TouchableOpacity一段时间后,弹出错误“付款失败。卡详细信息不完整”。不知道为什么它会通过,但随后我收到错误?我怎样才能解决这个问题?我还在stripe React Native包提供的confirmPayment功能上使用默认付款方式。另外,我没有使用 Stripe 提供的付款单。

reactjs react-native stripe-payments
1个回答
0
投票

根据文档https://stripe.dev/stripe-react-native/api-reference/modules/PaymentIntent.html#CardParams

{ paymentMethodType: "Card"; paymentMethodData?: { token?: string; billingDetails?: BillingDetails; mandateData?: MandateData } } | { paymentMethodType: "Card"; paymentMethodData: { paymentMethodId: string; cvc?: string; billingDetails?: BillingDetails; mandateData?: MandateData } }

paymentMethodId 属性应该位于 paymentMethodData 对象内部

尝试将其传递给confirmPayment方法

await confirmPayment(clientSecret, {
                paymentMethodType: 'Card', 
                paymentMethodData: {
                  paymentMethodId: selectedPaymentMethodId},
                });
© www.soinside.com 2019 - 2024. All rights reserved.