Zaheerbox - Version 3.0.0

Version Notes

Notes

Download this release

Release Info

Developer Magento Core Team
Extension Zaheerbox
Version 3.0.0
Comparing to
See all releases


Version 3.0.0

app/code/community/Coeus/FacebookBonus/Block/Facebook.php ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Coeus_FacebookBonus_Block_Facebook extends Mage_Core_Block_Template {
4
+
5
+ public function getApiId() {
6
+ return Mage::helper('facebookbonus/data')->getApiId();
7
+ }
8
+
9
+ public function getSecret() {
10
+ return Mage::helper('facebookbonus/data')->getSecret();
11
+ }
12
+
13
+ public function isEnabled() {
14
+ return Mage::getSingleton('facebookbonus/config')->isEnabled();
15
+ }
16
+
17
+ }
app/code/community/Coeus/FacebookBonus/Helper/Data.php ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Coeus_FacebookBonus_Helper_Data extends Mage_Core_Helper_Abstract {
4
+
5
+ public function getSecret() {
6
+ return Mage::getSingleton('facebookbonus/config')->getSecret();
7
+ }
8
+
9
+ public function getApiId() {
10
+ return Mage::getSingleton('facebookbonus/config')->getApiId();
11
+ }
12
+
13
+ public function convertToEuro($price) {
14
+ $currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
15
+ return Mage::helper('directory')->currencyConvert($price, $currentCurrencyCode, 'EUR');
16
+ }
17
+
18
+ public function convertFromEuro($price) {
19
+ $currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
20
+ return Mage::helper('directory')->currencyConvert($price, 'EUR', $currentCurrencyCode);
21
+ }
22
+
23
+ public function subtotal() {
24
+ $totals = Mage::getSingleton('checkout/cart')->getQuote()->getTotals();
25
+ return $totals["subtotal"]->getValue();
26
+ }
27
+
28
+
29
+
30
+ }
app/code/community/Coeus/FacebookBonus/Model/BonusboxConnect copy.php ADDED
@@ -0,0 +1,1032 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * This is generic php implementation of Bonusbox class
5
+ * independant of any framework or sytem
6
+ *
7
+ * Example:
8
+ * <code>
9
+ * Bonusbox::init( 'YOUR_APP_ID', 'YOUR_APP_SECRET', 'USER_TRACK_URL');
10
+ * Bonusbox::addItem( 'Item A', '200' );
11
+ * Bonusbox::addItem( 'Item B', '300' );
12
+ * Bonusbox::getDiscount();// It will return you the discount
13
+ * Bonusbox::addTransaction();
14
+ * </code>
15
+ *
16
+ * @author Shaikh Sonny Aman (mailtoaman@gmail.com)
17
+ * @date 21 July 2011
18
+ * @copyright Bonusbox GmbH
19
+ */
20
+ class Coeus_FacebookBonus_Model_BonusboxConnect {
21
+
22
+ private static $configFile = "./cache.txt";
23
+ private static $config = array();
24
+
25
+ /**
26
+ * Initializes bonusbox.
27
+ *
28
+ * This must be called at least once before any other Bonusbox
29
+ * method calls
30
+ *
31
+ * If userToken is not passed it will first check if payload is available
32
+ * in $_POST or $_GET and retrieves user token from that.
33
+ *
34
+ * If no parameter is passed it will assume Bonusbox is initialized before.
35
+ * It will check if appId and secret is available in $_SESSION and also try to
36
+ * retrieve userToken from paylod if any.
37
+ *
38
+ * If the merchant website is already a connected to facebook a facebook web app,
39
+ * then connected user's facebook uid can be passed as userToken.
40
+ *
41
+ * @param appId Application ID
42
+ * @param secret Application Secret
43
+ * @param track_url A url to monitor user login status to Facebook.
44
+ * The url just need to call
45
+ * <p>
46
+ * <code>Bonusbox::updateUserStatus();</code>
47
+ * </p>
48
+ * @param userToken User's facebook id.
49
+ */
50
+ public static function init($appId = '', $secret = '', $track_url = '', $userToken = '', $cacheFileLoc = '') {
51
+
52
+ //var_dump($_SESSION);
53
+ //self::$configFile = Mage::getBaseDir('code') . "/community/Coeus/FacebookBonus/Model/cache.txt";
54
+
55
+ if (!empty($appId)) {
56
+ self::setAppID($appId);
57
+ }
58
+
59
+ if (!empty($secret)) {
60
+ self::setAppSecret($secret);
61
+ }
62
+
63
+ if (!empty($userToken)) {
64
+ self::setUserToken($token);
65
+ }
66
+
67
+ if (!empty($track_url)) {
68
+ self::setTrackURL($track_url);
69
+ }
70
+
71
+
72
+ // checks if user token is available
73
+ if (!self::isLoggedinIntoFacebook()) { // nope, not available
74
+ $payload = NULL;
75
+ // check if payload is available as $_GET or $_POST parameter
76
+ if (isset($_GET[self::PAYLOAD]) && !empty($_GET[self::PAYLOAD])) {
77
+ $payload = $_GET[self::PAYLOAD];
78
+ } else if (isset($_POST[self::PAYLOAD]) && !empty($_POST[self::PAYLOAD])) {
79
+ $payload = $_POST[self::PAYLOAD];
80
+ }
81
+
82
+ if (!empty($payload)) {
83
+ $bb = self::getBonusboxResponse($payload);
84
+ if ($bb) {
85
+ $token = $bb->getUserToken();
86
+ self::setUserToken($token);
87
+ }
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Initialized cart if not initialized
93
+ */
94
+ if (!self::isKeySet(self::CART)) {
95
+ self::resetCart();
96
+ self::setCartTotalAmount(0);
97
+ }
98
+
99
+ // if (!empty($cacheFileLoc)) {
100
+ // self::$configFile = $cacheFileLoc;
101
+ // }
102
+ // $content = file_get_contents(self::$configFile);
103
+ //if (!empty($content)) {
104
+ // self::$config = json_decode($content, true);
105
+ //} else {
106
+ // self::$config = array();
107
+ //}
108
+ }
109
+
110
+ /**
111
+ * Checks if we have user token
112
+ */
113
+ public static function isLoggedinIntoFacebook() {
114
+ $token = self::getUserToken();
115
+ if (empty($token)) {
116
+ return false;
117
+ }
118
+
119
+ return true;
120
+ }
121
+
122
+ /**
123
+ * This is just for internal debug use
124
+ */
125
+ public static function unsetUserToken() {
126
+ unset($_SESSION['__bonusbox__' . self::TOKEN]);
127
+ //file_put_contents(self::$configFile, json_encode(self::$config));
128
+ }
129
+
130
+ /**
131
+ * Updates discount information
132
+ *
133
+ * Call this function after adding some items to cart or
134
+ * if user token is to be saved from payload
135
+ *
136
+ * @param $payload If passed empty it retrieves payload from server
137
+ */
138
+ public static function update($payload = '') {
139
+
140
+ $response = NULL;
141
+ if (empty($payload)) {
142
+ $response = self::getDiscountInfo();
143
+ } else {
144
+ $response = self::getBonusboxResponse($payload);
145
+ }
146
+ // if valid response, update the user token
147
+ if ($response) {
148
+ self::setUserToken($response->getUserToken());
149
+ }
150
+
151
+ return $response;
152
+ }
153
+
154
+ /**
155
+ * Saves a value to the session
156
+ * @param key The key for the value
157
+ * @param val The value to save
158
+ */
159
+ private static function setVal($key, $val) {
160
+
161
+ // $content = file_get_contents(self::$configFile);
162
+ // if (!empty($content)) {
163
+ // self::$config = json_decode($content, true);
164
+ // }
165
+
166
+ $_SESSION['__bonusbox__' . $key] = $val;
167
+ //file_put_contents(self::$configFile, json_encode(self::$config));
168
+ }
169
+
170
+ /**
171
+ * Returns value for the key from the session
172
+ * @param key Key for the value
173
+ */
174
+ private static function getVal($key) {
175
+
176
+
177
+ //$content = file_get_contents(self::$configFile);
178
+ // if (!empty($content)) {
179
+ // self::$config = json_decode($content, true);
180
+ //}
181
+
182
+ if (self::isKeySet($key)) {
183
+ return $_SESSION['__bonusbox__' . $key];
184
+ }
185
+
186
+ return NULL;
187
+ }
188
+
189
+ /**
190
+ * Checks if a key is set
191
+ * @param key Key to check
192
+ */
193
+ private static function isKeySet($key) {
194
+ return isset( $_SESSION[ '__bonusbox__'.$key] );
195
+ }
196
+
197
+ /**
198
+ * This method clear any item added to Bonusbox before
199
+ */
200
+ public static function resetCart() {
201
+ self::unsetLastError();
202
+ if (self::isKeySet(self::CART)) {
203
+ unset($_SESSION['__bonusbox__' . self::CART]); // direct access :(
204
+ }
205
+
206
+ self::setVal(self::CART, array());
207
+ }
208
+
209
+ /**
210
+ * This method returns items added to Bonusbox
211
+ */
212
+ private static function getCart() {
213
+ return self::getVal(self::CART);
214
+ }
215
+
216
+ /**
217
+ * Use this function to pupulate cartItems in Bonusbox
218
+ *
219
+ * @param article Name of the item
220
+ * @param amount Price of the item. Price is not rounded inside the function!
221
+ */
222
+ public static function addItem($article, $amount) {
223
+
224
+ // don't if there were errors previously
225
+ if (self::hasError()
226
+
227
+ )return false;
228
+
229
+ //////////// input validation ///////////
230
+ if (empty($article)) { // Item article name cannot be empty
231
+ return self::setError(self::EMPTY_ITEM_ARTICLE);
232
+ }
233
+
234
+ if (empty($amount)) { // Item amount cannot be empty
235
+ return self::setError(self::EMPTY_ITEM_AMOUNT);
236
+ }
237
+
238
+ if (!is_numeric($amount)) { // Item amount must be numeric
239
+ return self::setError(self::INVALID_TOTAL_AMOUNT);
240
+ }
241
+ ///////////////////////////
242
+ // finally add the item to the list
243
+ $cart = self::getCart();
244
+ if (!is_array($cart)) { // cart is not array! how comes?!
245
+ self::resetCart();
246
+ $cart = self::getCart();
247
+ }
248
+
249
+ $cart[] = array('article' => $article, 'amount' => $amount);
250
+ self::setVal(self::CART, $cart);
251
+
252
+ // unset error, reduntant
253
+ self::setError(self::NO_ERROR);
254
+
255
+ return true;
256
+ }
257
+
258
+ public static function setCartTotalAmount($amount) {
259
+
260
+ if (empty($amount)) {
261
+ return self::setError(self::EMPTY_TOTAL_AMOUNT);
262
+ }
263
+
264
+ if (!is_numeric($amount)) {
265
+ return self::setError(self::INVALID_TOTAL_AMOUNT);
266
+ }
267
+
268
+ self::setVal(self::TOTAL, $amount);
269
+ return true;
270
+ }
271
+
272
+ /**
273
+ * Returns discount information for the user and cart items
274
+ */
275
+ public static function getDiscountInfo() {
276
+ // get the payload
277
+ $payload = self::getPayload(false);
278
+
279
+ // check if it is valid
280
+ if (false == $payload) {
281
+ return false;
282
+ }
283
+
284
+ return self::getBonusboxResponse($payload, self::GET_URL);
285
+ }
286
+
287
+ /**
288
+ * If url is passed it retrieves server response
289
+ * for the given payload data.
290
+ *
291
+ * If no url is passed, it consider payload received from
292
+ * server and makes Bonusbox response from that
293
+ *
294
+ */
295
+ private static function getBonusboxResponse($payload, $url = '') {
296
+ if (empty($payload)) {
297
+ return self::setError(self::EMPTY_PAYLOAD);
298
+ }
299
+
300
+ $content = NULL;
301
+ if (empty($url)) {// payload is received from server
302
+ $content = $payload;
303
+ } else {// get response from server
304
+ // get the content
305
+ $content = self::curlGet($url, array(self::PAYLOAD => $payload));
306
+ }
307
+
308
+ // decode
309
+ $jsonString = self::decode64($content);
310
+ // die('----'.$jsonString);
311
+ // conver to json object
312
+ $obj = json_decode($jsonString);
313
+
314
+ // check if received valid json format
315
+ if (NULL == $obj) {
316
+ return self::setError(self::INVALID_API_RESPONSE);
317
+ }
318
+
319
+ return new BonusBoxResponse($obj);
320
+ }
321
+
322
+ public static function getPayload($isTransaction = false) {
323
+
324
+ if (self::hasError()) { // return if there were some previous error
325
+ return false;
326
+ }
327
+
328
+
329
+ /**
330
+ * Populate the data values
331
+ */
332
+ $values = array();
333
+
334
+ $values[self::APP_ID] = self::getAppId();
335
+ $values[self::TOKEN] = self::getUserToken();
336
+ $values[self::ITEMS] = self::getCart();
337
+ $values[self::AMOUNT] = self::getCartTotal();
338
+ $values[self::SHOP_TOKEN] = self::getShopUserToken();
339
+
340
+
341
+ // final validation check
342
+ if (false === self::isValidPaylodData($values, $isTransaction)) {
343
+ return false;
344
+ }
345
+
346
+ /**
347
+ * Encode data
348
+ */
349
+ $bonusboxdata = json_encode($values);
350
+ $bonusboxdata = self::encode64($bonusboxdata);
351
+
352
+ /**
353
+ * Encode Signature
354
+ */
355
+ //$signature = self::encode64( hash_hmac('sha256', $bonusboxdata, self::getAppSecret(), $raw = true) );
356
+
357
+ /**
358
+ * Join data and signature
359
+ */
360
+ //$payload = $signature . '.' . $bonusboxdata;
361
+ $payload = $bonusboxdata;
362
+ return $payload;
363
+ }
364
+
365
+ private static function isValidPaylodData($data, $isTransaction) {
366
+ // check app id
367
+ if (empty($data[self::APP_ID])) {
368
+ return self::setError(self::EMPTY_APP_ID);
369
+ }
370
+
371
+
372
+ // check amount set
373
+ if (empty($data[self::AMOUNT])) {
374
+ return self::setError(self::EMPTY_TOTAL_AMOUNT);
375
+ }
376
+
377
+ // check amount is numeric
378
+ if (!is_numeric($data[self::AMOUNT])) {
379
+ return self::setError(self::INVALID_TOTAL_AMOUNT);
380
+ }
381
+
382
+ // check appSecret though not in passed data
383
+ $secret = self::getAppSecret();
384
+ if (empty($secret)) {
385
+ return self::setError(self::EMPTY_APP_SECRET);
386
+ }
387
+
388
+
389
+ ///// now checking items ////
390
+ // check if items are array!
391
+ if (!is_array($data[self::ITEMS])) {
392
+ return self::setError(self::INVALID_ITEM_FORMAT);
393
+ }
394
+
395
+ // additional checks before sending transaction
396
+ if (true === $isTransaction) {
397
+
398
+ // check for user token
399
+ $tok = self::getUserToken();
400
+ if (empty($tok)) {
401
+ return self::setError(self::EMPTY_USER_TOKEN);
402
+ }
403
+
404
+ // check if any item added
405
+ if (empty($data[self::ITEMS])) {
406
+ return self::setError(self::EMPTY_CART);
407
+ }
408
+
409
+ // check if all items has valid format
410
+ foreach ($data[self::ITEMS] as $item) {
411
+ if (!isset($item[self::ARTICLE])) {
412
+ return self::setError(self::INVALID_ITEM_FORMAT);
413
+ }
414
+
415
+ if (!isset($item[self::AMOUNT])) {
416
+ return self::setError(self::INVALID_ITEM_FORMAT);
417
+ }
418
+
419
+ if (empty($item[self::ARTICLE])) {
420
+ return self::setError(self::EMPTY_ITEM_ARTICLE);
421
+ }
422
+
423
+ if (!is_numeric($item[self::AMOUNT])) {
424
+ return self::setError(self::INVALID_ITEM_AMOUNT);
425
+ }
426
+ }
427
+ }
428
+
429
+ // reached here!!? cool, everything is fine :)
430
+ return true;
431
+ }
432
+
433
+ // Decodes the payload returned by bonusbox
434
+ function decode_payload($encoded_payload) {
435
+ /**
436
+ * Get paylod and split into signature and data
437
+ */
438
+ list($encoded_sig, $encoded_payload) = explode('.', $encoded_payload, 2);
439
+
440
+ /**
441
+ * Decode data and signature
442
+ */
443
+ $data = json_decode($this->decode64($encoded_payload), true);
444
+ $sig = $this->decode64($encoded_sig);
445
+
446
+ /**
447
+ * Verify signature with shop app_secret
448
+ */
449
+ $expected_sig = $this->makeSignature($encoded_payload, $this->secret);
450
+
451
+ // If signature is correct, return it
452
+ if ($sig == $expected_sig)
453
+ return $data;
454
+ else
455
+ return false;
456
+ }
457
+
458
+ /**
459
+ * Adds the transaction to bonus box
460
+ *
461
+ * This will not submit to bonux box server if:
462
+ * <ol>
463
+ * <li>Bonusbox is NOT initialized</li>
464
+ * <li>NO item was added to Bonusbox</li>
465
+ * <li>User token NOT found </li>
466
+ * </ol>
467
+ *
468
+ */
469
+ public static function addTransaction() {
470
+
471
+ $payload = self::getPayload(true);
472
+
473
+ // check if it is valid
474
+ if (false == $payload) {
475
+ return false;
476
+ }
477
+ //echo $payload,'<br/>';
478
+ //echo self::decode64($payload),'<br/>';
479
+
480
+
481
+ // update addTransaction History
482
+ $history = self::getVal(self::TX_HISTORY);
483
+ if( empty( $history )){
484
+ $history = array();
485
+ }
486
+
487
+ $curtime = date(DATE_RFC822);
488
+ $history[ $curtime ] = $_SERVER['REQUEST_URI'];
489
+
490
+ self::setVal(self::TX_HISTORY, $history);
491
+
492
+ // get response
493
+ return self::getBonusboxResponse($payload, self::ADD_URL);
494
+
495
+ //return $bonusboxoutput->transaction_id;
496
+ }
497
+
498
+ public static function printLastTransactionHistory( $number = 10 ){
499
+ $history = self::getVal(self::TX_HISTORY);
500
+
501
+ echo 'printing transaction history:<hr/>';
502
+ $count = 0;
503
+ foreach( $history as $key=>$val ){
504
+ echo (1 + $count++ ),'Time:',$key,' from ',$val,'<br>';
505
+ if( $count == $number)break;
506
+ }
507
+ echo '<hr/>';
508
+ }
509
+
510
+
511
+ /**
512
+ * Decode input string
513
+ */
514
+ public static function decode64($input) {
515
+ return base64_decode(strtr($input, '-_', '+/'));
516
+ }
517
+
518
+ /**
519
+ * Encode input string
520
+ */
521
+ private static function encode64($input) {
522
+ return trim(strtr(base64_encode($input), '+/=', '-_ '));
523
+ }
524
+
525
+ /**
526
+ * Send a GET requst using cURL
527
+ * @param string $url to request
528
+ * @param array $get values to send
529
+ * @param array $options for cURL
530
+ * @return string
531
+ */
532
+ private static function curlGet($url, array $get = NULL, array $options = array()) {
533
+
534
+
535
+ $defaults = array(
536
+ CURLOPT_URL => $url . (strpos($url, '?') === FALSE ? '?' : '') . http_build_query($get),
537
+ CURLOPT_HEADER => 0,
538
+ CURLOPT_RETURNTRANSFER => TRUE,
539
+ CURLOPT_TIMEOUT => 4,
540
+ CURLOPT_SSL_VERIFYPEER => false,
541
+ CURLOPT_SSL_VERIFYHOST => 2
542
+ );
543
+ // echo $defaults[CURLOPT_URL];
544
+ $ch = curl_init();
545
+ curl_setopt_array($ch, ($options + $defaults));
546
+ if (!$result = curl_exec($ch)) {
547
+ trigger_error(curl_error($ch));
548
+ //debug_print_backtrace();
549
+ //die();
550
+ }
551
+
552
+ // dei('---'.$result);
553
+ curl_close($ch);
554
+ return $result;
555
+ }
556
+
557
+ /**
558
+ * Makes encrypted signature with payload and secret
559
+ */
560
+ private static function makeSignature($payload, $secret) {
561
+ return $payload;
562
+ return hash_hmac('sha256', $payload, $secret, $raw = true);
563
+ }
564
+
565
+ /**
566
+ * Saves user token to session
567
+ */
568
+ public static function setUserToken($token) {
569
+
570
+ if (empty($token)) { // user token cannot be empty
571
+ return self::setError(self::EMPTY_USER_TOKEN);
572
+ }
573
+
574
+ self::setVal(self::TOKEN, $token);
575
+ return true;
576
+ }
577
+
578
+ /**
579
+ * Returns the user token from session
580
+ */
581
+ public static function getUserToken() {
582
+ return self::getVal(self::TOKEN);
583
+ }
584
+
585
+ public static function setShopUserToken($token) {
586
+ self::setVal(self::SHOP_TOKEN, $token);
587
+ return true;
588
+ }
589
+
590
+ private static function getShopUserToken() {
591
+ return self::getVal(self::SHOP_TOKEN);
592
+ }
593
+
594
+ public static function setTrackURL($url) {
595
+ if (empty($url)) { // app id cannot be empty
596
+ return self::setError(self::EMPTY_M_TRACK_URL);
597
+ }
598
+
599
+ self::setVal(self::M_TRACK_URL, $url);
600
+ }
601
+
602
+ public static function getTrackURL() {
603
+ return self::getVal(self::M_TRACK_URL);
604
+ }
605
+
606
+ /**
607
+ * Sets the app id to session
608
+ */
609
+ public static function setAppID($appId) {
610
+ if (empty($appId)) { // app id cannot be empty
611
+ return self::setError(self::EMPTY_APP_ID);
612
+ }
613
+
614
+ self::setVal(self::APP_ID, $appId);
615
+
616
+ return true;
617
+ }
618
+
619
+ private static function getAppId() {
620
+ return self::getVal(self::APP_ID);
621
+ }
622
+
623
+ private static function getAppSecret() {
624
+ return self::getVal(self::SECRET);
625
+ }
626
+
627
+ private static function getCartTotal() {
628
+ return self::getVal(self::TOTAL);
629
+ }
630
+
631
+ /**
632
+ * Saves application secret to session
633
+ */
634
+ public static function setAppSecret($secret) {
635
+ if (empty($secret)) { // app secret cannot be empty
636
+ return self::setError(self::EMPTY_APP_SECRET);
637
+ }
638
+
639
+ self::setVal(self::SECRET, $secret);
640
+ return true;
641
+ }
642
+
643
+ public static function getCurrentURL() {
644
+ $url = (!empty($_SERVER['HTTPS'])) ? "https://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] : "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
645
+ return $url;
646
+ }
647
+
648
+ public static function updateUserStatus() {
649
+
650
+ // updateing config file
651
+ //self::$configFile = Mage::getBaseDir('code') . "/community/Coeus/FacebookBonus/Model/cache.txt";
652
+
653
+ //die( self::$configFile );
654
+
655
+ $payload = $_GET[self::PAYLOAD];
656
+ //echo $payload;
657
+ $data = json_decode(self::decode64($payload), true);
658
+ //var_dump($data);
659
+
660
+
661
+ $user_status = $_GET['user_status'];
662
+
663
+ if ('LOGGED_OUT' == $user_status) {
664
+ self::unsetUserToken();
665
+ //self::$config = array();
666
+ } else {
667
+ self::setUserToken($data[self::TOKEN]);
668
+ self::setShopUserToken($data[self::SHOP_TOKEN]);
669
+ }
670
+
671
+ if ('NO_CHANGE' != $user_status) {
672
+ ?>
673
+ <script>
674
+ window.parent.location.href = "<?php echo $data['last_url']; ?>";
675
+ </script>
676
+ <?
677
+ }
678
+ }
679
+
680
+ public static function trackUser($refresh_time = 10000) {
681
+
682
+ $uid = self::getUserToken();
683
+ $url = self::getCurrentURL();
684
+ $track_url = self::getTrackURL();
685
+
686
+ $data = array(
687
+ 'last_url' => $url
688
+ , 'uid' => $uid
689
+ , 'track_url' => $track_url
690
+ , 'refresh_time' => $refresh_time
691
+ );
692
+
693
+ $payload = self::encode64(json_encode($data));
694
+ ?>
695
+ <iframe
696
+ style='width:1px; height: 1px'
697
+ frameborder="0"
698
+ scrolling="no"
699
+ src="<?php echo self::TRACK_URL . '?' . self::PAYLOAD . '=' . $payload; ?>">
700
+ </iframe>
701
+ <?
702
+ }
703
+
704
+ /**
705
+ * If user is not logged in or connected call this method.
706
+ * The method will create a iframe
707
+ */
708
+ public static function addLoginButton($text = 'Login', $width='100', $height='23', $css='') {
709
+
710
+ $url = self::getCurrentURL();
711
+ $track_url = self::getTrackURL();
712
+
713
+ $data = array(
714
+ 'last_url' => $url
715
+ , 'text' => $text
716
+ , 'track_url' => $track_url
717
+ );
718
+
719
+ $payload = self::encode64(json_encode($data));
720
+
721
+ echo' <iframe ';
722
+ echo " style='width:{$width}px; height: {$height}px;$css;' ";
723
+ echo " frameborder='0' ";
724
+ echo " scrolling='no' ";
725
+ echo " src='" . self::LOGIN_URL . "?" . self::PAYLOAD . "={$payload}' >";
726
+ echo' </iframe>';
727
+ }
728
+
729
+ /**
730
+ * This method shows the facebook publish dialog in an iframe.
731
+ * Size of the iframe can be configured by parameters
732
+ *
733
+ * Example:
734
+ * <code>
735
+ * Bonusbox::showPublish( $res1->getTransactionID() );
736
+ * </code>
737
+ * @param $transactionId last transaction id
738
+ * @param $width specify width of the iframe
739
+ * @param $height specify height of the iframe
740
+ */
741
+ public static function showPublish($transactionId, $width = 600, $height=270) {
742
+ echo' <iframe ';
743
+ echo " style='width:{$width}px; height: {$height}px;$css;' ";
744
+ echo " frameborder='0' ";
745
+ echo " scrolling='no' ";
746
+ echo " src='" . self::PUBLISH_URL . $transactionId . "' >";
747
+ echo' </iframe>';
748
+ }
749
+
750
+ // urls! sync with latest version!
751
+
752
+ const ADD_URL = 'https://app.bonusbox.me/api/v2/transaction/add';
753
+ const GET_URL = 'https://app.bonusbox.me/api/v2/iframe/get';
754
+ const LOGIN_URL = 'https://app.bonusbox.me/api/v2/iframe/login';
755
+ const TRACK_URL = 'https://app.bonusbox.me/api/v2/iframe/track';
756
+ const PUBLISH_URL = 'https://app.bonusbox.me/api/v2/iframe/get_publish?transaction_id=';
757
+
758
+ // array keys, must match with backend!
759
+ const ARTICLE = 'article';
760
+ const AMOUNT = 'amount';
761
+ const ITEMS = 'items';
762
+ const TOKEN = 'uid';
763
+ const APP_ID = 'app_id';
764
+ const SECRET = 'secret';
765
+ const SHOP_TOKEN = 'shop_user_token';
766
+
767
+ const CART = 'cart';
768
+ const TOTAL = 'total_amount';
769
+ const PAYLOAD = 'payload';
770
+ const RESPONSE = 'response';
771
+
772
+ const M_TRACK_URL = 'm_track_url';
773
+
774
+ // used to track multple calling of addTransaction method
775
+ const TX_HISTORY = 'TX_HISTORY';
776
+
777
+
778
+
779
+ //////////////////////////////////////////////////////////////////////////
780
+ ////////////////////// Errors !! ////////////////////////////////////////
781
+ //////////////////////////////////////////////////////////////////////////
782
+ /**
783
+ * This variable holds the error
784
+ */
785
+ const NO_ERROR = 0;
786
+ const INVALID_TOTAL_AMOUNT = 1;
787
+ const INVALID_ITEM_FORMAT = 2;
788
+ const EMPTY_TOTAL_AMOUNT = 3;
789
+ const EMPTY_USER_TOKEN = 4;
790
+ const EMPTY_APP_ID = 5;
791
+ const EMPTY_CART = 6;
792
+ const EMPTY_ITEM_ARTICLE = 7;
793
+ const EMPTY_ITEM_AMOUNT = 8;
794
+ const INVALID_ITEM_AMOUNT = 9;
795
+ const EMPTY_APP_SECRET = 10;
796
+ const INVALID_API_RESPONSE = 11;
797
+ const EMPTY_API_URL = 12;
798
+ const EMPTY_PAYLOAD = 13;
799
+ const EMPTY_M_TRACK_URL = 14;
800
+ //const EMPTY_SHOP_USER_TOKEN = 15;
801
+
802
+ /**
803
+ * Messsages for error codes
804
+ */
805
+ private static $errorMessages = array(
806
+ self::NO_ERROR => 'No error! '
807
+ , self::EMPTY_USER_TOKEN => 'User token cannot be empty'
808
+ , self::INVALID_TOTAL_AMOUNT => 'Total amount is not numeric'
809
+ , self::INVALID_ITEM_FORMAT => 'Item array should have article and amount keys '
810
+ , self::EMPTY_TOTAL_AMOUNT => 'Total Amount empty'
811
+ , self::EMPTY_APP_ID => 'App ID cannot be empty'
812
+ , self::EMPTY_CART => 'Cart is empty!'
813
+ , self::EMPTY_ITEM_ARTICLE => 'Item article is empty'
814
+ , self::EMPTY_ITEM_AMOUNT => 'Item amount is empty'
815
+ , self::INVALID_ITEM_AMOUNT => 'Item amount is not numeric'
816
+ , self::EMPTY_APP_SECRET => 'App secret is empty'
817
+ , self::INVALID_API_RESPONSE => 'Received invalid data format from server!'
818
+ , self::EMPTY_API_URL => 'Failed to make server request: API url is empty'
819
+ , self::EMPTY_PAYLOAD => 'Failed to make server request: Empty payload passed'
820
+ , self::EMPTY_M_TRACK_URL => 'User status track url is empty: Please give one'
821
+ );
822
+ /**
823
+ * Holds the last error code
824
+ */
825
+ private static $lastErrorCode;
826
+
827
+ /**
828
+ * Get error message for an error code
829
+ *
830
+ * @param errorCode Error code of interest
831
+ */
832
+ public static function getErrorMessage($errorCode) {
833
+ return self::$errorMessages[$errorCode];
834
+ }
835
+
836
+ /**
837
+ * Returns the last error code
838
+ */
839
+ public static function getLastErrorCode() {
840
+ return self::$lastErrorCode;
841
+ }
842
+
843
+ private static function setError($errorCode) {
844
+ self::$lastErrorCode = $errorCode;
845
+ return false;
846
+ }
847
+
848
+ /**
849
+ * Returns the last error message
850
+ */
851
+ public static function getLastErrorMessage() {
852
+ return self::getErrorMessage(self::getLastErrorCode());
853
+ }
854
+
855
+ /**
856
+ * Unsets the last error code
857
+ */
858
+ public static function unsetLastError() {
859
+ self::$lastErrorCode = self::NO_ERROR;
860
+ }
861
+
862
+ /**
863
+ * Checks if there was an error
864
+ */
865
+ public static function hasError() {
866
+ return self::$lastErrorCode != self::NO_ERROR;
867
+ }
868
+
869
+ }
870
+
871
+ /**
872
+ * This class provides accessor methods to the reponse received
873
+ * by the bonusbox server
874
+ *
875
+ * This class is *NOT* meant be instantiated outside Bonusbox class.
876
+ */
877
+ class BonusBoxResponse {
878
+
879
+ /**
880
+ * Holds the json object created from the response data
881
+ */
882
+ private $response = NULL;
883
+
884
+ /**
885
+ * Constructor
886
+ * @param response Response data as received from server
887
+ */
888
+ public function BonusBoxResponse($response) {
889
+ $this->response = $response;
890
+ }
891
+
892
+ /**
893
+ * Returns credit amount
894
+ */
895
+ public function getCredit() {
896
+ return $this->get(self::CREDITS);
897
+ }
898
+
899
+ public function getFBUserName() {
900
+ $fbUser = $this->getUser();
901
+ $graphUrl = "https://graph.facebook.com/" . $fbUser->{'facebook_id'};
902
+ $fbUser = json_decode(file_get_contents($graphUrl));
903
+ return $fbUser->{'name'};
904
+ }
905
+
906
+ /**
907
+ * Returns user name if user is connected to facebook.
908
+ * If user is not connected to facebook, return facebook login tag
909
+ */
910
+ public function getUser() {
911
+
912
+ return $this->get(self::USER);
913
+ }
914
+
915
+ public function getUserToken() {
916
+ return $this->get(self::UID);
917
+ }
918
+
919
+ /**
920
+ * Discount amount on the cart total
921
+ */
922
+ public function getDiscount() {
923
+ return $this->get(self::DISCOUNT);
924
+ }
925
+
926
+ /**
927
+ * This is available if the user is new or never purchased before.
928
+ * The amount is calcualted based on the cart total and shop initial discount
929
+ */
930
+ public function getSavedAmount() {
931
+ return $this->get(self::SAVED_AMOUNT);
932
+ }
933
+
934
+ /**
935
+ * Returns true if user is connected to facebook else false
936
+ */
937
+ public function isConnected() {
938
+ return!(isset($this->response->{ self::NOT_CONNECTED }));
939
+ }
940
+
941
+ /**
942
+ * Returns debug info from server
943
+ */
944
+ public function getDebugInfo() {
945
+ return $this->get(self::DEBUG);
946
+ }
947
+
948
+ /**
949
+ * Returns true if user has never made any purchase before
950
+ */
951
+ public function isNewUser() {
952
+ return $this->response->{ self::INITIAL } == 'yes';
953
+ }
954
+
955
+ /**
956
+ * If the user has bought something before from this shop
957
+ * If not, user will receive initial discount
958
+ */
959
+ public function hasSpentBefore() {
960
+ return $this->response->{ self::HAS_SPENT_BEFORE } == 'yes';
961
+ }
962
+
963
+ /**
964
+ * Returns initial discount for first purchase! This amount
965
+ * will be applied to the cart total amount
966
+ */
967
+ public function getShopInitialDiscount() {
968
+ return $this->response->{ self::SHOP_INITIAL_DISCOUNT };
969
+ }
970
+
971
+ public function getCurrentBadgeName(){
972
+ return $this->response->{ self::CURRENT_BADGE };
973
+ }
974
+
975
+ public function getNextBadgeName(){
976
+ return $this->response->{ self::NEXT_BADGE };
977
+ }
978
+
979
+ /**
980
+ * Discount for user's current badge
981
+ */
982
+ public function getBadgeDiscount() {
983
+ return $this->response->{ self::BADGE_DISCOUNT };
984
+ }
985
+
986
+ public function isTransactionSuccess() {
987
+ return $this->response->{ self::TX_RESULT } === true;
988
+ }
989
+
990
+ public function getTransactionError() {
991
+ return $this->response->{ self::TX_ERROR };
992
+ }
993
+
994
+ public function getTransactionID() {
995
+ return $this->response->{ self::TX_ID };
996
+ }
997
+
998
+ public function getTransactionSignature() {
999
+ return $this->response->{ self::TX_SIGNATURE };
1000
+ }
1001
+
1002
+
1003
+ const USER = 'user';
1004
+ const INITIAL = 'initial';
1005
+ const CREDITS = 'credits';
1006
+ const DISCOUNT = 'discount';
1007
+ const BADGE_DISCOUNT = 'badge_discount';
1008
+ const SAVED_AMOUNT = 'saved_amount';
1009
+ const NOT_CONNECTED = 'not_connected';
1010
+ const CURRENT_BADGE = 'badge';
1011
+ const NEXT_BADGE = 'next_badge';
1012
+ const DEBUG = 'debug';
1013
+ const UID = 'uid';
1014
+
1015
+ const HAS_SPENT_BEFORE = 'has_spent_before';
1016
+
1017
+
1018
+ const TX_RESULT = 'success';
1019
+ const TX_ID = 'transaction_id';
1020
+ const TX_SIGNATURE = 'signature';
1021
+ const TX_ERROR = 'error';
1022
+
1023
+ const SHOP_INITIAL_DISCOUNT = 'shop_discount';
1024
+
1025
+ public function get($attr) {
1026
+ if (isset($this->response->$attr)) {
1027
+ return $this->response->$attr;
1028
+ }
1029
+ return NULL;
1030
+ }
1031
+
1032
+ }
app/code/community/Coeus/FacebookBonus/Model/BonusboxConnect.php ADDED
@@ -0,0 +1,995 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * This is generic php implementation of Bonusbox class
5
+ * independant of any framework or sytem
6
+ *
7
+ * Example:
8
+ * <code>
9
+ * Bonusbox::init( 'YOUR_APP_ID', 'YOUR_APP_SECRET', 'USER_TRACK_URL');
10
+ * Bonusbox::addItem( 'Item A', '200' );
11
+ * Bonusbox::addItem( 'Item B', '300' );
12
+ * Bonusbox::getDiscount();// It will return you the discount
13
+ * Bonusbox::addTransaction();
14
+ * </code>
15
+ *
16
+ * @author Shaikh Sonny Aman (mailtoaman@gmail.com)
17
+ * @date 21 July 2011
18
+ * @copyright Bonusbox GmbH
19
+ */
20
+ class Coeus_FacebookBonus_Model_BonusboxConnect {
21
+
22
+ private static $configFile = "./cache.txt";
23
+ private static $config = array();
24
+
25
+ /**
26
+ * Initializes bonusbox.
27
+ *
28
+ * This must be called at least once before any other Bonusbox
29
+ * method calls
30
+ *
31
+ * If userToken is not passed it will first check if payload is available
32
+ * in $_POST or $_GET and retrieves user token from that.
33
+ *
34
+ * If no parameter is passed it will assume Bonusbox is initialized before.
35
+ * It will check if appId and secret is available in $_SESSION and also try to
36
+ * retrieve userToken from paylod if any.
37
+ *
38
+ * If the merchant website is already a connected to facebook a facebook web app,
39
+ * then connected user's facebook uid can be passed as userToken.
40
+ *
41
+ * @param appId Application ID
42
+ * @param secret Application Secret
43
+ * @param track_url A url to monitor user login status to Facebook.
44
+ * The url just need to call
45
+ * <p>
46
+ * <code>Bonusbox::updateUserStatus();</code>
47
+ * </p>
48
+ * @param userToken User's facebook id.
49
+ */
50
+ public static function init($appId = '', $secret = '', $track_url = '', $userToken = '', $cacheFileLoc = '') {
51
+
52
+
53
+ if (!empty($appId)) {
54
+ self::setAppID($appId);
55
+ }
56
+
57
+ if (!empty($secret)) {
58
+ self::setAppSecret($secret);
59
+ }
60
+
61
+ if (!empty($userToken)) {
62
+ self::setUserToken($token);
63
+ }
64
+
65
+ if (!empty($track_url)) {
66
+ self::setTrackURL($track_url);
67
+ }
68
+
69
+
70
+ // checks if user token is available
71
+ if (!self::isLoggedinIntoFacebook()) { // nope, not available
72
+ $payload = NULL;
73
+ // check if payload is available as $_GET or $_POST parameter
74
+ if (isset($_GET[self::PAYLOAD]) && !empty($_GET[self::PAYLOAD])) {
75
+ $payload = $_GET[self::PAYLOAD];
76
+ } else if (isset($_POST[self::PAYLOAD]) && !empty($_POST[self::PAYLOAD])) {
77
+ $payload = $_POST[self::PAYLOAD];
78
+ }
79
+
80
+ if (!empty($payload)) {
81
+ $bb = self::getBonusboxResponse($payload);
82
+ if ($bb) {
83
+ $token = $bb->getUserToken();
84
+ self::setUserToken($token);
85
+ }
86
+ }
87
+ }
88
+
89
+ /**
90
+ * Initialized cart if not initialized
91
+ */
92
+ if (!self::isKeySet(self::CART)) {
93
+ self::resetCart();
94
+ self::setCartTotalAmount(0);
95
+ }
96
+ }
97
+
98
+ /**
99
+ * Checks if we have user token
100
+ */
101
+ public static function isLoggedinIntoFacebook() {
102
+ $token = self::getUserToken();
103
+ if (empty($token)) {
104
+ return false;
105
+ }
106
+
107
+ return true;
108
+ }
109
+
110
+ /**
111
+ * This is just for internal debug use
112
+ */
113
+ public static function unsetUserToken() {
114
+ unset($_SESSION['__bonusbox__' . self::TOKEN]);
115
+ }
116
+
117
+ /**
118
+ * Updates discount information
119
+ *
120
+ * Call this function after adding some items to cart or
121
+ * if user token is to be saved from payload
122
+ *
123
+ * @param $payload If passed empty it retrieves payload from server
124
+ */
125
+ public static function update($payload = '') {
126
+
127
+ $response = NULL;
128
+ if (empty($payload)) {
129
+ $response = self::getDiscountInfo();
130
+ } else {
131
+ $response = self::getBonusboxResponse($payload);
132
+ }
133
+ // if valid response, update the user token
134
+ if ($response) {
135
+ self::setUserToken($response->getUserToken());
136
+ }
137
+
138
+ return $response;
139
+ }
140
+
141
+ /**
142
+ * Saves a value to the session
143
+ * @param key The key for the value
144
+ * @param val The value to save
145
+ */
146
+ private static function setVal($key, $val) {
147
+ $_SESSION['__bonusbox__' . $key] = $val;
148
+ }
149
+
150
+ /**
151
+ * Returns value for the key from the session
152
+ * @param key Key for the value
153
+ */
154
+ private static function getVal($key) {
155
+
156
+ if (self::isKeySet($key)) {
157
+ return $_SESSION['__bonusbox__' . $key];
158
+ }
159
+
160
+ return NULL;
161
+ }
162
+
163
+ /**
164
+ * Checks if a key is set
165
+ * @param key Key to check
166
+ */
167
+ private static function isKeySet($key) {
168
+ return isset( $_SESSION[ '__bonusbox__'.$key] );
169
+ }
170
+
171
+ /**
172
+ * This method clear any item added to Bonusbox before
173
+ */
174
+ public static function resetCart() {
175
+ self::unsetLastError();
176
+ if (self::isKeySet(self::CART)) {
177
+ unset($_SESSION['__bonusbox__' . self::CART]); // direct access :(
178
+ }
179
+
180
+ self::setVal(self::CART, array());
181
+ }
182
+
183
+ /**
184
+ * This method returns items added to Bonusbox
185
+ */
186
+ private static function getCart() {
187
+ return self::getVal(self::CART);
188
+ }
189
+
190
+ /**
191
+ * Use this function to pupulate cartItems in Bonusbox
192
+ *
193
+ * @param article Name of the item
194
+ * @param amount Price of the item. Price is not rounded inside the function!
195
+ */
196
+ public static function addItem($article, $amount) {
197
+
198
+ // don't if there were errors previously
199
+ if (self::hasError()
200
+
201
+ )return false;
202
+
203
+ //////////// input validation ///////////
204
+ if (empty($article)) { // Item article name cannot be empty
205
+ return self::setError(self::EMPTY_ITEM_ARTICLE);
206
+ }
207
+
208
+ if (empty($amount)) { // Item amount cannot be empty
209
+ return self::setError(self::EMPTY_ITEM_AMOUNT);
210
+ }
211
+
212
+ if (!is_numeric($amount)) { // Item amount must be numeric
213
+ return self::setError(self::INVALID_TOTAL_AMOUNT);
214
+ }
215
+ ///////////////////////////
216
+ // finally add the item to the list
217
+ $cart = self::getCart();
218
+ if (!is_array($cart)) { // cart is not array! how comes?!
219
+ self::resetCart();
220
+ $cart = self::getCart();
221
+ }
222
+
223
+ $cart[] = array('article' => $article, 'amount' => $amount);
224
+ self::setVal(self::CART, $cart);
225
+
226
+ // unset error, reduntant
227
+ self::setError(self::NO_ERROR);
228
+
229
+ return true;
230
+ }
231
+
232
+ public static function setCartTotalAmount($amount) {
233
+
234
+ if (empty($amount)) {
235
+ return self::setError(self::EMPTY_TOTAL_AMOUNT);
236
+ }
237
+
238
+ if (!is_numeric($amount)) {
239
+ return self::setError(self::INVALID_TOTAL_AMOUNT);
240
+ }
241
+
242
+ self::setVal(self::TOTAL, $amount);
243
+ return true;
244
+ }
245
+
246
+ /**
247
+ * Returns discount information for the user and cart items
248
+ */
249
+ public static function getDiscountInfo() {
250
+ // get the payload
251
+ $payload = self::getPayload(false);
252
+
253
+ // check if it is valid
254
+ if (false == $payload) {
255
+ return false;
256
+ }
257
+
258
+ return self::getBonusboxResponse($payload, self::GET_URL);
259
+ }
260
+
261
+ /**
262
+ * If url is passed it retrieves server response
263
+ * for the given payload data.
264
+ *
265
+ * If no url is passed, it consider payload received from
266
+ * server and makes Bonusbox response from that
267
+ *
268
+ */
269
+ private static function getBonusboxResponse($payload, $url = '') {
270
+ if (empty($payload)) {
271
+ return self::setError(self::EMPTY_PAYLOAD);
272
+ }
273
+
274
+ $content = NULL;
275
+ if (empty($url)) {// payload is received from server
276
+ $content = $payload;
277
+ } else {// get response from server
278
+ // get the content
279
+ $content = self::curlGet($url, array(self::PAYLOAD => $payload));
280
+ }
281
+
282
+ // decode
283
+ $jsonString = self::decode64($content);
284
+
285
+ // conver to json object
286
+ $obj = json_decode($jsonString);
287
+
288
+ // check if received valid json format
289
+ if (NULL == $obj) {
290
+ return self::setError(self::INVALID_API_RESPONSE);
291
+ }
292
+
293
+ return new BonusBoxResponse($obj);
294
+ }
295
+
296
+ public static function getPayload($isTransaction = false) {
297
+
298
+ if (self::hasError()) { // return if there were some previous error
299
+ return false;
300
+ }
301
+
302
+
303
+ /**
304
+ * Populate the data values
305
+ */
306
+ $values = array();
307
+
308
+ $values[self::APP_ID] = self::getAppId();
309
+ $values[self::TOKEN] = self::getUserToken();
310
+ $values[self::ITEMS] = self::getCart();
311
+ $values[self::AMOUNT] = self::getCartTotal();
312
+ $values[self::SHOP_TOKEN] = self::getShopUserToken();
313
+
314
+
315
+ // final validation check
316
+ if (false === self::isValidPaylodData($values, $isTransaction)) {
317
+ return false;
318
+ }
319
+
320
+ /**
321
+ * Encode data
322
+ */
323
+ $bonusboxdata = json_encode($values);
324
+ $bonusboxdata = self::encode64($bonusboxdata);
325
+
326
+ $payload = $bonusboxdata;
327
+ return $payload;
328
+ }
329
+
330
+ private static function isValidPaylodData($data, $isTransaction) {
331
+ // check app id
332
+ if (empty($data[self::APP_ID])) {
333
+ return self::setError(self::EMPTY_APP_ID);
334
+ }
335
+
336
+
337
+ // check amount set
338
+ if (empty($data[self::AMOUNT])) {
339
+ return self::setError(self::EMPTY_TOTAL_AMOUNT);
340
+ }
341
+
342
+ // check amount is numeric
343
+ if (!is_numeric($data[self::AMOUNT])) {
344
+ return self::setError(self::INVALID_TOTAL_AMOUNT);
345
+ }
346
+
347
+ // check appSecret though not in passed data
348
+ $secret = self::getAppSecret();
349
+ if (empty($secret)) {
350
+ return self::setError(self::EMPTY_APP_SECRET);
351
+ }
352
+
353
+
354
+ ///// now checking items ////
355
+ // check if items are array!
356
+ if (!is_array($data[self::ITEMS])) {
357
+ return self::setError(self::INVALID_ITEM_FORMAT);
358
+ }
359
+
360
+ // additional checks before sending transaction
361
+ if (true === $isTransaction) {
362
+
363
+ // check for user token
364
+ $tok = self::getUserToken();
365
+ if (empty($tok)) {
366
+ return self::setError(self::EMPTY_USER_TOKEN);
367
+ }
368
+
369
+ // check if any item added
370
+ if (empty($data[self::ITEMS])) {
371
+ return self::setError(self::EMPTY_CART);
372
+ }
373
+
374
+ // check if all items has valid format
375
+ foreach ($data[self::ITEMS] as $item) {
376
+ if (!isset($item[self::ARTICLE])) {
377
+ return self::setError(self::INVALID_ITEM_FORMAT);
378
+ }
379
+
380
+ if (!isset($item[self::AMOUNT])) {
381
+ return self::setError(self::INVALID_ITEM_FORMAT);
382
+ }
383
+
384
+ if (empty($item[self::ARTICLE])) {
385
+ return self::setError(self::EMPTY_ITEM_ARTICLE);
386
+ }
387
+
388
+ if (!is_numeric($item[self::AMOUNT])) {
389
+ return self::setError(self::INVALID_ITEM_AMOUNT);
390
+ }
391
+ }
392
+ }
393
+
394
+ // reached here!!? cool, everything is fine :)
395
+ return true;
396
+ }
397
+
398
+ // Decodes the payload returned by bonusbox
399
+ function decode_payload($encoded_payload) {
400
+ /**
401
+ * Get paylod and split into signature and data
402
+ */
403
+ list($encoded_sig, $encoded_payload) = explode('.', $encoded_payload, 2);
404
+
405
+ /**
406
+ * Decode data and signature
407
+ */
408
+ $data = json_decode($this->decode64($encoded_payload), true);
409
+ $sig = $this->decode64($encoded_sig);
410
+
411
+ /**
412
+ * Verify signature with shop app_secret
413
+ */
414
+ $expected_sig = $this->makeSignature($encoded_payload, $this->secret);
415
+
416
+ // If signature is correct, return it
417
+ if ($sig == $expected_sig)
418
+ return $data;
419
+ else
420
+ return false;
421
+ }
422
+
423
+ /**
424
+ * Adds the transaction to bonus box
425
+ *
426
+ * This will not submit to bonux box server if:
427
+ * <ol>
428
+ * <li>Bonusbox is NOT initialized</li>
429
+ * <li>NO item was added to Bonusbox</li>
430
+ * <li>User token NOT found </li>
431
+ * </ol>
432
+ *
433
+ */
434
+ public static function addTransaction() {
435
+
436
+ $payload = self::getPayload(true);
437
+
438
+ // check if it is valid
439
+ if (false == $payload) {
440
+ return false;
441
+ }
442
+
443
+ // update addTransaction History
444
+ $history = self::getVal(self::TX_HISTORY);
445
+ if( empty( $history )){
446
+ $history = array();
447
+ }
448
+
449
+ $curtime = date(DATE_RFC822);
450
+ $history[ $curtime ] = $_SERVER['REQUEST_URI'];
451
+
452
+ self::setVal(self::TX_HISTORY, $history);
453
+
454
+ // get response
455
+ return self::getBonusboxResponse($payload, self::ADD_URL);
456
+
457
+ }
458
+
459
+ public static function printLastTransactionHistory( $number = 10 ){
460
+ $history = self::getVal(self::TX_HISTORY);
461
+
462
+ echo 'printing transaction history:<hr/>';
463
+ $count = 0;
464
+ foreach( $history as $key=>$val ){
465
+ echo (1 + $count++ ),'Time:',$key,' from ',$val,'<br>';
466
+ if( $count == $number)break;
467
+ }
468
+ echo '<hr/>';
469
+ }
470
+
471
+
472
+ /**
473
+ * Decode input string
474
+ */
475
+ public static function decode64($input) {
476
+ return base64_decode(strtr($input, '-_', '+/'));
477
+ }
478
+
479
+ /**
480
+ * Encode input string
481
+ */
482
+ private static function encode64($input) {
483
+ return trim(strtr(base64_encode($input), '+/=', '-_ '));
484
+ }
485
+
486
+ /**
487
+ * Send a GET requst using cURL
488
+ * @param string $url to request
489
+ * @param array $get values to send
490
+ * @param array $options for cURL
491
+ * @return string
492
+ */
493
+ private static function curlGet($url, array $get = NULL, array $options = array()) {
494
+
495
+
496
+ $defaults = array(
497
+ CURLOPT_URL => $url . (strpos($url, '?') === FALSE ? '?' : '') . http_build_query($get),
498
+ CURLOPT_HEADER => 0,
499
+ CURLOPT_RETURNTRANSFER => TRUE,
500
+ CURLOPT_TIMEOUT => 4,
501
+ CURLOPT_SSL_VERIFYPEER => false,
502
+ CURLOPT_SSL_VERIFYHOST => 2
503
+ );
504
+ // echo $defaults[CURLOPT_URL];
505
+ $ch = curl_init();
506
+ curl_setopt_array($ch, ($options + $defaults));
507
+ if (!$result = curl_exec($ch)) {
508
+ trigger_error(curl_error($ch));
509
+ }
510
+
511
+ curl_close($ch);
512
+ return $result;
513
+ }
514
+
515
+ /**
516
+ * Makes encrypted signature with payload and secret
517
+ */
518
+ private static function makeSignature($payload, $secret) {
519
+ return $payload;
520
+ return hash_hmac('sha256', $payload, $secret, $raw = true);
521
+ }
522
+
523
+ /**
524
+ * Saves user token to session
525
+ */
526
+ public static function setUserToken($token) {
527
+
528
+ if (empty($token)) { // user token cannot be empty
529
+ return self::setError(self::EMPTY_USER_TOKEN);
530
+ }
531
+
532
+ self::setVal(self::TOKEN, $token);
533
+ return true;
534
+ }
535
+
536
+ /**
537
+ * Returns the user token from session
538
+ */
539
+ public static function getUserToken() {
540
+ return self::getVal(self::TOKEN);
541
+ }
542
+
543
+ public static function setShopUserToken($token) {
544
+ self::setVal(self::SHOP_TOKEN, $token);
545
+ return true;
546
+ }
547
+
548
+ private static function getShopUserToken() {
549
+ return self::getVal(self::SHOP_TOKEN);
550
+ }
551
+
552
+ public static function setTrackURL($url) {
553
+ if (empty($url)) { // app id cannot be empty
554
+ return self::setError(self::EMPTY_M_TRACK_URL);
555
+ }
556
+
557
+ self::setVal(self::M_TRACK_URL, $url);
558
+ }
559
+
560
+ public static function getTrackURL() {
561
+ return self::getVal(self::M_TRACK_URL);
562
+ }
563
+
564
+ /**
565
+ * Sets the app id to session
566
+ */
567
+ public static function setAppID($appId) {
568
+ if (empty($appId)) { // app id cannot be empty
569
+ return self::setError(self::EMPTY_APP_ID);
570
+ }
571
+
572
+ self::setVal(self::APP_ID, $appId);
573
+
574
+ return true;
575
+ }
576
+
577
+ private static function getAppId() {
578
+ return self::getVal(self::APP_ID);
579
+ }
580
+
581
+ private static function getAppSecret() {
582
+ return self::getVal(self::SECRET);
583
+ }
584
+
585
+ private static function getCartTotal() {
586
+ return self::getVal(self::TOTAL);
587
+ }
588
+
589
+ /**
590
+ * Saves application secret to session
591
+ */
592
+ public static function setAppSecret($secret) {
593
+ if (empty($secret)) { // app secret cannot be empty
594
+ return self::setError(self::EMPTY_APP_SECRET);
595
+ }
596
+
597
+ self::setVal(self::SECRET, $secret);
598
+ return true;
599
+ }
600
+
601
+ public static function getCurrentURL() {
602
+ $url = (!empty($_SERVER['HTTPS'])) ? "https://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] : "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
603
+ return $url;
604
+ }
605
+
606
+ public static function updateUserStatus() {
607
+
608
+
609
+ $payload = $_GET[self::PAYLOAD];
610
+ $data = json_decode(self::decode64($payload), true);
611
+
612
+
613
+ $user_status = $_GET['user_status'];
614
+
615
+ if ('LOGGED_OUT' == $user_status) {
616
+ self::unsetUserToken();
617
+ } else {
618
+ self::setUserToken($data[self::TOKEN]);
619
+ self::setShopUserToken($data[self::SHOP_TOKEN]);
620
+ }
621
+
622
+ if ('NO_CHANGE' != $user_status) {
623
+ ?>
624
+ <script>
625
+ window.parent.location.href = "<?php echo $data['last_url']; ?>";
626
+ </script>
627
+ <?php
628
+ }
629
+ }
630
+
631
+ public static function trackUser($refresh_time = 10000) {
632
+
633
+ $uid = self::getUserToken();
634
+ $url = self::getCurrentURL();
635
+ $track_url = self::getTrackURL();
636
+
637
+ $data = array(
638
+ 'last_url' => $url
639
+ , 'uid' => $uid
640
+ , 'app_id' => self::getAppId()
641
+ , 'track_url' => $track_url
642
+ , 'refresh_time' => $refresh_time
643
+ );
644
+
645
+ $payload = self::encode64(json_encode($data));
646
+ ?>
647
+ <iframe
648
+ style='width:1px; height: 1px'
649
+ frameborder="0"
650
+ scrolling="no"
651
+ src="<?php echo self::TRACK_URL . '?' . self::PAYLOAD . '=' . $payload; ?>">
652
+ </iframe>
653
+ <?php
654
+ }
655
+
656
+ /**
657
+ * If user is not logged in or connected call this method.
658
+ * The method will create a iframe
659
+ */
660
+ public static function addLoginButton($text = 'Login', $width='100', $height='23', $css='') {
661
+
662
+ $url = self::getCurrentURL();
663
+ $track_url = self::getTrackURL();
664
+
665
+ $data = array(
666
+ 'last_url' => $url
667
+ , 'text' => $text
668
+ , 'app_id' => self::getAppId()
669
+ , 'track_url' => $track_url
670
+ );
671
+
672
+ $payload = self::encode64(json_encode($data));
673
+
674
+ echo' <iframe ';
675
+ echo " style='width:{$width}px; height: {$height}px;$css;' ";
676
+ echo " frameborder='0' ";
677
+ echo " scrolling='no' ";
678
+ echo " src='" . self::LOGIN_URL . "?" . self::PAYLOAD . "={$payload}' >";
679
+ echo' </iframe>';
680
+ }
681
+
682
+ /**
683
+ * This method shows the facebook publish dialog in an iframe.
684
+ * Size of the iframe can be configured by parameters
685
+ *
686
+ * Example:
687
+ * <code>
688
+ * Bonusbox::showPublish( $res1->getTransactionID() );
689
+ * </code>
690
+ * @param $transactionId last transaction id
691
+ * @param $width specify width of the iframe
692
+ * @param $height specify height of the iframe
693
+ */
694
+ public static function showPublish($transactionId, $width = 600, $height=270) {
695
+ echo' <iframe ';
696
+ echo " style='width:{$width}px; height: {$height}px;$css;' ";
697
+ echo " frameborder='0' ";
698
+ echo " scrolling='no' ";
699
+ echo " src='" . self::PUBLISH_URL . $transactionId . "' >";
700
+ echo' </iframe>';
701
+ }
702
+
703
+ // urls! sync with latest version!
704
+
705
+ const ADD_URL = 'https://app.bonusbox.me/api/v2/transaction/add';
706
+ const GET_URL = 'https://app.bonusbox.me/api/v2/iframe/get';
707
+ const LOGIN_URL = 'https://app.bonusbox.me/api/v2/iframe/login';
708
+ const TRACK_URL = 'https://app.bonusbox.me/api/v2/iframe/track';
709
+ const PUBLISH_URL = 'https://app.bonusbox.me/api/v2/iframe/get_publish?transaction_id=';
710
+
711
+ // array keys, must match with backend!
712
+ const ARTICLE = 'article';
713
+ const AMOUNT = 'amount';
714
+ const ITEMS = 'items';
715
+ const TOKEN = 'uid';
716
+ const APP_ID = 'app_id';
717
+ const SECRET = 'secret';
718
+ const SHOP_TOKEN = 'shop_user_token';
719
+
720
+ const CART = 'cart';
721
+ const TOTAL = 'total_amount';
722
+ const PAYLOAD = 'payload';
723
+ const RESPONSE = 'response';
724
+
725
+ const M_TRACK_URL = 'm_track_url';
726
+
727
+ // used to track multple calling of addTransaction method
728
+ const TX_HISTORY = 'TX_HISTORY';
729
+
730
+
731
+
732
+ //////////////////////////////////////////////////////////////////////////
733
+ ////////////////////// Errors !! ////////////////////////////////////////
734
+ //////////////////////////////////////////////////////////////////////////
735
+ /**
736
+ * This variable holds the error
737
+ */
738
+ const NO_ERROR = 0;
739
+ const INVALID_TOTAL_AMOUNT = 1;
740
+ const INVALID_ITEM_FORMAT = 2;
741
+ const EMPTY_TOTAL_AMOUNT = 3;
742
+ const EMPTY_USER_TOKEN = 4;
743
+ const EMPTY_APP_ID = 5;
744
+ const EMPTY_CART = 6;
745
+ const EMPTY_ITEM_ARTICLE = 7;
746
+ const EMPTY_ITEM_AMOUNT = 8;
747
+ const INVALID_ITEM_AMOUNT = 9;
748
+ const EMPTY_APP_SECRET = 10;
749
+ const INVALID_API_RESPONSE = 11;
750
+ const EMPTY_API_URL = 12;
751
+ const EMPTY_PAYLOAD = 13;
752
+ const EMPTY_M_TRACK_URL = 14;
753
+ //const EMPTY_SHOP_USER_TOKEN = 15;
754
+
755
+ /**
756
+ * Messsages for error codes
757
+ */
758
+ private static $errorMessages = array(
759
+ self::NO_ERROR => 'No error! '
760
+ , self::EMPTY_USER_TOKEN => 'User token cannot be empty'
761
+ , self::INVALID_TOTAL_AMOUNT => 'Total amount is not numeric'
762
+ , self::INVALID_ITEM_FORMAT => 'Item array should have article and amount keys '
763
+ , self::EMPTY_TOTAL_AMOUNT => 'Total Amount empty'
764
+ , self::EMPTY_APP_ID => 'App ID cannot be empty'
765
+ , self::EMPTY_CART => 'Cart is empty!'
766
+ , self::EMPTY_ITEM_ARTICLE => 'Item article is empty'
767
+ , self::EMPTY_ITEM_AMOUNT => 'Item amount is empty'
768
+ , self::INVALID_ITEM_AMOUNT => 'Item amount is not numeric'
769
+ , self::EMPTY_APP_SECRET => 'App secret is empty'
770
+ , self::INVALID_API_RESPONSE => 'Received invalid data format from server!'
771
+ , self::EMPTY_API_URL => 'Failed to make server request: API url is empty'
772
+ , self::EMPTY_PAYLOAD => 'Failed to make server request: Empty payload passed'
773
+ , self::EMPTY_M_TRACK_URL => 'User status track url is empty: Please give one'
774
+ );
775
+ /**
776
+ * Holds the last error code
777
+ */
778
+ private static $lastErrorCode;
779
+
780
+ /**
781
+ * Get error message for an error code
782
+ *
783
+ * @param errorCode Error code of interest
784
+ */
785
+ public static function getErrorMessage($errorCode) {
786
+ return self::$errorMessages[$errorCode];
787
+ }
788
+
789
+ /**
790
+ * Returns the last error code
791
+ */
792
+ public static function getLastErrorCode() {
793
+ return self::$lastErrorCode;
794
+ }
795
+
796
+ private static function setError($errorCode) {
797
+ self::$lastErrorCode = $errorCode;
798
+ return false;
799
+ }
800
+
801
+ /**
802
+ * Returns the last error message
803
+ */
804
+ public static function getLastErrorMessage() {
805
+ return self::getErrorMessage(self::getLastErrorCode());
806
+ }
807
+
808
+ /**
809
+ * Unsets the last error code
810
+ */
811
+ public static function unsetLastError() {
812
+ self::$lastErrorCode = self::NO_ERROR;
813
+ }
814
+
815
+ /**
816
+ * Checks if there was an error
817
+ */
818
+ public static function hasError() {
819
+ return self::$lastErrorCode != self::NO_ERROR;
820
+ }
821
+
822
+ }
823
+
824
+ /**
825
+ * This class provides accessor methods to the reponse received
826
+ * by the bonusbox server
827
+ *
828
+ * This class is *NOT* meant be instantiated outside Bonusbox class.
829
+ */
830
+ class BonusBoxResponse {
831
+
832
+ /**
833
+ * Holds the json object created from the response data
834
+ */
835
+ private $response = NULL;
836
+
837
+ /**
838
+ * Constructor
839
+ * @param response Response data as received from server
840
+ */
841
+ public function BonusBoxResponse($response) {
842
+ $this->response = $response;
843
+ }
844
+
845
+ /**
846
+ * Returns credit amount
847
+ */
848
+ public function getCredit() {
849
+ return $this->get(self::CREDITS);
850
+ }
851
+
852
+ public function getFBUserName() {
853
+ $fbUser = $this->getUser();
854
+ $graphUrl = "https://graph.facebook.com/" . $fbUser->{'facebook_id'};
855
+ $fbUser = json_decode(file_get_contents($graphUrl));
856
+ return $fbUser->{'name'};
857
+ }
858
+
859
+ /**
860
+ * Returns user name if user is connected to facebook.
861
+ * If user is not connected to facebook, return facebook login tag
862
+ */
863
+ public function getUser() {
864
+
865
+ return $this->get(self::USER);
866
+ }
867
+
868
+ public function getUserToken() {
869
+ return $this->get(self::UID);
870
+ }
871
+
872
+ /**
873
+ * Discount amount on the cart total
874
+ */
875
+ public function getDiscount() {
876
+ return $this->get(self::DISCOUNT);
877
+ }
878
+
879
+ /**
880
+ * This is available if the user is new or never purchased before.
881
+ * The amount is calcualted based on the cart total and shop initial discount
882
+ */
883
+ public function getSavedAmount() {
884
+ return $this->get(self::SAVED_AMOUNT);
885
+ }
886
+
887
+ /**
888
+ * Returns true if user is connected to facebook else false
889
+ */
890
+ public function isConnected() {
891
+ return!(isset($this->response->{ self::NOT_CONNECTED }));
892
+ }
893
+
894
+ /**
895
+ * Returns debug info from server
896
+ */
897
+ public function getDebugInfo() {
898
+ return $this->get(self::DEBUG);
899
+ }
900
+
901
+ /**
902
+ * Returns true if user has never made any purchase before
903
+ */
904
+ public function isNewUser() {
905
+ return $this->response->{ self::INITIAL } == 'yes';
906
+ }
907
+
908
+ /**
909
+ * If the user has bought something before from this shop
910
+ * If not, user will receive initial discount
911
+ */
912
+ public function hasSpentBefore() {
913
+ return $this->response->{ self::HAS_SPENT_BEFORE } == 'yes';
914
+ }
915
+
916
+ /**
917
+ * Returns initial discount for first purchase! This amount
918
+ * will be applied to the cart total amount
919
+ */
920
+ public function getShopInitialDiscount() {
921
+ return $this->response->{ self::SHOP_INITIAL_DISCOUNT };
922
+ }
923
+
924
+ public function getCurrentBadgeName(){
925
+ return $this->response->{ self::CURRENT_BADGE };
926
+ }
927
+
928
+ public function getNextBadgeName(){
929
+ return $this->response->{ self::NEXT_BADGE };
930
+ }
931
+
932
+ public function getCreditsUntilNextBadge(){
933
+ return $this->response->{ self::CREDIT_NEXT_BADGE };
934
+ }
935
+
936
+ public function getDiscountNextBadge(){
937
+ return $this->response->{ self::DISCOUNT_NEXT_BADGE };
938
+ }
939
+
940
+ /**
941
+ * Discount for user's current badge
942
+ */
943
+ public function getBadgeDiscount() {
944
+ return $this->response->{ self::BADGE_DISCOUNT };
945
+ }
946
+
947
+ public function isTransactionSuccess() {
948
+ return $this->response->{ self::TX_RESULT } === true;
949
+ }
950
+
951
+ public function getTransactionError() {
952
+ return $this->response->{ self::TX_ERROR };
953
+ }
954
+
955
+ public function getTransactionID() {
956
+ return $this->response->{ self::TX_ID };
957
+ }
958
+
959
+ public function getTransactionSignature() {
960
+ return $this->response->{ self::TX_SIGNATURE };
961
+ }
962
+
963
+
964
+ const USER = 'user';
965
+ const INITIAL = 'initial';
966
+ const CREDITS = 'credits';
967
+ const DISCOUNT = 'discount';
968
+ const BADGE_DISCOUNT = 'badge_discount';
969
+ const SAVED_AMOUNT = 'saved_amount';
970
+ const NOT_CONNECTED = 'not_connected';
971
+ const CURRENT_BADGE = 'badge';
972
+ const NEXT_BADGE = 'next_badge';
973
+ const CREDIT_NEXT_BADGE = 'credit_next_badge';
974
+ const DISCOUNT_NEXT_BADGE = ' ';
975
+ const DEBUG = 'debug';
976
+ const UID = 'uid';
977
+
978
+ const HAS_SPENT_BEFORE = 'has_spent_before';
979
+
980
+
981
+ const TX_RESULT = 'success';
982
+ const TX_ID = 'transaction_id';
983
+ const TX_SIGNATURE = 'signature';
984
+ const TX_ERROR = 'error';
985
+
986
+ const SHOP_INITIAL_DISCOUNT = 'shop_discount';
987
+
988
+ public function get($attr) {
989
+ if (isset($this->response->$attr)) {
990
+ return $this->response->$attr;
991
+ }
992
+ return NULL;
993
+ }
994
+
995
+ }
app/code/community/Coeus/FacebookBonus/Model/Config.php ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Coeus_FacebookBonus_Model_Config {
4
+ const XML_PATH_ENABLED = 'facebookbonus/options/enabled';
5
+ const XML_PATH_API_KEY = 'facebookbonus/options/app_id';
6
+ const XML_PATH_SECRET = 'facebookbonus/options/app_key';
7
+
8
+ public function isEnabled() {
9
+ $storeId = Mage::app()->getStore()->getId();
10
+ if (Mage::getStoreConfigFlag(self::XML_PATH_ENABLED, $storeId) &&
11
+ $this->getApiId($storeId) &&
12
+ $this->getSecret($storeId)) {
13
+ return true;
14
+ }
15
+
16
+ return false;
17
+ }
18
+
19
+ public function getApiId() {
20
+ $storeId = Mage::app()->getStore()->getId();
21
+ return trim(Mage::getStoreConfig(self::XML_PATH_API_KEY, $storeId));
22
+ }
23
+
24
+ public function getSecret() {
25
+ $storeId = Mage::app()->getStore()->getId();
26
+ return trim(Mage::getStoreConfig(self::XML_PATH_SECRET, $storeId));
27
+ }
28
+
29
+ }
app/code/community/Coeus/FacebookBonus/Model/Total/Wrapping.php ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Coeus_FacebookBonus_Model_Total_Wrapping extends Mage_Sales_Model_Quote_Address_Total_Abstract {
4
+
5
+ public function __construct() {
6
+ $this->setCode('bonusbox');
7
+ $helper = Mage::helper('facebookbonus/data');
8
+ Coeus_FacebookBonus_Model_BonusboxConnect::init($helper->getApiId()
9
+ , $helper->getSecret()
10
+ , Mage::getUrl('facebookbonus/index/track') . ''
11
+ );
12
+ }
13
+
14
+ public function collect(Mage_Sales_Model_Quote_Address $address) {
15
+
16
+ parent::collect($address);
17
+ $helper = Mage::helper('facebookbonus');
18
+ if (!Mage::getModel('facebookbonus/config')->isEnabled()) {
19
+ return $this;
20
+ }
21
+
22
+ $store = $address->getQuote()->getStore();
23
+ $items = $this->_getAddressItems($address);
24
+ if (!count($items)) {
25
+ return $this;
26
+ }
27
+ Coeus_FacebookBonus_Model_BonusboxConnect::resetCart();
28
+ foreach ($items as $item) {
29
+ Coeus_FacebookBonus_Model_BonusboxConnect::addItem($item->getSku(), $item->getBaseCalculationPrice() * $item->getQty() * 100);
30
+ }
31
+
32
+ Coeus_FacebookBonus_Model_BonusboxConnect::setCartTotalAmount($address->getSubtotalWithDiscount() * 100);
33
+ $bb_discount = 0;
34
+ $res = Coeus_FacebookBonus_Model_BonusboxConnect::getDiscountInfo();
35
+
36
+ Mage::getSingleton('core/session')->setBonusBoxWithoutDiscount($address->getSubtotalWithDiscount());
37
+ if ($res && Coeus_FacebookBonus_Model_BonusboxConnect::isLoggedinIntoFacebook()) {
38
+ $bb_discount = $res->getDiscount() / 100;
39
+ $address->setBonusBoxDiscountAmount($bb_discount);
40
+ $address->setBaseBonusBoxDiscountAmount($bb_discount);
41
+ $address->setGrandTotal($address->getGrandTotal() - $address->getBonusBoxDiscountAmount());
42
+ $address->setBaseGrandTotal($address->getBaseGrandTotal() - $address->getBaseBonusBoxDiscountAmount());
43
+ }
44
+
45
+ return $this;
46
+ }
47
+
48
+ public function fetch(Mage_Sales_Model_Quote_Address $address) {
49
+ $amount = $address->getBonusBoxDiscountAmount();
50
+ if ($amount != 0) {
51
+ $title = Mage::helper('facebookbonus')->__('Discount by Bonusbox items');
52
+ $address->addTotal(array(
53
+ 'code' => $this->getCode(),
54
+ 'title' => $title,
55
+ 'value' => -$amount
56
+ ));
57
+ }
58
+ return $this;
59
+ }
60
+
61
+ public function getLabel() {
62
+ return Mage::helper('facebookbonus')->__('BonusBox');
63
+ }
64
+
65
+ }
app/code/community/Coeus/FacebookBonus/Model/cache.txt ADDED
@@ -0,0 +1 @@
 
1
+ {"__bonusbox__app_id":"052ca5d","__bonusbox__secret":"ed8bc725b39805fd14cb1bfcb2fa189f","__bonusbox__m_track_url":"http:\/\/bonusbox.coeus-solutions.de\/index.php\/facebookbonus\/index\/track\/","__bonusbox__cart":[{"article":"54654","amount":10000}],"__bonusbox__total_amount":10000,"__bonusbox__uid":"100002491091084","__bonusbox__shop_user_token":"b9979300f83f973ecce011b9cf721962","__bonusbox__TX_HISTORY":{"Mon, 15 Aug 11 18:36:09 +0000":"\/index.php\/checkout\/onepage\/success\/"}}
app/code/community/Coeus/FacebookBonus/controllers/IndexController.php ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Coeus_FacebookBonus_IndexController extends Mage_Core_Controller_Front_Action {
4
+
5
+ public function trackAction() {
6
+ Coeus_FacebookBonus_Model_BonusboxConnect::updateUserStatus();
7
+ }
8
+
9
+ }
app/code/community/Coeus/FacebookBonus/etc/config.xml ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <global>
4
+ <modules>
5
+ <Coeus_FacebookBonus>
6
+ <version>1.0.0</version>
7
+ </Coeus_FacebookBonus>
8
+ </modules>
9
+ <blocks>
10
+ <facebookbonus>
11
+ <class>Coeus_FacebookBonus_Block</class>
12
+ </facebookbonus>
13
+ </blocks>
14
+ <helpers>
15
+ <facebookbonus>
16
+ <class>Coeus_FacebookBonus_Helper</class>
17
+ </facebookbonus>
18
+ </helpers>
19
+ <models>
20
+ <facebookbonus>
21
+ <class>Coeus_FacebookBonus_Model</class>
22
+ </facebookbonus>
23
+ </models>
24
+ <sales>
25
+ <quote>
26
+ <totals>
27
+ <facebookbonus>
28
+ <class>Coeus_FacebookBonus_Model_Total_Wrapping</class>
29
+ <after>grand_total</after>
30
+
31
+ </facebookbonus>
32
+ </totals>
33
+ </quote>
34
+ </sales>
35
+ </global>
36
+
37
+ <frontend>
38
+ <routers>
39
+ <facebookbonus>
40
+ <use>standard</use>
41
+ <args>
42
+ <module>Coeus_FacebookBonus</module>
43
+ <frontName>facebookbonus</frontName>
44
+ </args>
45
+ </facebookbonus>
46
+ </routers>
47
+ <layout>
48
+ <updates>
49
+ <facebookbonus>
50
+ <file>facebookbonus.xml</file>
51
+ </facebookbonus>
52
+ </updates>
53
+ </layout>
54
+ </frontend>
55
+ <adminhtml>
56
+ <acl>
57
+ <resources>
58
+ <admin>
59
+ <children>
60
+ <system>
61
+ <children>
62
+ <config>
63
+ <children>
64
+ <facebookbonus>
65
+ <title>Facebook bonus settings</title>
66
+ </facebookbonus>
67
+ </children>
68
+ </config>
69
+ </children>
70
+ </system>
71
+ </children>
72
+ </admin>
73
+ </resources>
74
+ </acl>
75
+ </adminhtml>
76
+
77
+
78
+ </config>
app/code/community/Coeus/FacebookBonus/etc/system.xml ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <sections>
4
+ <facebookbonus translate="label" module="facebookbonus">
5
+ <label>Bonusbox</label>
6
+ <tab>sales</tab>
7
+ <frontend_type>text</frontend_type>
8
+ <sort_order>305</sort_order>
9
+ <show_in_default>1</show_in_default>
10
+ <show_in_website>1</show_in_website>
11
+ <show_in_store>0</show_in_store>
12
+ <groups>
13
+ <options translate="label">
14
+ <label>Bonusbox Settings</label>
15
+ <frontend_type>text</frontend_type>
16
+ <sort_order>1</sort_order>
17
+ <show_in_default>1</show_in_default>
18
+ <show_in_website>1</show_in_website>
19
+ <show_in_store>0</show_in_store>
20
+ <fields>
21
+ <enabled translate="label">
22
+ <label>Enabled</label>
23
+ <frontend_type>select</frontend_type>
24
+ <source_model>adminhtml/system_config_source_yesno</source_model>
25
+ <sort_order>1</sort_order>
26
+ <show_in_default>1</show_in_default>
27
+ <show_in_website>1</show_in_website>
28
+ <show_in_store>1</show_in_store>
29
+ <comment>Make it enable of disable</comment>
30
+ </enabled>
31
+ <app_id translate="label">
32
+ <label>Application ID</label>
33
+ <frontend_type>text</frontend_type>
34
+ <sort_order>5</sort_order>
35
+ <show_in_default>1</show_in_default>
36
+ <show_in_website>1</show_in_website>
37
+ <show_in_store>1</show_in_store>
38
+ <comment>Application ID</comment>
39
+ </app_id>
40
+ <app_key translate="label">
41
+ <label>Application Secret</label>
42
+ <frontend_type>text</frontend_type>
43
+ <sort_order>10</sort_order>
44
+ <show_in_default>1</show_in_default>
45
+ <show_in_website>1</show_in_website>
46
+ <show_in_store>1</show_in_store>
47
+ <comment>Application Secret</comment>
48
+ </app_key>
49
+
50
+ </fields>
51
+ </options>
52
+ </groups>
53
+ </facebookbonus>
54
+ </sections>
55
+ </config>
app/design/frontend/default/default/layout/facebookbonus.xml ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+
3
+ <layout version="0.1.0">
4
+ <default>
5
+ <reference name="head">
6
+ <action method="addCss"><stylesheet>css/bonusbox.css</stylesheet></action>
7
+ </reference>
8
+ </default>
9
+ <checkout_cart_index>
10
+ <reference name="content">
11
+ <block type="facebookbonus/facebook" name="facebookbonus.connect" before="-" as="facebookbonus.connect" template="facebookbonus/cart.phtml"/>
12
+ </reference>
13
+ </checkout_cart_index>
14
+ <checkout_onepage_index>
15
+ <reference name="right">
16
+ <block type="facebookbonus/facebook" as="bonuxboxcart" name="bonusbox.onepage" template="facebookbonus/onepage.phtml"/>
17
+ </reference>
18
+ </checkout_onepage_index>
19
+
20
+
21
+ <checkout_onepage_success>
22
+ <reference name="content">
23
+ <block type="facebookbonus/facebook" as="facebookbonusonepagesuccess" name="facebookbonus.onepage.success" template="facebookbonus/ordersuccess.phtml"/>
24
+ </reference>
25
+ </checkout_onepage_success>
26
+
27
+ <checkout_multishipping_success>
28
+ <reference name="content">
29
+ <block type="facebookbonus/facebook" as="facebookbonusmultishippingsuccess" name="facebookbonus.multishipping.success" template="facebookbonus/ordersuccess.phtml"/>
30
+ </reference>
31
+ </checkout_multishipping_success>
32
+
33
+ </layout>
app/design/frontend/default/default/template/facebookbonus/cart.phtml ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ $session = Mage::getSingleton('checkout/session');
3
+ if ($this->isEnabled() && count($session->getQuote()->getAllItems())) {
4
+ Coeus_FacebookBonus_Model_BonusboxConnect::init($this->getApiId()
5
+ , $this->getSecret()
6
+ , $this->getUrl('facebookbonus/index/track'));
7
+ Coeus_FacebookBonus_Model_BonusboxConnect::resetCart();
8
+
9
+
10
+ foreach ($session->getQuote()->getAllItems() as $item) {
11
+ Coeus_FacebookBonus_Model_BonusboxConnect::addItem($item->getSku(), ($item->getBaseCalculationPrice() * $item->getQty() * 100));
12
+ }
13
+ $amount = Mage::getSingleton('core/session')->getBonusBoxWithoutDiscount();
14
+ Coeus_FacebookBonus_Model_BonusboxConnect::setCartTotalAmount((float) $amount * 100);
15
+
16
+ $bb_discount = 0;
17
+ $res = Coeus_FacebookBonus_Model_BonusboxConnect::getDiscountInfo();
18
+ ?>
19
+
20
+
21
+ <?php
22
+ if ($res) {
23
+ if (Coeus_FacebookBonus_Model_BonusboxConnect::isLoggedinIntoFacebook()) {
24
+ if ($res->isNewUser()) {
25
+ $text = 'Congratualtions! You are now a Herrenschmiede Loyalty Member!';
26
+ } else {
27
+ $badge = $res->getCurrentBadgeName();
28
+ if (empty($badge)) {// this is gonna first purchse
29
+ $badge = 'Herrenschmiede Loyalty Member';
30
+ }
31
+ $text = "as " . $badge . " you receive " . $res->getCredit() . " credits and a direct discount of " . Mage::app()->getLocale()->currency(Mage::app()->getStore()->
32
+ getCurrentCurrencyCode())->getSymbol() . ($res->getDiscount() / 100) . " for this purchase.";
33
+ }
34
+ ?>
35
+ <div id="bonusbox">
36
+ <div class="detail">
37
+ <p>Dear <?php print $res->getFBUserName() ?><br />
38
+ <?php print $text ?>
39
+ </p>
40
+ <p><strong>Continue shopping and get your <?php print Mage::app()->getLocale()->currency(Mage::app()->getStore()->
41
+ getCurrentCurrencyCode())->getSymbol() ?><?php print ($res->getDiscount() / 100) ?> discount!</strong></p>
42
+
43
+ </div>
44
+ </div>
45
+
46
+ <?php
47
+ } else {
48
+ ?>
49
+ <div id="bonusbox" class="bonusbox-no-member">
50
+ <div class="detail">
51
+ <p>Become a member of our loyalty<br />
52
+ Programme and get a direct discount of <?php print Mage::app()->getLocale()->currency(Mage::app()->getStore()->
53
+ getCurrentCurrencyCode())->getSymbol() ?><?php print $res->getShopInitialDiscount() ?></p>
54
+ <p id="facebook"><?php Coeus_FacebookBonus_Model_BonusboxConnect::addLoginButton($loginButtonText, '100', '30') ?></p>
55
+ </div>
56
+ </div>
57
+
58
+ <?php
59
+ }
60
+ }
61
+
62
+ Coeus_FacebookBonus_Model_BonusboxConnect::trackUser(100000);
63
+ }
64
+ ?>
app/design/frontend/default/default/template/facebookbonus/onepage.phtml ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ $session = Mage::getSingleton('checkout/session');
3
+ if ($this->isEnabled() && count($session->getQuote()->getAllItems())) {
4
+ Coeus_FacebookBonus_Model_BonusboxConnect::init($this->getApiId()
5
+ , $this->getSecret()
6
+ , $this->getUrl('facebookbonus/index/track'));
7
+ Coeus_FacebookBonus_Model_BonusboxConnect::resetCart();
8
+ foreach ($session->getQuote()->getAllItems() as $item) {
9
+ Coeus_FacebookBonus_Model_BonusboxConnect::addItem($item->getSku(), ($item->getBaseCalculationPrice() * $item->getQty() * 100));
10
+ }
11
+ $amount = Mage::getSingleton('core/session')->getBonusBoxWithoutDiscount();
12
+ Coeus_FacebookBonus_Model_BonusboxConnect::setCartTotalAmount((float) $amount * 100);
13
+ $bb_discount = 0;
14
+ $res = Coeus_FacebookBonus_Model_BonusboxConnect::getDiscountInfo();
15
+ ?>
16
+
17
+
18
+ <?php
19
+ if ($res) {
20
+ if (Coeus_FacebookBonus_Model_BonusboxConnect::isLoggedinIntoFacebook()) {
21
+ if ($res->isNewUser()) {
22
+ $text = 'Congratualtions! You are now a Herrenschmiede Loyalty Member!';
23
+ } else {
24
+ $badge = $res->getCurrentBadgeName();
25
+ if (empty($badge)) {// this is gonna first purchse
26
+ $badge = 'Herrenschmiede Loyalty Member';
27
+ }
28
+ $text = "as " . $badge . " you receive " . $res->getCredit() / 100 . " credits and a direct discount of " . Mage::app()->getLocale()->currency(Mage::app()->getStore()->
29
+ getCurrentCurrencyCode())->getSymbol() . ($res->getDiscount() / 100) . " for this purchase.";
30
+ }
31
+ ?>
32
+ <div id="checkout-progress-wrapper">
33
+ <div class="block block-progress opc-block-progress">
34
+ <dl class="block-content">
35
+ <dt class="complete">
36
+ BonusBox Membership
37
+ </dt>
38
+ <dd class="complete">
39
+
40
+ <div class="detail">
41
+ <p>Dear <?php print $res->getFBUserName() ?><br />
42
+ <?php print $text ?>
43
+ </p>
44
+ <p><strong>Continue shopping and get your <?php print Mage::app()->getLocale()->currency(Mage::app()->getStore()->
45
+ getCurrentCurrencyCode())->getSymbol() ?><?php print ($res->getDiscount() / 100) ?> discount!</strong></p>
46
+
47
+ </div>
48
+
49
+ </dd>
50
+ </dl>
51
+ </div>
52
+ </div>
53
+ <?php
54
+ } else {
55
+ ?>
56
+ <div id="checkout-progress-wrapper">
57
+ <div class="block block-progress opc-block-progress">
58
+ <dl class="block-content">
59
+ <dt class="complete">
60
+ BonusBox Membership
61
+ </dt>
62
+ <dd class="complete">
63
+ <div class="detail">
64
+ <p>Become a member of our loyalty<br />
65
+ Programme and get a direct discount of <?php print Mage::app()->getLocale()->currency(Mage::app()->getStore()->
66
+ getCurrentCurrencyCode())->getSymbol() ?><?php print $res->getShopInitialDiscount() ?></p>
67
+ <p id="facebook"><?php Coeus_FacebookBonus_Model_BonusboxConnect::addLoginButton($loginButtonText, '100', '30') ?></p>
68
+ </div>
69
+ </dd>
70
+ </dl>
71
+ </div>
72
+ </div>
73
+ <?php
74
+ }
75
+ }
76
+
77
+ Coeus_FacebookBonus_Model_BonusboxConnect::trackUser(100000);
78
+ }
79
+ ?>
app/design/frontend/default/default/template/facebookbonus/ordersuccess.phtml ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if ($this->isEnabled()) {
3
+ Coeus_FacebookBonus_Model_BonusboxConnect::init();
4
+ $res = Coeus_FacebookBonus_Model_BonusboxConnect::addTransaction();
5
+ if (isset($res)) {
6
+ ?>
7
+ <?php
8
+ if ($res->isTransactionSuccess()) {
9
+ ?>
10
+ Thank you for your order <br />
11
+ You got <?php print $res->getCredit() ?> credits for this purchase. Check your Herrenschmide Loyalty Account of Facebook to see your credit balance.
12
+ <?php
13
+ Coeus_FacebookBonus_Model_BonusboxConnect::showPublish($res->getTransactionID());
14
+ } else {
15
+ echo '<br/>Transaction Failed!:', $res->getTransactionError();
16
+ }
17
+ }
18
+ }
19
+ ?>
app/etc/modules/Coeus_FacebookBonus.xml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Coeus_FacebookBonus>
5
+ <active>true</active>
6
+ <codePool>community</codePool>
7
+ </Coeus_FacebookBonus>
8
+ </modules>
9
+ </config>
package.xml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Zaheerbox</name>
4
+ <version>3.0.0</version>
5
+ <stability>stable</stability>
6
+ <license>OSL</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Summary</summary>
10
+ <description>Description</description>
11
+ <notes>Notes</notes>
12
+ <authors><author><name>Zaheer</name><user>auto-converted</user><email>zaheer.abbass@coeus-solutions.de</email></author></authors>
13
+ <date>2011-10-11</date>
14
+ <time>13:15:03</time>
15
+ <contents><target name="magecommunity"><dir name="Coeus"><dir name="FacebookBonus"><dir name="Block"><file name="Facebook.php" hash="c46039ef48b08806c6c2491b240d9726"/></dir><dir name="Helper"><file name="Data.php" hash="994b7391688adcfd32e4b564c789fbd0"/></dir><dir name="Model"><dir name="Total"><file name="Wrapping.php" hash="930e1f78bdaad09abbf3fba2a3e80474"/></dir><file name="BonusboxConnect copy.php" hash="54c46635bfbc843c73e756fbf73ab099"/><file name="BonusboxConnect.php" hash="3c3023d90c97b27c2ddca9673a5d3918"/><file name="Config.php" hash="a7e6241ea0973d1087bce049bf660cd1"/><file name="cache.txt" hash="088d2b29e1acf745bbd13807d767797b"/></dir><dir name="controllers"><file name="IndexController.php" hash="56348b3ebc7c2bbcd9e2c9665b7ae5cc"/></dir><dir name="etc"><file name="config.xml" hash="a519d1409b6c4f98b99542ed82487c11"/><file name="system.xml" hash="a05457132e85f503d8066a0164ef7a4e"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Coeus_FacebookBonus.xml" hash="2c753c24cf82376d3ad55659735c754a"/></dir></target><target name="magedesign"><dir name="frontend"><dir name="default"><dir name="default"><dir name="template"><dir name="facebookbonus"><file name="cart.phtml" hash="ef77e2cca99e29a58892e05d4112d144"/><file name="onepage.phtml" hash="1791a7aa180c01a7e114ee08707cb2ea"/><file name="ordersuccess.phtml" hash="41c7b7968756d30c802c47562a2a467d"/></dir></dir><dir name="layout"><file name="facebookbonus.xml" hash="a2585d506596c381e47f28ef9c171cef"/></dir></dir></dir></dir></target><target name="mageskin"><dir name="frontend"><dir name="default"><dir name="default"><dir name="css"><file name="bonusbox.css" hash="c658bfd5b52d1dad85c462288123bb3c"/></dir></dir></dir></dir></target></contents>
16
+ <compatible/>
17
+ <dependencies/>
18
+ </package>
skin/frontend/default/default/css/bonusbox.css ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #bonusbox {
2
+ border:1px solid #d0cbc1;
3
+ background: none repeat scroll 0 0 #DEE5E8;
4
+ padding:12px 15px;
5
+ margin:0 0 18px;
6
+ }
7
+
8
+ #bonusbox .detail {
9
+ text-align: left;
10
+ }
11
+ #bonusbox .detail p {
12
+ line-height:16px;
13
+ margin: 0;
14
+ }
15
+ #bonusbox .detail #facebook{
16
+ margin-top:5px;
17
+ }
18
+ #bonusbox.bonusbox-no-member{
19
+ width: 30%;
20
+ }
21
+ .sidebar #bonusbox {
22
+ width:auto;
23
+ }