Guest User

Untitled

a guest
Nov 30th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1.  
  2. <?php
  3. define("LIB_PATH", '/opt/www/riu/vb4/maps/assets/invoicing/library');
  4. include_once LIB_PATH . '/FreshBooks/HttpClient.php';
  5. //you API url and token obtained from freshbooks.com
  6. $url = $modx->getOption('freshbooksuri');
  7.  
  8. $token = $modx->getOption('freshbookstoken');
  9.  
  10. //init singleton FreshBooks_HttpClient
  11. FreshBooks_HttpClient::init($url,$token);
  12.  
  13. //include particular file for entity you need (Client, Invoice, Category...)
  14. include_once LIB_PATH . "/FreshBooks/Client.php";
  15.  
  16. //new Client object
  17. $client = new FreshBooks_Client();
  18.  
  19. //get all the fields from the form
  20.  
  21. $FormFields = $hook->getValues();
  22.  
  23. //populate clientÒ€ℒs properties
  24. $client->email = $FormFields['email'];
  25. //explode the name
  26. if(is_numeric(strrpos($FormFields['fullname'],' '))){
  27. $name = explode(' ',$FormFields['fullname']);
  28. $client->firstName = $name[0];
  29. $client->lastName = $name[1];
  30. }else{
  31. $client->firstName = $FormFields['fullname'];
  32. }
  33. $client->username = $client->email;
  34. $client->password = $FormFields['password'];
  35. $client->language = 'en';
  36. $client->currencyCode = 'USD';
  37. //other company information
  38. $client->organization = isset($FormFields['company']) ? $FormFields['company'] : '';
  39. $client->workPhone = isset($FormFields['phone']) ? $FormFields['phone'] : '';
  40. $client->pStreet1 = isset($FormFields['address']) ? $FormFields['address'] : '';
  41. $client->pCity = isset($FormFields['city']) ? $FormFields['city'] : '';
  42. $client->pCountry = isset($FormFields['country']) ? $FormFields['country'] : '';
  43. $client->pCode = isset($FormFields['zip']) ? $FormFields['zip'] : '';
  44. $client->pState = isset($FormFields['state']) ? $FormFields['state'] : '';
  45.  
  46. //all other required properties should be populated
  47.  
  48. //try to create new client with provided data on FB server
  49. if(!$client->create()){
  50. //read error
  51. echo $client->lastError;
  52. }
  53. else{
  54. //investigate populated data
  55. $id = $client->clientId;
  56. }
  57.  
  58. if(is_numeric($id)){
  59. unset($client);
  60. $profile = $hook->getValue('register.profile');
  61. $extended = $profile->get('extended');
  62. $extended['FreshBooksId'] = $id;
  63. $profile->set('extended', json_encode($extended));
  64. $profile->save();
  65. $client = new FreshBooks_Client();
  66. $client->clientId = $id;
  67. $client->notes = 'Maps UserId: '.$profile->get('internalKey');
  68. $client->update();
  69. }
Add Comment
Please, Sign In to add comment