Apple TV端React Native IAP首次订阅购买失败问题求助
解决tvOS中react-native-iap首次订阅购买报错ASDErrorDomain Code=825的问题
问题描述
tvOS项目集成react-native-iap后,首次调用requestSubscription发起订阅购买时,会弹出Apple ID登录提示,登录后支付失败并触发purchaseErrorListener,但后续所有购买尝试均正常运行。
环境信息
- "react-native-iap": "^12.10.5"
- "react-native": "npm:react-native-tvos@0.69.8-1"
- 运行平台:Apple TV(真机)
错误日志
Xcode原生日志
[react-native-iap] Purchase Started 2023-06-28 12:54:56.113414+0530 TopCourt-tvOS[341:60954] <SKPaymentQueue: 0x283bb1030>: Payment completed with error: Error Domain=ASDErrorDomain Code=825 "No transactions in response" UserInfo={NSDebugDescription=No transactions in response} [react-native-iap] Purchase Failed
purchaseErrorListener日志
2023-06-28 12:54:56.122519+0530 TopCourt-tvOS[341:60967] [javascript] 'purchaseErrorListener=====', { message: 'An unknown error occurred', code: 'E_UNKNOWN', responseCode: '0', productId: 'tcgold_a600_1y', debugMessage: 'An unknown error occurred' }
解决方案
1. 启动时清理缓存的失败交易
首次登录后的失败交易可能残留于SKPaymentQueue中,导致首次请求异常。在App启动或组件初始化时调用flushFailedPurchasesCachedAsPending清理无效交易:
import { flushFailedPurchasesCachedAsPending } from 'react-native-iap'; // 在组件挂载或App启动逻辑中执行 useEffect(() => { const initIAP = async () => { await flushFailedPurchasesCachedAsPending(); }; initIAP(); }, []);
2. 延迟首次购买请求
首次登录Apple ID后,App Store服务器会话可能未完全同步,延迟几秒发起首次购买请求:
// 首次购买时添加延迟 setTimeout(() => { requestSubscription({sku: sku}) .then(result => { console.log('Subscription result-', result); subscriptionReceiptValidate(result); }) .catch(error => { console.log("Error==", error); // 针对825错误重试一次 if (error.responseCode === '0' || error.message.includes('No transactions in response')) { requestSubscription({sku: sku}) .then(retryResult => subscriptionReceiptValidate(retryResult)) .catch(retryError => console.log("Retry Error==", retryError)); } }); }, 2000); // 延迟2秒,可根据实际情况调整
3. 监听并处理交易队列状态
确保SKPaymentQueue的观察者正确初始化,在错误回调中主动处理未完成的交易:
import { purchaseErrorListener } from 'react-native-iap'; useEffect(() => { const errorSubscription = purchaseErrorListener(err => { console.log('purchaseErrorListener=====', err); // 检测到825错误时触发重试 if (err.debugMessage.includes('ASDErrorDomain Code=825')) { requestSubscription({sku: 'tcgold_a600_1y'}); // 替换为实际SKU } }); return () => { errorSubscription.remove(); }; }, []);
说明
ASDErrorDomain Code=825是Apple App Store服务器返回的临时错误,本质是首次登录后服务器未及时返回交易响应。通过清理缓存交易、延迟请求或针对性重试,可有效解决首次购买失败的问题。
内容的提问来源于stack exchange,提问作者vishal dodiya
相关产品推荐
相关产品推荐

