PHP code example of maestrano / maestrano-php

1. Go to this page and download the library: Download maestrano/maestrano-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

maestrano / maestrano-php example snippets




Maestrano::autoConfigure('/path/to/dev-platform.json');

{
  # ===> Developer Platform Configuration
  # This is the host and base path that should be used by your environment to connect to the developer platform API.
  "dev-platform": {
    "host": "https://developer.maestrano.com",
    "api_path": "/api/config/v1/"
  },
  # => Environment credentials
  # These are your environment credentials, you can get them by connecting on the developer platform, then go on your app, they will be display under the technical view on each environment.
  "environment": {
    "api_key": "<your environment key>",
    "api_secret": "<your environment secret>"
  }
}

Maestrano::autoConfigure();


// Build SSO request - Make sure GET parameters gets passed
// to the constructor
$marketplace = $_GET['marketplace'];
$req = Maestrano_Saml_Request::with($marketplace)->new($_GET);

// Redirect the user to Maestrano Identity Provider
header('Location: ' . $req->getRedirectUrl());


session_start();

$marketplace = $_GET['marketplace'];

// Build SSO Response using SAMLResponse parameter value sent via
// POST request
$resp = Maestrano_Saml_Response::with($marketplace)->new($_POST['SAMLResponse']);

if ($resp->isValid()) {
    // Get the user as well as the user group
    $user = new Maestrano_Sso_User($resp);
    $group = new Maestrano_Sso_Group($resp);

    //-----------------------------------
    // No database model in this project. We just keep the
    // relevant details in session
    //-----------------------------------
    $_SESSION["loggedIn"] = true;
    $_SESSION["firstName"] = $user->getFirstName();
    $_SESSION["lastName"] = $user->getLastName();
    $_SESSION["marketplace"] = $_GET['marketplace'];

    // Important - Real id/email and Virtual id/email (recommended)
    // getId() and getEmail() return the actual id and email of the user which are only unique across users.
    // If you chose to use the 'virtual mode' then use getVirtualId() and getVirtualEmail().
    // They return a virtual (or composite) attribute which is truly unique across users and groups
    // Do not use the virtual email address to send emails to the user
    $_SESSION["id"] = $user->getId();
    $_SESSION["email"] = $user->getEmail();

    $_SESSION["vid"] = $user->getVirtualId();
    $_SESSION["vemail"] = $user->getVirtualEmail();

    // Store group details
    $_SESSION["groupId"] = $group->getId();
    $_SESSION["groupName"] = $group->getName();

    // Once the user is created/identified, we store the maestrano
    // session. This session will be used for single logout
    $mnoSession = new Maestrano_Sso_Session($_SESSION["marketplace"], $_SESSION, $user);
    $mnoSession->save();

    // Redirect the user to home page
    header('Location: /');
} else {
    echo "Holy Banana! Saml Response does not seem to be valid";
}

$mnoSession = new Maestrano_Sso_Session($_SESSION["marketplace"], $_SESSION);

// Trigger SSO handshake if session not valid anymore
if (!$mnoSession->isValid()) {

  // Redirect to the init URL of current marketplace
  header('Location: ' . Maestrano::with($_SESSION['marketplace'])->sso()->getInitUrl());
  
}


session_start();
session_destroy();

// Redirect to IDP logout url
$mnoSession = new Maestrano_Sso_Session($_SESSION["marketplace"], $_SESSION);
$logoutUrl = $mnoSession->getLogoutUrl();

header("Location: $logoutUrl");


Maestrano_Account_Bill

// for priceCents field
$bill->getPriceCents();
$bill->setPriceCents(2000);

$bills = Maestrano_Account_Bill::with($SESSION['marketplace'])->all();

$bills = Maestrano_Account_Bill::with($SESSION['marketplace'])->retrieve("bill-f1d2s54");

$bill = Maestrano_Account_Bill::with($SESSION['marketplace'])->create(array(
  'groupId' => 'cld-3',
  'priceCents' => 2000,
  'description' => "Product purchase"
));

$bills = Maestrano_Account_Bill::with($SESSION['marketplace'])->retrieve("bill-f1d2s54");
$bill->cancel();

Maestrano_Account_RecurringBill

// for priceCents field
$bill->getPriceCents();
$bill->setPriceCents(2000);

$recBills = Maestrano_Account_RecurringBill::with($SESSION['marketplace'])->all();

$recBills = Maestrano_Account_RecurringBill::with($SESSION['marketplace'])->retrieve("rbill-f1d2s54");

$recBill = Maestrano_Account_RecurringBill::with($SESSION['marketplace'])->create(array(
  'groupId' => 'cld-3',
  'priceCents' => 2000,
  'description' => "Product purchase",
  'period' => 'Month',
  'startDate' => (new DateTime('NOW'))
));

$recBill = Maestrano_Account_RecurringBill::with($SESSION['marketplace'])->retrieve("bill-f1d2s54");
$recBill->cancel();

Maestrano_Account_User

$users = Maestrano_Account_User::with($SESSION['marketplace'])->all();

// With configuration preset
$user = Maestrano_Account_User::with($SESSION['marketplace'])->retrieve("usr-f1d2s54");
$user->getFirstName();

Maestrano_Account_Group

$groups = Maestrano_Account_Group::with($SESSION['marketplace'])->all();

$group = Maestrano_Account_Group::with($SESSION['marketplace'])->retrieve("usr-f1d2s54");
$group->getName();

# Pass the customer group id as argument or use the default one specified in the json configuration
$client = Maestrano_Connec_Client::with($SESSION['marketplace'])->new("cld-f7f5g4")

# Retrieve all organizations (customers and suppliers) created in other applications
$resp = $client->get('/organizations')
$resp['body'] # returns the raw response "{\"organizations\":[ ... ]}"
$resp['code'] # returns the response code. E.g. "200"

# Create a new organization
$client->post('/organizations', array('organizations' => array('name' => "DoeCorp Inc.")) )

# Update an organization
$client->put('/organizations/e32303c1-5102-0132-661e-600308937d74', array('organizations' => array('is_customer_' => true)))

# Retrieve a report
$client->getReport('/profit_and_loss', array('from' => '2015-01-01', 'to' => '2015-01-01', 'period' => 'MONTHLY'))

{
  ""maestrano/maestrano-php": "2.0.*"
  }
}