private static function _sendRequest($httpMethod, $url, $requestParams = array()) { $curl = curl_init(); curl_setopt($curl, CURLOPT_TIMEOUT, 120); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $httpMethod); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_ENCODING, 'gzip'); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8', 'User-Agent: Pencepay PHP Library ' . Pencepay_Version::get(), 'X-ApiVersion: ' . Pencepay_Context::SERVER_API_VERSION)); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_USERPWD, self::_getAuthCredentials()); if (count($requestParams) > 0) { curl_setopt($curl, CURLOPT_POSTFIELDS, self::encodeParameters($requestParams)); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); // Not supported by gateway $responseBody = curl_exec($curl); $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); if (self::_isErrorResponse($httpStatus)) { switch ($httpStatus) { case 400: $error = Pencepay_Util_Json::fromJson($responseBody); throw new Pencepay_Exception_InvalidRequest($error->message, $error->code, $error->parameter); case 401: throw new Pencepay_Exception_ApiAuthentication($responseBody); case 402: $error = Pencepay_Util_Json::fromJson($responseBody); throw new Pencepay_Exception_Processing($error->message, $error->code, $error->parameter); case 403: $error = Pencepay_Util_Json::fromJson($responseBody); throw new Pencepay_Exception_Authorization($error->message, $error->code); case 404: $error = Pencepay_Util_Json::fromJson($responseBody); throw new Pencepay_Exception_ItemNotFound($error->message, $error->code); case 501: case 502: $error = Pencepay_Util_Json::fromJson($responseBody); throw new Pencepay_Exception_ServerSide($error->message, $error->code); default: throw new Pencepay_Exception_Unexpected("Unexpected response received from server", $httpStatus); } } return Pencepay_Util_Json::fromJson($responseBody); }
/** * @param Pencepay_Request_Transaction $request * @return array */ public static function generateCheckoutParameters(Pencepay_Request_Transaction $request) { $required = array('amount', 'currencyCode', 'orderId', 'cancelUrl', 'redirectUrl'); $requestParams = $request->getParams(); // Check if required properties are set foreach ($required as $propertyName) { if (!self::arrayValueNotEmpty($propertyName, $requestParams)) { throw new Pencepay_Exception_InvalidRequest('Required Checkout request property not set: ' . $propertyName); } } $params = array(); foreach ($requestParams as $key => $value) { if (self::arrayValueNotEmpty($key, $requestParams)) { $params[$key] = $value; } } $params['apiVersion'] = Pencepay_Version::get(); $params['publicKey'] = Pencepay_Context::getPublicKey(); $digestInput = ''; $signatureFields = array('publicKey', 'amount', 'currencyCode', 'orderId'); foreach ($signatureFields as $field) { $digestInput .= $params[$field]; } $digestInput .= Pencepay_Context::getSecretKey(); $params['signature'] = hash('sha256', $digestInput); return $params; }