Help | Custom Carrier Service Not Getting Rates

New Member
4 0 0

We are having trouble getting shopify to receive rates from our custom carrier service app. The app is written in php and hosted on heroku (code below). Whenever a customer tries to checkout from our store the shipping methods will not load properly and we get this error message. shopify-shipping-bug.PNG

I wanted to make sure that nothing was happening inside the app that could cause our responses to be invalid so I hard-coded in valid rate values (completely removing the functionality of the app at this point) just to test if shopify was even getting a reply back. but still nothing

 

<?php

	// shipping calculation performed at checkout

	//this is the first wake function sent from shopify when the page is loaded
	//its referenced in the theme.liquid file in a <script> tag

	if($_POST['data'] == 'wake'){
		$wakeResp = "success: true";
		echo json_encode($wakeResp);
	}

	//Get data from shopify as a json string and decode it into a php $obj 
	$obj = file_get_contents('php://input');
	$obj = json_decode($obj, true);
	
	
	//if the object file is empty don't do anything
	if( !empty($obj) ) {

		//hardcoded in an rates output that I know is properly formatted for shopify just to see if it will even take that, there are no Api calls
		//this should be an instant reply


		$string = '{"rates":[{"service_name":"Old Dominion","service_code":"ST","total_price":"34720","currency":"USD","min_delivery_date":"2019-10-18 15:43:46 -0400","max_delivery_date":"2019-10-18 15:43:46 -0400"},{"service_name":"New Penn Motor Express Inc","service_code":"ST","total_price":"38689","currency":"USD","min_delivery_date":"2019-10-18 15:43:46 -0400","max_delivery_date":"2019-10-18 15:43:46 -0400"},{"service_name":"XPO Logistics","service_code":"ST","total_price":"39091","currency":"USD","min_delivery_date":"2019-10-18 15:43:46 -0400","max_delivery_date":"2019-10-18 15:43:46 -0400"}]}';
		
                $response = json_decode($string);
                //json decode and then encoding again is kind of dumb but it works so ill leave it for now



		//send our our response
		header('Content-Type: application/json');
		echo json_encode($response);

	
	}

?>

 

This is the rates reply that this sends back in a cleaner format:

{
    "rates": [
        {
            "service_name": "Old Dominion",
            "service_code": "ST",
            "total_price": "34720",
            "currency": "USD",
            "min_delivery_date": "2019-10-18 15:43:46 -0400",
            "max_delivery_date": "2019-10-18 15:43:46 -0400"
        },
        {
            "service_name": "New Penn Motor Express Inc",
            "service_code": "ST",
            "total_price": "38689",
            "currency": "USD",
            "min_delivery_date": "2019-10-18 15:43:46 -0400",
            "max_delivery_date": "2019-10-18 15:43:46 -0400"
        },
        {
            "service_name": "XPO Logistics",
            "service_code": "ST",
            "total_price": "39091",
            "currency": "USD",
            "min_delivery_date": "2019-10-18 15:43:46 -0400",
            "max_delivery_date": "2019-10-18 15:43:46 -0400"
        }
    ]
}

 

Is php echo not the proper way to send my reply? I don't know how I would do a curl statement because the input from shopify doesn't have a url it just goes to php://input, I would really appreciate some input from someone more experienced with php or from anyone experiencing a similar issue.

 

thx in advance.

0 Likes
Shopify Staff
Shopify Staff
163 26 26

Hey @PCCecom,

 

{
    "rates": [
        {
            "service_name": "Old Dominion",
            "service_code": "ST",
            ...

^ I just tested your JSON on my demo store with success.

 

Here's an example of a request Shopify is sending to your Heroku app. Consider posting this to your Heroku app to test the response. I just ran a quick test on your Heroku endpoint (assuming it's the subdomain ending in 8530?) and saw a different result to the JSON you provided - perhaps you've resolved this one?

1 Like
New Member
4 0 0

thanks for the advice @scottydont I just sent a POST to our endpoint from postman and received back an empty reply, just out of curiosity is this also the response you got/ do you see anything in my code I posted that would indicate why this is the response I got. I can't seem to find anything it's a pretty straightforward code.

0 Likes
New Member
4 0 0

hi @scottydont thanks so much for your help, I adjusted some things on my end and am now able to get responses from our heroku endpoint again (yes the one ending in 8530) but I still can't get shopify to accept any of our shipping rates. Would you mind sending me the http response you got with headers so I can see how yours is different from mine. 

 

NEW-CODE:

// web/index.php 
	require('../vendor/autoload.php');

	use Symfony\Component\HttpFoundation\Request;
	use Symfony\Component\HttpFoundation\Response;
	
	$app = new Silex\Application();
	$app['debug'] = true;

	//parse as json if ctype = json
	$app->before(function (Request $request) {
		if (0 == strpos($request->headers->get('Content-Type'), 'application/json')) {
			$data = json_decode($request->getContent(),true);
			$request->request->replace(is_array($data) ? $data : array());
		}
	});

	$app->post('/', function (Request $request) use ($app) {
		
		
		$string = '{"rates":[{"service_name":"Old Dominion","service_code":"ST","total_price":"34720","currency":"USD","min_delivery_date":"2019-10-18 15:43:46 -0400","max_delivery_date":"2019-10-18 15:43:46 -0400"},{"service_name":"New Penn Motor Express Inc","service_code":"ST","total_price":"38689","currency":"USD","min_delivery_date":"2019-10-18 15:43:46 -0400","max_delivery_date":"2019-10-18 15:43:46 -0400"},{"service_name":"XPO Logistics","service_code":"ST","total_price":"39091","currency":"USD","min_delivery_date":"2019-10-18 15:43:46 -0400","max_delivery_date":"2019-10-18 15:43:46 -0400"}]}';
		$array = array(json_decode($string));

		$response = new \Symfony\Component\HttpFoundation\JsonResponse();
		$response ->setEncodingOptions(JSON_NUMERIC_CHECK);
		$response ->setData($array);

		//send out our response
		return $response;


	});

	$app->run();
0 Likes
New Member
4 0 0

the carrier service we are using also has no "callback url" associated with it. I think that deleting the carrier service and then re-attaching it might fix things but I get this response whenever I try?

{
    "errors": [
        "Action not permitted"
    ]
}

carrier service info

{
    "carrier_service": {
        "id": 427973,
        "name": "Shipping Application",
        "active": true,
        "service_discovery": true,
        "carrier_service_type": "api",
        "admin_graphql_api_id": "gid://shopify/DeliveryCarrierService/427973",
        "format": "json"
    }
}
0 Likes
Highlighted
Shopify Staff
Shopify Staff
163 26 26

Looks like you resolved the callback issue:

22-05-47gn0-048v6

 

When I post an empty body, the response is:

{
    "success": true
}


When I post this example address the response is broken JSON (which will prevent any rates from coming through).

{
    "success": true
}{
    "success": true
}{
    "rates": [
        {
            ...


Don't forget to slightly change the shipping address each time you test to get around caching.

 

0 Likes