CommentedClientImplementation.js

/**
 * @module Implementation Example
 */

/**
 * One should trigger zlick.identifyClient() 
 * on window.onload or similar event.
 */
window.onload =  async function () {
    try {
      let response = await zlick.identifyClient(token)
      userId = response.userId
  
      if (response.hasAccessRights) {
        /**
        * show content behind paywall
        */
      }
  
      if (response.allowedMethods.paymentsNotAllowed) {
        /**
        * payments to this user are nor allowed. It's probably because they don't have enough credit
        * or they have disabled that possibility. Fall back to other payment methods.
        */
      }
      /**
      * decide on next actions by looking into response.allowedMehtods object. user might have right to 
      * refund, unsubscribe, purchase or subscribe
      */
    } catch (error) {
      /**
      * Handle error. Error response example 
      * can be found under verifyPinCode() function
      */
    }
  }
  
  async function makePurchase() {
    try {
      let response = await zlick.purchase({token, userId})
      
      if (response.hasAccessRights) {
        /**
        * show content behind paywall
        */
      } else if (!response.hasAccessRights && response.allowedMethods.smsAuth) {
        /**
        * Seems that Zlick was not that successful with identification. SMS auth is needed
        * to identify client before placing purchase. So get users phone number and call sendPinCodeSMSToClient() method
        */
      }
      /**
      * decide on next actions by looking into response.allowedMehtods object. user might have right to 
      * refund, unsubscribe, purchase or subscribe
      */
    } catch (error) {
      /**
      * Handle error. Error response example 
      * can be found under verifyPinCode() function
      */
    }
  }
  
  async function subscribeClientToService() {
    try {
      let response = await zlick.subscribe({token, userId})
      
      if (response.hasAccessRights) {
        /**
        * show content behind paywall
        */
      } else if (!response.hasAccessRights && response.allowedMethods.smsAuth) {
        /**
        * Seems that Zlick was not that successful with identification. SMS auth is needed
        * to identify client before placing purchase. So get users phone number and call sendPinCodeSMSToClient() method
        */
      }
      /**
      * decide on next actions by looking into response.allowedMehtods object. user might have right to 
      * refund, unsubscribe, purchase or subscribe
      */
    } catch (error) {
      /**
      * Handle error. Error response example 
      * can be found under verifyPinCode() function
      */
    }
  } 
  
  async function unsubscribeClientFromService() {
    try {
      let response = await zlick.unsubscribe({token, userId})
  
      if (response.hasAccessRights) {
        /**
        * show content behind paywall
        */
      }
      /**
      * decide on next actions by looking into response.allowedMehtods object. user might have right to 
      * refund, unsubscribe, purchase or subscribe
      */
    } catch (error) {
      /**
      * Handle error. Error response example 
      * can be found under verifyPinCode() function
      */
    }
  } 
  
  async function refundPurchase() {
    try {
      /**
       * get refundReason as user input
       */
      let refundResponse = await zlick.refundPurchase({token, userId, refundReason})
  
      if (response.hasAccessRights) {
        /**
        * show content behind paywall
        */
      }
      /**
      * decide on next actions by looking into response.allowedMehtods object. user might have right to 
      * refund, unsubscribe, purchase or subscribe
      */
    } catch (error) {
      /**
      * Handle error. Error response example 
      * can be found under verifyPinCode() function
      */
    } 
  }
  
  async function sendPinCodeSMSToClient() {
    try {
      /**
       * Get user mobile phone number
       */
      let response = await zlick.sendPinCodeSMS({token, mobilePhoneNumber})
      challengeId = response.challengeId
      /**
       * Ask client to insert PIN code and call verifyPinCode()
       */
    } catch (error) {
      /**
      * Handle error. Error response example 
      * can be found under verifyPinCode() function
      */
    }
  }
  
  async function verifyPinCode() {
    try {
      /**
       * get confirmationCode from user input and add paymentMethod,
       * so zlick could make payment / subscription if needed.
       */
      let response = await zlick.verifyPinCode({token, confirmationCode, challengeId})
      userId = response.userId
      /**
       *  SMS authentication was completed successfully
       *  Decide your next actions by looking into response.allowedMethods Object
       *  and call either purchase() or subscribe() method
       */
      makePurchase()
  
    } catch(error) {
      if (error.retryAttempts && error.retryAttempts > 0) {
        /**
        * We allow 3 reties. Show user input, so they can re-enter pin code
        */
      } else {
        /**
        * Handle error, customer has had 3 retries.
        */
      }
    }
  }