The Foxy forums are on the move!

We're in the process of moving our forums over to a new system, and so these forums are now read-only.
If you have a question about your store in the meantime, please don't hesitate to reach out to us via email.

Datafeed used to do post handling logic

in Help edited July 2012
I have to admit, as many years as I've used FoxyCart, I've never interfaced much with the datafeed (if at all). I'm getting a little tripped up.

The only use or function I have for the datafeed at this time is to have my PHP script (after successful ordering and payment) log our database with some data indicating that a special "code" has been used/redeemed, and cannot be used again. From that standpoint, it's a simple MySQL update query, but I'm not sure I'm using the datafeed right.

Other than the query I need to run in my PHP script (the datafeed URL), do I need to do anything else other than to echo "foxy" out to the page? Maybe that's all, and I'm over-thinking this.
Comments
  • In fact, maybe I don't really need the datafeed at all. I really just need my special PHP script to function/trigger after a successful payment has been collected for a user, and the information I need to "retire" in our database is nothing more than a promo code stored in a PHP session variable, so by the time FoxyCart calls to the script, that information is already in the user's session.

    If there's something odd with FoxyCart making an ajaxy type of call or something that would make this session variable non-applicable, please let me know. I may be complicating this unnecessarily.
  • sparkwebsparkweb Member, Integration Developer, FoxyShop, Order Desk
    I think you do probably want to still use the datafeed webhook. Have FoxyCart post back the order info to your site after a successful transaction, process the data, save it to your datafeed, and echo "foxy". Is that all working okay?

    I'll just mention here that whenever I'm developing using the datafeed there's a lot of back and forth, echoing, checking the FoxyCart error log, etc. I've built a developer tool in Order Desk to pull the XML for a transaction, mimic the datafeed post, and display the results in one step. Makes developing apps with the datafeed a lot easier. Check it out if you like at http://foxytools.com/orderdesk/
  • Well, I don't really need any data from the feed. I really just want to use the feed as a "trigger" to let me do some "after order" handling back on the site, but the handling I'm doing has nothing to do with the data for the order.

    It's possible that maybe a thankyou.php page to send people to after ordering could do the handling too. We really just need to clean up some session stuff in PHP after the order's done. What I'm not sure about yet is whether or not FoxyCart's call to my feed PHP file is going to allow me to session_start() and access the variable in the session. In theory, it should...since $_SESSION is server side, but I've just never done this with FC before.

  • sparkwebsparkweb Member, Integration Developer, FoxyShop, Order Desk
    You won't be able to access the session because FoxyCart is sending the data in a totally separate process. If you were to rely on the thank you page, you could do this, but the problem with that is that you can't rely on the user to click the "Continue" button. The user might just decide to go to Google after they saw the receipt page.

    You could set a timeout on your system variables in the db and just let them expire after 8 hours or something.

    It's possible that you might be able to make an AJAX call from the thank you page that cleaned up your session. The thank you page is not HTTPS so I think that might be okay.

    The other more crazy option is set the session's ID as a hidden checkout custom field that you could use to map back to the session on the datafeed end. You could parse the datafeed, pull out the session ID and then do your processing from there.

    Just a few ideas for ya.
  • fc_adamfc_adam FoxyCart Team
    @sparkweb covered it pretty well - if you have any further questions don't hesitate to ask!

    We also have some example datafeed scripts on our integrations page (http://wiki.foxycart.com/integration) if that helps at all.
  • lukeluke FoxyCart Team
    Another possibility would be to use the ^^receipt_only_begin^^ and ^^receipt_only_end^^ (http://wiki.foxycart.com/v/0.7.2/cheat_sheet#analytics_order_tracking) place holders in your receipt template. The HTML within those tags will show up only once when the order is complete. You could put a script in there to do an AJAX call to your server with the session information pulled from the fc_json cart. Would that work?
  • Hi Luke -

    Maybe...then again, maybe not. The $_SESSION[] data I'm trying to access after purchasing is stored via PHP before they enter the cart. What I'm aiming to do is "change" part of that session after the order has taken place.

    If I make the AJAX call from the receipt page, and have it call back to a PHP script on our server, will that PHP script be able to access the same $_SESSION previously setup with the user's information via session_start()? Right now, the data ID stored in the session is the only reference to a special "deal" code that we have, as we do not wish to pass this to the cart.

    I really want to be able to do clean up things, like "hey, user ordered...retire this special promo code". Things of that nature. I don't think I need a data feed for this, but wanted to check here with the community.

    Am I thinking about this totally wrong?
  • OK, I just spent the last 60 minutes setting up a test environment for this. Doing the AJAX call back to the site definitely does not result in the called PHP file having access to the session that was active before going into the checkout process.

    I'm at a loss now, because that session variable is how we're storing and keeping track of the user's progress in our script. So far, I've not found a way to access that information from within FoxyCart.

    What would work, however, is if I could somehow pass the string to FoxyCart before entering checkout. Then, maybe having our AJAX call pass that through as part of a $_REQUEST string to the PHP script. Then I'd be able to retire the code properly.

    Is there an easy way to get this passed to FC behind the scenes during the checkout process? If not, my next bet may be to try a browser cookie, but I'd prefer not to.
  • sparkwebsparkweb Member, Integration Developer, FoxyShop, Order Desk
    You could hijack FoxyCart's SSO feature to run some calls before checkout and then redirect back to the cart.

    I really think, though, the last option I presented before (using the session ID in a datafeed) would be the best and most reliable.

    So in the script on your site, store the PHP session ID and the user's data in your database. Then every time a user adds something to the cart include a hidden input:
    <input type="hidden" name="h:sessionid" value="YOUR PHP SESSION ID HERE" />
    

    Then in the datafeed endoint, loop through the order's custom fields until you find the sessionid one, then run your processes. Here's a datafeed script that exposes all the variables and makes it easy to grab just what you need: http://wiki.foxycart.com/v/0.7.2/transaction_xml_datafeed#example_parsing_code
  • Thanks. Couple questions.

    I think the image tag option only works for forms, correct? Should I be doing it differently for the query string (hashed) method of linking an item to the cart?

    Also, while testing, does it matter if I'm still working with MAMP locally? IE., "mystore.localhost:8888" for my hostname for the data feed (until live). That may help speed up the process a little.
  • fc_adamfc_adam FoxyCart Team
    @bdcadvertising,

    The FoxyCart datafeed can't be sent to your local system, it needs to be sent to a script that is actually online. That said though, you can manually pass the datafeed to a local endpoint to trigger it that way.
  • edited July 2012
    Hey Spark.

    I'm using your sample data feed, but for some reason I can't seem to isolate the promo code. When I use FoxyTools to fetch XML for a transaction, I can hard code that XML in my script and find the variable with the below code.

    However, if I use the "Send XML" feature from within the tools area, it says "Your feed is working great!", but that variable isn't being found anymore (even though it's the exact same XML data).

    So, in other words, hard-coded XML works, but FoxyCart-fed XML doesn't work...but structurally they're identical (I literally copied and pasted). Any ideas?
    <?php
    foreach($transaction_detail->transaction_detail_options->transaction_detail_option as $transaction_detail_option) {
    	$product_option_name = $transaction_detail_option->product_option_name;
    	$product_option_value = (string)$transaction_detail_option->product_option_value;
    	$price_mod = (double)$transaction_detail_option->price_mod;
    	$weight_mod = (double)$transaction_detail_option->weight_mod;
    	
    	if($product_option_name == 'promo_code') {				
     		
     		// update mysql and retire the code
     		mysql_update_col ( $product_option_value, 'deal_codes', 'code_completed', date("Y-m-d H:i:s") );
     		
     		$message = "got it!: $product_option_value";
    		mail('me@mydomain.com', 'Datafeed Testing ' . date('Y-m-d H:i:s'), $message, "Content-Type: text/html; charset=iso-8859-1\n");
    	}else{
    		// die('could not find the promo code');
    	}
    	
    }
    ?>
    

    What the heck am I doing wrong? :D
  • sparkwebsparkweb Member, Integration Developer, FoxyShop, Order Desk
    The first thing to do is to put a die(); at the top of your page and make sure you are hitting the right feed. Then put a die right before the code you pasted above to make sure you are getting there. Then put a die($message); after your code to see if it's picking up the promo_code. If it's not, append what it did pick up and die($that) to see what it did pick up.

    When code does crazy stuff like this that's the troubleshooting process I go through, start basic and see where it breaks. Using die('something'); all over the place will help you pinpoint exactly where it isn't working.
  • Yeah, I know it's the right feed. I can change code and die('test'); and stuff in different places, and Order Desk picks up on it. But for whatever reason, it almost seems like that particular order doesn't have the transaction "options" data....but I know it does, since I can hand-copy/paste into my script and read the XML from a variable and it all works fine.

    Been at this for about 2 hours with everything you suggested and I'm striking out. I guess I was just wondering if the data gets served up differently (formatted or whatever) via AJAX vs. the local $xml.
  • Some additional info. I'm seriously hoping it triggers something in memory from what you've already gone through in integrating via the feed.
    <?php
    $FoxyData_decrypted = foxycart_decrypt($_POST["FoxyData"]);
    die($FoxyData_decrypted);
    
    $xml = simplexml_load_string($FoxyData_decrypted, NULL, LIBXML_NOCDATA);
    die($xml);
    ?>
    

    The first die() works (but is all garbled) and shows up in the response box in Order Desk, but the second die() doesn't work, as it returns nothing to the response box in Order Desk.
  • I did notice, however, that doing the following produces garbled output in the response box as well, which leads me to ask: "is the data even being decrypted correctly?". Maybe there's a plugin for PHP that needs to be installed that I'm unaware of in order to do the decryption properly.
    <?php die($xml->transactions); ?>
    

    0s.png
  • sparkwebsparkweb Member, Integration Developer, FoxyShop, Order Desk
    When you see all those funny characters it usually means that there is an API key mismatch and it can't decrypt the data properly.
  • edited July 2012
    OK, I think we've got it. You were a huge help getting through this. The data feed makes a lot more sense now, and it all seems to be working.
  • fc_adamfc_adam FoxyCart Team
    @sparkweb, you are awesome!
Sign In or Register to comment.