/** * Creates a datacash request. * * @param string $client_id * Datacash client id. * @param $client_pass * Datacash client password. * @param array $request_params * Datacash request parameters. * * @return SetupManagerInterface * * @throws DatacashRequestException */ public function createRequest($client_id, $client_pass, $request_params) { if (empty($request_params['method'])) { throw new DatacashRequestException('Request should have a method.'); } // Create a request instance and prepare request. $this->request = new Request($client_id, $client_pass); $this->request->prepareRequest($request_params); return $this; }
/** * Makes a datacash request. * * @param Request $request * * @return string * XML response * * @throws Exceptions\DatacashRequestException */ public function send(Request $request) { // Set the CURL options for Datacash request. $options = array(CURLOPT_URL => $this->hostName, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => '<?xml version="1.0" encoding="UTF-8"?>' . $request->getXml(), CURLOPT_HTTPHEADER => array('Expect: '), CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => 0); // Verify SSL certificate based on the configuration. if (isset($this->sslVerify)) { $options[CURLOPT_SSL_VERIFYHOST] = $this->sslVerify; } // Set the timeout. $options[CURLOPT_TIMEOUT] = $this->timeout; // Set SSL certification related options. if ($this->sslCertPath) { // We've a certificate location. Check whether it's exists. if (!file_exists($this->sslCertPath)) { $err_str = "Cannot find cacert_location: " . $this->sslCertPath; throw new DatacashRequestException($err_str); } // Check if the certificate file is readable. if (!is_readable($this->sslCertPath)) { $err_str = "Cannot read cacert_location: " . $this->sslCertPath; throw new DatacashRequestException($err_str); } // Set the various options. $options[CURLOPT_SSL_VERIFYPEER] = TRUE; $options[CURLOPT_CAINFO] = $this->sslCertPath; } if (isset($this->proxyUrl)) { $options[CURLOPT_PROXY] = $this->proxyUrl; } $ch = curl_init(); curl_setopt_array($ch, $options); // Store response for later use. $curl_response = curl_exec($ch); $err_no = curl_errno($ch); $err_str = curl_error($ch); $curl_get_info = curl_getinfo($ch); curl_close($ch); if ($err_no > 0) { throw new DatacashRequestException("Datacash request curl error with error no. {$err_no} ({$err_str})"); } // Throw an exception where the curl request encountered no errors but // the http response code != 200 $http_response_code = $curl_get_info['http_code']; if ($http_response_code !== 200) { throw new DatacashRequestException("Datacash curl request issue. Curl got http response code: {$http_response_code} instead of 200"); } // Prepare the response instance. $response = XmlHelper::parseFromXml($curl_response); // It's possible, although unlikely, for the transaction to succeed but // for an HTTP Response code to not be 200, such as a warning code. // Therefore, we don't want to assume that the transaction failed if a // 200 was not received. What we will do is see if the Response.status // element isn't there, and if not, if the HTTP Response code is also not // 200, then we can safely assume that things didn't go according to plan // and we didn't get an XML Response document back. if ("Response" !== $response->getName() || !$response->status && 200 != $curl_get_info["http_code"]) { $err_str = "HTTP Error: Response Code " . $curl_get_info["http_code"] . " received."; $err_no = $curl_get_info["http_code"]; throw new DatacashRequestException("Datacash request curl error with error no. {$err_no} ({$err_str})"); } return $response; }