How to create affiliate sale tracking code

In this example you can see how to create new affiliate sale tracking code for a specific affiliate and specific campaign via API. By default each sale tracking code is created as Pending and merchant need to approve it. How to approve existing affiliate sale tracking code via API is described here.

<?php

//the PapApi.class.php can be downloaded from the merchant panel at Tools > Integration > API Integration > Download PAP API
include 'PapApi.class.php'; //this include assumes the PapApi.class.php is in the same directory as this api script itself

$session = new Pap_Api_Session("http://URL_to_your_Post_Affilite_Pro/server/scripts/server.php"); //set to your Post Affiliate Pro installation URL

if(!$session->login('merchant@username.com', 'password')) { //set your real merchant username/email and password
  die("Cannot login. Message: ". $session->getMessage());
}

$userID = '96beb999'; //set user ID of the affiliate for which you want to create sale tracking code
$campaignID = '11111111'; //set ID of the campaign 
$commissionTypeID = '649baf03'; //set ID of commission type in your campaign. More info here: https://support.qualityunit.com/606422-How-to-get-commissiontypeid-for-campaign-and-commissiontype

$request = new Gpf_Rpc_Request("Pap_Features_AffiliateTrackingCode_AffiliateCodesForm", "saveFields", $session);

$recordset = new Gpf_Data_RecordSet();
$recordset->setHeader(array('id', 'name', 'value'));
$recordset->add(array('NEW_'.$commissionTypeID, 'code', 'http://www.google.com')); //replace http://www.google.com with desired sale tracking code
$recordset->add(array('NEW_'.$commissionTypeID, 'rtype', 'C')); //set type of sale tracking code: C=call url, S=execute javascript, H=HTML code
$recordset->add(array('NEW_'.$commissionTypeID, 'execution', 'C')); //if using type C(call url) then you have to set when it should be called, C=on commission creation, A=on commission approval
$recordset->add(array('NEW_'.$commissionTypeID, 'userid', $userID));

$request->addParam('fields', $recordset);
$request->addParam('campaignId', $campaignID);

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

$response = $request->getStdResponse();

if ($response->success == 'Y') {
  echo 'Success: ' . $response->infoMessage. '<br>';
} else {
  echo 'Unsuccess: ' . $response->errorMessage. '<br>';
}

?>