Can't seem to fix "Cart Validation Error"

Hi,

I am using the FoxyShop 4.7.4 plugin for Wordpress, and am building my ByNow button links as follows. I have a Foxycart php class that looks like this:

<?php
class FoxyCart
{

private $apiKey;
private $siteUrl;

public function __construct($apiKey,$siteUrl)
{
$this->apiKey = $apiKey;
$this->siteUrl = $siteUrl;
}

/**
* @param $buyButtonParams array price,code,Weight,Category,image,quantity
*
* @return string
*/
public function generateCartURL($buyButtonParams)
{

// $atc = ’https://cloudaloud-dev.foxycart.com/cart';
$buyButtonUrl = $this->siteUrl;

//$buyButtonParams[‘code’]
$first = true;
foreach($buyButtonParams as $key => $value){
$prefix = $first === true ? '?' : '&';
$buyButtonUrl .= $prefix . $this->getVerification($key, $value, $buyButtonParams['code']. $value) . "=" . urlencode($value);
$first = false;
}

return $buyButtonUrl;
}

function getVerification($var_name, $var_value, $var_code, $var_parent_code = "", $for_value = false) {
$encodedVal = htmlspecialchars($var_code . $var_parent_code . $var_name . $var_value);
$label = ($for_value) ? $var_value : $var_name;
return $label . '||' . hash_hmac('sha256', $encodedVal, $this->apiKey) . ($var_value === "--OPEN--" ? "||open" : "");
}

}
I build my button link like this:

$buy = new FoxyCart($myApiKey,'https://******.foxycart.com/cart'); // **** redacted
$buyLink = $buy->generateCartURL(array(
'name'=>$ebook_product_name,
'code'=>$ebook_product_code,
'category'=>'ebook_echapter_pdf_downloadables',
'price'=>'100'
));
The link that gets built looks like this:

https://******.foxycart.com/cart
?name||d3f1f7cd1cb1e6ca2b08ca053581d20c72fa92a8f2fe26e301934b0a05348aa5=Single+Atom+Nanoelectronics
&code||0bc0286b60c0b39a3074fd82814ca9217d5c0f5c3d9d112fc7ab9cbb9be5f0c6=9789814316699
&category||717bc6843d7a69ac35ecd6708fbfd4d307d97afb9bdd3720bef3e171df65be9d=ebook_echapter_pdf_downloadables
&price||f7c54501d4a053d4744194af7910e6cc40c6800d5747c46efd187295e503427d=100
&fcsid=61jp7pftv2ivt8jdrslk4d3vp7
&callback=jQuery1124041815627772128705_1567407828906
&output=json
&fcsid=61jp7pftv2ivt8jdrslk4d3vp7
&_=1567407828908
What I see on the panel created on the RHS of the page is:
a window showing the following messages:

Cart Validation Error: name
Cart Validation Error: code
Cart Validation Error: category
Cart Validation Error: price
The product is a downloadable and I have created a category for it called ebook_echapter_pdf_downloadables under downloadables and uploaded the file to it in my Foxy account. This product shows in the list of available products in the ebook_echapter_pdf_downloadables as having been uploaded on 2019-09-01 23:37:40 and having zero sales so far.

The API Key I am using is the store secret I see on my Advanced Settings page.

Can anyone help me understand what I'm doing wrong?

Many thanks,
Nick
Comments
  • fc_adamfc_adam FoxyCart Team
    @nickweavers,

    Thanks for including such a detailed summary of what you're doing. I believe the issue you are seeing is caused by how you're passing through the code attribute - it's also including the value with it: $buyButtonParams['code']. $value.

    I think if you update your encoding line to look like this, it should work for you:
    $buyButtonUrl .= $prefix . $this->getVerification($key, $value, $buyButtonParams['code']) . "=" . urlencode($value);
    Just as a quick aside - FoxyShop would take care of encoding any of the product buy buttons for you automatically, removing the need to do it manually. You can enable product signing from the FoxyShop settings page.
  • Perfect. Working now.

    Thank you!
Sign In or Register to comment.