/**
  * returns API response array()
  */
 public static function restApi($action = null, $data = null)
 {
     try {
         $hipay = new HiPay_Tpp();
         HipayLogger::addLog($hipay->l('API call initiated', 'hipay'), HipayLogger::APICALL, 'Action : ' . $action . ' - Data : ' . Tools::jsonEncode($data));
         if ($action == null) {
             Tools::redirect('index.php?controller=order&xer=6');
         }
         if ($data == null) {
             Tools::redirect('index.php?controller=order&xer=7');
         }
         define('API_ENDPOINT', HipayClass::getAPIURL());
         define('API_USERNAME', HipayClass::getAPIUsername());
         define('API_PASSWORD', HipayClass::getAPIPassword());
         $credentials = API_USERNAME . ':' . API_PASSWORD;
         $resource = API_ENDPOINT . $action;
         // create a new cURL resource
         $curl = curl_init();
         // set appropriate options
         $options = array(CURLOPT_URL => $resource, CURLOPT_USERPWD => $credentials, CURLOPT_HTTPHEADER => array('Accept: application/json'), CURLOPT_RETURNTRANSFER => true, CURLOPT_FAILONERROR => false, CURLOPT_HEADER => false, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $data, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false);
         foreach ($options as $option => $value) {
             curl_setopt($curl, $option, $value);
         }
         $result = curl_exec($curl);
         $status = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE);
         $response = Tools::jsonDecode($result);
         // execute the given cURL session
         if (false === $result) {
             $errorCurl = curl_error($curl);
             throw new Exception('Curl error: ' . $errorCurl);
         }
         if (floor($status / 100) != 2) {
             throw new Exception('Hipay message: ' . $response->message, $response->code);
         }
         curl_close($curl);
         HipayLogger::addLog($hipay->l('API call success', 'hipay'), HipayLogger::APICALL, 'Appel vers API avec success : ' . Tools::jsonEncode($response));
         return $response;
     } catch (Exception $e) {
         HipayLogger::addLog($hipay->l('API call error', 'hipay'), HipayLogger::ERROR, $e->getMessage());
         return $e;
     }
 }
 /**
  * returns API response array()
  */
 public static function restMaintenanceApi($transaction_reference = null, $data = null)
 {
     try {
         $hipay = new HiPay_Tpp();
         HipayLogger::addLog($hipay->l('API Refund call initiated', 'hipay'), HipayLogger::APICALL, 'Transaction_reference : ' . $transaction_reference . ' - Data : ' . Tools::jsonEncode($data));
         if ($transaction_reference == null) {
             return 'Error - No transaction reference';
         }
         if ($data == null) {
             return 'Error - No data';
         }
         define('API_ENDPOINT', HipayClass::getAPIURL());
         define('API_USERNAME', HipayClass::getAPIUsername());
         define('API_PASSWORD', HipayClass::getAPIPassword());
         $credentials = API_USERNAME . ':' . API_PASSWORD;
         $resource = API_ENDPOINT . 'maintenance/transaction/' . $transaction_reference;
         // create a new cURL resource
         $curl = curl_init();
         // set appropriate options
         $options = array(CURLOPT_URL => $resource, CURLOPT_USERPWD => $credentials, CURLOPT_HTTPHEADER => array('Accept: application/json'), CURLOPT_RETURNTRANSFER => true, CURLOPT_FAILONERROR => false, CURLOPT_HEADER => false, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $data, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false);
         foreach ($options as $option => $value) {
             curl_setopt($curl, $option, $value);
         }
         $result = curl_exec($curl);
         $status = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE);
         $response = Tools::jsonDecode($result);
         // execute the given cURL session
         if (false === $result) {
             throw new Exception(curl_error($curl));
         }
         if (floor($status / 100) != 2) {
             throw new Exception('Err Msg : ' . $response->message . ', Err Desc : ' . $response->description . ', Err Code : ' . $response->code);
         }
         curl_close($curl);
         HipayLogger::addLog($hipay->l('API call success', 'hipay'), HipayLogger::APICALL, 'Appel vers API avec success : ' . mysql_real_escape_string(Tools::jsonEncode($response)));
         return $response;
     } catch (Exception $e) {
         HipayLogger::addLog($hipay->l('API call error', 'hipay'), HipayLogger::ERROR, mysql_real_escape_string($e->getMessage()));
         return false;
     }
 }
 public static function createToken($cardNumber = null, $cardHolder = null, $cardExpiryMonth = null, $cardExpiryYear = null, $cardSecurityCode = null, $firstname = null, $lastname = null, $paymentproductswitcher = null)
 {
     try {
         $hipay = new HiPay_Tpp();
         HipayLogger::addLog($hipay->l('Token Create call initiated', 'hipay'), HipayLogger::APICALL, 'Action : Create Token');
         define('API_ENDPOINT_TOKEN', HipayClass::getAPITokenURL());
         define('API_USERNAME_TOKEN', HipayClass::getAPIUsername());
         define('API_PASSWORD_TOKEN', HipayClass::getAPIPassword());
         $credentials_token = API_USERNAME_TOKEN . ':' . API_PASSWORD_TOKEN;
         $resource_token = API_ENDPOINT_TOKEN . 'create';
         // Multi_use : only boolean
         // 0 = Generate a single-use token
         // 1 = Generate a multi-use token (default)
         $multi_use = 1;
         if ($paymentproductswitcher == 'american-express') {
             $data_token = array('card_number' => $cardNumber, 'card_expiry_month' => $cardExpiryMonth, 'card_expiry_year' => $cardExpiryYear, 'firstname' => $firstname, 'lastname' => $lastname, 'cvc' => $cardSecurityCode, 'multi_use' => $multi_use);
         } elseif ($paymentproductswitcher == 'bcmc') {
             $data_token = array('card_number' => $cardNumber, 'card_expiry_month' => $cardExpiryMonth, 'card_expiry_year' => $cardExpiryYear, 'card_holder' => $cardHolder, 'multi_use' => $multi_use);
         } else {
             $data_token = array('card_number' => $cardNumber, 'card_expiry_month' => $cardExpiryMonth, 'card_expiry_year' => $cardExpiryYear, 'card_holder' => $cardHolder, 'cvc' => $cardSecurityCode, 'multi_use' => $multi_use);
         }
         // create a new cURL resource
         $curl_token = curl_init();
         // set appropriate options
         $options_token = array(CURLOPT_URL => $resource_token, CURLOPT_USERPWD => $credentials_token, CURLOPT_HTTPHEADER => array('Accept: application/json'), CURLOPT_RETURNTRANSFER => true, CURLOPT_FAILONERROR => false, CURLOPT_HEADER => false, CURLOPT_POST => true, CURLOPT_POSTFIELDS => http_build_query($data_token), CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false);
         foreach ($options_token as $option => $value) {
             curl_setopt($curl_token, $option, $value);
         }
         $result_token = curl_exec($curl_token);
         $status_token = (int) curl_getinfo($curl_token, CURLINFO_HTTP_CODE);
         $response_token = Tools::jsonDecode($result_token);
         // execute the given cURL session
         if (false === $result_token) {
             throw new Exception(curl_error($curl_token));
         }
         if (floor($status_token / 100) != 2) {
             throw new Exception($status_token);
         }
         curl_close($curl_token);
         HipayLogger::addLog($hipay->l('Token Create call success', 'hipay'), HipayLogger::APICALL, 'Creation token avec success');
         return $response_token;
     } catch (Exception $e) {
         HipayLogger::addLog($hipay->l('Token Create call status error', 'hipay'), HipayLogger::ERROR, Db::getInstance()->escape($e->getMessage()));
         return $e->getMessage();
     }
 }