【问题标题】:Expo InAppPurchases connectAsync returns undefinedExpo InAppPurchases connectAsync 返回未定义
【发布时间】:2021-03-20 13:15:54
【问题描述】:

我在尝试让 InAppPurchases 使用 Expo 在我的 React Native 应用程序中工作时遇到问题。 https://docs.expo.io/versions/latest/sdk/in-app-purchases

这是我从标准 Expo 应用中弹出的一个裸工作流应用。

我确保仔细遵循此处的安装说明:https://docs.expo.io/bare/installing-unimodules/

我确实测试了 unimodules 是否正确安装:

import { Constants } from "react-native-unimodules";
useEffect(() => {
  alert("installed: " + JSON.stringify(Constants.systemFonts));
}, []);

上面的代码有效。

我正在使用react-native-unimodules 版本0.11.0

这是我的代码:

useEffect(() => {
    (async function init() {
        
        try {
            const connect_res = await connectAsync();
            alert("connect: " + JSON.stringify(connect_res));
        } catch (err) {
                alert("general error for connect async: " + err);
        }
        
    })();
}, []);

这是在App.js 入口点文件中。它总是为connect_res 返回undefined,所以我认为这就是我无法让任何代码工作的原因。

就在connectAsync()下方,我有以下内容。这个也没有返回任何东西:

setPurchaseListener(({ responseCode, results, errorCode }) => {

    if (responseCode === IAPResponseCode.OK) {
        results.forEach((purchase) => {
            if (!purchase.acknowledged) {
                alert("purchase successful!");
                
                finishTransactionAsync(purchase, true);
            }
        });
    }

    if (responseCode === IAPResponseCode.USER_CANCELED) {
        alert("user cancelld!");
    } else if (responseCode === IAPResponseCode.DEFERRED) {
        alert("user does not have permission to buy");
    } else {
        alert("something went wrong: " + errorCode);
    }
});

然后在我的付款屏幕上显示以下内容:

import { getProductsAsync, purchaseItemAsync } from "expo-in-app-purchases";

这是支付按钮的代码:

const makePayment = async () => {
    alert("now making payment...");
    try {
        const items = Platform.select({
            ios: ["abc"],
            android: ["my-sub-id", "sku-my-sub-id"],
        });

        alert("items: " + JSON.stringify(items));

        const products = await getProductsAsync(items);
        alert("products: " + JSON.stringify(products));

        if (products.results.length > 0) {
            alert("found products!");
            await purchaseItemAsync("my-sub-id");
            alert("done making payment!");
        } else {
            alert("no products..");
        }
    } catch (err) {
        alert("error occured while trying to purchase: " + err);
    }
};

在这种情况下,getProductsAsync() 确实会返回类似于结果应有的格式的内容。但它不会返回我创建的任何订阅(我复制了该列中列出的产品 ID 值并将其提供给 getProductsAsyncpurchaseItemAsync。我还提供了基本上只有前缀的 url 版本sku-:

我还为我在 Google Play 中使用的电子邮件启用了许可测试:

请注意,我正在将.aab 文件上传到内部测试轨道上的 Google Play,然后我使用以下 URL 格式安装它:https://play.google.com/apps/test/com.myname.appname/versionNumber

但是当我打开该链接时,似乎 google play 将其检测为旧版本,即使它是最新版本。我所做的更改显示了,所以我很确定这是正确的安装 URL。

我还能错过什么?

【问题讨论】:

    标签: android react-native in-app-purchase expo


    【解决方案1】:

    对于任何有同样问题的人。您所要做的就是将其添加到您的 android/app/src/main/AndroidManifest.xml 文件中:

    <uses-permission android:name="com.android.vending.BILLING" />
    

    在本文发布时的文档中没有提到它,并且它不会在您安装模块时自动添加。如果您假设所有 Expo 模块都是如此,可能会帮助您避免头疼的问题。

    【讨论】:

    • 我在 ios 中面临同样的问题。我该怎么办?有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 2016-11-18
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多