/** * 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; }
} 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()); } /********************************************** * Create a recurring profile with multiple line items **********************************************/ $fb = new FreshBooksRequest('recurring.create'); $fb->post(array('recurring' => array('client_id' => 41, 'lines' => array('line' => array(array('name' => 'A prod name', 'description' => 'The description', 'unit_cost' => 10, 'quantity' => 2), array('name' => 'Another prod name', 'description' => 'The other description', 'unit_cost' => 20, 'quantity' => 1)))))); //print_r($fb->getGeneratedXML()); $fb->request(); if ($fb->success()) { $res = $fb->getResponse(); $recurrng_id = $res['recurring_id']; // Do something with the recurring_id you were returned } else { echo $fb->getError(); print_r($fb->getResponse()); }