Adding variably priced donation on checkout

jingarijingari Member
We're in the process of moving to 2.0 and we need a way to allow our customers to add a variably priced donation on checkout - a way for them to "round up" their purchase by adding a donation of a calculated amount or any other amount they chose. In our 1.1 version of the store, we are able to achieve this by having an entirely JSON cart and forcing the customer through the cart page before they reach checkout. As we move to 2.0, I would like to make use of the sidecart and add this "round up" donation functionality to the checkout page. I am currently working with this snippet - https://wiki.foxycart.com/v/2.0/snippets/gift_wrapping_checkbox_on_checkout - as a starting point, however we need our price value to be variable, so I am struggling to get the hmac verification working. Is there a way I can have an --OPEN-- price and still have it get verified so that the link works?

I'll whisper our development url. If you add any product and proceed to the checkout, you'll see this "Increase your impact" in section 4, but will notice there's an error on the price mismatch because of the verification failure.

Any thoughts? Will it be possible to have this functionality on the Checkout page? Another option might be to have it on the sidecart where I can use php to generate the hashes?

Thanks for your help,
Jessica
Comments
  • fc_jedfc_jed FoxyCart Team
    @jingari

    You would need to edit FC.client.request('https://'+FC.settings.storedomain+'/cart?name=Gift+Wrapping&price=10&code=giftwrapping&quantity_max=1').done(function(dataJSON) to your desired link, and append ||open at the end of the name of price (after the hash). You can find more information here: https://wiki.foxycart.com/v/2.0/hmac_validation

    If you're open to the sidecart option, I personally think that it's the easier route to take, since it would be readily passed to checkout along with your other products.
  • I tried adding open to the link after the hash, so I start with getting this link from the sample code -> encode HTML:

    https://storedomain.foxycart.com/cart?name=AdditionalDonation||79b690a06389d0eeacc0d8c6f4d8cf0cce2836f293d64d032ebe0911b2787af0&price=price||0d574b67e33857901348fb21b133d3ab6bdc5d8e6e613d94e1e48623ff202f36||open&code=donation||e7b344a629084fa7b86c3bbeaf1ea19c2ab63b2fb638848b4057f9ebf96bc963&quantity=1||98a8673b40e1c755e8e379391b488c2e5c424dd37ef58a85eca9d75ae23764d8

    Then I modify to this, changing the price=price to price=25
    https://storedomain.foxycart.com/cart?name=AdditionalDonation||79b690a06389d0eeacc0d8c6f4d8cf0cce2836f293d64d032ebe0911b2787af0&price=25||0d574b67e33857901348fb21b133d3ab6bdc5d8e6e613d94e1e48623ff202f36||open&code=donation||e7b344a629084fa7b86c3bbeaf1ea19c2ab63b2fb638848b4057f9ebf96bc963&quantity=1||98a8673b40e1c755e8e379391b488c2e5c424dd37ef58a85eca9d75ae23764d8

    But I still get Cart Validation Error: price, because the hash doesn't match up with the price amount even though it should be open.

    I'll work at doing it in the sidecart instead, thanks for the input.



  • fc_jedfc_jed FoxyCart Team
    @jingari

    Take note that you shouldn't change the values after it has been encoded. You need to set the value to "25" before you encode otherwise you would just end up with a mismatched hash.
  • Is there a way to encode dynamically on the checkout page? Maybe using jquery?
  • fc_jedfc_jed FoxyCart Team
    @jingari

    Here's a link to the PHP automatic validation https://github.com/FoxyCart/FoxyCart-Cart-Validation--PHP/blob/master/foxycart.cart_validation.php. However, you could indeed try it out with javascript/jQuery, although there would be security issues since you would need to input your private key and javascript could be seen by everyone.
  • Right, but I can't use PHP on the checkout page. I'll have to go back to the sidecart implementation.

    Thanks again.
  • brettbrett FoxyCart Team
    Hi @jingari
    I might be misunderstanding, but I think what you're attempting could definitely work. What's the URL you're signing? In the admin under "sample code" you can copy/paste HTML there and sign it. So take this:

    https://storedomain.foxycart.com/cart?name=AdditionalDonation&price=--OPEN--&code=donation&quantity_max=1
    And sign that. Notice the &price=--OPEN--. I'm not sure if your previous code you pasted above was maybe obfuscated intentionally, but the --OPEN-- should be the value of the &price=. Maybe that was the problem?

    The idea should totally work, though.
  • Yes, that's what I was attempting, but then how do I pass the "price" that is set by the user?

    I used something like what you put above and generated the encoded version. But I thought I would then have to then modify the encoded URL using jquery once the user inputs the amount that she wants to donate. Otherwise, how will clicking the "add donation" link add the correctly "priced" donation to the cart?
  • brettbrett FoxyCart Team
    Hi @jingari. I'm really sorry for the trouble here. I think there may be an issue with "open" values in links. I'll look into it, but if you're needing a solution asap, you could perhaps try doing 99 different price values (assuming you have products from .01 to .99¢) and swapping that out as needed.

    I'll see if we can figure out what's going on, though.

  • That's the thing, a donor could enter any amount they want - $1 - $10,000. If this is something that can work with an open in a link, than I would love to have it be on the checkout page. In the mean time, I'll continue implementing on the sidecart where I should be able to access the php function to generate the hash.

    Thanks for your help, Brett.
  • brettbrett FoxyCart Team
    @jingari, I should have clarified that my "99 different price values" was a pretty bad idea to begin with, but thanks for clarifying it wouldn't even work with what you need.

    I think I have a solution for you, though. Try this:
    * Go to the "sample code" section of the admin.
    * On the "Step 2" textarea, change the code delete the "10" from the name="price" input. So it'll look like this:

    <input type="hidden" name="price" value="" />
    <input type="hidden" name="code" value="donation" />
    * Click the encode HTML button.

    That'll get you something like this:

    <input type="hidden" name="price||b43f5a451foobard96e732d4af6762f2498389d||open" value="" />
    Here's the magic: Copy that whole price||foo||open string, and use that in your JSONP request. So your JSONP URL would look something like:

    &price||foo||open=10
    or

    &price||foo||open=8473
    Does that make sense?

    (For reference: The two issues that made getting to this point more difficult were 1) I forgot that our automatic signing script assumes a value="" is an open input, and 2) our automatic signing script doesn't really handle open values in links, as they're really more common in forms.)
  • I had tried that as well, that's what is currently up on my site, but it still gives the "Cart Validation Error: price", no matter if I generate the hash using the link with --OPEN-- or using a form with input value="", both hashes give the validation error when I try to use them as a link with an added value for price.

    This is my current url, with [storedomain] substitute w/ my actual domain.

    https://[storedomain].foxycart.com/cart?name=Additional+Donation||5bc7437cb17de225cdb0c1f2f637984644bcb7efc9e30cea694751321a1e5f5e&price=price||b43f5a45188e6d43648082d5c023dc17b2d2d921d96e732d4af6762f2498389d||open=50&code=donation||e7b344a629084fa7b86c3bbeaf1ea19c2ab63b2fb638848b4057f9ebf96bc963&quantity=1||98a8673b40e1c755e8e379391b488c2e5c424dd37ef58a85eca9d75ae23764d8
  • brettbrett FoxyCart Team
    Alrighty. Posting publicly just in case this comes up again. A little tricky.

    Start with something like this, both a link and a form to see the difference in how they get signed.

    <form action="https://[REDACTED].foxycart.com/cart"; method="post" accept-charset="utf-8">
    <input type="hidden" name="name" value="Additional Donation" />
    <input type="hidden" name="price" value="" />
    <input type="hidden" name="code" value="donation" />
    <input type="submit" value="Add a Cool Example" class="submit" />
    </form>

    <a href="https://[REDACTED].foxycart.com/cart?name=Additional+Donation&amp;price=50&amp;code=donation&amp;quantity=1">foo</a>;
    That'll get you something like this:

    <form action="https://[REDACTED].foxycart.com/cart"; method="post" accept-charset="utf-8">
    <input type="hidden" name="name||5bc7437cb17de225cdb0c1f2f637984644bcb7efc9e30cea694751321a1e5f5e" value="Additional Donation" />
    <input type="hidden" name="price||0d574b67e33857901348fb21b133d3ab6bdc5d8e6e613d94e1e48623ff202f36||open" value="" />
    <input type="hidden" name="code||e7b344a629084fa7b86c3bbeaf1ea19c2ab63b2fb638848b4057f9ebf96bc963" value="donation" />
    <input type="submit" value="Add a Cool Example" class="submit" />
    </form>

    <a href="https://[REDACTED].foxycart.com/cart?name=Additional+Donation||5bc7437cb17de225cdb0c1f2f637984644bcb7efc9e30cea694751321a1e5f5e&price=50||aa331c1df2028c416ddae15c9e6d6f545f047d3970624c3631dc60f94a16d094&code=donation||e7b344a629084fa7b86c3bbeaf1ea19c2ab63b2fb638848b4057f9ebf96bc963&quantity=1||98a8673b40e1c755e8e379391b488c2e5c424dd37ef58a85eca9d75ae23764d8">foo</a>
    Now take that &amp;price||0d57...f36||open bit from the form and stick it in the link, so you get an href like this:

    https://[REDACTED].foxycart.com/cart?name=Additional+Donation||5bc7437cb17de225cdb0c1f2f637984644bcb7efc9e30cea694751321a1e5f5e&price||0d574b67e33857901348fb21b133d3ab6bdc5d8e6e613d94e1e48623ff202f36||open=50&code=donation||e7b344a629084fa7b86c3bbeaf1ea19c2ab63b2fb638848b4057f9ebf96bc963&quantity=1||98a8673b40e1c755e8e379391b488c2e5c424dd37ef58a85eca9d75ae23764d8
    (Also, your last comment had &price=price||b43...89d||open=50, so you had an extra price= in there, but I'm not sure if that was the problem.)

    Make sense? (Change the [REDACTED] to your subdomain and it should work for you.)
  • This works perfectly, thank you!
  • brettbrett FoxyCart Team
    @jingari, glad to hear it. Keep us posted. We're happy to help :)
Sign In or Register to comment.