Skip to content

Instantly share code, notes, and snippets.

@jaromirmuller
Last active December 19, 2015 14:29
Show Gist options
  • Save jaromirmuller/5969858 to your computer and use it in GitHub Desktop.
Save jaromirmuller/5969858 to your computer and use it in GitHub Desktop.
JSONP Client sample implementation for Session Initialization flow.
<?php
header('Content-type: application/javascript');
$cookie_name = 'ss_session';
$result = array();
if ( isset($_GET['callback']) && isset($_GET['session_id']) ) {
$jsonp = $_GET['callback'];
$data[] = "your session_id is '" . $_GET['session_id']."'";
$result[] = $jsonp . "(" . json_encode($data) . ");";
} else {
$result[] = "alert('huh');";
}
echo implode("\n", $result);
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE_AFL.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package js
* @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
// old school cookie functions grabbed off the web
if (!window.Mage) var Mage = {};
Mage.Cookies = {};
Mage.Cookies.expires = null;
Mage.Cookies.path = '/';
Mage.Cookies.domain = null;
Mage.Cookies.secure = false;
Mage.Cookies.set = function(name, value){
var argv = arguments;
var argc = arguments.length;
var expires = (argc > 2) ? argv[2] : Mage.Cookies.expires;
var path = (argc > 3) ? argv[3] : Mage.Cookies.path;
var domain = (argc > 4) ? argv[4] : Mage.Cookies.domain;
var secure = (argc > 5) ? argv[5] : Mage.Cookies.secure;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
};
Mage.Cookies.get = function(name){
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
var j = 0;
while(i < clen){
j = i + alen;
if (document.cookie.substring(i, j) == arg)
return Mage.Cookies.getCookieVal(j);
i = document.cookie.indexOf(" ", i) + 1;
if(i == 0)
break;
}
return null;
};
Mage.Cookies.clear = function(name) {
if(Mage.Cookies.get(name)){
document.cookie = name + "=" +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
};
Mage.Cookies.getCookieVal = function(offset){
var endstr = document.cookie.indexOf(";", offset);
if(endstr == -1){
endstr = document.cookie.length;
}
return unescape(document.cookie.substring(offset, endstr));
};
<html>
<head>
<title>A - website</title>
<script type="text/javascript" src="J50Npi.js"></script>
<script type="text/javascript" src="cookies.js"></script>
</head>
<body>
<script type="text/javascript">
var session_plugin = function () {
var _id = false;
var sessionService = {
cookie_name : 'ss_session',
jsonp_url : 'http://sessionservice.somewebsite.co.uk/?callback=J50Npi.success'
}
var _session_receive_jsonp = function ( sessionService, callback ) {
console.log( '_session_receive_jsonp' );
var url = sessionService.jsonp_url;
// No specific data need to be sent there
var data = {};
// We need a function callback to be executed after the response is received
var _callback = function(session){
_session_set_cookie(sessionService, session.session, callback);
};
// And here is the magic:
J50Npi.getJSON(url, data, _callback);
}
var _session_check_cookie = function ( sessionService, callback ) {
console.log( '_session_check_cookie' );
var my_session = Mage.Cookies.get(sessionService.cookie_name);
console.log( sessionService.cookie_name, my_session );
if ( my_session ) {
_id = my_session;
callback(my_session); // ok, we're done
} else {
_session_receive_jsonp(sessionService, callback);
}
}
var _session_set_cookie = function ( sessionService, session_id, callback ) {
console.log( '_session_set_cookie' );
_id = session_id;
Mage.Cookies.set(sessionService.cookie_name, session_id);
}
var _getId = function ( callback ) {
console.log( '_getId' );
_session_check_cookie(sessionService, callback);
}
return {
id : _getId
}
}
var print_session = function ( message ) {
console.log( "Your session ID is " + message );
}
var ss = session_plugin();
ss.id( print_session );
</script>
</body>
</html>
var J50Npi = {
currentScript: null,
getJSON: function(url, data, callback) {
var src = url + (url.indexOf("?")+1 ? "&" : "?");
var head = document.getElementsByTagName("head")[0];
var newScript = document.createElement("script");
var params = [];
var param_name = ""
this.success = callback;
data["callback"] = "J50Npi.success";
for(param_name in data){
params.push(param_name + "=" + encodeURIComponent(data[param_name]));
}
src += params.join("&")
newScript.type = "text/javascript";
newScript.src = src;
if(this.currentScript) head.removeChild(currentScript);
head.appendChild(newScript);
},
success: null
};
<html>
<head>
<title>A - website</title>
<script type="text/javascript" src="J50Npi.js"></script>
<script type="text/javascript" src="cookies.js"></script>
</head>
<body>
<script type="text/javascript">
var url = "http://sessionservice.localhost-bcc.nl/add.php?callback=J50Npi.success";
var data = {'session_id': 'this_session_id'};
var callback = function(data){ console.log(data)};
J50Npi.getJSON(url, data, callback);
</script>
</body>
</html>
// response for http://sessionservice.somewebsite.co.uk/?callback=J50Npi.success service
J50Npi.success({"session":"c7479aa13927ba16741fa00846be9c62"});
<form action="https://secure.bcc.nl/schapi/basket/add" method="POST">
<inptu type="hidden" name="session_id" value="rtnsiull4qhpsi9ji577ugpv81" />
<input type="hidden" name="source_system_id" value="fdc30101-2580-4686-bda3-da33a5494ae9" />
<input type="hidden" name="source_hash" value="somehash" />
<input type="hidden" name="show_pre_basket" value="true" />
<input type="hidden" name="item_sku" value="SKU-123" />
<input type="hidden" name="qty" value="1" />
<input type="hidden" name="sales_price" value="99.99" />
<input type="hidden" name="delivery_type" value="hd" />
<input type="hidden" name="additional_params[url]" value="http://dgsite.com/product.html" />
<input type="hidden" name="additional_params[image]" value="http://dgsite.com/image.jpg" />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment