---- dataentry integration ----
type : integration # do not change this line
supports-foxycart-version-from : 0.7.0 # Minimum required FoxyCart version for this to work
supports-foxycart-version-to : # Last FoxyCart version that supports this (leave empty if unknown)
systems : Python # the system(s) that the integration is for, separated with commas if more than one
name : Python HMAC Validation Helpers # the name of the integration
description : Helper functions to generate FoxyCart HMAC signed URLs
tags : hmac # tags, separated by commas. don't include the "system" here.
date_dt : 2011-06-21 # the date in YYYY-MM-DD format
version : # if you have a version for your code, enter it here
developer_url : # if you'd like a link back to your site, stick it here
----
====== FoxyCart HMAC Validation Helper Functions for Python ======
//**Please note:**//
The code on this page is submitted by members of the FoxyCart community, and may not verified by FoxyCart.com LLC in any way, shape, or form. Please double check the code before installing. If you need help with it please [[http://forum.foxycart.com/|post in our forum]], but if we cannot offer assistance (due to unfamiliarity with this particular system or language) we apologize in advance.
===== Description =====
This script generates FoxyCart hyperlink urls that are hmac signed using the sha256 algorithm with the goal of eliminating the possibility that someone could tamper with the link before going to checkout. For more info on FoxyCart's HMAC validation, see [[http://wiki.foxycart.com/static/redirect/price_validation|the price validation documentation]].
If you use this or extend it, please edit this page to share your improvements back to the community.
===== Requirements =====
* Knowledge of Python.
===== Code =====
"""
This script generates foxycart hyperlink urls that are hmac signed using the sha256 algorithm
with the goal of eliminating the possibility that someone could tamper with the link before
going to checkout. This is the official documentation from FoxyCart:
http://wiki.foxycart.com/v/0.7.1/advanced/hmac_validation
"""
import hashlib
import hmac
import urllib
import webbrowser
from product_data import PRODUCTS
FOXYCART_API_KEY = 'YOUR KEY HERE'
URL = 'https://your-store-here.foxycart.com/cart?name={0}||{1}&price={2}||{3}&code={4}||{5}&quantity_max={6}||{7}'
def main():
generate_urls()
generate_test_html()
generate_text_file()
def generate_urls():
for product in PRODUCTS:
product['url'] = generate_url(product)
def generate_url(product):
hashed_name = hash_name(product)
hashed_price = hash_price(product)
hashed_code = hash_code(product)
hashed_max_quantity = hash_max_quantity(product)
return concatenate(product, hashed_name, hashed_price, hashed_code, hashed_max_quantity)
def hash_name(product):
return sha256digest(product['ProductId'], 'name', product['Name'])
def hash_price(product):
return sha256digest(product['ProductId'], 'price', product['Price'])
def hash_code(product):
return sha256digest(product['ProductId'], 'code', product['ProductId'])
def hash_max_quantity(product):
return sha256digest(product['ProductId'], 'quantity_max', '1')
def sha256digest(code, name, value):
return hmac.new(FOXYCART_API_KEY, msg=code+name+value, digestmod=hashlib.sha256).hexdigest()
def concatenate(product, hashed_name, hashed_price, hashed_code, hashed_max_quantity):
name = urllib.quote(product['Name'])
price = product['Price']
code = product['ProductId']
return URL.format(name, hashed_name, price, hashed_price, code, hashed_code, '1', hashed_max_quantity)
def generate_test_html():
HTML_FILE = 'urls.html'
with open(HTML_FILE, 'w') as html:
html.write('\n')
html.write('\t\n')
html.write(file('foxycart_scripts.html').read())
html.write('\t\n')
html.write('\t\n')
for product in PRODUCTS:
html.write('\t\t{1}
\n'.format(product['url'], product['ProductId']))
html.write('\t\n')
html.write('\n')
webbrowser.open(HTML_FILE)
def generate_text_file():
with open('urls.txt', 'w') as urls:
for product in PRODUCTS:
urls.write('{0}\t{1}\t{2}\n'.format(product['ProductId'], product['Name'], product['url']))
if __name__ == '__main__':
main()