This is an old revision of the document!
Table of Contents
- type:
- integration
- supports-foxycart-version-from:
- 0.7.0
- system:
- Python
- name:
- Python HMAC Validation Helpers
- description:
- Helper functions to generate FoxyCart HMAC signed URLs
- date:
- 2011-06-21
FoxyCart HMAC Validation Helper Functions for Python
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 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('<html>\n') html.write('\t<head>\n') html.write(file('foxycart_scripts.html').read()) html.write('\t</head>\n') html.write('\t<body>\n') for product in PRODUCTS: html.write('\t\t<a href="{0}">{1}</a><br/><br/>\n'.format(product['url'], product['ProductId'])) html.write('\t</body>\n') html.write('</html>\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()