/** * Make a remote request to PayPal API. * * @see https://developer.paypal.com/docs/classic/api/NVPAPIOverview/#creating-an-nvp-request * * @param array $params NVP request parameters * @return array NVP response */ protected function _request(array $params) { try { wc_gateway_ppec_log(sprintf('%s: trying to make a request to PayPal with params: %s', __METHOD__, print_r($params, true))); // Make sure $_credential and $_environment have been configured. if (!$this->_credential) { throw new Exception(__('Missing credential', 'woocommerce-gateway-paypal-express-checkout'), self::INVALID_CREDENTIAL_ERROR); } if (!is_a($this->_credential, 'WC_Gateway_PPEC_Client_Credential')) { throw new Exception(__('Invalid credential object', 'woocommerce-gateway-paypal-express-checkout'), self::INVALID_CREDENTIAL_ERROR); } if (!in_array($this->_environment, array('live', 'sandbox'))) { throw new Exception(__('Invalid environment', 'woocommerce-gateway-paypal-express-checkout'), self::INVALID_ENVIRONMENT_ERROR); } // First, add in the necessary credential parameters. $body = array_merge($params, $this->_credential->get_request_params()); $args = array('method' => 'POST', 'body' => $body, 'user-agent' => __CLASS__, 'httpversion' => '1.1', 'timeout' => 30); // For cURL transport. add_action('http_api_curl', array($this->_credential, 'configure_curl'), 10, 3); wc_gateway_ppec_log(sprintf('%s: remote request to %s with args: %s', __METHOD__, $this->get_endpoint(), print_r($args, true))); $resp = wp_safe_remote_post($this->get_endpoint(), $args); wc_gateway_ppec_log(sprintf('%s: response from remote request to %s: %s', __METHOD__, $this->get_endpoint(), print_r($resp, true))); if (is_wp_error($resp)) { throw new Exception(sprintf(__('An error occurred while trying to connect to PayPal: %s', 'woocommerce-gateway-paypal-express-checkout'), $resp->get_error_message()), self::REQUEST_ERROR); } parse_str(wp_remote_retrieve_body($resp), $result); if (!array_key_exists('ACK', $result)) { throw new Exception(__('Malformed response received from PayPal', 'woocommerce-gateway-paypal-express-checkout'), self::REQUEST_ERROR); } wc_gateway_ppec_log(sprintf('%s: acknowleged response body: %s', __METHOD__, print_r($result, true))); remove_action('http_api_curl', array($this->_credential, 'configure_curl'), 10); // Let the caller deals with the response. return $result; } catch (Exception $e) { remove_action('http_api_curl', array($this->_credential, 'configure_curl'), 10); // TODO: Maybe returns WP_Error ? $error = array('ACK' => 'Failure', 'L_ERRORCODE0' => $e->getCode(), 'L_SHORTMESSAGE0' => 'Error in ' . __METHOD__, 'L_LONGMESSAGE0' => $e->getMessage(), 'L_SEVERITYCODE0' => 'Error'); wc_gateway_ppec_log(sprintf('%s: exception is thrown while trying to make a request to PayPal: %s', __METHOD__, $e->getMessage())); wc_gateway_ppec_log(sprintf('%s: returns error: %s', __METHOD__, print_r($error, true))); return $error; } }
/** * {@inheritdoc} */ public function get_request_params() { $params = parent::get_request_params(); $params['SIGNATURE'] = $this->_signature; return $params; }