Email = $Email;
}
/** Adds a product to send. Callable multiple times.
* @param string $ProductName - A product name from the customer's cart
* @param string $ProductId - A product id, it must be same as in the feed. */
public function AddProduct($ProductName, $ProductId = null)
{
$Content = [];
$Content['Name'] = $ProductName;
if (!empty($ProductId)) {
$Content['Id'] = $ProductId;
}
$this->Products[] = $Content;
}
/** Prepares the Trusted code, which provides data sending from the customer's browser to us.
* @return string - Prepared Trusted code (HTML). */
public function Prepare()
{
if (empty($this->WebApiKey)) {
throw new \Exception(self::ERROR_EMPTY_WEBAPIKEY);
}
if (empty($this->Email)) {
throw new \Exception(self::ERROR_EMPTY_EMAIL);
}
if ($this->Email == 'somebody@example.com') {
throw new \Exception(self::ERROR_EXAMPLE_EMAIL);
}
$Examples = ['Name of first purchased product', 'Name of second purchased product'];
foreach ($Examples as $Example) {
foreach ($this->Products as $Product) {
if ($Product['Name'] == $Example) {
throw new \Exception(self::ERROR_EXAMPLE_PRODUCT);
}
}
}
$Params = [];
$Params['Version'] = self::VERSION;
$Params['WebApiKey'] = $this->WebApiKey;
$Params['Email'] = $this->Email;
$Params['Products'] = json_encode($this->Products);
$Random = md5($this->WebApiKey.microtime());
$Query = $this->GetQuery($Params);
// Sending:
$Output = '';
// Include:
$Output .= '';
// Fallback:
$Output .= '';
return $Output;
}
/** Performs a request on our servers to get a token and assembles query params with it.
* @param array $Params - Parameters to send with token request
*
* @return string - Query string to assemble sending code snipet on client's side with it. */
protected function GetQuery($Params)
{
// Prepare curl request:
$Curl = curl_init();
curl_setopt($Curl, CURLOPT_URL, $this->service_url_send.self::SERVICE_TOKEN_REQUEST);
curl_setopt($Curl, CURLOPT_POST, 1);
curl_setopt($Curl, CURLOPT_POSTFIELDS, http_build_query($Params));
curl_setopt($Curl, CURLOPT_CONNECTTIMEOUT_MS, 500);
curl_setopt($Curl, CURLOPT_TIMEOUT_MS, 500);
curl_setopt($Curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($Curl, CURLOPT_HEADER, true);
// Execute the request:
$Response = curl_exec($Curl);
if (curl_errno($Curl) === 0 && $Response !== false) {
$Info = curl_getinfo($Curl);
$StatusCode = $Info['http_code'];
$JsonBody = substr($Response, $Info['header_size']);
$JsonArray = json_decode($JsonBody, true);
$JsonError = json_last_error();
curl_close($Curl);
if (empty($JsonError)) {
if ($StatusCode == 200) {
$Query = [];
$Query[] = 'Token='.$JsonArray['Token'];
$Query[] = 'WebApiKey='.$this->WebApiKey;
$Query[] = 'C=';
return '?'.join('&', $Query);
} elseif ($StatusCode == 400) {
throw new \Exception(self::ERROR_TOKEN_BAD_REQUEST.$JsonArray['ErrorCode'].' - '.$JsonArray['ErrorMessage']);
} else {
throw new \Exception(self::ERROR_TOKEN_REQUEST_FAILED);
}
} else {
throw new \Exception('Json error: '.$JsonError);
}
} else {
return '';
}
return null;
}
public function setWebApiKey(string $WebApiKey)
{
$this->WebApiKey = $WebApiKey;
}
public function setServiceUrlSend(string $service_url_send)
{
$this->service_url_send = $service_url_send;
}
}