-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[APISDK-5] Added device fingerprint builder as frontend service
- Loading branch information
1 parent
2ec7ead
commit 32a7e57
Showing
2 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace RatePAY\Exception; | ||
|
||
class FrontendException extends ExceptionAbstract | ||
{ | ||
|
||
/** | ||
* @param string $message | ||
*/ | ||
public function __construct($message) | ||
{ | ||
parent::__construct("Frontend exception : " . $message); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
namespace RatePAY\Frontend; | ||
|
||
use RatePAY\Exception\FrontendException; | ||
|
||
class DeviceFingerprintBuilder | ||
{ | ||
|
||
/** | ||
* DFP Snippet Id (vendor) | ||
* | ||
* @var string | ||
*/ | ||
private $snippetId; | ||
|
||
/** | ||
* Location of DFP snippet | ||
* | ||
* @var string | ||
*/ | ||
private $location = "checkout"; | ||
|
||
/** | ||
* DFP token | ||
* | ||
* @var string | ||
*/ | ||
private $token; | ||
|
||
/** | ||
* DFP URI | ||
* | ||
* @var string | ||
*/ | ||
private $uri = "//d.ratepay.com"; | ||
|
||
|
||
/** | ||
* DeviceFingerprintBuilder constructor. | ||
* | ||
* @param $uniqueIdentifier | ||
*/ | ||
public function __construct($snippetId = "", $uniqueIdentifier = "") | ||
{ | ||
if (!empty($snippetId)) { | ||
$this->setSnippetId($snippetId); | ||
} else { | ||
throw new FrontendException("DeviceFingerprintBuilder: Snippet id must be set"); | ||
} | ||
|
||
if (!empty($uniqueIdentifier)) { | ||
$this->createToken($uniqueIdentifier); | ||
} else { | ||
throw new FrontendException("DeviceFingerprintBuilder: Transaction identifier must be set"); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $snippetId | ||
*/ | ||
public function setSnippetId($snippetId) | ||
{ | ||
$this->snippetId = $snippetId; | ||
} | ||
|
||
/** | ||
* Sets DFP token | ||
* | ||
* @param string $token | ||
*/ | ||
public function setToken($token) | ||
{ | ||
$this->token = $token; | ||
} | ||
|
||
/** | ||
* Returns DFP token | ||
* | ||
* @return string | ||
*/ | ||
public function getToken() | ||
{ | ||
return $this->token; | ||
} | ||
|
||
/** | ||
* Returns DFP snippet code (JS code) | ||
* @return string | ||
*/ | ||
public function getDfpSnippetCode($smarty = false) | ||
{ | ||
$snippet = sprintf( | ||
(!$smarty) ? "<script language=\"JavaScript\">var di = %s;</script>" : "<script language=\"JavaScript\">{literal}var di = %s;{/literal}</script>", | ||
json_encode([ | ||
't' => $this->token, | ||
'v' => $this->snippetId, | ||
'l' => $this->location | ||
]) | ||
); | ||
|
||
$snippet .= sprintf( | ||
"<script type=\"text/javascript\" src=\"%s/%s/di.js\"></script>", | ||
$this->uri, | ||
$this->snippetId | ||
); | ||
|
||
$snippet .= sprintf( | ||
"<noscript><link rel=\"stylesheet\" type=\"text/css\" href=\"%s/di.css?t=%s&v=%s&l=%s\"></noscript>", | ||
$this->uri, | ||
$this->token, | ||
$this->snippetId, | ||
$this->location | ||
); | ||
|
||
return $snippet; | ||
} | ||
|
||
/** | ||
* Creates unique token | ||
* | ||
* @param $uniqueIdentifier | ||
*/ | ||
private function createToken($uniqueIdentifier) { | ||
$this->token = md5($this->snippetId . "_" . $uniqueIdentifier . "_" . microtime()); | ||
} | ||
|
||
} |