This is an old revision of the document!
Table of Contents
- type:
- integration
- supports-foxycart-version-from:
- 0.7.0
- system:
- MODx Evolution
- name:
- HMAC Price Validation
- description:
- A plugin to automatically verify all links and forms output using MODX.
- tag:
- hmac
- date:
- 2011-06-28
Automatic HMAC Price Validation for MODX Evolution + FoxyCart
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 post in our forum, but if we cannot offer assistance (due to unfamiliarity with this particular system or language) we apologize in advance.
Description
Automatically validates all links and forms on pages generated by MODX Evolution, according to FoxyCart's HMAC Price Validation functionality.
This plugin is easy to install, and should “just work”, so it is recommended for all MODX Evolution installs.
Installation
- Create a directory in your
assets/plugins/directory calledfoxycart_validation. - Download the source file here and put it into the newly created
assets/plugins/foxycart_validation/directory. - Edit that file and enter the appropriate
$secretand$cart_urlvalues. (The$secretvalue is your store's API / datafeed key.) - Create a plugin named “FoxyCart Validation”.
- In the “System Events” tab, check the
OnWebPagePrerendercheckbox. - Paste in the code below and save.
- Go to your FoxyCart admin and enable the price validation under your “advanced” section. Save.
- Test.
- Test some more.
Requirements
- Form “code” values should not have leading or trailing whitespace.
- Cannot use double-pipes in an input's name.
- Empty textareas are assumed to be “open”.
Code
/** * FoxyCart Validation Plugin for MODx 1.x (Evolution) * * @author FoxyCart.com * @copyright FoxyCart.com LLC, 2010 * @version 0.7.0.20100730 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License * @example http://wiki.foxycart.com/docs/cart/validation * * Requirements: * - Form "code" values should not have leading or trailing whitespace. * - Cannot use double-pipes in an input's name * - Empty textareas are assumed to be "open" */ $e = &$modx->Event; switch ($e->name) { case "OnWebPagePrerender": $validate = $modx->config["base_path"].'assets/plugins/foxycart_validation/foxycart.cart_validation.php'; if (file_exists($validate)) { include_once($validate); $doc = $modx->documentOutput; $modx->documentOutput = FoxyCart_Helper::fc_hash_html($doc); } break; default: // stop here return; break; }
Manual HMAC Price Validation for MODX Evolution + FoxyCart
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 post in our forum, but if we cannot offer assistance (due to unfamiliarity with this particular system or language) we apologize in advance.
Description
Creates a snippet to manually validates links and forms in your pages, according to FoxyCart's HMAC Price Validation functionality.
Installation
Create a MODX snippet
Snippet Name: FoxyCartGetHMAC
Snippet Code (paste into text box):
<?php if(!function_exists(get_verification)) { function get_verification($var_name, $var_value, $var_code) { $api_key = 'KEY HERE'; $encodingval = htmlspecialchars($var_code) . htmlspecialchars($var_name) . htmlspecialchars($var_value); return '||'.hash_hmac('sha256', $encodingval, $api_key).($var_value === "--OPEN--" ? "||open" : ""); //Use the following to test whether api_key has a value or not (only use in safe, sandbox scenarios!) //return '$api_key||'.hash_hmac('sha256', $encodingval, $api_key).($var_value === "--OPEN--" ? "||open" : ""); } } ( isset($varName) ) ? $varName : $varName = ''; ( isset($varValue) ) ? $varValue : $varValue = ''; ( isset($varCode) ) ? $varCode : $varCode = ''; if (empty($varName) || empty($varValue) || empty($varCode)) { return; } return get_verification($varName, $varValue, $varCode); ?>
Snippet Usage
[[FoxyCartGetHMAC &varName=`input-name-attrib-tag-here` &varValue=`input-value-attrib-tag-here` &varCode=`sku-code-here` ]]
Example:
<input type="hidden" name="code[[FoxyCartGetHMAC &varName=`code` &varValue=`[*sku*]` &varCode=`[*sku*]`]]" value="[*sku*]"/>
Notice the parts where the variable name code is the same: name=“code and &varName=`code`
and where the value is the same: value=”[*sku*]“ and &varValue=`[*sku*]`
and where &varCode=`[*sku*]` should be something unique to your product, like the SKU code
Example Form
Assuming you're using template variables price as [*price*] and sku as [*sku*] and using the pagetitle and SKU for the product name as [*pagetitle*] - [*sku*], and that there's only a single SKU for a single product represented by a resource in the MODX Tree:
<form name="buypanel" class="buypanel" id="buypanel" action="https://SHOPNAME.foxycart.com/cart" method="post" accept-charset="utf-8"> <input type="hidden" name="code[[FoxyCartGetHMAC &varName=`code` &varValue=`[*sku*]` &varCode=`[*sku*]`]]" value="[*sku*]"/> <input type="hidden" name="name" value="[*pagetitle*] - [*sku*][[FoxyCartGetHMAC &varName=`name` &varValue=`[*pagetitle*] - [*sku*]` &varCode=`[*sku*]`]]"/> <input type="hidden" name="price[[FoxyCartGetHMAC &varName=`price` &varValue=`[*price*]` &varCode=`[*sku*]`]]" value="[*price*]"/> <input type="input" name="quantity[[FoxyCartGetHMAC &varName=`quantity` &varValue=`--OPEN--` &varCode=`[*sku*]`]]" value="1"/> <input type="submit" name="submit[[FoxyCartGetHMAC &varName=`submit` &varValue=`--OPEN--` &varCode=`[*sku*]`]]" value="BUY NOW"/> </form>