Handling Affiliate Cookies with Rest API and Custom Thank You Pages

Introduction

UltraCart created a new method for handling affiliate cookies using javascript checkouts which have custom thank you pages.  This scenario is common with PHP developers that are calling finalizeOrder directly instead of handing the customer's browser off for the finalization.  In this scenario UltraCart does not have an opportunity to properly inspect the affiliate cookies and attribute the sale to the affiliate.

To solve this problem, there is a snippet of JavaScript code that will make a JSONP request to the proper domain and cause the affiliate cookies to track properly.

 

Steps to Implement

  • Add the following code to your site, adapting it as needed.
  • Test and place a few orders for thoroughness.

 

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>

  var cart = {
        merchantId: 'DEMO',
        shoppingCartId: '12345'
  };

  function unifyCookies() {

    // Perform the JSONP request to unify cookies it (asynchronous by nature)
	// Make sure to change images.ultracart.com to whatever domain UltraCart is providing in the affiliate links generated for the affiliate.
    // images.ultracart.com is the default.
 	jQuery.getJSON('https://images.ultracart.com/cgi-bin/UCCheckoutAPIUnifiedAffiliate?callback=?',
      {
        merchantId: cart.merchantId,
        shoppingCartId: cart.shoppingCartId
      }
    ).done(function(data) {
      if (data.success) {
        // Successful call
        if (data.unified) {
			// Cookies existed and the information was unified properly.
		}
      }
    });
  }

  jQuery(document).ready(function(){
	 // TODO: You will want to load the cart here in your production code before calling unifyCookies.
     // A fake stubbed out cart is shown above for this tutorial.
	 unifyCookies();
  });

</script>