user1993724
user1993724

Reputation: 1

Ecwid SSO integration with django

I'm trying integrate my website with Ecwid so that my users can have a seamless shopping experience. Ecwid gives an example of how to encode the payload using PHP and then sending off the data via JavaScript. I need a Python/Django implementation. The Ecwid example can be found here: http://api.ecwid.com/#sso-payload

Ecwid example:

<?php
$sso_secret = "TEST";
$message = base64_encode("{appId:'123',userId:'234',profile:{email:'[email protected]'}}");
$timestamp = time();
$hmac = hash_hmac('sha1', "$message $timestamp", $sso_secret);
echo "<script> var ecwid_sso_profile = '$message $hmac $timestamp' </script>";
?>

My Python/django translation of the Ecwid script example:

import time, hmac
from hashlib import sha1
def ecwid_sso(request):
    sso_password = "XXXXXXXXXX"
    message = base64.b64encode("{appId:'bc',userId:'123',profile:{email:'[email protected]'}}")
    time_stamp = time.time()
    payload = hmac.new(sso_password, "%s %s" %(message,time_stamp), sha1).hexdigest()
    template_data = {'message':message,'payload':payload, 'timestamp':time_stamp}
    return render_to_response("site/ecwid.html", template_data, context_instance=RequestContext(request))

HTML/JavaScript output:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Ecwid test</title>
</head>
<body>
<script src="http://app.ecwid.com/script.js?1003"></script>
<script>
    var ecwid_sso_profile = '{{ message }} {{ payload  }} {{ timestamp }}' ;
    window.Ecwid.setSsoProfile(ecwid_sso_profile);
</script>
</body>
</html>

The error I'm getting from Ecwid is "Unable to reach the store. Please check your internet connection." Which is obviously not true because I can send this post. I think I'm close but, my current assumption is that I didn't package my payload correctly? Thoughts?

Upvotes: 0

Views: 512

Answers (1)

Christopher Lee
Christopher Lee

Reputation: 36

The error above was based on the timestamp being returned as a float. Ecwid requires the timestamp to be in integer format. I also read the instructions more clearly and now understand how the whole process works. I refactored and the code works as follows:

View code that will be showing the shop:

from django.shortcuts import render_to_response
from django.template.context import RequestContext
import time, hmac, base64
from hashlib import sha1
def any_view_showing_ecwid_shopping_pages(request):    
    sso_password = "XXXXXXXX"
    message = base64.b64encode("{appId:'bc',userId:'234',profile:{email:'[email protected]'}}")
    time_stamp = int(time.time())
    payload = hmac.new(sso_password, "%s %s" %(message,time_stamp), sha1).hexdigest()
    return render_to_response("site/ecwid.html", {'message':message,'payload':payload, 'timestamp':time_stamp},
                              context_instance=RequestContext(request))

JavaScript:

<script>
    var ecwid_sso_profile = '{{ message }} {{ payload  }} {{ timestamp }}' ;    
</script>

Upvotes: 1

Related Questions