Skip to content

Commit

Permalink
✨ add isReadyToPay to amp-subscriptions and amp-subscriptions-google. (
Browse files Browse the repository at this point in the history
…#18196)

* add isReadyToPay

* defaults
  • Loading branch information
jpettitt committed Sep 20, 2018
1 parent a80546c commit 7467569
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
Expand Up @@ -106,6 +106,9 @@ export class GoogleSubscriptionsPlatform {
this.isGoogleViewer_ = false;
this.resolveGoogleViewer_(Services.viewerForDoc(ampdoc));

/** @private {boolean} */
this.isReadyToPay_ = false;

// Install styles.
installStylesForDoc(ampdoc, CSS, () => {}, false, TAG);
}
Expand Down Expand Up @@ -161,6 +164,13 @@ export class GoogleSubscriptionsPlatform {
/** @override */
getEntitlements() {
return this.runtime_.getEntitlements().then(swgEntitlements => {
// Get and store the isReadyToPay signal which is independent of
// any entitlments existing.
if (swgEntitlements.isReadyToPay) {
this.isReadyToPay_ = true;
}

// Get the specifc entitlement we're looking for
const swgEntitlement = swgEntitlements.getEntitlementForThis();
if (!swgEntitlement) {
return null;
Expand Down Expand Up @@ -211,6 +221,8 @@ export class GoogleSubscriptionsPlatform {
switch (factorName) {
case SubscriptionsScoreFactor.SUPPORTS_VIEWER:
return this.isGoogleViewer_ ? 1 : 0;
case SubscriptionsScoreFactor.IS_READY_TO_PAY:
return this.isReadyToPay_ ? 1 : 0;
default:
return 0;
}
Expand Down
Expand Up @@ -29,6 +29,8 @@ import {
} from '../../../../third_party/subscriptions-project/config';
import {ServiceAdapter} from '../../../amp-subscriptions/0.1/service-adapter';
import {Services} from '../../../../src/services';
import {SubscriptionsScoreFactor}
from '../../../amp-subscriptions/0.1/score-factors.js';


describes.realWin('amp-subscriptions-google', {amp: true}, env => {
Expand Down Expand Up @@ -379,4 +381,97 @@ describes.realWin('amp-subscriptions-google', {amp: true}, env => {
});
});
});

describe('isReadyToPay', () => {
// #TODO(jpettitt) remove fake entitlements when swj.js
// isRadyToPay is available
/**
* return a fake entitlements object
* @param {boolean} isReadyToPay
* @return {Object}
*/
function fakeEntitlements(isReadyToPay) {
return {
isReadyToPay,
entitlements: {},
getEntitlementForThis: () => {},
};
}

it('should treat missing isReadyToPay as false', () => {
const entitlementResponse = {
source: 'google',
products: ['example.org:basic'],
subscriptionToken: 'tok1',
};
sandbox.stub(xhr, 'fetchJson').callsFake(() => {
return Promise.resolve({
json: () => {
return Promise.resolve({
entitlements: entitlementResponse,
});
},
});
});
return platform.getEntitlements().then(() => {
expect(platform
.getSupportedScoreFactor(SubscriptionsScoreFactor.IS_READY_TO_PAY))
.to.equal(0);
});
});

it('should handle isReadyToPay true', () => {
const entitlementResponse = {
source: 'google',
products: ['example.org:basic'],
subscriptionToken: 'tok1',
};
sandbox.stub(xhr, 'fetchJson').callsFake(() => {
return Promise.resolve({
json: () => {
return Promise.resolve({
isReadyToPay: true,
entitlements: entitlementResponse,
});
},
});
});
//#TODO(jpettitt) remove stub when swj.js isRadyToPay is available
sandbox.stub(platform.runtime_, 'getEntitlements')
.resolves(fakeEntitlements(true));

return platform.getEntitlements().then(() => {
expect(platform
.getSupportedScoreFactor(SubscriptionsScoreFactor.IS_READY_TO_PAY))
.to.equal(1);
});
});

it('should handle isReadyToPay false', () => {
const entitlementResponse = {
source: 'google',
products: ['example.org:basic'],
subscriptionToken: 'tok1',
};
sandbox.stub(xhr, 'fetchJson').callsFake(() => {
return Promise.resolve({
json: () => {
return Promise.resolve({
isReadyToPay: false,
entitlements: entitlementResponse,
});
},
});
});
//#TODO(jpettitt) remove stub when swj.js isRadyToPay is available
sandbox.stub(platform.runtime_, 'getEntitlements')
.resolves(fakeEntitlements(false));

return platform.getEntitlements().then(() => {
expect(platform
.getSupportedScoreFactor(SubscriptionsScoreFactor.IS_READY_TO_PAY))
.to.equal(0);
});
});
});
});
7 changes: 5 additions & 2 deletions extensions/amp-subscriptions/0.1/score-factors.js
Expand Up @@ -19,13 +19,16 @@
* @const @enum {string}
*/
export const SubscriptionsScoreFactor = {
// User is known to platform and has a form of payment registered
IS_READY_TO_PAY: 'isReadyToPay',
// Platform supports the current viewer environment
SUPPORTS_VIEWER: 'supportsViewer',
};

/**
* Optional per factor default score config values, overridden in
* on page config if needed.
* Default value for supportsViewer, requied by selectPlatformForLogin().
* All other score factors are ignored if not specifed in the publisher
* config so adding a default here would be meaningless.
*/
export const DEFAULT_SCORE_CONFIG = {
supportsViewer: 10,
Expand Down

0 comments on commit 7467569

Please sign in to comment.