javascript 如何使用 Cordova 插件实现应用内购买?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27967792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:13:33  来源:igfitidea点击:

How to implement in-app-purchase using Cordova plugin?

javascriptandroidcordova

提问by tkhm

Please tell me the way to implement in-app-purchase using Cordova plugin.

请告诉我使用 Cordova 插件实现应用内购买的方法。

I'm developing Android application using Cordova. There are some in-app-purchase plugins but I decide to use Cordova Purchase Plugin.

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

I did some setups along README.md of In-App Purchase for PhoneGap / Cordova iOS and Android. As a result, I could call the Plugin using Demo of the Purchase Plugin for Cordovawith my little modification. (See the following, it is a portion of code.)

在 PhoneGap / Cordova iOS 和 Android 的应用内购买的README.md 中做了一些设置。因此,我可以使用Cordova 购买插件的 Demo调用插件,只需稍作修改。(见下文,它是代码的一部分。)

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();
};

It did not show 'Store not available' that is shown when plugin is not available, show 'registerProducts', and 'refresh.' (*Of course I added 'myProductA' to in-app Products on Google Play Developer Console.)

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

But I noticed that the below function is not called.

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

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

And also I couldn't understand what the parameter should fill in it, so I commented out the below. (*I did remove the comment out, but it still not working.)

而且我不明白参数应该填什么,所以我在下面注释掉了。(*我确实删除了注释,但它仍然不起作用。)

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

I guess those things make something wrong. I'm not sure what is stack on me, so my question is not clearly. I want some clues to solve it... or I shouldn't implement in-app-purchase using Cordova plugin?

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

Please give me your hand.

请把手给我。

(I'm not fluent in English, so I'm sorry for any confusion.)

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

采纳答案by Alex

You can try this plugin as an alternative: https://github.com/AlexDisler/cordova-plugin-inapppurchase

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

Here's an example of loading products and making a purchase:

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

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);
  });

It supports both Android and iOS.

它支持安卓和iOS。

回答by Alex

Step for Integrate In-App billing in Phone-gap app.

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

1>> clone this project in your pc from this link In-App billing Library

1>> 从此链接您的电脑中克隆此项目应用内计费库

2>> using CMD go to your root directory of your phonegap application

2>> 使用 CMD 转到您的 phonegap 应用程序的根目录

3>> then run this command cordova plugin add /path/to/your/cloned project --variable BILLING_KEY="QWINMERR..........RIGR"

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

Notes : for BILLING_KEY go to Developer console then open your application and go to Service& APIs for more info Please refer attached screenshotsBILLING_KEY

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