Source of file CTCTWebhookUtil.php
Size: 2,701 Bytes - Last Modified: 2021-12-23T10:20:09+00:00
/var/www/docs.ssmods.com/process/src/src/Ctct/WebHooks/CTCTWebhookUtil.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 | <?php namespace Ctct\WebHooks; use Ctct\Exceptions\CtctException; /** * Main Webhook Utility class.<br/> * This is meant to be used by users to validate and parse Webhooks received from ConstantContact.<br/> * * @package WebHooks * @author Constant Contact */ class CTCTWebhookUtil { /** * The client secret associated with the api key */ private $clientSecret = ''; /** * Constructor that creates a validation Object for WebHooks. * * @param string $clientSecret - The client secret associated with the api key * @return CTCTWebhookUtil */ function __construct($clientSecret='') { $this->setClientSecret($clientSecret); } /** * CTCTWebhookUtil::getClientSecret() * * @return string - the secret API key */ public function getClientSecret() { return $this->clientSecret; } /** * CTCTWebhookUtil::setClientSecret() * Set the clientSecret * * @param string $clientSecret - The client secret associated with the api key * @return void */ public function setClientSecret($clientSecret) { $this->clientSecret = $clientSecret; } /** * @param $xCtctHmacSHA256 - The value in the x-ctct-hmac-sha256 header. * @param $bodyMessage - The body message from the POST received from ConstantContact in Webhook callback. * @return object corresponding to bodyMessage in case of success; an exception is thrown otherwise. * @throws CtctException Thrown when: * <ul> * <li>message encryption does not correspond with x-ctct-hmac-sha256 header value</li> * <li>or an error is raised when parsing the bodyMessage</li> * </ul> * <p/> */ public function getBillingChangeNotification($xCtctHmacSHA256, $bodyMessage) { if ($this->isValidWebhook($xCtctHmacSHA256, $bodyMessage)) { return json_decode($bodyMessage); } else { throw new CtctException("Invalid WebHook"); } } /** * @param $xCtctHmacSHA256 - The value in the x-ctct-hmac-sha256 header. * @param $bodyMessage - The body message from the POST received from ConstantContact in Webhook callback. * @return bool * @throws CtctException */ public function isValidWebhook($xCtctHmacSHA256, $bodyMessage) { if ($this->getClientSecret() == null) { throw new CtctException("NO_CLIENT_SECRET"); } $encodedString = hash_hmac("sha256", $bodyMessage, $this->clientSecret); return ($encodedString == $xCtctHmacSHA256)?true:false; } } |