Giter Club home page Giter Club logo

web-native-integration-ios's Introduction

iOS integration of Web Javascript SDK

Mastercard Javascript library provides Client-side APIs to complete Click-to-Pay checkout experience. Merchant partners can use this Sample Merchant application as a guide to integrate Mastercard Javascript library with native mobile application.

Table of Contents

The sample app shows integration of Mastercard Javascript methods which can be used to perform Recognized User, Recognition of a User through Email lookup or First Time User Checkout flow. Integrators can test the different checkout flows from the sample Merchant screen as below:

Web Integration

  1. Download Mastercard Javascript library by adding <script> tag on index.html
<script src="https://sandbox.src.mastercard.com/srci/merchant/2/lib.js?dpaId={DpaId}&locale=en_US"></script>
  1. Add srcDpaId on NewMerchantViewController.swift

IMPORTANT: Do not build the sample app without providing srcDpaId. Failing to do so will block user from calling any Mastercard APIs.

var srcDpaId = ""

It is important to have the same DpaId on both index.html and NewMerchantViewController.swift before building the Sample app.

  1. Native to Web Layer: API calls from native layer are invoked by calling the evaluateJavascript method on webView object.

Merchant application converts the request object to JSON string before invoking Javascript APIs.

Example: init API called from WebViewIntegration.swift and API invocation completes on Javascript library.

self.evaluateJavaScript("initSdk(\"ios\", JSON.stringify(\(initRequestString)))")
  1. API call inside Web layer:

Example: init API being invoked on click2Pay object provided by Mastercard Javascript library.

function initSdk(platform, reqString) {
  console.log('inside init:')
  console.log("init req: "+ reqString);

  let request = JSON.parse(reqString);
  let promise = new Promise(
    function(resolve, reject) {
      click2payInstance.init(request).then(resolve)
    }
  );
  promise.then(
    value => {
      sendMessageToNative(platform, value, "init");
    },
    error => {
      sendMessageToNative(platform, error, "init");
      console.log('Init API rejected '+ error)
    }
  );
}
  1. Web Layer to Native: Once the API call is complete, the results are passed back to native layer from web layer using WebView.JavascriptInterface

Example: init API results being sent from javascript function back to Native Android activity. Results are received on JavascriptInterface on Android Activity.

function sendMessageToNative(json, methodName){
  console.log("sending value to native...")
    var formattedJSON = JSON.stringify(json, null, 2);
    window.webkit.messageHandlers.jsMessageHandler.postMessage(formattedJSON);
}
  1. New User Checkout - To test this checkout flow call APIs in this order on Sample App: init, encryptCard, checkoutWithNewCard. Once checkout is initiated, this will launch DCF window and user can complete checkout.

  2. Return User Checkout (Unrecognized flow) - To test this checkout flow call APIs in this order on Sample App: init, idLookup, initiateValidation, validate, getCards, checkoutWithCard

  3. Recognized User Checkout(Remembered User flow) - To test this checkout flow either New user checkout or Return user checkout flow must be completed with "Remember Me" option selected during checkout experience. Now the cookie is persisted on webView and user can test Recognized user flow by calling these APIs - init, getCards, checkoutWithCard

Sample app provides optional styling of DCF window to be displayed as action sheet. This action sheet style mode can be enabled/disabled by selecting the checkbox located on top of the Merchant screen.

DCF Action Sheet Mode DCF Regular Mode
Action Sheet Regular Sheet

web-native-integration-ios's People

Contributors

venkatanagendla avatar

Watchers

James Cloos avatar Kieran Tully avatar Jean-Alexis Aufauvre avatar Kaushik Ragavan avatar Deepak Divakaran avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.