<?php require 'lib/FreshBooksRequest.php'; // Setup the login credentials $domain = ''; $token = ''; FreshBooksRequest::init($domain, $token); /********************************************** * Fetch all clients by a specific id **********************************************/ $fb = new FreshBooksRequest('client.list'); $fb->post(array('email' => '*****@*****.**')); $fb->request(); if ($fb->success()) { echo 'successful! the full response is in an array below'; print_r($fb->getResponse()); } else { echo $fb->getError(); print_r($fb->getResponse()); } /********************************************** * List invoices from a specific client **********************************************/ $fb = new FreshBooksRequest('invoice.list'); $fb->post(array('client_id' => 41)); $fb->request(); if ($fb->success()) { print_r($fb->getResponse()); } else { echo $fb->getError(); print_r($fb->getResponse());
/** * Final step is to import payments * @return */ public static function import_payments() { // Store the import progress $progress = get_option(self::PROGRESS_OPTION, array()); // Suppress notifications add_filter('suppress_notifications', '__return_true'); $total_records = 0; if (!isset($progress['payments_complete'])) { require_once SI_PATH . '/importers/lib/freshbooks/FreshBooksRequest.php'; FreshBooksRequest::init(self::$freshbooks_account, self::$freshbooks_token); $progress_key = 'payments_import_progress'; if (!isset($progress[$progress_key])) { $progress[$progress_key] = 1; update_option(self::PROGRESS_OPTION, $progress); } // Start importing the clients 10 at a time $fb = new FreshBooksRequest('payment.list'); $fb->post(array('page' => $progress[$progress_key], 'per_page' => 10)); $fb->request(); if (!$fb->success()) { $error = $fb->getError() == 'System does not exist.' ? self::__('Authentication error.') : $fb->getError(); self::return_error($error); } $response = $fb->getResponse(); $pages = $response['payments']['@attributes']['pages']; $total_records = $response['payments']['@attributes']['total']; $total_imported = $pages * $progress[$progress_key]; if ($progress[$progress_key] <= $pages) { foreach ($response['payments']['payment'] as $key => $payment) { self::create_payment($payment); } $progress[$progress_key]++; update_option(self::PROGRESS_OPTION, $progress); // Return the progress self::return_progress(array('authentication' => array('message' => sprintf(self::__('Attempting to import %s payments...'), $total_records), 'progress' => 75 + $progress[$progress_key]), 'payments' => array('message' => sprintf(self::__('Imported about %s payments so far.'), $total_imported), 'progress' => intval($progress[$progress_key] / $pages * 100), 'next_step' => 'payments'))); } // Mark as complete $progress['payments_complete'] = 1; update_option(self::PROGRESS_OPTION, $progress); // Complete self::return_progress(array('authentication' => array('message' => sprintf(self::__('Successfully imported %s payments...'), $total_records), 'progress' => 100), 'payments' => array('message' => sprintf(self::__('Imported %s payments!'), $total_records), 'progress' => 100, 'next_step' => 'complete'))); } // Completed previously self::return_progress(array('authentication' => array('message' => sprintf(self::__('Successfully imported %s estimates already, moving on...'), $total_records), 'progress' => 100), 'payments' => array('message' => sprintf(self::__('Successfully imported %s payments already.'), $total_records), 'progress' => 100, 'next_step' => 'complete'))); // If this is needed something went wrong since json should have been printed and exited. return; }
/** * Sets API Url and API token * * @author Ashish Kataria */ public function SetApiDetails($_freshbooksURL, $_freshbooksToken) { FreshBooksRequest::init(trim($_freshbooksURL), trim($_freshbooksToken)); }