Import lifetime relationships

Sometimes when you are using the lifetime commissions feature (and you have a lot of new affiliates with their own customers), you may want to import all their relationships to PAP at once.

 

In this case, we made the following script which imports relationships from a CSV file. The CSV file has to be in this exact format (below you can download an example).

 

In the API script you have to replace PAP URL, merchant's username and password with your own details.

$papURL = "https://your_pap_url.com";

$merchantUsername = "merchant@example.com";

$merchantPassword = "demo";

<?php
$papURL = "https://your_pap_url.com"; //PAP URL without trailing slash

$merchantUsername = "merchant@example.com"; //enter merchant username
$merchantPassword = "demo"; //enter merchant password

$pathToApiFile = 'PapApi.class.php';

$handle = @fopen("import_lifetime_from_file.csv", "r");
$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) {
    //HERE COMES THE IMPORT FUNCTION createAffiliate() with essential parameters    
    $username=trim($affiliate[0]);  //AFFILIATE USER NAME/EMAIL
    $customerEmail=trim($affiliate[1]);  //EMAIL OF CUSTOMER/LITETIME IDENTIFIER
    if ($username == '') {
        file_put_contents("error.log", "Error importing affiliate email: '".$username."', customer identifier: " .$customerEmail."\n", FILE_APPEND);
        continue;
    }
    $userId = getAffiliateUserid($session, $username);    
    if ($userId == '') {
        file_put_contents("error.log", "Error importing affiliate email: '".$username."', customer identifier: " .$customerEmail."\n", FILE_APPEND);
        continue;
    }
    //createAffiliate($session, $refid, $username, $firstname, $lastname, $parentuserid, $street, $city, $state, $country, $zip, $phone, $paypalemail);    
    $result = createAffiliateRelation($session, $userId, $customerEmail);    
    if (!$result) {
        file_put_contents("error.log", "Error importing affiliate email: '".$username."', customer identifier: " .$customerEmail."\n", FILE_APPEND);
    }    
}
echo "Import finished<hr>Check the 'error.log' file for any errors that might occured\n";
}

    
//fields set as mandatory in merch.panel: 
//refid, data3=street,data4=city,data5=state,data6=country,data7=zipcode,data8=phone + force choosing payout option, 
function createAffiliateRelation($session, $userId, $customerEmail) {
      $request = new Gpf_Rpc_FormRequest('Pap_Features_LifetimeReferralsManager_LifetimeReferralsForm', 'add', $session);
  // Set the commissiontype id for which we want to get commission settings
  $request->setField('Id', '');
  $request->setField('userid', $userId);
  $request->setField('campaignid', '');
  $request->setField('bannerid', '');
  $request->setField('channelid', '');
  $request->setField('identifier', $customerEmail);
  
  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 creating lifetime record: '".$responseForm->getErrorMessage()."', customer identifier: " .$customerEmail."\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');
    }
}

?>

NOTE 1: don't forget to upload Php.Api.class.php script to the directory from which your API script will be triggered. You can find Php.Api.class.php in your merchant panel > Tools > Integration > API integration

NOTE 2:  API script, CSV file and Php.Api.class.php should be uploaded in the same directory.

 

Download the CSV file example:

×