针对Phonegap的InAppPurchase.js问题

时间:2014-08-20 03:36:57

标签: cordova in-app-purchase

我正在尝试使用InAppPurchase插件为phonegap做一个简单的概念验证,我似乎无法获得最简单的工作。

我的bundleID是com.XXXX.app,我的应用内商品是Test_Prod。

我只是想加载产品,这似乎可以工作并购买产品。

代码:

<html>
<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1,  minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
    <link rel="stylesheet" type="text/css" href="css/index.css" />
    <title>Hello World</title>
<script type="text/javascript" charset="utf-8">

        // Call onDeviceReady when PhoneGap is loaded.
        //
        // At this point, the document has loaded but phonegap-1.0.0.js has not.
        // When PhoneGap is loaded and talking with the native device,
        // it will call the event `deviceready`.
        //
        document.addEventListener("deviceready", onDeviceReady, false);

        // PhoneGap is loaded and it is now safe to make calls PhoneGap methods
        //
        function onDeviceReady() {
            // Now safe to use the PhoneGap API
            window.storekit.init({

               debug: true, /* Because we like to see logs on the console */

               purchase: function (transactionId, productId) {
                  console.log('purchased: ' + productId);
               },
               restore: function (transactionId, productId) {
                  console.log('restored: ' + productId);
               },
               restoreCompleted: function () {
                  console.log('all restore complete');
               },
               restoreFailed: function (errCode) {
                  console.log('restore failed: ' + errCode);
               },
               error: function (errno, errtext) {
                  console.log('Failed: ' + errtext);
               },
               ready: function () {

                  var IAP = {list: [ "com.XXXX.app" ]};

                  document.write("Sup Homies2");
                  //IAP.load = function () {
                     // Check availability of the storekit plugin
                     if (!window.storekit) {
                        console.log("In-App Purchases not available");
                        return;
                     }
                     // Initialize
                     storekit.init({
                            debug:    true, // Enable IAP messages on the console
                            ready:    IAP.onReady,
                            purchase: IAP.onPurchase,
                            restore:  IAP.onRestore,
                            error:    IAP.onError
                     });
                   //};
                   storekit.load("com.XXXX.app", 1);
                   // StoreKit's callbacks (we'll talk about them later)
                   //IAP.onReady = function () {
                      // Once setup is done, load all product data.
                   /*   storekit.load(IAP.list, function (products, invalidIds) {
                         IAP.products = products;
                         IAP.loaded = true;
                         for (var i = 0; i < invalidIds.length; ++i) {
                            console.log("Error: could not load " + invalidIds[i]);
                         }
                       });
                   //};*/

                   storekit.purchase("com.XXXX.app.Test_Prod");

                   IAP.onPurchase = function () {};
                   IAP.onRestore = function () {};
                   IAP.onError = function () {};
               }
           });
       }
</script>
</head>
<body>
    <div class="app">
        <h1>Apache Cordova</h1>
        <div id="deviceready" class="blink">
            <p class="event listening">Connecting to Your Brain</p>
            <p class="event received">Device is Attached to Brain</p>
        </div>
    </div>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
        app.initialize();
    </script>
</body>
</html>

XCode的输出是:

2014-08-19 20:32:01.764 sample[63043:60b] Multi-tasking -> Device: YES, App: YES
2014-08-19 20:32:01.939 sample[63043:60b] Resetting plugins due to page load.
2014-08-19 20:32:02.050 sample[63043:60b] Finished load of: file:///Users/mattkuhns/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/65C605A3-1040-40EA-9A61-D28FE7F4CE80/sample.app/www/index.html
2014-08-19 20:32:02.086 sample[63043:60b] InAppPurchase[objc]: Getting products data
2014-08-19 20:32:02.087 sample[63043:60b] InAppPurchase[objc]: Set has 1 elements
2014-08-19 20:32:02.087 sample[63043:60b] InAppPurchase[objc]: - com.XXXX.app.Test_Prod
2014-08-19 20:32:02.088 sample[63043:60b] InAppPurchase[objc]: start
2014-08-19 20:32:02.089 sample[63043:60b] InAppPurchase[objc]: About to do IAP
2014-08-19 20:32:02.091 sample[63043:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid product identifier: (null)'
*** First throw call stack:
(
    0   CoreFoundation                      0x000d21e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x0317e8e5 objc_exception_throw + 44
    2   CoreFoundation                      0x000d1fbb +[NSException raise:format:] + 139
    3   StoreKit                            0x03147a9f -[SKPaymentQueue addPayment:] + 659
    4   sample                              0x00021b68 -[InAppPurchase purchase:] + 424
    5   sample                              0x000199f6 -[CDVCommandQueue execute:] + 902
    6   sample                              0x0001920f -[CDVCommandQueue executePending] + 671
    7   sample                              0x00018d69 -[CDVCommandQueue enqueCommandBatch:] + 153
    8   sample                              0x00018f16 -[CDVCommandQueue fetchCommandsFromJs] + 182
    9   sample                              0x00018e2f -[CDVCommandQueue maybeFetchCommandsFromJs:] + 159
    10  libobjc.A.dylib                     0x0319082b -[NSObject performSelector:withObject:] + 70
    11  Foundation                          0x019ebe48 __NSThreadPerformPerform + 285
    12  CoreFoundation                      0x0005b77f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    13  CoreFoundation                      0x0005b10b __CFRunLoopDoSources0 + 235
    14  CoreFoundation                      0x000781ae __CFRunLoopRun + 910
    15  CoreFoundation                      0x000779d3 CFRunLoopRunSpecific + 467
    16  CoreFoundation                      0x000777eb CFRunLoopRunInMode + 123
    17  GraphicsServices                    0x0464a5ee GSEventRunModal + 192
    18  GraphicsServices                    0x0464a42b GSEventRun + 104
    19  UIKit                               0x00339f9b UIApplicationMain + 1225
    20  sample                              0x0001ff2c main + 92
    21  sample                              0x0001febd start + 53
)
libc++abi.dylib: terminating with uncaught exception of type NSException

非常感谢任何帮助。我已经加载了产品,现在我应该可以购买了。 感谢。

1 个答案:

答案 0 :(得分:0)

看起来您在保证加载产品列表之前开始购买,您需要提供storekit.load - 函数回调:

尝试类似:

 //this list needs to contain the inapp-ids, not your bundle id
 //atleast it does in their tests on github (see below)
 var IAP = {list: [ "Test_Prod" ]};

//Give the load-function a callback which gets executed after the products are guaranteed
//to be loaded
IAP.onReady = function() {

   storekit.load(IAP.list, function(products, invalidIds){

       storekit.purchase("com.XXXX.app.Test_Prod");
       //If the above doesn't work, try storekit.purchase("Test_Prod");
   });
}

tests on github

另外,你在ios-simulator上测试这个吗? 至少在我处理inapps时,他们无法在模拟器上进行适当的测试, relevant info