Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define("LIB_PATH", '/opt/www/riu/vb4/maps/assets/invoicing/library');
- include_once LIB_PATH . '/FreshBooks/HttpClient.php';
- //you API url and token obtained from freshbooks.com
- $url = $modx->getOption('freshbooksuri');
- $token = $modx->getOption('freshbookstoken');
- //init singleton FreshBooks_HttpClient
- FreshBooks_HttpClient::init($url,$token);
- //include particular file for entity you need (Client, Invoice, Category...)
- include_once LIB_PATH . "/FreshBooks/Client.php";
- //new Client object
- $client = new FreshBooks_Client();
- //get all the fields from the form
- $FormFields = $hook->getValues();
- //populate clientΓ’β¬β’s properties
- $client->email = $FormFields['email'];
- //explode the name
- if(is_numeric(strrpos($FormFields['fullname'],' '))){
- $name = explode(' ',$FormFields['fullname']);
- $client->firstName = $name[0];
- $client->lastName = $name[1];
- }else{
- $client->firstName = $FormFields['fullname'];
- }
- $client->username = $client->email;
- $client->password = $FormFields['password'];
- $client->language = 'en';
- $client->currencyCode = 'USD';
- //other company information
- $client->organization = isset($FormFields['company']) ? $FormFields['company'] : '';
- $client->workPhone = isset($FormFields['phone']) ? $FormFields['phone'] : '';
- $client->pStreet1 = isset($FormFields['address']) ? $FormFields['address'] : '';
- $client->pCity = isset($FormFields['city']) ? $FormFields['city'] : '';
- $client->pCountry = isset($FormFields['country']) ? $FormFields['country'] : '';
- $client->pCode = isset($FormFields['zip']) ? $FormFields['zip'] : '';
- $client->pState = isset($FormFields['state']) ? $FormFields['state'] : '';
- //all other required properties should be populated
- //try to create new client with provided data on FB server
- if(!$client->create()){
- //read error
- echo $client->lastError;
- }
- else{
- //investigate populated data
- $id = $client->clientId;
- }
- if(is_numeric($id)){
- unset($client);
- $profile = $hook->getValue('register.profile');
- $extended = $profile->get('extended');
- $extended['FreshBooksId'] = $id;
- $profile->set('extended', json_encode($extended));
- $profile->save();
- $client = new FreshBooks_Client();
- $client->clientId = $id;
- $client->notes = 'Maps UserId: '.$profile->get('internalKey');
- $client->update();
- }
Add Comment
Please, Sign In to add comment