How to automatically process your commissions as an advertiser - 2Parale/2Performant-php GitHub Wiki
This is a very common task that you can accomplish. There are 2 ways you can achieve this effect.
WARNING! You can update only the value of pending commissions. Please update it before accepting/rejecting it, wait for the HTTP call to finish, and then make the call to accept or reject.
Hook into your ecommerce app
We recommend changing a commission's value and status as soon as an order is updated. Assuming that you already have:
- the order ID (in a variable called
$orderId
) - the order status (in a variable called
$orderStatus
), if the amount of the order changed: - the order's old amount, without taxes, shipping and discounts (in a variable called
$orderOldAmount
), - the order's new amount, without taxes, shipping and discounts (in a variable called
$orderNewAmount
),
you should do something like below.
use TPerformant\API\HTTP\Advertiser;
use TPerformant\API\Exception\TPException;
...
$me = new Advertiser('[email protected]', 'password');
// get all commissions with that order ID
$commissions = $me->getTransaction($orderId);
try {
if($commissions) {
foreach($commisions as $c) {
if($c->getStatus() == 'pending') {
// $orderOldAmount and $orderNewAmount must not include tax, shipping and discounts
if($orderOldAmount != $orderNewAmount) {
// Commissions are returned in the defaul currency, EUR
// Adjust proportionally with the change in the order amount, and send it in EUR as well
$c->edit('Order changed', [
'amount' => round($c->getAmount() * $orderNewAmount / $orderOldAmount, 2),
'currencyCode' => 'EUR'
], $newOrderDescription)
}
switch($orderStatus) {
// These are example statuses, you have to check the values
// that your ecommerce app uses
case 'finalized':
$c->accept();
break;
case 'cancelled':
$c->reject('Order cancelled');
break;
case 'customer_return':
$c->reject('Customer returned order');
break;
// case 'in_progress':
// Do not update anything
}
}
}
}
} catch(TPException $e) {
// handle the error somehow
}