This example demonstrates how to integrate server side library

Configuring and running the Server Side library

  1. Register an account or login to the Mobile Connect Developer Portal and create an application to obtain your sandbox credentials.
  2. Download the Mobile Connect Server Side project

     git clone https://github.com/Mobile-Connect/php_server_side_sdk.git
  3. Copy php-server-side-sdk and mobile-connect-sdk folders into \xampp\htdocs.
    And then run next commands from both folder (php-server-side-sdk and mobile-connect-sdk):
    install npm
    composer install
  4. Before application running you need to create 3 tables in database: discovery, nonce, discovery_cache. Discovery and nonce values will be written into these tables. The structure these tables:

    Discovery: Table for temporary DiscoveryResponse storage, Discovery Response written to database after successful Discovery request and deleted after receiving any auth response (both success and error)
    id varchar(64)
    value varchar(4096)

    Nonce fields: Table for temporary nonce storage, nonce written to database after auth request generation and deleted after receiving any token requeset response (both success and error)
    id varchar(64)
    value varchar(64)

    discovery_cache fields: Table implements discovery caching feature
    msisdn varchar(64)
    mcc varchar(4)
    mnc varchar(4)
    ip(64)
    value(4096)

  5. Open the configuration file: \php-server-side-sdk\app\data\data.json . Here are 10 parameters:

    Note: if you operate in the EU then you should use EU Discovery Service domain: eu.discover.mobileconnect.io

    {
      “msisdn”: your msisdn
      "clientID": your client Id,
      "clientSecret": your client Secret,
      "discoveryURL": your Discovery endpoint,
      "redirectURL": "http://your redirect url",
      "xRedirect": "True",
      "includeRequestIP": "False",
      "apiVersion": api version: "mc_v1.1" or "mc_v1.2",
      "scopes": scopes,
      "clientName":  your client name
    }
  6. Move to the root of the project and call
    php artisan key:generate
  7. Open the configuration file .env at the root of the project
    Set correct values for variables DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME and DB_PASSWORD
  8. Build the project.
    You can configure your application (clientID, clientSecret, discoveryURL, redirectURL). You can also configure your parameters for auth request (xRedirect, . includeRequestIP, apiVersion, scope, acrValues)
  9. Download and install any missing dependencies
  10. Build the SDK
  11. Run the MobileConnect demo

Using the Server Side Application

With your configuration specified in the configuration file you have everything you need to make a successful call to the server side application.
Example request:
If you are using only msisdn the request to your server side part will be:

 http://localhost/php-server-side-sdk/public/start_discovery?msisdn=447700900907

When you are using mcc and mnc it will be: http://localhost/php-server-side-sdk/public/start_discovery?mcc=907&mnc=07
If you choose 'None' mode: http://localhost/php-server-side-sdk/public/start_discovery

Send your request. Makes StartDiscovery method call with your configuration in parameters:

 public function StartDiscovery(Request $request) {
        $msisdn = Input::get("msisdn");
        $mcc = Input::get("mcc");
        $mnc = Input::get("mnc");
        $sourceIp = $request->header("X-Source-IP");
        return $this->AttemptDiscoveryWrapper($request, $msisdn, $mcc, $mnc, $sourceIp);
    } 

This method calls AttemptDiscovery SDK method with your parameters from config file.
If discovery request with msisdn, mcc+mnc or source ip fails - method perform Discovery UI flow to discover operator
Then:
1) msisdn mode: method generates auth URL and respond with 302 redirect to this URL. After you follow the redirect, and pass consent confirmation screen, redirect will be proceed by Server Side HandleRedirect method
 public function HandleRedirect(Request $request) {
        $mcc_mnc = Input::get("mcc_mnc");
        $code = Input::get("code");
        $state = Input::get("state");
        $databaseHelper =  new DatabaseHelper();
        $requestUri = $request->getRequestUri();
        if(!empty($code)){
            $discoveryResponse = $databaseHelper->getDiscoveryResponseFromDatabase($state);
            $nonce = $databaseHelper->getNonceFromDatabase($state);
            $response = Controller::$_mobileConnect->HandleUrlRedirectWithDiscoveryResponse($requestUri, $discoveryResponse, $state, $nonce, new MobileConnectRequestOptions());
            return $this->CreateResponse($response);
        }
        if(!empty($mcc_mnc)){
            $response = Controller::$_mobileConnect->HandleUrlRedirectWithDiscoveryResponse($requestUri, null, $state, null, new MobileConnectRequestOptions());
            $authResponse = $this->StartAuthentication($response->getSDKSession(), $response->getDiscoveryResponse()->getResponseData()['subscriber_id'],
                Controller::$_scopes, Controller::$_clientName);
            $databaseHelper->writeDiscoveryResponseToDatabase($authResponse->getState(), $response->getDiscoveryResponse());
            $databaseHelper->writeNonceToDatabase($authResponse->getState(), $authResponse->getNonce());
            return redirect($authResponse->getUrl());
        }
        $databaseHelper->clearDiscoveryCache($databaseHelper->getDiscoveryResponseFromDatabase($state));
        return $this->AttemptDiscoveryWrapper($request,null, null, null, null);
    }
 

2) mcc_mnc, sourceIp or none mode: you will be redirected to the operator selection page. Here you can type your msisdn and process page. Then the scenario should be the same as on first mode

After getting URL with 'code' and 'state' parameters application calls token endpoint with received data and get token, that could be used in authentication process in your web application

Discovery caching

Discovery response is cached in SDK with following rules:
1) After succeed Discovery request, Discovery response is cached with used mcc/mnc, msisdn or sourceIp.
2) Expiration time for Discovery Response cache is the same as "ttl" field in Discovery Response - if Discovery Response expired - that deleted from cache
3) Next requests for Discovery with the same mcc/mnc, msisdn or sourceIp - Discovery request shouldn't be performed, Discovery response should be taken from cache
4) If auth call respond with error - Discovery Response deleted from cache, and SDK redirect you to Discovery UI