我正在使用此模块https://github.com/appcelerator-modules/ti.storekit进行消耗性应用内购买。 突然停止工作并表示已购买In-APP购买,一切正常。 我在Google上搜索,发现交易必须完成。由于Storekit.autoFinishTransactions设置为false,因此它在transactionState侦听器中完成。另外另一个解决方案是设置观察者 已经存在的“ Storekit.addTransactionObserver()”。 谢谢
var Storekit = require('ti.storekit');
Storekit.autoFinishTransactions = false;
Storekit.bundleVersion = "1.4";
Storekit.bundleIdentifier = "com.xxx.xyz";
var verifyingReceipts = false;
var loading = Ti.UI.createActivityIndicator({
bottom : 10,
height : 50,
width : 50,
backgroundColor : 'black',
borderRadius : 10,
style : Ti.UI.ActivityIndicatorStyle.BIG
});
var loadingCount = 0;
function showLoading() {
loadingCount += 1;
if (loadingCount == 1) {
loading.show();
}
}
function hideLoading() {
if (loadingCount > 0) {
loadingCount -= 1;
if (loadingCount == 0) {
loading.hide();
}
}
}
function requestProduct(identifier, success) {
showLoading();
Storekit.requestProducts([identifier], function(evt) {
hideLoading();
if (!evt.success) {
Ti.API.error('ERROR: We failed to talk to Apple!');
} else if (evt.invalid) {
Ti.API.error('ERROR: We requested an invalid product (' + identifier + '):' + "--" + JSON.stringify(evt));
Ti.API.error(evt);
} else {
Ti.API.info('Valid Product:');
Ti.API.info("evt:---" + JSON.stringify(evt) + "---" + evt.products[0].formattedPrice);
success(evt.products[0]);
}
});
}
Storekit.addEventListener('transactionState', function(evt) {
hideLoading();
switch (evt.state) {
case Storekit.TRANSACTION_STATE_FAILED:
if (evt.cancelled) {
Ti.API.warn('Purchase cancelled');
} else {
Ti.API.error('ERROR: Buying failed! ' + evt.message);
}
evt.transaction && evt.transaction.finish();
break;
case Storekit.TRANSACTION_STATE_PURCHASED:
if (verifyingReceipts) {
var msg = Storekit.validateReceipt() ? 'Receipt is Valid!' : 'Receipt is Invalid.';
Ti.API.info('Validation: ' + msg);
Ti.API.info("Purchase is valid");
console.log("evt.productIdentifier:" + evt.productIdentifier);
evt.transaction && evt.transaction.finish();
}
break;
case Storekit.TRANSACTION_STATE_PURCHASING:
Ti.API.info('Purchasing ' + evt.productIdentifier+"---"+JSON.stringify(evt));
break;
case Storekit.TRANSACTION_STATE_DEFERRED:
Ti.API.info('Deferring ' + evt.productIdentifier + ': The transaction is in the queue, but its final status is pending external action.');
break;
case Storekit.TRANSACTION_STATE_RESTORED:
Ti.API.info('Restored ' + evt.productIdentifier);
evt.transaction && evt.transaction.finish();
break;
}
});
Storekit.addEventListener('updatedDownloads', function(evt) {});
function purchaseProduct(product) {
if (product.downloadable) {
Ti.API.info('Purchasing a product that is downloadable');
}
showLoading();
Storekit.purchase({
product : product
// applicationUsername is a opaque identifier for the user’s account on your system.
// Used by Apple to detect irregular activity. Should hash the username before setting.
// applicationUsername: '<HASHED APPLICATION USERNAME>'
});
}
Storekit.addEventListener('restoredCompletedTransactions', function(evt) {
console.log("inside restoredCompletion");
hideLoading();
if (evt.error) {
Ti.API.error(evt.error);
} else if (evt.transactions == null || evt.transactions.length == 0) {
Ti.API.warn('There were no purchases to restore!');
} else {
if (verifyingReceipts) {
if (Storekit.validateReceipt()) {
Ti.API.info('Restored Receipt is Valid!');
} else {
Ti.API.error('Restored Receipt is Invalid.');
}
}
for (var i = 0; i < evt.transactions.length; i++) {
Ti.API.info("---"+evt.transactions[i].productIdentifier);
}
Ti.API.info('Restored ' + evt.transactions.length + ' purchases!');
}
});
Storekit.addTransactionObserver();
$.window.addEventListener('open', function() {
function validate() {
Ti.API.info('Receipt is Valid: ' + Storekit.validateReceipt());
}
if (!Storekit.receiptExists) {
Ti.API.info('Receipt does not exist yet. Refreshing to get one.');
Storekit.refreshReceipt(null, function() {
validate();
});
} else {
Ti.API.info('Receipt does exist.');
validate();
}
});
if (!Storekit.canMakePayments)
Ti.API.error('This device cannot make purchases!');
else {
requestProduct("com.xxx.xyz.unlock", function(product) {
console.log('product.formattedPrice---'+product.formattedPrice);
$.allPrice.setText(product.formattedPrice);
$.allCategories.addEventListener('click', function() {
purchaseProduct(product);
});
});
}
$.window.open();