Skip to content

Commit

Permalink
Merge 23a8995 into 9128c09
Browse files Browse the repository at this point in the history
  • Loading branch information
eatsjobs committed Mar 19, 2018
2 parents 9128c09 + 23a8995 commit 6480a41
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions src/initialization/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ var Utility = require('../utility');
* @param {string} options.secretId secret id of the application
* @param {boolean} [options.enable=false] enable calls to Newton library
* @param {boolean} [options.waitLogin=false] all Newton calls wait login (logged and unlogged)
* @param {boolean} [options.waitDeviceReady=false] wait deviceReady event to initialize Newton
* @param {integer} [options.newtonversion=2] version of Newton (1 or 2)
* @param {Object} [options.logger=disabled logger] object with debug, log, info, warn, error
* @param {Object} [options.properties={}] custom data for Newton session (not supported for v1)
Expand All @@ -28,7 +27,6 @@ var Utility = require('../utility');
* secretId: '123456789',
* enable: true,
* waitLogin: true,
* waitDeviceReady: false,
* newtonversion: 2,
* logger: console,
* config: {
Expand Down Expand Up @@ -73,7 +71,6 @@ module.exports = function(options){
} else {
var args = [options.secretId, Utility.createSimpleObject(options.properties)];
if (options.config) { args.push(options.config); }
// if (options.pushCallback) { args.push(options.pushCallback); }
Global.newtonInstance = Newton.getSharedInstanceWithConfig.apply(null, args);
}

Expand All @@ -92,11 +89,7 @@ module.exports = function(options){
resolve(false);
Global.logger.warn('NewtonAdapter', 'Init', 'Newton not enabled');
} else {
if(options.waitDeviceReady){
document.addEventListener('deviceready', initNewton, false);
} else {
initNewton();
}
initNewton();
}
}
});
Expand Down
15 changes: 14 additions & 1 deletion src/login/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var Bluebus = require('bluebus');
var Global = require('../global');
var Utility = require('../utility');

var loginWithReceipt = require('./loginWithReceipt');

/**
* @ngdoc function
* @name login
Expand All @@ -13,7 +15,7 @@ var Utility = require('../utility');
*
* @param {Object} options configuration object
* @param {boolean} [options.logged=false] new state of the user
* @param {string} [options.type="custom"] (custom, external, msisdn, autologin, generic or oauth)
* @param {string} [options.type="custom"] (custom, external, msisdn, autologin, generic, oauth, receipt)
* @param {string} options.userId required for custom and external login
* @param {Object} [options.userProperties={}] available only for custom and external login
* @param {string} options.pin required for msisdn login
Expand Down Expand Up @@ -43,6 +45,13 @@ var Utility = require('../utility');
* }).catch(function(err){
* console.log('login failed', err);
* });
const offerId = await NewtonAdapter.getOfferFor("nativeItemId", "googlePlay");
const receipt = await NativeNewton.buy(offerId, "nativeItemId")
NewtonAdapter.login({
type: 'receipt',
receipt: receipt
});
*
* // for unlogged users
* NewtonAdapter.login({
Expand Down Expand Up @@ -197,6 +206,10 @@ module.exports = function(options){
reject('OAuth login requires provider and access_token');
Global.logger.error('NewtonAdapter', 'Login', 'OAuth login requires provider and access_token');
}
} else if(loginType === 'receipt') {
loginWithReceipt(options.receipt, options.customData)
.then(callCallback)
.catch(callCallback);
} else {
reject('This type of login is unknown');
Global.logger.error('NewtonAdapter', 'Login', 'This type of login is unknown');
Expand Down
28 changes: 28 additions & 0 deletions src/login/loginWithReceipt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var Promise = require('promise-polyfill');
var Global = require('../global');
var Utility = require('../utility');
/**
*
* @param {Object} receipt
* @param {String} receipt.serializedPayment
* @returns {Promise}
*/
function loginWithReceipt(receipt, customData) {
return new Promise(function(resolve, reject) {
Global.newtonInstance.getLoginBuilder()
.setCustomData(Utility.createSimpleObject.fromJSONObject(customData))
.setSerializedPayment(receipt.serializedPayment)
.setOnFlowCompleteCallback(function(err) {
if (!err) {
resolve();
return;
}
reject(err);
})
.getPaymentReceiptLoginFlow()
.startLoginFlow();
});

}

module.exports = loginWithReceipt;
4 changes: 3 additions & 1 deletion src/login/setUserStateChangeListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ var Global = require('../global');
*/
module.exports = function(callback){
if(Global.newtonInstance && callback){
Global.newtonInstance.setUserStateChangeListener(callback);
Global.newtonInstance.setUserStateChangeListener({
onLoginStateChange: callback
});
return true;
} else {
return false;
Expand Down
10 changes: 10 additions & 0 deletions src/login/test/login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,5 +275,15 @@ describe('login/login', function(){
done.fail(reason);
});
});

it('login with serializePayment', function(done){
NewtonAdapter.login({
receipt: { serializedPayment: "o2ih1290ew9h90qhe1290eh12h" },
customData: { loginWithPayment: 1 }
}).then(function() {
done();
}).catch(done.fail);
});

});
});
6 changes: 6 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,10 @@ module.exports = new function(){
this.recoverPassword = require('./user/recoverPassword');
this.resetPassword = require('./user/resetPassword');
this.userDelete = require('./user/userDelete');

this.setPushCallback = require('./push/index').setPushCallback;
this.registerDevice = require('./push/index').registerDevice;

this.addSerializedPayment = require('./payment/index').addSerializedPayment;
this.getOfferFor = require('./payment/index').getOfferFor;
};
47 changes: 47 additions & 0 deletions src/payment/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var Global = require('../global');
var Promisify = require('../promisify');

/**
* Flow example
* if (user.logged) {
* getOfferFor(nativeItemId, store)
* .then(function(offer_id) {
* return NativeNewton.buy(offer_id, nativeItemId)
* })
* .then(function(receipt){
* // NativeStorage.setItem('receipt', receipt) save it
* return addPayment(receipt.serializedPayment);
* })
* .catch(function(){
* // handle item already owned etc
*
* })
* }
*/


/**
*
* @param {String} nativeItemId
* @param {String} store - googlePlay|appleStore
* @returns {Promise}
*/
function getOfferFor(nativeItemId, store) {
var getOfferForPromise = Promisify(Global.newtonInstance.getPaymentManager().getOfferFor);
return getOfferForPromise(nativeItemId, store);
}

/**
*
* @param {String} serializedPayment
* @returns {Promise}
*/
function addSerializedPayment(serializedPayment) {
var addPaymentPromise = Promisify(Global.newtonInstance.getPaymentManager().addSerializedPayment);
return addPaymentPromise(serializedPayment);
}

module.exports = {
getOfferFor: getOfferFor,
addSerializedPayment: addSerializedPayment
};
41 changes: 41 additions & 0 deletions src/payment/test/payment.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var NewtonAdapter = require('../../main');
var Mock = require('../../../test/mock');
var getOfferFor = require('../index').getOfferFor;
var addSerializedPayment = require('../index').addSerializedPayment;
var NewtonMock;

describe('Payment', function(){
beforeEach(function(done){
Mock.boostrap();
NewtonMock = Mock.NewtonMock;
Newton = Mock.Newton;

NewtonAdapter.init({
secretId: '<local_host>',
enable: true,
waitLogin: false
}).then(function(){
done();
}).catch(function(reason){
done.fail(reason);
});
})


it('getOfferFor', function(done){
getOfferFor('test_subscription', 'googlePlay')
.then(function(offerId) {
expect(offerId).toEqual('offerIdMock');
done();
})
.catch(done.fail)
});

it('addSerializedPayment', function(done){
addSerializedPayment('serializedPayment')
.then(function(){
done();
})
.catch(done.fail);
});
});
24 changes: 24 additions & 0 deletions src/promisify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var Promise = require('promise-polyfill');
/**
* Promisify the node js style callbacks
* @param {Function} func
* @returns {Function}
*/
module.exports = function promisify(func) {
return function promisified() {
var args = [].slice.call(arguments);
return new Promise(function(resolve, reject) {
var callback = function() {
var realArgs = [].slice.call(arguments);
if(realArgs[0]) {
reject(realArgs[0]);
return;
}
realArgs.shift();
resolve.apply(null, realArgs);
};
args[args.length] = callback;
func.apply(this, args);
});
};
}
17 changes: 17 additions & 0 deletions src/push/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var Promise = require('promise-polyfill');
var Global = require('../global');

function registerDevice() {
return new Promise(function(resolve, reject) {
Global.newtonInstance.getPushManager().registerDevice(resolve, reject);
});
}

function setPushCallback(callback) {
Global.newtonInstance.getPushManager().setCallback(callback);
}

module.exports = {
registerDevice: registerDevice,
setPushCallback: setPushCallback
};
16 changes: 14 additions & 2 deletions test/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ var Mock = {
isUserLogged: function(){ Mock.calls.push('isUserLogged'); return Mock.logged; },
rankContent: function(){ Mock.calls.push('rankContent'); },
getUserToken: function(){ Mock.calls.push('getUserToken'); },
setUserStateChangeListener: function(callback){ Mock.calls.push('setUserStateChangeListener'); callback.call(); },
setUserStateChangeListener: function(callbacks){
Mock.calls.push('setUserStateChangeListener'); callbacks.onLoginStateChange();
},
userLogout: function(){ Mock.calls.push('userLogout'); },
finalizeLoginFlow: function(callback){ Mock.calls.push('finalizeLoginFlow'); callback.call(); },
// login methods
Expand Down Expand Up @@ -57,7 +59,17 @@ var Mock = {
startForgotFlow: function(){ Mock.calls.push('startForgotFlow'); return this; },
setLogViewInfo: function(){ Mock.calls.push('setLogViewInfo'); return this; },
setUserProperties: function () { Mock.calls.push('setUserProperties'); return this; },
getEmailSignupFlow: function () { Mock.calls.push('getEmailSignupFlow'); return this; }
getEmailSignupFlow: function () { Mock.calls.push('getEmailSignupFlow'); return this; },
getPaymentManager: function() {
return {
getOfferFor: function(str, str2, fn) {
fn(null, 'offerIdMock');
},
addSerializedPayment: function(str, fn) {
fn();
}
};
}
};
Mock.Newton = {
getSharedInstanceWithConfig: function(){ Mock.calls.push('getCustomFlow'); return Mock.NewtonMock; },
Expand Down

0 comments on commit 6480a41

Please sign in to comment.