Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JamieS/562953 to your computer and use it in GitHub Desktop.
Save JamieS/562953 to your computer and use it in GitHub Desktop.
<?php
/**
* Application Controller
*
*
* @package cake
* @subpackage cake.app
*/
class AppController extends Controller {
var $components = array(
'Session',
'RequestHandler'
);
var $helpers = array('Session', 'Html', 'Form', 'Js', 'Time');
/**
* The Shopify API Object
*
* @var Object
*/
var $Shopify = false;
/**
* Before Filter.
*
* Sets up any Language Stuff. If you override beforeFilter in your controllers and don't call parent::beforeFilter(),
* then don't forget you need to trigger initLanguage() in your beforeFilter
*
* This includes initializing the Shopify Session, and checking for validity.
* If it is invalid, by default we redirect to the Home Page
*
* Otherwise, we make sure we have the token.
* If not, we will redirect the user to the Request Permission Url generated by the API.
*
* Lastly, if we've gotten this far, but don't have a Merchant in the session,
* then we initalize the merchant session.
*
*
*
* @return void
**/
function beforeFilter() {
$this->initLanguage();
$this->initShopifySession();
//Invalid token for some reason. Trigger an error
if(!$this->Shopify->valid()) {
$this->Session->setFlash(__d('errors', 'please_login', true ));
$this->redirect('/');
// trigger_error('API Not Valid', E_USER_ERROR);
}
//Do we have the token set? If not, redirect to the perm url
if(!$this->Session->read('Shopify.token')) {
$this->redirect($this->Shopify->create_permission_url());
}
//Everything else has passed, so store the merchant info in the session
if(!$this->Session->read('Merchant')) {
$this->initMerchantSession();
}
if(!$this->Session->read('Shop')) {
$this->initShopSession();
}
}
/**
* Will initalize any requirements for language.
*
* @return void
**/
function initLanguage() {
Configure::write('Config.language', 'eng');
}
/**
* initShopifySession
*
* Check the various $_GET query params to setup $this->Shopify API object
*
* Sample URL coming back from Login in via Shopify
*
* Set up Shopify session values as well.
*
* @return void
*/
function initShopifySession() {
// We create a Shopify Config which holds our API_KEY and SECRET
// See shopify.php.default
Configure::load('shopify');
// Load up the shopify_api.
// Unfortunately, it uses some pretty generic class names - in this case, Session.
App::import('Vendor', 'Session', array('file'=>'shopify_api/lib/shopify_api.php'));
if(isset($_GET['shop'])){
$this->Session->write('Shopify.shop', $_GET['shop']);
// We also use this as the Merchant ID - subdomain of myshopify.com
$this->Session->write('Shopify.shop_id', str_replace('.myshopify.com','', $_GET['shop']));
}
if(isset($_GET['t'])){
$this->Session->write('Shopify.token', $_GET['t']);
}
$shop = $this->Session->read('Shopify.shop');
$token = $this->Session->read('Shopify.token');
$this->Shopify = new Session(
$shop,
$token,
Configure::read('Shopify.api_key'),
Configure::read('Shopify.secret')
);
}
/**
* initMerchantSession
*
* Initialize the Merchant Session Data.
*
* Looks up the Merchant record, and if it doesn't exist, will create an entry for the Merchant
*
* If the Merchant is set to recurring (for billing) then we check with Shopify if it is still active.
*
* You would modify this function if there's any additional data you need added to your Merchant session info.
*
* @todo Reexamine the use of paid field to do double duty of checking whether merchant is paid/ when they paid.
* See Merchant model for more info
* @return void
**/
function initMerchantSession() {
$Merchant = ClassRegistry::init('Merchant');
$merchantData = $Merchant->read(false, $this->Session->read('Shopify.shop_id'));
if(!$merchantData) {
$merchantData = $Merchant->create(array(
'id'=>$this->Session->read('Shopify.shop_id'),
'title' => $this->Session->read('Shopify.shop')
));
if(!$Merchant->save()) {
$this->Session->setFlash(__d('errors','problem_saving_merchant', true));
$this->redirect('/');
}
}
//If recurring, make sure still valid
if(1 == $merchantData['Merchant']['recurring']) {
$chargeInfo = $this->Shopify->recurring_application_charge->get($merchantData['Merchant']['charge_id']);
if('active' != $chargeInfo['status']) {
$merchantData['Merchant']['paid'] = null;
}
}
$this->Session->write('Merchant', $merchantData['Merchant']);
}
/**
* Load up the Shop settings.
* Set whether we are in test more.
*
* @return void
**/
function initShopSession() {
$shop = $this->Shopify->shop->get();
$this->Session->write('Shop',$shop);
//this used in billing. Can check for it for other cases.
Configure::write('Shopify.test', false);
if('development' == $shop['plan-name']) {
Configure::write('Shopify.test', true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment