React Native 应用内购买 Android

React Native In App Purchase Android

我正在尝试将 In App Purchases(或者 Google 喜欢称呼它;"In App Billing")添加到我的 React Native 应用程序中。

我做了什么:

  1. 在 Google Play 管理中心创建了一个产品(标识符 "unlock_premium"
  2. yarn add react-native-iapthis 图书馆)
  3. 将下面的代码添加到我的应用程序中的一个组件
  4. 在物理设备上使用 react-native run-android 进行测试,并通过 Google Play Console
  5. 将其发布到 Alpha 测试

注意:应用已签名并在清单文件中启用了计费权限,react-native-iap版本为0.3.23。

问题: 当 运行 调试版本时,console.log() 仅打印产品为 undefined,而当 运行 Alpha 部署版本时 productInfo 不显示在屏幕上(所以产品在那里也未定义)。 products 变量只是一个空数组。

try 语句似乎成功了,因为我没有看到它打印出任何错误。)

import InAppPurchase from "react-native-iap";

const itemSKUs = Platform.select({
    android: [
        "com.kimer.unlock_premium" // I've also tried just "unlock_premium"
    ]
})
...

constructor(props) {
        super(props);
        this.state = {
            modalVisible: false,
            premiumProduct: undefined
        };
}

...

async componentDidMount() {
        try {
            await InAppPurchase.prepare();
            const products = await InAppPurchase.getProducts(itemSKUs);
            console.log("finished call to get products");
            console.log(products[0]);
            this.setState({premiumProduct: products[0]});
        } catch (err) {
            console.warn(err);
        }
}

...

render() {
        let productInfo;
        if (this.state.premiumProduct !== undefined) {
            productInfo = (
                <Text>{this.state.premiumProduct}
                {this.state.premiumProduct.localizedPrice}
                {this.state.premiumProduct.currency} {this.state.premiumProduct.productID}
                {this.state.premiumProduct.title}
                {this.state.premiumProduct.description}</Text>
            );
        }

        return (
            <View>
                ...
                {productInfo}
                ...
            </View>
        );
}

已解决: 它现在正在为我工​​作!我尝试了几件事,但我不确定让它工作的关键是什么无论如何这就是我所做的:

  1. 重新安装包
  2. 更改了我的导入语句 import InAppPurchase from "react-native-iap;" to "import * as InAppPurchase from 'react-native-iap';"
  3. 将代码中的 SKU 从 "com.kimer.unlock_premium" 更改为 "unlock_premium"