コード例 #1
0
 public function do_call($params = false)
 {
     $this->reset_call_result();
     if (empty($params) or !is_array($params)) {
         $params = array();
     }
     if (empty($params['user_agent']) or !is_string($params['user_agent'])) {
         $params['user_agent'] = '';
     }
     if (empty($params['allow_remote_calls'])) {
         $params['allow_remote_calls'] = false;
     }
     if (empty($params['quick_return_request'])) {
         $params['quick_return_request'] = false;
     }
     if (empty($params['custom_validators']) or !is_array($params['custom_validators'])) {
         $params['custom_validators'] = array();
     }
     if (empty($this->_method)) {
         $this->set_error(self::ERR_METHOD, self::s2p_t('Method not set'));
         return false;
     }
     if (!($api_key = $this->get_api_key())) {
         $this->set_error(self::ERR_APIKEY, self::s2p_t('API Key not set.'));
         return false;
     }
     if (!($site_id = $this->get_site_id())) {
         $this->set_error(self::ERR_SITE_ID, self::s2p_t('Site ID not set.'));
         return false;
     }
     if (!$this->validate_base_url() or empty($this->_base_url)) {
         $this->set_error(self::ERR_URL, self::s2p_t('Couldn\'t obtain base URL.'));
         return false;
     }
     if (!($request_data = $this->_method->prepare_for_request($params))) {
         if ($this->_method->has_error()) {
             $this->copy_error($this->_method);
         } else {
             $this->set_error(self::ERR_PREPARE_REQUEST, self::s2p_t('Couldn\'t prepare request data.'));
         }
         return false;
     }
     if ($hook_result = $this->trigger_hooks('rest_api_prepare_request_after', array('api_obj' => $this, 'request_data' => $request_data)) and is_array($hook_result)) {
         if (array_key_exists('request_data', $hook_result)) {
             $request_data = $hook_result['request_data'];
         }
     }
     $final_url = $this->_base_url . $request_data['full_query'];
     if (!empty($params['quick_return_request'])) {
         $return_arr = array();
         $return_arr['final_url'] = $final_url;
         $return_arr['request_data'] = $request_data;
         return $return_arr;
     }
     if (!($this->_request = new S2P_SDK_Rest_API_Request())) {
         $this->set_error(self::ERR_PREPARE_REQUEST, self::s2p_t('Couldn\'t prepare API request.'));
         return false;
     }
     if (empty($request_data['http_method']) or !$this->_request->set_http_method($request_data['http_method'])) {
         $this->set_error(self::ERR_HTTP_METHOD, self::s2p_t('Couldn\'t set HTTP method.'));
         return false;
     }
     if (!$this->_request->set_url($final_url)) {
         $this->set_error(self::ERR_URL, self::s2p_t('Couldn\'t set final URL.'));
         return false;
     }
     if (!empty($request_data['request_body'])) {
         $this->_request->set_body($request_data['request_body']);
     }
     $this->_request->add_header('Content-Type', 'application/json; charset=utf-8');
     $call_params = array();
     if (empty($params['user_agent'])) {
         $call_params['user_agent'] = 'APISDK_' . S2P_SDK_VERSION . '/PHP_' . phpversion() . '/' . php_uname('s') . '_' . php_uname('r');
     } else {
         $call_params['user_agent'] = trim($params['user_agent']);
     }
     $call_params['userpass'] = array('user' => $site_id, 'pass' => $api_key);
     $this->trigger_hooks('rest_api_call_before', array('api_obj' => $this));
     if (!($request_result = $this->_request->do_curl($call_params))) {
         $request_result = null;
     }
     if ($hook_result = $this->trigger_hooks('rest_api_request_result', array('api_obj' => $this, 'request_result' => $request_result)) and is_array($hook_result)) {
         if (array_key_exists('request_result', $hook_result)) {
             $request_result = S2P_SDK_Rest_API_Request::validate_request_array($hook_result['request_result']);
         }
     }
     if (empty($request_result)) {
         if ($this->_request->has_error()) {
             $this->copy_error($this->_request);
         } else {
             $this->set_error(self::ERR_CURL_CALL, self::s2p_t('Error sending API call'));
         }
         return false;
     }
     $return_arr = self::default_call_result();
     $return_arr['final_url'] = $final_url;
     $return_arr['request'] = $request_result;
     $this->_call_result = $return_arr;
     if (!in_array($request_result['http_code'], S2P_SDK_Rest_API_Codes::success_codes())) {
         $code_str = $request_result['http_code'];
         if ($code_details = S2P_SDK_Rest_API_Codes::valid_code($request_result['http_code'])) {
             $code_str .= ' (' . $code_details . ')';
         }
         // Set a generic error as maybe we will get more specific errors later when parsing response. Don't throw this error yet...
         $this->set_error(self::ERR_CURL_CALL, self::s2p_t('Request responded with error code %s', $code_str), '', array('prevent_throwing_errors' => true));
         if (empty($request_result['response_buffer'])) {
             // In case there's nothing to parse, throw generic error...
             if ($this->throw_errors()) {
                 $this->throw_error();
             }
             return false;
         }
     }
     if (!($response_data = $this->_method->parse_response($request_result))) {
         if ($this->_method->has_error()) {
             $this->copy_error($this->_method);
         }
         if ($this->has_error()) {
             if ($this->throw_errors()) {
                 $this->throw_error();
             }
             return false;
         }
         $this->set_error(self::ERR_PARSE_RESPONSE, self::s2p_t('Error parsing server response.'));
         return false;
     }
     if ($hook_result = $this->trigger_hooks('rest_rest_api_response_data', array('api_obj' => $this, 'response_data' => $response_data)) and is_array($hook_result)) {
         if (array_key_exists('response_data', $hook_result)) {
             $response_data = $hook_result['response_data'];
         }
     }
     if (!$this->_method->validate_response($response_data)) {
         if ($this->_method->has_error()) {
             $this->copy_error($this->_method);
         } else {
             $this->set_error(self::ERR_VALIDATE_RESPONSE, self::s2p_t('Error validating server response.'));
         }
         return false;
     }
     $return_arr['response'] = $response_data;
     $this->_call_result = $return_arr;
     // Make sure errors get thrown if any...
     if ($this->has_error() and $this->throw_errors()) {
         $this->throw_error();
         return false;
     }
     return $return_arr;
 }
コード例 #2
0
ファイル: s2p_sdk_method.inc.php プロジェクト: maxism/SDK-PHP
 public static function get_http_code_error($http_code)
 {
     $http_code = intval($http_code);
     if (in_array($http_code, S2P_SDK_Rest_API_Codes::success_codes())) {
         return false;
     }
     if (!($error_str = S2P_SDK_Rest_API_Codes::valid_code($http_code))) {
         $error_str = self::s2p_t('Unknown error code');
     }
     return $error_str;
 }
コード例 #3
0
 /**
  * This method should be overridden by methods which have to check any errors in response data
  *
  * @param array $response_data
  *
  * @return bool Returns true if response doesn't have errors
  */
 public function validate_response($response_data)
 {
     $response_data = self::validate_response_data($response_data);
     switch ($response_data['func']) {
         case self::FUNC_INIT_PAYMENT:
         case self::FUNC_CANCEL_PAYMENT:
             // in case we have an error for payments_list we will receive a payment object back
         // in case we have an error for payments_list we will receive a payment object back
         case self::FUNC_LIST_PAYMENTS:
         case self::FUNC_PAYMENT_CAPTURE:
             if (!empty($response_data['response_array']['payment'])) {
                 if (!empty($response_data['response_array']['payment']['status']) and is_array($response_data['response_array']['payment']['status'])) {
                     if (!empty($response_data['response_array']['payment']['status']['reasons']) and is_array($response_data['response_array']['payment']['status']['reasons'])) {
                         $error_msg = '';
                         foreach ($response_data['response_array']['payment']['status']['reasons'] as $reason_arr) {
                             if (($error_reason = (!empty($reason_arr['code']) ? $reason_arr['code'] . ' - ' : '') . (!empty($reason_arr['info']) ? $reason_arr['info'] : '')) != '') {
                                 $error_msg .= $error_reason;
                             }
                         }
                         if (!empty($error_msg)) {
                             $error_msg = self::s2p_t('Returned by server: %s', $error_msg);
                             $this->set_error(self::ERR_REASON_CODE, $error_msg);
                             return false;
                         }
                     }
                 }
                 if (empty($response_data['request_http_code']) or !in_array($response_data['request_http_code'], S2P_SDK_Rest_API_Codes::success_codes())) {
                     $this->set_error(self::ERR_EMPTY_ID, self::s2p_t('API call failed with error code %s.', $response_data['request_http_code']));
                     return false;
                 }
                 if (empty($response_data['response_array']['payment']['id'])) {
                     $this->set_error(self::ERR_EMPTY_ID, self::s2p_t('Payment ID is empty.'));
                     return false;
                 }
                 if ($response_data['func'] == self::FUNC_CANCEL_PAYMENT and isset($response_data['response_array']['payment']['status']['id']) and $response_data['response_array']['payment']['status']['id'] != self::STATUS_CANCELLED) {
                     $this->set_error(self::ERR_EMPTY_ID, self::s2p_t('Payment not cancelled.'));
                     return false;
                 }
             }
             break;
         case self::FUNC_REFUNDS_LIST:
         case self::FUNC_REFUND:
             if (!empty($response_data['response_array']['refund'])) {
                 if (!empty($response_data['response_array']['refund']['status']) and is_array($response_data['response_array']['refund']['status'])) {
                     if (!empty($response_data['response_array']['refund']['status']['reasons']) and is_array($response_data['response_array']['refund']['status']['reasons'])) {
                         $error_msg = '';
                         foreach ($response_data['response_array']['refund']['status']['reasons'] as $reason_arr) {
                             if (($error_reason = (!empty($reason_arr['code']) ? $reason_arr['code'] . ' - ' : '') . (!empty($reason_arr['info']) ? $reason_arr['info'] : '')) != '') {
                                 $error_msg .= $error_reason;
                             }
                         }
                         if (!empty($error_msg)) {
                             $error_msg = self::s2p_t('Returned by server: %s', $error_msg);
                             $this->set_error(self::ERR_REASON_CODE, $error_msg);
                             return false;
                         }
                     }
                 }
                 if (empty($response_data['response_array']['refund']['id'])) {
                     $this->set_error(self::ERR_EMPTY_ID, self::s2p_t('Refund ID is empty.'));
                     return false;
                 }
             }
             break;
     }
     return true;
 }