Skip to content

Commit

Permalink
Merge remote-tracking branch 'tehmaestro/developer-payload'
Browse files Browse the repository at this point in the history
  • Loading branch information
j3k0 committed Oct 22, 2016
2 parents cec592a + 8f2f3e4 commit 93d0848
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,40 @@ public boolean execute(String action, JSONArray data, final CallbackContext call
// Buy an item
// Get Product Id
final String sku = data.getString(0);
buy(sku);
final JSONObject additionalData = data.getJSONObject(1);

// Get the developer payload
String developerPayload = "";
if(additionalData.has("developerPayload")) {
developerPayload = additionalData.getString("developerPayload");
}
buy(sku, developerPayload);
} else if ("subscribe".equals(action)) {
// Subscribe to an item
// Get Product Id
final String sku = data.getString(0);
final JSONObject additionalData = data.getJSONObject(1);

// Get the developer payload
String developerPayload = "";
if(additionalData.has("developerPayload")) {
developerPayload = additionalData.getString("developerPayload");
}

// Get the subscriptions SKUs to upgrade
final List<String> oldPurchasedSkus = new ArrayList<String>();
String oldSkuList = data.getString(1);
oldSkuList = (oldSkuList == "null")?null:oldSkuList;
if (oldSkuList != null){
if(additionalData.has("oldPurchasedSkus")) {
String oldSkuList = additionalData.getString("oldPurchasedSkus");
JSONArray jsonOldSkuList = new JSONArray(oldSkuList);

int len = jsonOldSkuList.length();
Log.d(TAG, "Num old SKUs Found: "+len);
for (int i=0;i<len;i++){
oldPurchasedSkus.add(jsonOldSkuList.get(i).toString());
Log.d(TAG, "Subscription SKU Added: "+jsonOldSkuList.get(i).toString());
}
}
subscribe(sku, oldPurchasedSkus);
}
subscribe(sku, developerPayload, oldPurchasedSkus);
} else if ("consumePurchase".equals(action)) {
consumePurchase(data);
} else if ("getAvailableProducts".equals(action)) {
Expand Down Expand Up @@ -193,7 +209,7 @@ public void onIabSetupFinished(IabResult result) {
}

// Buy an item
private void buy(final String sku){
private void buy(final String sku, final String developerPayload){
/* TODO: for security, generate your payload here for verification. See the comments on
* verifyDeveloperPayload() for more info. Since this is a sample, we just use
* an empty string, but on a production app you should generate this. */
Expand All @@ -207,12 +223,12 @@ private void buy(final String sku){
this.cordova.setActivityResultCallback(this);

mHelper.launchPurchaseFlow(cordova.getActivity(), sku, RC_REQUEST,
mPurchaseFinishedListener, payload);
mPurchaseFinishedListener, developerPayload);

}

// Buy an item
private void subscribe(final String sku, final List<String> oldPurchasedSkus){
private void subscribe(final String sku, final String developerPayload, final List<String> oldPurchasedSkus){
if (mHelper == null){
callbackContext.error(IabHelper.ERR_PURCHASE + "|Billing plugin was not initialized");
return;
Expand All @@ -232,7 +248,7 @@ private void subscribe(final String sku, final List<String> oldPurchasedSkus){
this.cordova.setActivityResultCallback(this);
Log.d(TAG, "Launching purchase flow for subscription.");

mHelper.launchPurchaseFlow(cordova.getActivity(), sku, IabHelper.ITEM_TYPE_SUBS, oldPurchasedSkus, RC_REQUEST, mPurchaseFinishedListener, payload);
mHelper.launchPurchaseFlow(cordova.getActivity(), sku, IabHelper.ITEM_TYPE_SUBS, oldPurchasedSkus, RC_REQUEST, mPurchaseFinishedListener, developerPayload);
}


Expand Down
2 changes: 1 addition & 1 deletion src/js/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var ERROR_CODES_BASE = 6777000;
/*///*/ store.ERR_REFRESH = ERROR_CODES_BASE + 19; // Failed to refresh the store.
/*///*/ store.ERR_PAYMENT_EXPIRED = ERROR_CODES_BASE + 20;
/*///*/ store.ERR_DOWNLOAD = ERROR_CODES_BASE + 21;
/*///*/ store.ERR_SUBSCRIPTION_UPDATE_NOT_AVAILABLE = ERROR_CODES_BASE + 22;
/*///*/ store.ERR_SUBSCRIPTION_UPDATE_NOT_AVAILABLE = ERROR_CODES_BASE + 22;

///
/// ### product states
Expand Down
10 changes: 5 additions & 5 deletions src/js/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var callbackId = 0;
/// The `additionalData` argument can be either:
/// - null
/// - object with attribute `oldPurchasedSkus`, a string array with the old subscription to upgrade/downgrade on Android. See: [android developer](https://developer.android.com/google/play/billing/billing_reference.html#upgrade-getBuyIntentToReplaceSkus) for more info
/// - object with attribute `developerPayload`, string representing the developer payload as described in [billing best practices](https://developer.android.com/google/play/billing/billing_best_practices.html)
///
/// See the ["Purchasing section"](#purchasing) to learn more about
/// the purchase process.
Expand All @@ -35,13 +36,12 @@ store.order = function(pid, additionalData) {
p = new store.Product({
id: pid,
loaded: true,
valid: false,
additionalData: additionalData
valid: false
});
}
else if (additionalData) {
p.additionalData = additionalData;
}
}
if (additionalData) {
p.additionalData = additionalData;
}

var localCallbackId = callbackId++;
Expand Down
3 changes: 2 additions & 1 deletion src/js/platforms/plugin-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ store.when("requested", function(product) {
if (product.type === store.FREE_SUBSCRIPTION || product.type === store.PAID_SUBSCRIPTION) {
method = 'subscribe';
}

store.inappbilling[method](function(data) {
// Success callabck.
//
Expand Down Expand Up @@ -153,7 +154,7 @@ store.when("requested", function(product) {
else {
product.set("state", store.VALID);
}
}, product.id);
}, product.id, product.additionalData);
});
});

Expand Down
13 changes: 9 additions & 4 deletions src/js/platforms/plugin-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,22 @@ InAppBilling.prototype.getPurchases = function (success, fail) {
}
return cordova.exec(success, errorCb(fail), "InAppBillingPlugin", "getPurchases", ["null"]);
};
InAppBilling.prototype.buy = function (success, fail, productId) {
InAppBilling.prototype.buy = function (success, fail, productId, additionalData) {
if (this.options.showLog) {
log('buy called!');
}
return cordova.exec(success, errorCb(fail), "InAppBillingPlugin", "buy", [productId]);
additionalData = (!!additionalData) && (additionalData.constructor === Object) ? additionalData : {};
return cordova.exec(success, errorCb(fail), "InAppBillingPlugin", "buy", [productId, additionalData]);
};
InAppBilling.prototype.subscribe = function (success, fail, productId) {
InAppBilling.prototype.subscribe = function (success, fail, productId, additionalData) {
if (this.options.showLog) {
log('subscribe called!');
}
return cordova.exec(success, errorCb(fail), "InAppBillingPlugin", "subscribe", [productId]);
additionalData = (!!additionalData) && (additionalData.constructor === Object) ? additionalData : {};
if (additionalData.oldPurchasedSkus && this.options.showLog) {
log('subscribe called with upgrading of old SKUs!');
}
return cordova.exec(success, errorCb(fail), "InAppBillingPlugin", "subscribe", [productId, additionalData || {}]);
};
InAppBilling.prototype.consumePurchase = function (success, fail, productId, transactionId) {
if (this.options.showLog) {
Expand Down
6 changes: 3 additions & 3 deletions src/js/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ store.Product = function(options) {
/// - `product.downloaded` - Non-consumable content has been successfully downloaded for this product
this.downloaded = options.downloaded;

/// - `product.additionalData` - additional data possibly required for product purchase
this.additionalData = options.additionalData || null;

/// - `product.transaction` - Latest transaction data for this product (see [transactions](#transactions)).
this.transaction = null;

/// - `product.additionalData` - additional data possibly required for passing info in event based behavior.
this.additionalData = null;

this.stateChanged();
};

Expand Down
34 changes: 20 additions & 14 deletions www/store-android.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ var ERROR_CODES_BASE = 6777000;
/*///*/ store.ERR_REFRESH = ERROR_CODES_BASE + 19; // Failed to refresh the store.
/*///*/ store.ERR_PAYMENT_EXPIRED = ERROR_CODES_BASE + 20;
/*///*/ store.ERR_DOWNLOAD = ERROR_CODES_BASE + 21;
/*///*/ store.ERR_SUBSCRIPTION_UPDATE_NOT_AVAILABLE = ERROR_CODES_BASE + 22;
/*///*/ store.ERR_SUBSCRIPTION_UPDATE_NOT_AVAILABLE = ERROR_CODES_BASE + 22;

///
/// ### product states
Expand Down Expand Up @@ -460,12 +460,12 @@ store.Product = function(options) {
/// - `product.downloaded` - Non-consumable content has been successfully downloaded for this product
this.downloaded = options.downloaded;

/// - `product.additionalData` - additional data possibly required for product purchase
this.additionalData = options.additionalData || null;

/// - `product.transaction` - Latest transaction data for this product (see [transactions](#transactions)).
this.transaction = null;

/// - `product.additionalData` - additional data possibly required for passing info in event based behavior.
this.additionalData = null;

this.stateChanged();
};

Expand Down Expand Up @@ -1110,6 +1110,7 @@ var callbackId = 0;
/// The `additionalData` argument can be either:
/// - null
/// - object with attribute `oldPurchasedSkus`, a string array with the old subscription to upgrade/downgrade on Android. See: [android developer](https://developer.android.com/google/play/billing/billing_reference.html#upgrade-getBuyIntentToReplaceSkus) for more info
/// - object with attribute `developerPayload`, string representing the developer payload as described in [billing best practices](https://developer.android.com/google/play/billing/billing_best_practices.html)
///
/// See the ["Purchasing section"](#purchasing) to learn more about
/// the purchase process.
Expand All @@ -1124,13 +1125,12 @@ store.order = function(pid, additionalData) {
p = new store.Product({
id: pid,
loaded: true,
valid: false,
additionalData: additionalData
valid: false
});
}
else if (additionalData) {
p.additionalData = additionalData;
}
}
if (additionalData) {
p.additionalData = additionalData;
}

var localCallbackId = callbackId++;
Expand Down Expand Up @@ -2059,17 +2059,22 @@ InAppBilling.prototype.getPurchases = function (success, fail) {
}
return cordova.exec(success, errorCb(fail), "InAppBillingPlugin", "getPurchases", ["null"]);
};
InAppBilling.prototype.buy = function (success, fail, productId) {
InAppBilling.prototype.buy = function (success, fail, productId, additionalData) {
if (this.options.showLog) {
log('buy called!');
}
return cordova.exec(success, errorCb(fail), "InAppBillingPlugin", "buy", [productId]);
additionalData = (!!additionalData) && (additionalData.constructor === Object) ? additionalData : {};
return cordova.exec(success, errorCb(fail), "InAppBillingPlugin", "buy", [productId, additionalData]);
};
InAppBilling.prototype.subscribe = function (success, fail, productId) {
InAppBilling.prototype.subscribe = function (success, fail, productId, additionalData) {
if (this.options.showLog) {
log('subscribe called!');
}
return cordova.exec(success, errorCb(fail), "InAppBillingPlugin", "subscribe", [productId]);
additionalData = (!!additionalData) && (additionalData.constructor === Object) ? additionalData : {};
if (additionalData.oldPurchasedSkus && this.options.showLog) {
log('subscribe called with upgrading of old SKUs!');
}
return cordova.exec(success, errorCb(fail), "InAppBillingPlugin", "subscribe", [productId, additionalData || {}]);
};
InAppBilling.prototype.consumePurchase = function (success, fail, productId, transactionId) {
if (this.options.showLog) {
Expand Down Expand Up @@ -2266,6 +2271,7 @@ store.when("requested", function(product) {
if (product.type === store.FREE_SUBSCRIPTION || product.type === store.PAID_SUBSCRIPTION) {
method = 'subscribe';
}

store.inappbilling[method](function(data) {
// Success callabck.
//
Expand Down Expand Up @@ -2302,7 +2308,7 @@ store.when("requested", function(product) {
else {
product.set("state", store.VALID);
}
}, product.id);
}, product.id, product.additionalData);
});
});

Expand Down

0 comments on commit 93d0848

Please sign in to comment.