我正在使用React Native和Stripe SDK开发一个应用程序。该应用程序在Expo Go
和development build
中运行良好,但是,当我尝试在真实的设备上构建它时,应用程序崩溃。下面是与错误相关的代码和错误日志。
import { useStripe } from '@stripe/stripe-react-native';
const { initPaymentSheet, presentPaymentSheet, confirmPaymentSheetPayment } = useStripe();
useEffect(() => {
if (isInactive(user)) {
initializePayment().catch(err => {
console.error("Failed to initialize payment", err);
});
}
}, [subscriptionSecret, customerId])
const initializePayment = async (): Promise<void> => {
setIsLoading(true);
try {
if (subscriptionSecret) {
const { error } = await initPaymentSheet({
customerId: customerId,
paymentIntentClientSecret: subscriptionSecret,
merchantDisplayName: 'a',
customFlow: true,
});
if (error) {
console.log('Failed to initialize payment sheet', error);
alert('支払いの初期化エラーが発生しました。もう一度やり直してください。');
} else {
console.log('initialize succeeded');
}
}
} catch (e) {
console.error('An error occurred:', e);
alert('予期せぬエラーが発生しました。');
} finally {
setIsLoading(false);
}
};
const handlePay = async (): Promise<void> => {
try {
if (!paymentIntentClientSecret) return;
const { error } = await presentPaymentSheet();
if (error) {
console.error('Payment failed', error);
alert('Payment error occurred, please try again.');
return;
} else {
console.log('Payment succeeded');
}
} catch (e) {
console.log(e)
return;
}
try {
setIsLoading(true)
const { error: confirmError } = await confirmPaymentSheetPayment();
console.log('error', confirmError);
if (confirmError) {
Alert.alert(`error code: ${confirmError.code}`, confirmError.message);
} else {
// (payment success handling code)
}
} catch (e) {
console.error(e)
alert('Payment error occurred, please try again.');
} finally {
setIsLoading(false)
}
}
字符串
错误日志:
Exception java.lang.IllegalStateException:
at com.stripe.android.paymentsheet.flowcontroller.DefaultFlowController.presentPaymentOptions (DefaultFlowController.kt:225)
at com.reactnativestripesdk.PaymentSheetFragment.present (PaymentSheetFragment.kt:157)
at com.reactnativestripesdk.StripeSdkModule.presentPaymentSheet (StripeSdkModule.kt:176)
at java.lang.reflect.Method.invoke
at com.facebook.react.bridge.JavaMethodWrapper.invoke (JavaMethodWrapper.java:372)
at com.facebook.react.bridge.JavaModuleWrapper.invoke (JavaModuleWrapper.java:188)
at com.facebook.jni.NativeRunnable.run
at android.os.Handler.handleCallback (Handler.java:907)
at android.os.Handler.dispatchMessage (Handler.java:105)
at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage (MessageQueueThreadHandler.java:27)
at android.os.Looper.loop (Looper.java:216)
at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run (MessageQueueThreadImpl.java:228)
at java.lang.Thread.run (Thread.java:784)
型
package.json
- "expo": "~48.0.21"
- "@stripe/stripe-react-native": "0.23.3"
- "react-native": "0.71.14",
型
在await presentPaymentSheet()调用期间,DefaultFlowController.presentPaymentOptions方法调用中出现错误消息java.lang.IllegalStateException,这让我特别困惑。卡片输入没有出现,应用程序反而崩溃。对此错误的原因和解决方案有什么想法吗?此外,此错误不会发生在Expo Go中,只会发生在真实的设备构建中。提前感谢您。
1条答案
按热度按时间x6yk4ghg1#
1.在哪里获取
subscriptionSecret
?还应该确保initializePayment
在handlePay
之前被调用,一种方法是使用一个xrc函数,它首先等待initialize payment
,然后等待handlePay
。1.为什么你打电话给
confirmPaymentSheetPayment
时,presentPaymentSheet
将返回,如果支付失败与否。