How to approve affiliate sale tracking code

In this example you can see how to approve existing affiliate sale tracking code via API. Creating of new 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 approve sale tracking code
$commissionTypeID = '649baf03'; //set ID of commission type for which is the sale tracking code defined. More info here: https://support.qualityunit.com/606422-How-to-get-commissiontypeid-for-campaign-and-commissiontype

$request = new Gpf_Rpc_GridRequest("Pap_Features_AffiliateTrackingCode_MerchantCodesGrid","getRows",$session);
$request->addFilter('comtypeId', Gpf_Data_Filter::EQUALS, $commissionTypeID); //this filter is only available since version 5.5.10.1
$request->addFilter('userid', Gpf_Data_Filter::EQUALS, $userID);

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

$grid = $request->getGrid();
$recordset = $grid->getRecordset();

foreach($recordset as $rec) {
    updateStatus($rec->get('id'), $session);
    break;
}

function updateStatus($id, $session) {
    $request = new Gpf_Rpc_Request("Pap_Features_AffiliateTrackingCode_MerchantCodesForm", "saveFields", $session);

    $recordset = new Gpf_Data_RecordSet();

    $recordset->setHeader(array('id', 'name', 'value'));
    $recordset->add(array($id, 'rstatus', 'A')); //set desired status you want to set here, A=approved, P=pending, D=declined
    
    $request->addParam('fields', $recordset);

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

?>