10, 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ) ) ); $http_answer = wp_remote_get($report_url, $args); if (is_wp_error($http_answer) ) return false; if($http_answer['response']['code'] == 204) return "No publisher accounts connected"; if($http_answer['response']['code'] != 200) return "Failed login"; $publishers = json_decode($http_answer['body'], true); $publisherIds = array(); foreach( $publishers as $publisher ) { if ( $publisher['id'] == $publisherid ) { return true; } $publisherIds[] = $publisher['id']; } return "Publisher id ".$publisherid." is not connected your account, publisher ids available (".implode(", ", $publisherIds).")."; } static public function downloadTransactions($username, $password, $publisherid, $mediaid, $fromTS, $tillTS) { $StartDate = date('Y-m-d', $fromTS); $EndDate = date('Y-m-d', $tillTS); $output_transactions = array(); $perPage = 250; $totalPages = 1; for( $page = 1; $page <= $totalPages; ++$page ) { $report_url = 'https://services.daisycon.com'; $report_url.= '/publishers/'.$publisherid; $report_url.= '/transactions?page='.$page.'&per_page='.$perPage.'&start='.$StartDate.'&end='.$EndDate; if (!empty($mediaid)) $report_url.= '&media_id='.$mediaid; $args = array( 'timeout' => 30, 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ) ) ); $http_answer = wp_remote_get($report_url, $args); if (is_wp_error($http_answer) || $http_answer['response']['code'] != 200) { //todo: error handling, mail to admin etc. return array(); } //determine total paged requests todo if ( $totalPages == 1) $totalPages = (int)ceil(intval($http_answer['headers']['x-total-count']) / $perPage); $report = json_decode($http_answer['body'], true); if ($report === false) { //todo: error handling, mail to admin etc. return array(); } foreach($report as $transaction) { $firstPart = array_shift($transaction['parts']); $number = str_replace('"', '', $transaction['affiliatemarketing_id']); $datetime_db = $transaction['date']; $shop_id = $transaction['program_id']; $shop_name = $transaction['program_name']; $sub_id = $firstPart['subid'] == null ? "" : $firstPart['subid']; $commission = $firstPart["commission"]; $confirmed = $firstPart["status"] == 'approved' ? $firstPart["commission"] : 0.0; $status = array( $firstPart["status"] => $firstPart["status"] ); $checkdatetime_db = strtotime($firstPart["last_modified"]); foreach($transaction['parts'] as $part) { $commission+= $part["commission"]; $confirmed+= $part["status"] == 'approved' ? $part["commission"] : 0.0; $status[$firstPart["status"]] = $firstPart["status"]; $tmpDate = strtotime($part["last_modified"]); $checkdatetime_db = $checkdatetime_db < $tmpDate ? $tmpDate : $checkdatetime_db; } if ( count( $status ) == 1 ) { $status = ucfirst(reset($status)); } else { $status = "Open"; } $output_transactions[] = array( 'network' => 'daisycon', 'number' => $number, 'datetime_db' => $datetime_db, 'sub_id' => $sub_id, 'shop_id' => $shop_id, 'shop_name' => $shop_name, 'transaction_type' => 'T', 'price' => 0, 'commission' => $commission, 'confirmed' => $confirmed, 'checkdatetime_db' => date("Y-m-d H:i:s", $checkdatetime_db), 'status' => $status ); } } return $output_transactions; } }