所以我需要创建一个PhoneGap应用程序,在注册过程中,需要用户注册定期的每月订阅。
我在PhoneGap In App购买时发现了这个很好的教程:https://fovea.cc/blog/index.php/3-steps-tutorial-for-phonegap-2x-in-app-purchase-on-ios/
然后跟着它(代码如下)。
所以我的问题是我点击了订阅按钮,并浏览了iOS的所有对话框,onPurchase似乎没有被调用...
如果我关闭应用程序并返回并立即调用onPurchase,然后每次加载应用程序时都会调用它。
有一种想法是iOS告诉我这个用户有一个有效的订阅,这很酷......但它似乎没有告诉我何时首次成功完成购买。
购买按钮只是说他们开始了这个过程...他们可以随时取消,所以我不能放弃。我需要告诉我用户已成功注册我的每月订阅。
思想?
var IAP = {
list: [config.IAP],
products: {}
};
// Called when I load the app on a device
function InitializePayment(){
LogInfo("Loading IAPs " + IAP.list[0]);
// Check availability of the storekit plugin
if (!window.storekit) {
HandleError('In-App Purchases not available');
return;
}
storekit.init({
ready: IAP.onReady,
purchase: IAP.onPurchase,
restore: IAP.onRestore,
error: IAP.onError
});
}
// IN APP PAYMENT
IAP.onReady = function () {
// Once setup is done, load all product data.
storekit.load(IAP.list, function (products, invalidIds) {
LogInfo('IAPs Loaded...');
for (var j = 0; j < products.length; ++j) {
var p = products[j];
LogInfo('Loaded IAP(' + j + '). title:' + p.title + ' description:' + p.description + ' price:' + p.price + ' id:' + p.id);
IAP.products[p.id] = p;
}
IAP.loaded = true;
for (var i = 0; i < invalidIds.length; ++i) {
HandleError("Error: could not load " + invalidIds[i]);
}
});
};
IAP.onPurchase = function (transactionId, productId, receipt) {
LogInfo("IAP Purchase Success!!!");
LogInfo("Transaction: " + transactionId);
LogInfo("Product: " + productId);
LogInfo("Receipt: " + receipt);
var n = (_localStorage['storekit.' + productId]|0) + 1;
_localStorage['storekit.' + productId] = n;
if (IAP.purchaseCallback) {
IAP.purchaseCallback(productId);
delete IAP.purchaseCallback;
}
PaymentSignup(transactionId, receipt, function() {
MovePageLeft("#pageSignupWelcome", _currentPage);
});
};
IAP.onError = function (errorCode, errorMessage) {
if (errorMessage == 'Cannot connect to iTunes Store'){
// this just means that they cancelled a payment...no problem.
return;
}
HandleError('IAP Error: ' + errorMessage);
};
IAP.buy = function (productId, callback) {
LogInfo("User clicked the subscribe button. Woot.");
IAP.purchaseCallback = callback;
storekit.purchase(productId);
};
IAP.onRestore = function (transactionId, productId, transactionReceipt) {
var n = (localStorage['storekit.' + productId]|0) + 1;
localStorage['storekit.' + productId] = n;
};
IAP.restore = function () {
storekit.restore();
};
var renderIAPs = function (el) {
if (IAP.loaded) {
var html = "<ul>";
for(var id in IAP.products) {
var prod = IAP.products[id];
html += "<li>" +
"<div class='iapTitle'>" + prod.title + "</div>" +
"<div class='iapDescription'>" + prod.description + "</div>" +
"<button type='button' " +
"onclick='IAP.buy(\"" + id + "\")'>Subscribe</button>" +
"</li>";
}
html += "</ul>";
el.innerHTML = html;
}
else {
el.innerHTML = "In-App Purchases not available.";
}
};