Skip to content

Instantly share code, notes, and snippets.

@tigerhawkvok
Created June 10, 2015 05:24
Show Gist options
  • Save tigerhawkvok/4008f45bce2794478c11 to your computer and use it in GitHub Desktop.
Save tigerhawkvok/4008f45bce2794478c11 to your computer and use it in GitHub Desktop.
Easypost Integration
<?php
/***
* Target file for asynchronous hits
***/
if(isset($_SERVER['QUERY_STRING'])) parse_str($_SERVER['QUERY_STRING'],$_GET);
require_once('./core/core.php');
$start_script_timer = microtime_float();
if(!function_exists('elapsed'))
{
function elapsed($start_time = null)
{
/***
* Return the duration since the start time in
* milliseconds.
* If no start time is provided, it'll try to use the global
* variable $start_script_timer
*
* @param float $start_time in unix epoch. See http://us1.php.net/microtime
***/
if(!is_numeric($start_time))
{
global $start_script_timer;
if(is_numeric($start_script_timer)) $start_time = $start_script_timer;
else return false;
}
return 1000*(microtime_float() - (float)$start_time);
}
}
function returnAjax($data)
{
if(!is_array($data)) $data=array($data);
$data["execution_time"] = elapsed();
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
print json_encode($data,JSON_FORCE_OBJECT);
exit();
}
/************************
* Main async hooks
************************/
$a = array("status"=>false,"error"=>"Uncaught Switch, or unreturned value","problemDesc"=>"with our server","humanErrorMsg"=>"A script on our server didn't respond. If this continues, please report it.");
switch($_REQUEST['action'])
{
case "buy":
try
{
require_once("./charge.php");
}
catch(Exception $e)
{
$a = array("status"=>false,"error"=>$e->getMessage(),"problemDesc"=>"with our server","humanErrorMsg"=>"There was a problem with a server side script. If this continues, please report it.");
}
break;
case "ship":
require_once("./shipping.php");
break;
case "details":
# Return details of an item
$sample_details = array(
#
);
$a = $sample_details;
break;
case "search":
break;
case "similar":
break;
default:
$a = array("status"=>false,"error"=>"Invalid Action","action"=>$_REQUEST['action'],"problemDesc"=>"with our server","humanErrorMsg"=>"Our server recieved bad data. Please try again.");
}
returnAjax($a);
?>
<?php
require_once("./easypost/lib/easypost.php");
$easypost_test_key = "TEST_KEY_HERE";
$easypost_live_key = "LIVE_KEY_HERE";
# $easypost_key = $easypost_live_key;
$easypost_key = $easypost_test_key;
\EasyPost\EasyPost::setApiKey($easypost_key);
$from_address_shipping = array (
"company" => "COMPANY_NAME",
"street1" => "COMPANY_ADDR",
"zip" => "COMPANY_ZIP+4",
"phone" => "CONTACT_PHONE",
"residential" => true,
"email" => "COMPANY_CONTACT_SALES"
);
$allowed_carriers = array(
"FedEx",
"GSO"
);
$require_overnight = true;
?>
<?php
$easypost_key = "TEST_KEY_HERE";
$require_overnight = false;
require_once("./easypost-key-config.php");
/***
* Source:
* https://github.com/EasyPost/easypost-php
*
* Docs:
* https://www.easypost.com/docs/api?lang=php#authentication
***/
$error = "";
$from_address = \EasyPost\Address::create_and_verify($from_address_shipping);
$provided_to_address = array(
"name" => $_REQUEST["name"],
"street1" => urldecode($_REQUEST["street"]),
"zip" => $_REQUEST["zip"],
"phone" => $_REQUEST["phone"],
"email" => $_REQUEST["email"]
);
try
{
$to_address = \EasyPost\Address::create_and_verify($provided_to_address);
}
catch (EasyPost\Error $e)
{
returnAjax(array("status"=>false,"error"=>$e->getMessage(),"humanMsg"=>"Invalid address, please try again."));
}
$parcel_details = array(
"length"=>$_REQUEST["parcel_length"],
"width"=>$_REQUEST["parcel_width"],
"height"=>$_REQUEST["parcel_height"],
"weight" => $_REQUEST["parcel_weight"]
);
$parcel = \EasyPost\Parcel::create($parcel_details);
$shipment = \EasyPost\Shipment::create(
array(
"to_address" => $to_address,
"from_address" => $from_address,
"parcel" => $parcel,
"options" => array("deliver_confirmation"=>"ADULT_SIGNATURE")
));
$shipmentArray = json_decode($shipment,true);
try
{
$shipmentBackup = $shipment;
$shipmentRates = $shipmentArray["rates"];
$shipment->insure(array("amount"=>$_REQUEST["value"]));
$shipmentArray = json_decode($shipment,true);
$shipmentRates = $shipmentArray["rates"];
}
catch(EasyPost\Error $e)
{
# Do nothing
$error = "EasyPost error in getting insured rates: ".$e->getMessage();
$shipment = $shipmentBackup;
$shipmentArray = json_decode($shipment,true);
$shipmentRates = $shipmentArray["rates"];
}
$availableRates = array();
foreach($shipmentRates as $carrierOption)
{
if(in_array($carrierOption["carrier"],$allowed_carriers))
{
$option = array(
"service"=>$carrierOption["carrier"] . " " . $carrierOption["service"],
"rate"=>$carrierOption["rate"]
);
if($require_overnight)
{
if(strpos(strtolower($carrierOption["service"]),"overnight") === false) continue;
}
$availableRates[] = $option;
}
}
returnAjax(array(
"status"=>true,
"rates"=>$availableRates,
"all_options"=>$shipmentRates,
"ship_details"=> array (
"to_address" => json_decode($to_address),
"provided_to_address" => $provided_to_address,
"from_address" => json_decode($from_address),
"provided_from_address" => $from_address_shipping,
"parcel" => $parcel_details,
"shipment_object" => json_decode($shipment)
),
//"test_response" => $test_shipment_response,
"error"=>$error
));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment