Last active
December 11, 2015 07:19
-
-
Save marcguyer/4565656 to your computer and use it in GitHub Desktop.
Basic ZF1 controller for handling CheddarGetter webhooks
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
<?php | |
class HookController extends Zend_Controller_Action { | |
public function preDispatch() { | |
$this->_helper->viewRenderer->setNoRender(true); | |
if (!$this->_request->isPost()) { | |
throw new Exception('POST required'); | |
} | |
$post = $this->_request->getPost(); | |
if (empty($post['product']['id'])) { | |
throw new Exception('Malformed request'); | |
} | |
$token = $this->_request->getHeader('X-CG-TOKEN'); | |
$calcToken = md5($this->_request->getRawBody()); | |
$signature = $this->_request->getHeader('X-CG-SIGNATURE'); | |
if (empty($token) || $token != $calcToken) { | |
throw new Exception( | |
'Token is missing or token mismatch' | |
); | |
} | |
if (empty($signature)) { | |
throw new Exception('Missing request signature'); | |
} | |
$apiKey = [api key from your app config]; | |
// check signature | |
if ($signature != hash_hmac('sha256', $calcToken, $apiKey)) { | |
throw new Exception('Invalid request signature'); | |
} | |
} | |
public function indexAction() { | |
// do a standard hook | |
$post = $this->_request->getPost(); | |
$this->{$post['activityType'] . 'Hook'}($post); | |
} | |
public function __call($method, $args) { | |
if ('Action' == substr($method, -6)) { | |
$action = substr($method, 0, -6); | |
$script = $this->getViewScript(); | |
$paths = $this->view->getScriptPaths( | |
$this->_request->getActionName() | |
); | |
foreach ($paths as $path) { | |
if (is_readable($path . '/' . $script)) { | |
return; | |
} | |
} | |
return; | |
} else if ('Hook' == substr($method, -4)) { | |
// ignore anything not implemented | |
// HTTP/1.1 501 Not Implemented | |
$this->_response->setHttpResponseCode(501); | |
$this->_response->sendResponse(); | |
exit; | |
} | |
return parent::__call($method, $args); | |
} | |
protected function newSubscriptionHook(array $post) { | |
} | |
protected function subscriptionChangedHook(array $post) { | |
} | |
protected function subscriptionCanceledHook(array $post) { | |
} | |
protected function subscriptionReactivatedHook(array $post) { | |
} | |
protected function subscriptionBillableHook(array $post) { | |
} | |
protected function transactionHook(array $post) { | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment