Custom Live Rate Endpoint

This snippet has been replaced. In place of the following snippet, we now have a custom shipping endpoint feature in FoxyCart 2.0 that allows you to provide custom rates to your customers without needing to add custom javascript to change the way the checkout works. Please use the custom shipping endpoint instead of this snippet. This page will remain for reference.

If you have requirements outside of the live shipping rate integrations that FoxyCart currently has, whether to make use of a specific shipping carrier, or make use of functionality that we don't current support, you can use the following snippet to roll your own endpoint.

Requirements

This script requires communicating with an external script. As the cart and checkout pages where this call will happen are secured over HTTPS, to allow for the communication to happen, your endpoint needs to be secured over HTTPS as well. Without it, the requests will be blocked by the browser and customers won't see any rates.

Setup

Step 1: Your Endpoint

On your side, your endpoint will be passed a group of post values containing the customers shipping address.

  • shipping_address_name Used for multiship - will contain the multiship name
  • shipping_state 2 character code if auto-completed, or the name as entered
  • shipping_country 2 character country code like US or GB
  • shipping_city:
  • shipping_postal_code

Your endpoint should print the result out in the following format for them to be handled correctly within your store:

Success JSON

{
  "ok":true,
  "data": {
    "shipping_results": [
      {
        "service_id": 57,
        "price": 642.2,
        "method":"FedEx",
        "service_name": "International Economy"
      },
      {
        "service_id": 56,
        "price": 765.47,
        "method": "FedEx",
        "service_name": "International Priority"
      }
    ]
  }
}

Note: The service_id value needs be set to an integer.

Errors JSON

{
    "ok":false,
    "details": "Error Message"
}

Example JSONP Output

<?php
// Mitigate against Rosetta Flash vulnerability: https://miki.it/blog/2014/7/8/abusing-jsonp-with-rosetta-flash/
header('Content-Type:application/json;charset=utf-8');
header('X-Content-Type-Options: nosniff');
$output = '/**/';
 
// Create some JSON. Preferably using your programming languages native JSON functionality, not like this :)
$rates = '{
  "ok":true,
  "data": {
    "shipping_results": [
      {
        "service_id": 57,
        "price": 642.2,
        "method":"FedEx",
        "service_name": "International Economy"
      },
      {
        "service_id": 56,
        "price": 765.47,
        "method": "FedEx",
        "service_name": "International Priority"
      }
    ]
  }
}';
 
// Output JSONP. Wrap the JSON in the callback, prepended with the comment
echo $output . $_GET['callback'] . '(' . $rates . ')';

Step 2: Add javascript

Add the following in the “custom footer” field of the template configuration's “Add custom header and footer code to your templates” option of your store's FoxyCart administration.

{% if context == 'cart' or context == 'checkout' %}
<script type="text/javascript" charset="utf-8">
    FC.api.getShippingOptions = function(address) {
        var request_data = {
            'shipping_address_name': address.address_name,
            'shipping_state': address.region,
            'shipping_country': address.country,
            'shipping_city': address.city,
            'shipping_postal_code': address.postal_code
        };
 
        return $.Deferred(function (deferred) {
            $.ajax({
                url: YOUR-ENDPOINT-URL,
                dataType: 'jsonp',
                data: request_data
            }).done(function (response) {
                if (response.ok) {
                    deferred.resolve(response);
                } else {
                    deferred.reject(Error(response.details || 'Unknown error'));
                }
            }).fail(function (jqXHR, textStatus, errorThrown) {
                deferred.reject(Error(errorThrown));
            });
        }).promise();
    };
</script>
{% endif %}

If you have created your own cart or checkout templates, ensure you've followed the steps to include the custom code placeholders as detailed here

In the above code, you'll need to update YOUR-ENDPOINT-URL to be your actual endpoint script. You have to wrap the url of your actual endpoint script in single quotes (' ').

Site Tools