Display campaigns, commission groups and commission settings of an affiliate via API

This is a complete sample code that shows the campaigns, commission groups, and commission settings of the commission group in which a particular affiliate is involved. 
 
<?php
//Download PapApi.class.php from the merchant panel:
// Tools > Integration > API Integration > Download PAP API

// the include_once below assumes the PapApi.class.php is in the same folder as this script
include_once 'PapApi.class.php'; 

$papURL = "https://yoursite.com/pap"; //change it for the real URL of your PAP
$merchantUsername = "merchant@username.com"; //change it for your merchant username
$merchantPassword = "merchant password"; //change it for your merchant password

//ID of the affiliate whose campaigns, commission groups you wish to display
//you can dynamically read it from a $_GET or $_POST parameter
$userid = '11111111'; 


$session = new Pap_Api_Session($papURL."/scripts/server.php");

if(!$session->login($merchantUsername,$merchantPassword)) {
  die("Cannot login. Message: ". $session->getMessage());
}


$campaigngrups = array();
$campaigns = array();
$commissionGroups = array();
$commissionGroupsDefaultByCampaignId = array();

$request = new Gpf_Rpc_GridRequest("Pap_Features_CommissionGroups_CommissionsAffiliateGrid", "getRows", $session);

$request->addParam('userid', $userid);

// sets limit to 150 rows, offset to 0 (first row starts)
$request->setLimit(0, 150);

// send request
try {
  $request->sendNow();
} catch(Exception $e) {
  die("API call error: ".$e->getMessage());
}

// request was successful, get the grid result
$grid = $request->getGrid();

// get recordset from the grid
$recordset = $grid->getRecordset();

$campaigngrups = array_merge($campaigngrups, iterateGroups($recordset));

$totalRecords = $grid->getTotalCount();
$maxRecords = $recordset->getSize();
if ($maxRecords != 0) {
  $cycles = ceil($totalRecords / $maxRecords);
  for($i=1; $i<$cycles; $i++) {
    // now get next 150 records
    $request->setLimit($i * $maxRecords, $maxRecords);
    $request->sendNow();
    $recordset = $request->getGrid()->getRecordset();
  
    $campaigngrups = array_merge($campaigngrups, iterateGroups($recordset));
  }
}

function iterateGroups($recordset) {
  $campaigngrups = array();
  // iterate through the records
  foreach($recordset as $rec) {  
    $campaigngrups['\''.$rec->get('campaignid').'\''] = $rec->get('commissiongroupid');
  }
  return $campaigngrups;
}

$request = new Gpf_Rpc_FormRequest("Pap_Db_Table_CommissionGroups", "getAllCommissionGroupsForAllCampaigns", $session);

try  {
  $request->sendNow();
} catch(Exception $e){
  die("API call error: " . $e->getMessage());
}

$responseForm = $request->getStdResponse();
$i = 0;
foreach ($responseForm->rows as $row) {
  $i++;
  if ($i==1) {
      $valuesNames = array_flip($row);
      continue;
  }
  $groupName = $row[$valuesNames['name']];
  $groupId = $row[$valuesNames['commissiongroupid']];
  $campaignId = $row[$valuesNames['campaignid']];
  $groupValue = $row[$valuesNames['commissiongroupvalue']];
  $commissionGroups[$groupId] = array($groupName, $groupValue);
  if ($groupName == 'Default commission group') {
    $commissionGroupsDefaultByCampaignId['\''.$campaignId.'\''] = array($groupId, $groupValue);
  }
}

$request = new Gpf_Rpc_GridRequest("Pap_Merchants_Campaign_CampaignsGrid", "getRows", $session);
//$request = new Gpf_Rpc_GridRequest("Pap_Affiliates_Promo_CampaignsGrid", "getRows", $session);

// adding parameters to the request to work with desired columns
$request->addParam('columns', new Gpf_Rpc_Array(array(array('id'), array('campaignid'), 
array('rtype'), array('rstatus'), array('name'), array('description'),
array('dateinserted'), array('rorder'), array('isdefault'), array('networkstatus'), 
array('logourl'), array('productid'), array('discontinueurl'), array('validfrom'), array('validto'), 
array('validnumber'), array('validtype'), array('overwritecookie'), array('accountid'), array('commissionsexist'), array('commissionsdetails'))));

// sets limit to 150 rows, offset to 0 (first row starts)
$request->setLimit(0, 150);

$request->addFilter('rstatus', '=', 'A');  //only active campaigns


// send request
try {
  $request->sendNow();
} catch(Exception $e) {
  die("API call error: ".$e->getMessage());
}

// request was successful, get the grid result
$grid = $request->getGrid();

// get recordset from the grid
$recordset = $grid->getRecordset();

$campaigns = array_merge($campaigns, iterateCampaigns($recordset));

$totalRecords = $grid->getTotalCount();
$maxRecords = $recordset->getSize();

//in case there are more than 150 campaigns, load them in cycles. 
if ($maxRecords != 0) {
    $cycles = ceil($totalRecords / $maxRecords);
    for($i=1; $i<$cycles; $i++) {
      // now get next 150 records
      $request->setLimit($i * $maxRecords, $maxRecords);
      $request->sendNow();
      $recordset = $request->getGrid()->getRecordset();
      
      $campaigns = array_merge($campaigns, iterateCampaigns($recordset));
    }
}

function iterateCampaigns($recordset) {
  $campaigns = array();
  $i = 1;
  // iterate through the records
  foreach($recordset as $rec) {    
    $campaigns['\''.$rec->get('campaignid').'\''] = array($rec->get('name'), $rec->get('rtype'), $rec->get('commissionsdetails'));
    $i ++;
  }
  return $campaigns;
}

$i = 1;
foreach ($campaigns as $campaignid=>$campaign) {
  
  if (array_key_exists($campaignid, $campaigngrups)) {
    
    echo $i."." .' campaignid: ' . $campaignid;
    echo '<br>campaignname: ' . $campaign[0];
    echo '<br>campaigntype: ' . $campaign[1]; // P = Public, M = Public with manual approval, I = Private
    echo '<br>commissiongroupID: ' . $campaigngrups[$campaignid];
    if ($campaigngrups[$campaignid] == 'notincampaign') {
        echo '<br>commissiondetails: ' . 'Affiliate is not assigned to this private or manual approval campaign (this campaign should be hidden for this affiliate)';
    } else {
        $commissionGroupDetail = $commissionGroups[$campaigngrups[$campaignid]];
        echo '<br>commissiongroupName: ' . $commissionGroupDetail[0];
        echo '<br>commissiondetails: ' . $commissionGroupDetail[1];
    }
  }
  $i ++;
  echo '<hr>';
}
?>
 
×