Last active
December 19, 2015 14:29
-
-
Save jaromirmuller/5969858 to your computer and use it in GitHub Desktop.
JSONP Client sample implementation for Session Initialization flow.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// response for http://sessionservice.somewebsite.co.uk/?callback=J50Npi.success service | |
J50Npi.success({"session":"c7479aa13927ba16741fa00846be9c62"}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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