How to assign coupon using API

There are 2 possible ways to assign coupons to affiliate using API:
 
1) Merchant assigns an exact coupon to a particular affiliate
 
In this case you have to load the coupons via php API while you have to find out the IDs of so far not assigned coupons
So in the example available through that hyperlink above, you will have to change 
$rec->get('userid'))!=NULL
 
to 
 
$rec->get('userid'))==NULL
 
Notice, that the "!=" changed to "==" (double equation sign)
 
You will also use there in that 'foreach' loop " $rec->get('id') " to get the IDs of so far not assigned coupons
 
And then you may use the following function to assign a particular coupon to a particular affiliate:
 
<?php
include 'PapApi.class.php'; 

$session = new Pap_Api_Session('url_to_your_pap_installation/scripts/server.php');

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

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

$recordset = new Gpf_Data_RecordSet();
$recordset->setHeader(array('id', 'name', 'value'));
$recordset->add(array('here_comes_the_coupon_ID', 'userid', 'here_comes_the_userid_of_the_affiliate'));

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

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

$response = $request->getStdResponse();

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

?>
 
2) Affiliate requests a coupon
 
In this case you will not be able to assign an exact coupon code to the particular affiliate. It works the same way as if the affiliate requested a coupon in the affiliate panel  -- the first available coupon code will be automatically chosen and assigned to him. 
 
Here is an example:
you have one Coupon C1 with these coupon codes:
C1-000-0001
C1-000-0002
C1-000-0003
 
You want to assign one coupon to affiliate A1. You call the code below, and first available coupon code from codes of C1 will be chosen and assigned to A1. If you cal the code 3 times, then every next code will be assigned to the particular affiliate.
 
Code example:
<?php
include 'PapApi.class.php';
$session = new Pap_Api_Session('url_to_your_pap_installation/scripts/server.php');
if(!@$session->login('affiliate_name', 'affiliate_password', Pap_Api_Session::AFFILIATE)) {
  die("Cannot login. Message: ". $session->getMessage());
}

$bannerId = 'b19fc1e9';  //<--insert coupon code from banner manager here!

$request = new Gpf_Rpc_ActionRequest('Pap_Features_Coupon_PreviewData', 'assignCoupon', $session);

$request->setField('id', $bannerId);

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

$answer = $request->getResponseObject()->toObject();
if ($answer->success != GPf::YES) {
  echo 'Error occured: ' . $answer->errorMessage; //<--this will print error code to the screen!
}

?>
In example above:
  • you must include our PapApi.class.php (find it in your merchant panel at Start > Tools > Integration > API Integration)
  • you must login as affiliate, to which you want to assign some coupon codes
  • you must define the couponId - id of the coupon banner in the Banner manager
 
If you want to load coupon details after assignment of coupon, you can use method: 'assignCouponLoadDetails':
 
<?php
include 'PapApi.class.php';
$session = new Pap_Api_Session('url_to_your_pap_installation/scripts/server.php');
if(!@$session->login('affiliate_name', 'affiliate_password', Pap_Api_Session::AFFILIATE)) {
  die("Cannot login. Message: ". $session->getMessage());
}

$bannerId = 'b19fc1e9';  //insert here coupon banner id

$request = new Gpf_Rpc_FormRequest('Pap_Features_Coupon_PreviewData', 'assignCouponLoadDetails', $session);

$request->setField('id', $bannerId);

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

$responseForm = $request->getForm();

if ($responseForm->isSuccessful()) {
  echo $responseForm->getFieldValue('couponid');
  echo '<br>';
  echo $responseForm->getFieldValue('couponcode');
  echo '<br>';
  echo $responseForm->getFieldValue('bannerid');
  echo '<br>';
  echo $responseForm->getFieldValue('campaignid');
  echo '<br>';
  echo $responseForm->getFieldValue('userid');
  echo '<br>';
  echo $responseForm->getFieldValue('rstatus');
  echo '<br>';
  echo $responseForm->getFieldValue('validfrom');
  echo '<br>';
  echo $responseForm->getFieldValue('validto');
  echo '<br>';
  echo $responseForm->getFieldValue('usecount');
  echo '<br>';
  echo $responseForm->getFieldValue('maxusecount'); //limit_use
  echo '<br>';
  echo $responseForm->getInfoMessage();
} else {
  echo $responseForm->getErrorMessage();
}

?>