PHP code example of vendasta / g-suite

1. Go to this page and download the library: Download vendasta/g-suite 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/ */

    

vendasta / g-suite example snippets


$environment = getenv("ENVIRONMENT");
if ($environment == null) {
    $environment = "DEMO";
}

$client = new Vendasta\GSuite\V1\PartnerServiceClient($environment);

// Choose environment and partnerId
$env = "DEMO";
$pid = "<PID>";

// Instantiate clients
$accountGroupClient = new AccountGroupServiceClient($env);
$gSuiteClient = new GSuitePartnerClient($env);
$salesOrdersClient = new SalesOrdersClient($env);

// Build request to create a business
$createReq = new CreateAccountGroupRequest();
$location = new AccountGroupLocation();
$location->setCompanyName("Test Company");
$location->setAddress("123 Street Name");
$location->setCity("Chicago");
$location->setState("IL");
$location->setCountry("US");
$location->setZip("88888");
$workNumber = array("(999) 999-9999");
$location->setWorkNumber($workNumber);
$createReq->setAccountGroupNap($location);
$createReq->setPartnerId($pid);

// Make call and store returned accountGroupId
$resp = $accountGroupClient->Create($createReq);
$accountGroupId = $resp->getAccountGroupId();

// Build sales order request & activate the products
// Create the request
$req = new CreateAndActivateOrderRequest();

// Create the line items
$gSuite = SalesOrdersUtils::buildLineItem('MP-6XDHVMQ84K4THNNP2Z7W2GC28VLHRC4Q');
$lineItems = array($gSuite);

// Create the custom field
$gSuiteCustomField = SalesOrdersUtils::buildGSuiteCustomField("MP-6XDHVMQ84K4THNNP2Z7W2GC28VLHRC4Q", 'testdomain123.com', 'adminusername', 'First', 'Last', '[email protected]');
$customFields = array($gSuiteCustomField);

// Create the order
$order = SalesOrdersUtils::buildOrder($pid, $accountGroupId, $lineItems, $customFields);
$req->setOrder($order);

// Call CreateAndActivateOrder
$resp = $salesOrdersClient->CreateAndActivateOrder($req);

// Poll the pending activation process using GetSalesOrder

// Build request to get Google Workspace domain information
$req = new GetDomainInformationRequest();
$req->setDomain("<DOMAIN>");

// Make request and store the DNS TXT record
$resp = $gSuiteClient->GetDomainInformation($req);
$txtRecord=  $resp->getDomainInformation()->getDnsTxtRecord();

// Choose environment and partnerId
$env = "DEMO";
$pid = "";

// Select the app ID and business ID to deactivate
$businessId = "";
$appId = "";

// The activation ID will be populated after we list all the products on the business
$activationId = "";


$client = new AccountsServiceClient($env);

// First list all of the current products activated for that business and find the one matching the appID
$listReq = new ListRequest();
$listReq->setPartnerId($pid);
$listReq->setBusinessId($businessId);
$resp = $client->List($listReq);
foreach($resp->getAccounts() as $account) {
    if ($account->getAppId() == $appId) {
        // The activation ID is necessary for deactivating an app
        $activationId = $account->getActivationId();
    }
}

// Build the deactivation request with the activation ID
$deactivationReq = new DeactivateAppRequest();
$deactivationReq->setBusinessId($businessId);
$deactivationReq->setAppId($appId);
$deactivationReq->setActivationId($activationId);
$deactivationReq->setDeactivationType(DeactivationType::DEACTIVATION_TYPE_CANCEL);

$req = new GSuite\V1\GetDomainInformationRequest();
$req->setDomain("<domain>");
$resp = $client->GetDomainInformation($req);

$req = new ListSubscriptionsRequest();
$req->setDomain("<domain>");

$resp = $client->ListSubscriptions($req);

$subscriptions = $resp->getSubscriptions();
$subscriptionID = $subscriptions[0]->getSubscriptionId();

$req = new ChangeSeatsRequest();
$req->setCustomerId("<domain>");
$req->setSubscriptionId($subscriptionID);
$req->setSeats(1);

$resp = $client->ChangeSeats($req);

$req = new UpdateSSORequest();
$req->setDomain("<domain>");
// disable SSO
$req->setEnableSso(false);

$resp = $client->UpdateSSO($req);