Adding affiliate to commission group

First script shows how to add affiliate to commission group via API.
Second script shows how to import commission groups from a .csv file.

1. This simple example assumes that you already know the desired affiliate id, campaign id and commission group id.
<?php
  include 'affiliate/api/PapApi.class.php';
  
  $session = new Pap_Api_Session("https://localhost/affiliate/scripts/server.php");
  if(!$session->login("merchant@example.com", "demo")) {
    die("Cannot login. Message: ".$session->getMessage());
  }else{
    echo "Success";
  }
  $request = new Gpf_Rpc_FormRequest("Pap_Features_CommissionGroups_AffiliateGroupForm", "add", $session);
  // Id should be blank, it is populated automatically
  $request->setField('Id','');

  // Affiliate's user id.
  $request->setField('userid','252da3f5');

  // Affiliate's status in the commission group.
  // A = Approved, R = Ascending, F = Descending, X = Fixed
  $request->setField('rstatus','A');

  // Note about the affiliate in the commission group
  $request->setField('note','');

  // Commission group id
  $request->setField('commissiongroupid','7afb3b88');

  // Campaign id in which is the commission group
  $request->setField('campaignid','11111111');
  
  try  {
    // Send the request to PAP
    $request->sendNow();
  } catch(Exception $e){
    die("API call error: " . $e->getMessage());
  }

  // Get response. Not required, but you might want to know if it was successful.  
  $response = $request->getStdResponse();
  
  // Display the response
  echo '<pre>' . print_r($response,true) . '</pre>';

?>

2. This example assumes that you have affiliate username/email, campaign ID and commission group ID saved in a .csv file in this format:
username;campaignID;groupID
affiliate@email.com;11111111;22222222

 

<?php
$papURL = "https://YOUR_ACCOUNT.postaffiliatepro.com"; //SET PATH TO YOUR POST AFFILIATE PRO ACCOUNT HERE

$merchantUsername = "merchant_email"; //ENTER YOUR MERCHANT USERNAME HERE
$merchantPassword = "merchant_password"; //ENTER YOUR MERCHANT PASSWORD HERE

$pathToApiFile = 'PapApi.class.php'; //path to PapApi.class.php file(do not edit if it's in the same folder as this script)

$handle = @fopen("YOUR_FILE_NAME.csv", "r"); //change the name or path to the .csv file here
$separatorInFile = ";"; //separator of .csv file
$aff = array();

if ($handle) {
  $i = 0;
  $changed = 0;
  while (($buffer = fgets($handle, 4096)) !== false) {
    if ($i == 0) {    // SKIP FIRST ROW WHEN CSV FILE CONTAINS HEADER. IF NOT, REMOVE THESE 4 LINES (condition (IF))
      $i++;
      continue;
    }
    $i++;
    $aff[$i] = explode($separatorInFile,$buffer);
  }
  
  if (!feof($handle)) {
      echo "Error: unexpected fgets() fail\n";
  }
  
  fclose($handle);
  
include_once ($pathToApiFile);
$session = new Pap_Api_Session($papURL."/scripts/server.php"); 
if(!@$session->login($merchantUsername, $merchantPassword)) {
    die("Cannot login. Message: ".$session->getMessage());
}
//$session->setDebug();

$cache = array();
foreach ($aff as $affiliate) {
    $username=trim($affiliate[0]);  //AFFILIATE USER NAME/EMAIL
    $campaign=trim($affiliate[1]);  //Campaign ID
    $group=trim($affiliate[2]);  //Commission group ID
    if ($username == '') {
        file_put_contents("error.log", "Error importing affiliate email: '".$username."', campaign: " .$campaign."\n", FILE_APPEND);
        continue;
    }
    $userId = getAffiliateUserid($session, $username);    
    if ($userId == '') {
        file_put_contents("error.log", "Error importing affiliate email: '".$username."', campaign: " .$campaign."\n", FILE_APPEND);
        continue;
    }
    $result = addToGroup($session, $userId, $campaign, $group);    
    if (!$result) {
        file_put_contents("error.log", "Error importing affiliate email: '".$username."', campaign: " .$campaign."\n", FILE_APPEND);
    }    
}
echo "Import finished<hr>Check the 'error.log' file for any errors that might occured\n";
}

    

function addToGroup($session, $userId, $campaign, $group) {
  $request = new Gpf_Rpc_FormRequest("Pap_Features_CommissionGroups_AffiliateGroupForm", "add", $session);
  $request->setField('Id','');

  // Affiliate's user id.
  $request->setField('userid', $userId);

  // Affiliate's status in the commission group.
  // A = Approved, R = Ascending, F = Descending, X = Fixed
  $request->setField('rstatus','A');

  // Note about the affiliate in the commission group
  $request->setField('note','');

  // Commission group id
  $request->setField('commissiongroupid', $group);

  // Campaign id in which is the commission group
  $request->setField('campaignid', $campaign);


  try  {
    $request->sendNow();
  } catch(Exception $e){
    die("API call error: " . $e->getMessage());
  }
  
  $responseForm = $request->getForm();
    
  if($responseForm->isSuccessful()) {
    return true;
  }
  file_put_contents("error.log", "Error setting group: '".$responseForm->getErrorMessage()."', group: " .$group."\n", FILE_APPEND);
  return false;
}


function getAffiliateUserid($session, $username) {
    global $cache;
    
    if (array_key_exists($username, $cache)) {
        return $cache[$username];
    }

    $request = new Pap_Api_AffiliatesGrid($session);
    $request->addFilter('username', Gpf_Data_Filter::EQUALS, $username);
    $request->setLimit(0, 30);
    $request->addParam('columns', new Gpf_Rpc_Array(array(array('id'), array('refid'), array('userid'))));

    // send request
    try {
      $request->sendNow();
    } catch(Exception $e) {
        $errorMessage = $e->getMessage(); 
        file_put_contents("error.log", date('Y-m-d H:i:s')."\n", FILE_APPEND);
        file_put_contents("error.log", "Error: " .$errorMessage."\n", FILE_APPEND);
        return;
    }
    
    $grid = $request->getGrid();
    $recordset = $grid->getRecordset();
    foreach($recordset as $rec) {
        $cache[$username] = $rec->get('userid');
        return $rec->get('userid');
    }
}

?>

Guide explaining how to retrieve the commission group id for a campaign can be found here: https://support.qualityunit.com/235875-How-to-get-commission-group-id.
 
×