Skip to content

Commit

Permalink
Fixing NON_RENEWING_SUBSCRIPTIONS on iOS
Browse files Browse the repository at this point in the history
Non-renewing subscriptions need to trigger "approved" only once. Once
finished, it shouldn't be called again. That's the fix.
  • Loading branch information
j3k0 committed May 21, 2016
1 parent 8aae582 commit 03adba5
Show file tree
Hide file tree
Showing 10 changed files with 7,524 additions and 3,103 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@

npm-debug.log
/.idea
/demo
/git_modules
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ all: build doc

build: sync-android test-js
@echo "- Preprocess"
@node_modules/.bin/preprocess src/js/store-ios.js src/js | node_modules/.bin/uglifyjs -b > www/store-ios.js
@node_modules/.bin/preprocess src/js/store-android.js src/js | node_modules/.bin/uglifyjs -b > www/store-android.js
@node_modules/.bin/preprocess src/js/store-windows.js src/js | node_modules/.bin/uglifyjs -b > www/store-windows.js
@node_modules/.bin/preprocess src/js/store-ios.js src/js > www/store-ios.js
@node_modules/.bin/preprocess src/js/store-android.js src/js > www/store-android.js
@node_modules/.bin/preprocess src/js/store-windows.js src/js > www/store-windows.js
@echo "- Done"
@echo ""

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cc.fovea.cordova.purchase",
"version": "5.0.2",
"version": "5.1.0",
"description": "Cordova Purchase plugin for iOS and Android (AppStore and PlayStore)",
"cordova": {
"id": "cc.fovea.cordova.purchase",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ SOFTWARE.
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cc.fovea.cordova.purchase"
version="5.0.2">
version="5.1.0">

<name>Purchase</name>
<description>Cordova Purchase plugin for iOS (AppStore), Android (PlayStore) and Windows</description>
Expand Down
2 changes: 1 addition & 1 deletion src/ios/InAppPurchase.m
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ - (void) load: (CDVInvokedUrlCommand*)command

if ((unsigned long)[inArray count] == 0) {
DLog(@"Empty array");
NSArray *callbackArgs = [NSArray arrayWithObjects: nil, nil, nil];
NSArray *callbackArgs = [NSArray arrayWithObjects: [NSNull null], [NSNull null], nil];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:callbackArgs];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
return;
Expand Down
18 changes: 14 additions & 4 deletions src/js/platforms/ios-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,25 @@ store.when("requested", function(product) {
//! When a product enters the store.FINISHED state, `finish()` the storekit transaction.
//!
store.when("finished", function(product) {
store.log.debug("ios -> finishing " + product.id);
store.log.debug("ios -> finishing " + product.id + " (a " + product.type + ")");
storekitFinish(product);
if (product.type === store.CONSUMABLE)
if (product.type === store.CONSUMABLE || product.type === store.NON_RENEWING_SUBSCRIPTION) {
product.set("state", store.VALID);
else
setOwned(product.id, false);
}
else {
product.set("state", store.OWNED);
}
});

function storekitFinish(product) {
if (product.type === store.CONSUMABLE || product.type === store.NON_RENEWING_SUBSCRIPTION) {
if (product.transaction && product.transaction.id) {
storekit.finish(product.transaction.id);
}
else if (storekit.transactionForProduct[product.id]) {
storekit.finish(storekit.transactionForProduct[product.id]);
}
else {
store.log.debug("ios -> error: unable to find transaction for " + product.id);
}
Expand Down Expand Up @@ -314,7 +320,7 @@ function storekitPurchased(transactionId, productId) {
if (!product) {
store.error({
code: store.ERR_PURCHASE,
message: "Unknown product purchased"
message: "The purchase queue contains unknown product " + productId
});
return;
}
Expand Down Expand Up @@ -395,6 +401,9 @@ function storekitError(errorCode, errorText, options) {
store.when("re-refreshed", function() {
storekit.restore();
storekit.refreshReceipts(function(data) {
// What the point of this?
// Why create a product whose ID equals the application bundle ID (?)
// Is it just to trigger force a validation of the appStoreReceipt?
if (data) {
var p = data.bundleIdentifier ? store.get(data.bundleIdentifier) : null;
if (!p) {
Expand Down Expand Up @@ -512,6 +521,7 @@ function isOwned(productId) {
//! #### *setOwned(productId, value)*
//! store the boolean OWNED status of a given product.
function setOwned(productId, value) {
store.log.debug("ios -> product " + productId + " owned=" + (value ? "true" : "false"));
localStorage["__cc_fovea_store_ios_owned_ " + productId] = value ? '1' : '0';
}

Expand Down
8 changes: 8 additions & 0 deletions src/js/platforms/ios-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ var InAppPurchase = function () {

this.receiptForTransaction = {};
this.receiptForProduct = {};
this.transactionForProduct = {};
if (window.localStorage && window.localStorage.sk_receiptForTransaction)
this.receiptForTransaction = JSON.parse(window.localStorage.sk_receiptForTransaction);
if (window.localStorage && window.localStorage.sk_receiptForProduct)
this.receiptForProduct = JSON.parse(window.localStorage.sk_receiptForProduct);
if (window.localStorage && window.localStorage.sk_transactionForProduct)
this.transactionForProduct = JSON.parse(window.localStorage.sk_transactionForProduct);
};

var noop = function () {};
Expand Down Expand Up @@ -333,6 +336,11 @@ InAppPurchase.prototype.updatedTransactionCallback = function (state, errorCode,
return;
}

if (productId && transactionIdentifier) {
log("product " + productId + "had a pending transaction: " + transactionIdentifier);
this.transactionForProduct[productId] = transactionIdentifier;
}

if (transactionReceipt) {
this.receiptForProduct[productId] = transactionReceipt;
this.receiptForTransaction[transactionIdentifier] = transactionReceipt;
Expand Down
Loading

0 comments on commit 03adba5

Please sign in to comment.