Example #1
0
 public function getBitrix24(&$arAccessData, &$btokenRefreshed, &$errorMessage, $arScope = array())
 {
     $btokenRefreshed = null;
     $obB24App = new \Bitrix24\Bitrix24();
     if (!is_array($arScope)) {
         $arScope = array();
     }
     if (!in_array('user', $arScope)) {
         $arScope[] = 'user';
     }
     $obB24App->setApplicationScope($arScope);
     $obB24App->setApplicationId(APP_ID);
     $obB24App->setApplicationSecret(APP_SECRET_CODE);
     // set user-specific settings
     $obB24App->setDomain($arAccessData['domain']);
     $obB24App->setMemberId($arAccessData['member_id']);
     $obB24App->setRefreshToken($arAccessData['refresh_token']);
     $obB24App->setAccessToken($arAccessData['access_token']);
     try {
         $resExpire = $obB24App->isAccessTokenExpire();
     } catch (\Exception $e) {
         $errorMessage = $e->getMessage();
         // cnLog::Add('Access-expired exception error: '. $error);
     }
     if ($resExpire) {
         // cnLog::Add('Access - expired');
         $obB24App->setRedirectUri(APP_REG_URL);
         try {
             $result = $obB24App->getNewAccessToken();
         } catch (\Exception $e) {
             $errorMessage = $e->getMessage();
             // cnLog::Add('getNewAccessToken exception error: '. $error);
         }
         if ($result === false) {
             $errorMessage = 'access denied';
         } elseif (is_array($result) && array_key_exists('access_token', $result) && !empty($result['access_token'])) {
             $arAccessData['refresh_token'] = $result['refresh_token'];
             $arAccessData['access_token'] = $result['access_token'];
             $obB24App->setRefreshToken($arAccessData['refresh_token']);
             $obB24App->setAccessToken($arAccessData['access_token']);
             // \cnLog::Add('Access - refreshed');
             $btokenRefreshed = true;
         } else {
             $btokenRefreshed = false;
         }
     } else {
         $btokenRefreshed = false;
     }
     return $obB24App;
 }
 private function get_api()
 {
     if (self::$api) {
         return self::$api;
     }
     $settings = $this->get_plugin_settings();
     $api = null;
     $require_vars = array('domain', 'clientId', 'clientSecret', 'memberId', 'authId', 'refreshId', 'endOfLife');
     foreach ($require_vars as $require_var) {
         if (empty($settings[$require_var])) {
             $this->oauth_login();
             return false;
         }
     }
     if ($settings['endOfLife'] < time()) {
         $settings = $this->refresh_token();
     }
     if (!empty($settings['domain']) && !empty($settings['clientId']) && !empty($settings['clientSecret'])) {
         // init lib
         $obB24App = new \Bitrix24\Bitrix24();
         $obB24App->setApplicationScope(array('crm'));
         $obB24App->setApplicationId($settings['clientId']);
         $obB24App->setApplicationSecret($settings['clientSecret']);
         // set user-specific settings
         $obB24App->setDomain($settings['domain']);
         $obB24App->setMemberId($settings['memberId']);
         $obB24App->setAccessToken($settings['authId']);
         $obB24App->setRefreshToken($settings['refreshId']);
         $this->log_debug('Retrieving API Info');
         try {
             $api = new \Bitrix24\CRM\Lead($obB24App);
         } catch (Exception $e) {
             $this->log_error('Failed to set up the API');
             $this->log_error($e->getCode() . ' - ' . $e->getMessage());
             $this->oauth_login();
             return null;
         }
     } else {
         $this->log_debug('API credentials not set');
         return null;
     }
     if (!is_object($api)) {
         $this->log_error('Failed to set up the API');
         return null;
     }
     $this->log_debug('Successful API response received');
     self::$api = $api;
     return self::$api;
 }