@arc-publishing/sdk-swg

SDK for using Subscribe with Google with Arc Subscriptions

Usage no npm install needed!

<script type="module">
  import arcPublishingSdkSwg from 'https://cdn.skypack.dev/@arc-publishing/sdk-swg';
</script>

README

@arc-publishing/sdk-swg

The Arc SwG SDK is an out of the box SDK to integrate Subscribe with Google with your Front End article/paywall, checkout and login pages.

This SDK is responsible for the following:

  • initializes SwG.js when a visitor loads the page
  • handles various Subscribe with Google flows:
    • Syncing entitlements between Arc and Google & vice versa on article reads and on login
    • Handle purchase responses from Google’s web Subscribe with Google flows to sync Subscribe with Google purchases with Arc
  • provides callbacks for your application to use to: - handle syncing on login - linking an Arc purchase of a SwG enabled product with SwG - pass to Arc Paywall to check Google entitlements for paywalling purposes

Pre-requisites

  • Article pages are annotated with SwG JSON-LD annotations
  • swg.js is loaded on the page
<script
  async
  subscriptions-control="manual"
  src="https://news.google.com/swg/js/v1/swg.js"
></script>
  • Google's Auth2 SDK is loaded on the page
<script>
  window.ongoogleload = function() {
    window.gapi.load('auth2');
  };
</script>
<script src="https://apis.google.com/js/platform.js" onload="window.ongoogleload()"></script>
  • Arc Identity and Sales SDKs are installed in your application and used for auth and checkout.

Usage

import initSwg from '@arc-publishing/sdk-swg/lib';
initSwg({
  Identity, // required: fully initialized Arc Identity SDK
  Sales, // required: fully initialized Arc Sales SDK
  googleAuth, // required: fully initialized Google Auth2 SDK
  swgSku, // required: swg sku, in this format: "{site}:{sku}", i.e. "example.com:premium"
  onSwgPaymentSuccess, // required: callback function for when a user successfully checks out from Subscribe with Google dialogs. Usually used to reroute the user to the homepage, or the
  onLoginRequest, // required: callback function for when a user clicks "already subscribed" on SwG checkout screens. This should direct the user to the login screen on your site.
  customOnRejectConsent, // optional: custom handler for user rejecting consent
  customEntitlementsHandler, // optional: custom handler for comparing swg entitlement to user entitlements which should take care of any necessary logging in, linking or creating new accounts to ensure user is allowed access if the entitlement exists.
  customSwgPaymentHandler, // optional: custom handler for ensuring Arc and google are aware of swg subscription purchases
  customLoginHandler, // optional: custom handler for linking Arc subscriptions to SwG or vice versa when a user logs in.
  customIsSwgEntitledCheck, // optional: custom handler for checking whether or not a logged in user has a SwG entitlement.
  customLinkToSwgHandler //optional: custom handler for linking an Arc purchase of a SwG enabled product to a user's Google account
}).then(
  ({
    swg, // swg subscriptions object. see: https://github.com/subscriptions-project/swg-js/blob/master/docs/core-apis.md
    entitlementsHandler, // returns either the default SwG entitlements handler, or your custom entitlements handler
    isSwgEntitledCheck, // returns either the default isSwgEntitledCheck, or your custom check. This should be passed to ArcP.run() as an option for checking whether or not a logged in user has a SwG entitlement.
    loginHandler, // returns either the default loginHandler, or your custom login handler. This is used to link Arc subscriptions to SwG or vice versa when a user logs in.
    linkToSwgHandler // returns either the default linkToSwgHandler, or your custom linkToSwgHandler. This is used to link an Arc purchase of a SwG enabled product to a user's Google account
  }) => {
    // render your application
  }
);