/
app_controller.php
executable file
·192 lines (159 loc) · 5.99 KB
/
app_controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* Application Controller
*
*
* @package cake
* @subpackage cake.app
*/
class AppController extends Controller {
var $components = array(
'DebugKit.Toolbar',
'Session',
'RequestHandler',
'Json' => array('debug'=>false, 'fakeAjax'=>false)
);
var $helpers = array('Session', 'Html', 'Form', 'Js', 'Merchant', '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 $this->params['url'] query params to setup $this->Shopify API object
*
* Sample URL coming back from Login in via Shopify
* http://localhost/app_folder/merchants/welcome?shop=klocko-and-sons3230.myshopify.com×tamp=1279691529&signature=14689583eeda99da4175a886828&t=037698b5622121122873276c1f
*
* 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($this->params['url']['shop'])){
$this->Session->write('Shopify.shop', $this->params['url']['shop']);
// We also use this as the Merchant ID - subdomain of myshopify.com
$this->Session->write('Shopify.shop_id', str_replace('.myshopify.com','', $this->params['url']['shop']));
}
if(isset($this->params['url']['t'])){
$this->Session->write('Shopify.token', $this->params['url']['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.
$this->Session->write('Shop.test', false);
if('development' == $shop['plan-name']) {
$this->Session->write('Shop.test', true);
}
}
}