mirror of
https://github.com/retailcrm/mailgun-php.git
synced 2024-11-22 12:36:02 +03:00
Added new OptInHandler and tests.
This commit is contained in:
parent
afef8b4c88
commit
07837600e6
34
src/Mailgun/Lists/OptInHandler.php
Normal file
34
src/Mailgun/Lists/OptInHandler.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?PHP
|
||||
|
||||
namespace Mailgun\Lists;
|
||||
|
||||
use Mailgun\Messages\Expcetions\InvalidParameter;
|
||||
use Mailgun\Messages\Exceptions\TooManyParameters;
|
||||
use Mailgun\Messages\Expcetions\InvalidParameterType;
|
||||
|
||||
/*
|
||||
This class is used for creating a unique hash for
|
||||
mailing list subscription double-opt in requests.
|
||||
*/
|
||||
|
||||
class OptInHandler{
|
||||
|
||||
function __construct(){
|
||||
|
||||
}
|
||||
|
||||
public function generateHash($mailingList, $secretAppId, $recipientAddress){
|
||||
$concatStrings = $secretAppId . "" . $recipientAddress;
|
||||
return urlencode(base64_encode(json_encode(array('s' => hash('md5', $concatStrings), 'l' => $mailingList, 'r' => $recipientAddress))));
|
||||
}
|
||||
|
||||
public function validateHash($secretAppId, $uniqueHash){
|
||||
$urlParameters = json_decode(base64_decode(urldecode($uniqueHash)));
|
||||
$concatStrings = $secretAppId . "" . $urlParameters->r;
|
||||
|
||||
if($urlParameters->s == hash('md5', $concatStrings)){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -8,6 +8,7 @@ use Mailgun\Messages\Messages;
|
||||
use Mailgun\Connection\Exceptions;
|
||||
use Mailgun\Connection\RestClient;
|
||||
use Mailgun\Messages\BatchMessage;
|
||||
use Mailgun\Lists\OptInHandler;
|
||||
use Mailgun\Messages\MessageBuilder;
|
||||
|
||||
/*
|
||||
@ -75,6 +76,10 @@ class Mailgun{
|
||||
return new MessageBuilder();
|
||||
}
|
||||
|
||||
public function OptInHandler(){
|
||||
return new OptInHandler();
|
||||
}
|
||||
|
||||
public function BatchMessage($workingDomain, $autoSend = true){
|
||||
return new BatchMessage($this->restClient, $workingDomain, $autoSend);
|
||||
}
|
||||
|
32
tests/Mailgun/Tests/Lists/OptInHandlerTest.php
Normal file
32
tests/Mailgun/Tests/Lists/OptInHandlerTest.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?PHP
|
||||
namespace Mailgun\Tests\Lists;
|
||||
|
||||
use Mailgun\Tests\MailgunTest;
|
||||
|
||||
class OptInHandler extends \Mailgun\Tests\MailgunTestCase{
|
||||
|
||||
private $client;
|
||||
private $sampleDomain = "samples.mailgun.org";
|
||||
private $optInHandler;
|
||||
|
||||
public function setUp(){
|
||||
$this->client = new MailgunTest("My-Super-Awesome-API-Key");
|
||||
$this->optInHandler = $this->client->OptInHandler();
|
||||
}
|
||||
|
||||
public function testReturnOfGenerateHash(){
|
||||
$generatedHash = $this->optInHandler->generateHash('mytestlist@example.com', 'mysupersecretappid', 'testrecipient@example.com');
|
||||
$knownHash = "eyJzIjoiOGM2NmVmYzYwNzhmNGVkYjFkZGJiY2RhM2M2MmMzMTQiLCJsIjoibXl0ZXN0bGlzdEBleGFtcGxlLmNvbSIsInIiOiJ0ZXN0cmVjaXBpZW50QGV4YW1wbGUuY29tIn0%3D";
|
||||
$this->assertEquals($generatedHash, $knownHash);
|
||||
}
|
||||
|
||||
public function testGoodHash(){
|
||||
$validation = $this->optInHandler->validateHash('mysupersecretappid', 'eyJzIjoiOGM2NmVmYzYwNzhmNGVkYjFkZGJiY2RhM2M2MmMzMTQiLCJsIjoibXl0ZXN0bGlzdEBleGFtcGxlLmNvbSIsInIiOiJ0ZXN0cmVjaXBpZW50QGV4YW1wbGUuY29tIn0%3D');
|
||||
$this->assertTrue($validation);
|
||||
}
|
||||
public function testBadHash(){
|
||||
$validation = $this->optInHandler->validateHash('mybadsecretappid', 'eyJzIjoiOGM2NmVmYzYwNzhmNGVkYjFkZGJiY2RhM2M2MmMzMTQiLCJsIjoibXl0ZXN0bGlzdEBleGFtcGxlLmNvbSIsInIiOiJ0ZXN0cmVjaXBpZW50QGV4YW1wbGUuY29tIn0%3D');
|
||||
$this->assertFalse($validation);
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Reference in New Issue
Block a user