Develop app for shipping rates using third party carrier shipping api

Highlighted
Shopify Partner
3 0 1

Hi,

 

I am new in shopify, I want to develop a app in PHP for shipping rates using third party shipping API. At the time of shipping rates calculation API needs store address,cart item and shipping address. I am try to get all of these but these is no luck....please help me how to develop shipping rates app. 

 

 

1 Like
Highlighted
Shopify Staff
Shopify Staff
1040 139 165

Hey @yashgaurav7190,

 

Once you create a carrier service, the callback_url will receive origin, destination and items. Here's an example.

 

Let us know if you have any specific questions.

2 Likes
Highlighted
Shopify Partner
3 0 1

Hey @scottydont 

 

Thanks for your reply,

 

I'm using this API but, name, email, the phone is missing in origin and destination, How can I get these value and right now I an error message show on my screen "{"errors":{"base":["CSM Carrier Demo is already configured"]}}". I,m sharing my code with you please help me out

 

function registerCarrier() {

       $carierRequest['carrier_service'] = array(
                   'name' => "CSM Carrier Demo",
                   'callback_url' => base_url('settings/rate'),
                    'service_discovery' => TRUE
        );


       $url = 'https://development-csm-info.myshopify.com/admin/carrier_services';

       $curl = curl_init();

            curl_setopt_array($curl, array(
                  CURLOPT_URL => $url,
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_ENCODING => "",
                  CURLOPT_MAXREDIRS => 10,
                  CURLOPT_TIMEOUT => 30,
                  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                  CURLOPT_CUSTOMREQUEST => "POST",
                  CURLOPT_POSTFIELDS => json_encode($carierRequest),
                  CURLOPT_HTTPHEADER => array(
                            "Accept: application/json",
                            "Content-Type: application/json",
                            "X-Shopify-Access-Token: my-access-token"
                    ),
        ));

     $response = curl_exec($curl);
      $err = curl_error($curl);

       curl_close($curl);

     if ($err) {
        echo "cURL Error #:" . $err;
      } else {
        echo $response;
     }
}

 

and this is a call back URL code

 

// log the raw request -- this makes debugging much easier
$filename = time();
$input = file_get_contents('php://input');
file_put_contents($filename.'-input', $input);

// parse the request
$rates = json_decode($input, true);

// log the array format for easier interpreting
file_put_contents($filename.'-debug', print_r($rates, true));

// total up the cart quantities for simple rate calculations
$quantity = 0;
foreach($rates['rate']['items'] as $item) {
    $quantity =+ $item['quantity'];
}

// use number_format because shopify api expects the price to be "25.00" instead of just "25"

// overnight shipping is 5.50 per item
$overnight_cost = number_format($quantity * 5.50, 2, '', '');
// regular shipping is 2.75 per item
$regular_cost = number_format($quantity * 2.75, 2, '', '');

// overnight shipping is 1 to 2 days after today
$on_min_date = date('Y-m-d H:i:s O', strtotime('+1 day'));
$on_max_date = date('Y-m-d H:i:s O', strtotime('+2 days'));

// regular shipping is 3 to 7 days after today
$reg_min_date = date('Y-m-d H:i:s O', strtotime('+3 days'));
$reg_max_date = date('Y-m-d H:i:s O', strtotime('+7 days'));

// build the array of line items using the prior values
$output = array('rates' => array(
    array(
        'service_name' => 'Endertech Overnight',
        'service_code' => 'ETON',
        'total_price' => $overnight_cost,
        'currency' => 'USD',
        'min_delivery_date' => $on_min_date,
        'max_delivery_date' => $on_max_date
    ),
    array(
        'service_name' => 'Endertech Regular',
        'service_code' => 'ETREG',
        'total_price' => $regular_cost,
        'currency' => 'USD',
        'min_delivery_date' => $reg_min_date,
        'max_delivery_date' => $reg_max_date
    )
));

// encode into a json response
$json_output = json_encode($output);

// log it so we can debug the response
file_put_contents($filename.'-output', $json_output);

// send it back to shopify
print $json_output;

 

 

0 Likes
Highlighted
Shopify Partner
3 0 1

Hey @scottydont 

 

All the things are solved thank you for your help, but I want some field on the same API but its null. (I'm using this API but, name, email, the phone is missing in origin and destination(Carrier Service API)). How could I get these value, please help me..

0 Likes