如何使用 ReactJS 和 Capacitor 在应用程序内购买

发布于 2025-01-10 00:56:46 字数 599 浏览 2 评论 0原文

我正在使用 Capacitor 生成 IOS 和 Android 应用程序(不使用 Iconic) - 这效果很好,但我们正在尝试实现 IAP(现阶段仅适用于 IOS)并且无法弄清楚。

我遵循了各种指南(https://ionicframework.com/docs/native/ in-app-purchase-2https://purchase.cordova.fovea.cc/https://capacitorjs.com/docs/guides/in-app-purchases),但根本无法让它与React(而不是React Native)一起工作。

有人能给我指出正确的方向,或者提供示例代码吗?

I am using Capacitor to generate both the IOS and Android apps (not using Iconic) - this works well, but we are trying to implement IAP (for IOS only at this stage) and cannot figure it out.

I have followed various guides (https://ionicframework.com/docs/native/in-app-purchase-2 and https://purchase.cordova.fovea.cc/ and https://capacitorjs.com/docs/guides/in-app-purchases) but simply cannot get it working with React (not React Native)

Can someone point me in the right direction, or provide sample code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜你已久 2025-01-17 00:56:46

你没有描述出了什么问题,但这里有一个适用于我的 iOS 的基本配置。

我只包含有关商店的部分:

index.tsx

import { IAPProduct, InAppPurchase2 } from '@ionic-native/in-app-purchase-2';

const startStoreEventListeners = () => {
  if (isPlatformMobile()) {
    document.addEventListener(
      'deviceready',
      () => {    
        const store = InAppPurchase2;
        // Needed to use IAP + cordova plugins.

        // Set debug messages.
        // Default.
        store.verbosity = store.QUIET;
        // store.verbosity = store.DEBUG;

        store.register([
          {
            id: subMonthly,
            type: store.PAID_SUBSCRIPTION,
          },
          {
            id: subAnnual,
            type: store.PAID_SUBSCRIPTION,
          },
        ]);
    
        // Upon approval, verify the receipt.
        store.when(subMonthly).approved((product: IAPProduct) => {
          product.verify();
        });
        store.when(subAnnual).approved((product: IAPProduct) => {
          product.verify();
        });

        // Upon receipt validation, mark the subscription as owned.
        store.when(subMonthly).verified((product: IAPProduct) => {
          product.finish();
        });
        store.when(subAnnual).verified((product: IAPProduct) => {
          product.finish();
        });
    
        // Track all store errors
        store.error((err: Error) => {
          debugLog('Store Error', JSON.stringify(err));
        });
    
        // https://billing-dashboard.fovea.cc/setup/cordova
        store.validator =
          'https://validator.fovea.cc/v1/validate?appName=secret';
        store.refresh();

        startIonic();
      },
      false,
    );
  } else {
    startIonic();
  }
};

startStoreEventListeners();

serviceWorker.unregister();

请注意 @ionic-native 软件包已弃用,需要转换

You didn't describe what is going wrong, but here's a basic configuration that works for me on iOS.

I'm only including the part about the store:

index.tsx

import { IAPProduct, InAppPurchase2 } from '@ionic-native/in-app-purchase-2';

const startStoreEventListeners = () => {
  if (isPlatformMobile()) {
    document.addEventListener(
      'deviceready',
      () => {    
        const store = InAppPurchase2;
        // Needed to use IAP + cordova plugins.

        // Set debug messages.
        // Default.
        store.verbosity = store.QUIET;
        // store.verbosity = store.DEBUG;

        store.register([
          {
            id: subMonthly,
            type: store.PAID_SUBSCRIPTION,
          },
          {
            id: subAnnual,
            type: store.PAID_SUBSCRIPTION,
          },
        ]);
    
        // Upon approval, verify the receipt.
        store.when(subMonthly).approved((product: IAPProduct) => {
          product.verify();
        });
        store.when(subAnnual).approved((product: IAPProduct) => {
          product.verify();
        });

        // Upon receipt validation, mark the subscription as owned.
        store.when(subMonthly).verified((product: IAPProduct) => {
          product.finish();
        });
        store.when(subAnnual).verified((product: IAPProduct) => {
          product.finish();
        });
    
        // Track all store errors
        store.error((err: Error) => {
          debugLog('Store Error', JSON.stringify(err));
        });
    
        // https://billing-dashboard.fovea.cc/setup/cordova
        store.validator =
          'https://validator.fovea.cc/v1/validate?appName=secret';
        store.refresh();

        startIonic();
      },
      false,
    );
  } else {
    startIonic();
  }
};

startStoreEventListeners();

serviceWorker.unregister();

Note that @ionic-native packages are deprecated and need to be converted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文