/** * Constructor. * * @throws Trustly_ConnectionException When the response was invalid and * the HTTP response code indicates an error. * * @throws Trustly_DataException When the response is not valid. * * @param string $response_body RAW response body from the API call * * @param integer $response_code HTTP response code from the API call */ public function __construct($response_body, $response_code = NULL) { parent::__construct(); $this->response_code = $response_code; $this->response_body = $response_body; $payload = json_decode($response_body, TRUE); if ($payload === FALSE) { /* Only throw the connection error exception here if we did not * receive a valid JSON response, if we did recive one we will use * the error information in that response instead. */ if (isset($this->response_code) and $this->response_code !== 200) { throw new Trustly_ConnectionException('HTTP ' . $this->response_code); } else { throw new Trustly_DataException('Failed to decode response JSON, reason code ' . json_last_error()); } } if (isset($payload)) { $this->payload = $payload; } /* Attempt to detect the type of the response. A successful call will * have a 'result' on toplevel in the payload, while an failure will * have a 'error' on the tyoplevel */ $this->response_result =& $this->payload['result']; if ($this->response_result === NULL) { $this->response_result =& $this->payload['error']; if ($this->response_result === NULL) { throw new Trustly_DataException('No result or error in response'); } } }
/** * Constructor. * * @param string $method Method name for the call * * @param array $payload Call payload */ public function __construct($method = NULL, $payload = NULL) { parent::__construct(); $vpayload = $this->vacuum($payload); if (isset($vpayload)) { $this->payload = $vpayload; } $this->method = $method; }
/** * Constructor. * * @param Trustly_Data_JSONRPCNotificationRequest $request Incoming * notification request to which we are responding * * @param boolean $success Set to true to indicate that the notification * was successfully processed. */ public function __construct($request, $success = NULL) { parent::__construct(); $uuid = $request->getUUID(); $method = $request->getMethod(); if (isset($uuid)) { $this->setResult('uuid', $uuid); } if (isset($method)) { $this->setResult('method', $method); } if (isset($success)) { $this->setSuccess($success); } $this->set('version', '1.1'); }
/** * Constructor. * * @throws Trustly_DataException When the incoming data is invalid. * * @throws Trustly_JSONRPCVersionException When the incoming notification * request seems to be valid but is for a JSON RPC version we do not * support. * * @param string $notification RAW incoming notification body */ public function __construct($notification_body) { parent::__construct(); $this->notification_body = $notification_body; if (empty($notification_body)) { throw new Trustly_DataException('Empty notification body'); } $payload = json_decode($notification_body, TRUE); if (is_null($payload)) { $error = ''; if (function_exists('json_last_error_msg')) { $error = ': ' . json_last_error_msg(); } throw new Trustly_DataException('Failed to parse JSON' . $error); } $this->payload = $payload; if ($this->getVersion() != '1.1') { throw new Trustly_JSONRPCVersionException('JSON RPC Version ' . $this->getVersion() . 'is not supported'); } }
/** * Set a value in the payload to a given value. * * @param string $name * * @param mixed $value */ public function set($name, $value) { $this->payload[$name] = Trustly_Data::ensureUTF8($value); }
/** * Set a value in the params->Data->Attributes part of the payload. * * @param string $name The name of the Attributes parameter to set * * @param mixed $value The value of the Attributes parameter to set * * @return mixed $value */ public function setAttribute($name, $value) { if (!isset($this->payload['params']['Data'])) { $this->payload['params']['Data'] = array(); } if (!isset($this->payload['params']['Data']['Attributes'])) { $this->payload['params']['Data']['Attributes'] = array(); } $this->payload['params']['Data']['Attributes'][$name] = Trustly_Data::ensureUTF8($value); return $value; }