【问题标题】:How to implement in-app-purchase using Cordova plugin?如何使用 Cordova 插件实现应用内购买?
【发布时间】:2015-01-15 16:08:55
【问题描述】:

请告诉我如何使用 Cordova 插件实现应用内购买。

我正在使用 Cordova 开发 Android 应用程序。 有一些应用内购买插件,但我决定使用 Cordova 购买插件。

我在In-App Purchase for PhoneGap / Cordova iOS and Android 的 README.md 中做了一些设置。 结果,我只需稍加修改就可以使用Demo of the Purchase Plugin for Cordova 调用插件。 (见下文,是部分代码。)

app.initStore = function() {

if (!window.store) {
    log('Store not available');
    return;
}

// Enable maximum logging level
store.verbosity = store.DEBUG;

// Enable remote receipt validation
//    store.validator = "https://api.fovea.cc:1982/check-purchase";

// Inform the store of your products
log('registerProducts');
store.register({
    id:    'myProductA',
    alias: 'myProductA',
    type:   store.CONSUMABLE
});

// When any product gets updated, refresh the HTML.
store.when("product").updated(function (p) {
    console.info("app.renderIAP is called");
    app.renderIAP(p);
});

// Log all errors
store.error(function(error) {
    log('ERROR ' + error.code + ': ' + error.message);
});

// When purchase of an extra life is approved,
// deliver it... by displaying logs in the console.
store.when("myProductA").approved(function (order) {
    log("You got a ProductA");
    order.finish();
});

// When the store is ready (i.e. all products are loaded and in their "final"
// state), we hide the "loading" indicator.
//
// Note that the "ready" function will be called immediately if the store
// is already ready.
store.ready(function() {
    var el = document.getElementById("loading-indicator");
    console.info(el + "ready is called")
    if (el)
        el.style.display = 'none';
});

// When store is ready, activate the "refresh" button;
store.ready(function() {
    var el = document.getElementById('refresh-button');
    console.info(el + "ready is called and refresh-button show?");
    if (el) {
        el.style.display = 'block';
        el.onclick = function(ev) {
            store.refresh();
        };
    }
});

// Refresh the store.
//
// This will contact the server to check all registered products
// validity and ownership status.
//
// It's fine to do this only at application startup, as it could be
// pretty expensive.
log('refresh');
store.refresh();
};

它没有显示插件不可用时显示的“商店不可用”,显示“registerProducts”和“刷新”。 (*当然,我在 Google Play 开发者控制台上的应用内产品中添加了“myProductA”。)

但是我注意到下面的函数没有被调用。

store.when("product").updated(function (p)

而且我也看不懂参数应该填什么,所以我把下面的注释掉了。 (*我确实删除了评论,但它仍然无法正常工作。)

store.validator = "https://api.fovea.cc:1982/check-purchase";

我猜这些事情会出错。 我不确定我的堆栈是什么,所以我的问题不清楚。 我想要一些线索来解决它...或者我不应该使用 Cordova 插件实现应用内购买?

请把手给我。

(我的英语不流利,所以如有任何混淆,我很抱歉。)

【问题讨论】:

  • 有一些关于验证器函数here的文档。目前,cordova 插件的作者似乎提供了一种免费的方式来验证购买的物品,但我仍然不清楚他的服务是否真的有效(根据他的网站,当前为私人测试版)以及该服务可以访问什么样的信息到。
  • @antogerva 感谢您的评论。我忽略了你提到的链接。它看起来很有帮助,谢谢。无论如何,最后我放弃了使用这个插件并在cordova框架上实现了“应用内购买”功能。对我来说太难了。哈哈
  • 大家好,请帮帮我。这是我的问题,请stackoverflow.com/questions/38657305/…

标签: javascript android cordova


【解决方案1】:

你可以试试这个插件:https://github.com/AlexDisler/cordova-plugin-inapppurchase

以下是加载产品和进行购买的示例:

inAppPurchase
  .buy('com.yourapp.consumable_prod1')
  .then(function (data) {
    // ...then mark it as consumed:
    return inAppPurchase.consume(data.productType, data.receipt, data.signature);
  })
  .then(function () {
    console.log('product was successfully consumed!');
  })
  .catch(function (err) {
    console.log(err);
  });

它同时支持Android和iOS。

【讨论】:

  • 谢谢,亚历克斯!我阅读了链接和your blog。看起来真的很棒,谢谢!
【解决方案2】:

在 Phone-gap 应用中集成应用内计费的步骤。

1>> 从此链接In-App billing Library 将这个项目克隆到你的电脑中

2>> 使用 CMD 进入你的 phonegap 应用程序的根目录

3>> 然后运行这个命令 cordova plugin add /path/to/your/cloned project --variable BILLING_KEY="QWINMERR..​​........RIGR"

注意:对于 BILLING_KEY 转到开发者控制台,然后打开您的应用程序并转到服务和 API 以获取更多信息请参阅附件截图

【讨论】:

    猜你喜欢
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 2011-01-02
    • 2019-12-12
    • 1970-01-01
    相关资源
    最近更新 更多