Created
December 21, 2022 18:54
-
-
Save chaddupuis/c1db2d4b7c189eeccc7e91aad827e389 to your computer and use it in GitHub Desktop.
Processwire and Foxycart Integration with ShippingEasy Labels and Status Management
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace ProcessWire { | |
// Processwire Store Management with Foxycart (from JSON webhook) and ShippingEasy (API). | |
// Manages order status and sends customer data to shipping easy for label printing via dashboard. | |
require_once('/your/pw-site.com/wire/core/ProcessWire.php'); | |
$st = new ProcessWire('/your/pw-site.com/', 'https://pw-site.com/'); | |
} // END PW namespace | |
namespace { | |
// Find all orders that are marked as unprocessed | |
// Process them - then update status | |
// Runs in cron at desired intervals | |
// store_shipping_easy_status | |
//1=error | |
//2=not_submitted | |
//3=ready_for_shipment | |
//4=shipped | |
// get orders with not_submitted (2) or ready_for_shipment (3) | |
$myNotCompletedOrders = $st->pages->find("template=store-order-page, store_shipping_easy_status=2|3"); | |
foreach ($myNotCompletedOrders as $noship) { | |
if ($noship->store_shipping_easy_status == '2') { echo 'ORDER NOT SHIPPED::: ' . $noship->title . PHP_EOL; } | |
if ($noship->store_shipping_easy_status == '3') { echo 'ORDER IN BUT NO TRACKING::: ' . $noship->title . PHP_EOL; } | |
if ($noship->store_foxycart_json != '') { | |
$parsedData = json_decode($noship->store_foxycart_json, true); | |
} else { | |
echo 'NO JSON DATA FOR ' . $noship->title . PHP_EOL; | |
} | |
// Setup stored json from initial foxycart hook | |
if (is_array($parsedData)) { | |
// General Order Info | |
$gfname = $parsedData['customer_first_name']; | |
$glname = $parsedData['customer_last_name']; | |
$gemail = $parsedData['customer_email']; | |
$gid = $parsedData['id']; | |
$greceipturl = $parsedData['receipt_url']; | |
$gtotalorder = $parsedData['total_order']; | |
$gdate = $parsedData['transaction_date']; | |
// Make More Sense of the JSON | |
// Billing | |
$bfname = $parsedData['_embedded']['fx:billing_addresses'][0]['first_name']; | |
$blname = $parsedData['_embedded']['fx:billing_addresses'][0]['last_name']; | |
$bcompname = $parsedData['_embedded']['fx:billing_addresses'][0]['company']; | |
$badd1name = $parsedData['_embedded']['fx:billing_addresses'][0]['address1']; | |
$badd2name = $parsedData['_embedded']['fx:billing_addresses'][0]['address2']; | |
$bcityname = $parsedData['_embedded']['fx:billing_addresses'][0]['city']; | |
$bstatename = $parsedData['_embedded']['fx:billing_addresses'][0]['region']; | |
$bcountryname = $parsedData['_embedded']['fx:billing_addresses'][0]['customer_country']; | |
$bzipname = $parsedData['_embedded']['fx:billing_addresses'][0]['customer_postal_code']; | |
$bphonename = $parsedData['_embedded']['fx:billing_addresses'][0]['customer_phone']; | |
// Payment | |
$bcardfour = $parsedData['_embedded']['fx:payments'][0]['cc_number_masked']; | |
$borderdate = $parsedData['_embedded']['fx:payments'][0]['date_created']; | |
$bpayresponse = $parsedData['_embedded']['fx:payments'][0]['processor_response']; | |
$bpaypalid = $parsedData['_embedded']['fx:payments'][0]['paypal_payer_id']; | |
$bpaidamount = $parsedData['_embedded']['fx:payments'][0]['amount']; | |
// Payments from shipping | |
$borderitemsamount = $parsedData['_embedded']['fx:shipments'][0]['total_item_price']; | |
$bordershippingamount = $parsedData['_embedded']['fx:shipments'][0]['total_shipping']; | |
$bordertaxamount = $parsedData['_embedded']['fx:shipments'][0]['total_tax']; | |
// Shipping | |
$sfname = $parsedData['_embedded']['fx:shipments'][0]['first_name']; | |
$slname = $parsedData['_embedded']['fx:shipments'][0]['last_name']; | |
$scompname = $parsedData['_embedded']['fx:shipments'][0]['company']; | |
$sadd1name = $parsedData['_embedded']['fx:shipments'][0]['address1']; | |
$sadd2name = $parsedData['_embedded']['fx:shipments'][0]['address2']; | |
$scityname = $parsedData['_embedded']['fx:shipments'][0]['city']; | |
$sstatename = $parsedData['_embedded']['fx:shipments'][0]['region']; | |
$scountryname = $parsedData['_embedded']['fx:shipments'][0]['country']; | |
$szipname = $parsedData['_embedded']['fx:shipments'][0]['postal_code']; | |
$sphonename = $parsedData['_embedded']['fx:shipments'][0]['phone']; | |
// Create Bz billing array of variables | |
$generalinfo = compact('gfname', 'glname', 'gemail', 'gid', 'greceipturl', 'gtotalorder', 'gdate'); | |
$billingadd = compact('bfname', 'blname', 'bcompname', 'badd1name', 'badd2name', 'bcityname', 'bstatename', 'bcountryname', 'bzipname', 'bphonename'); | |
$shippingadd = compact('sfname', 'slname', 'scompname', 'sadd1name', 'sadd2name', 'scityname', 'sstatename', 'scountryname', 'szipname', 'sphonename'); | |
$payment = compact('bpaidamount', 'bcardfour', 'borderdate', 'bpayresponse', 'bpaypalid', 'borderitemsamount', 'bordershippingamount', 'bordertaxamount'); | |
} // end of if in_array | |
// SETUP SHIPPING EASY | |
require_once('/your/pw-site.com/site/templates/includes/shipping_easy-php/lib/ShippingEasy.php'); | |
ShippingEasy::setApiKey('yourapikey'); | |
ShippingEasy::setApiSecret('yourapisecret'); | |
$devstoreapikey = 'siteapikey'; | |
// IF WEVE NEVER SHIPPED IT THEN SEND GID AS SHIP ID - IF WE HAVE CREATE A RANDOM UNIQUE | |
// THIS IS BECAUSE SHIPPNG EASY NEVER DELETES AN ORDER, EVER | |
// Do only if it isnt already marked as ready_for_shipment | |
// Then we are just finding and setting | |
if ($noship->store_shipping_easy_status != "3") { | |
if ($noship->store_shippingeasy_order_id == '') { | |
echo 'ORDER ID IS NULL SO SET TO GID' . PHP_EOL; | |
echo 'CURRENTLY ' . $noship->store_shippingeasy_order_id . PHP_EOL; | |
$shiporderid = $gid; | |
} else { | |
echo 'ORDER ID IS SET SO SET TO RAND' . PHP_EOL; | |
$shiporderid = $gid . '-' . rand(100,10000); | |
echo 'RAND IS ' . $shiporderid . PHP_EOL; | |
} | |
} else {$shiporderid = $noship->store_shippingeasy_order_id;} | |
// END if isnt already marked as ready_for_shipment | |
// Send JSON to ShippingEasy for label | |
$order_array = array( | |
"external_order_identifier" => $shiporderid, | |
"ordered_at" => $gdate, | |
"order_status" => "awaiting_shipment", | |
"total_including_tax" => $gtotalorder, | |
"billing_company" => $bcompname, | |
"billing_first_name" => $bfname, | |
"billing_last_name" => $blname, | |
"billing_address" => $badd1name, | |
"billing_address2" => $badd2name, | |
"billing_city" => $bcityname, | |
"billing_state" => $bstatename, | |
"billing_postal_code" => $bzipname, | |
"billing_country" => $bcountryname, | |
"billing_phone_number" => $bphonename, | |
"billing_email" => $gemail, | |
"recipients" => array( | |
array ( | |
"first_name" => $sfname, | |
"last_name" => $slname, | |
"company" => $scompname, | |
"email" => $gemail, | |
"phone_number" => $sphone, | |
"residential" => "true", | |
"address" => $sadd1name, | |
"address2" => $sadd2name, | |
"province" => "", | |
"state" => $sstatename, | |
"city" => $scityname, | |
"postal_code" => $szipname, | |
"country" => $scountryname, | |
"items_total" => "1", | |
"items_shipped" => "1", | |
"line_items" => array ( | |
array( | |
"item_name" => "Shipping Notice - Details On Store Receipt", | |
"unit_price" => "0", | |
"weight_in_ounces" => "16", | |
"quantity" => "1" | |
) | |
) | |
) | |
) | |
); | |
// Send the info to the SE dashboard | |
if ($noship->store_shipping_easy_status != "3") { | |
$order = new ShippingEasy_Order($devstoreapikey , $order_array); | |
$order->create(); | |
// Webhook later should mark as shipped and add tracking but for now set to 3 ready_for_shipment | |
$noship->store_shipping_easy_status = 3; | |
$noship->store_shippingeasy_order_id = $shiporderid; | |
$noship->save(); | |
} | |
if ($noship->store_shipping_easy_status == "3") { | |
// now check to see if this has been shipped and mark accordingly | |
$ordercheck = new ShippingEasy_Order($devstoreapikey); | |
$mySEInfo = $ordercheck->findByStore($shiporderid); | |
$myInfo = $mySEInfo; | |
$myStatus = $myInfo['order']['order_status']; | |
$myTracking = $myInfo['order']['shipments'][0]['tracking_number']; | |
echo 'MY ORDER STATUS IS::: ' . $myStatus . PHP_EOL; | |
echo 'MY TRACKING IS::: ' . $myTracking . PHP_EOL; | |
if ($myStatus == 'drop_shipped') {$myStatus = "shipped";} | |
if (($myTracking != '') && ($myStatus == 'shipped')) { | |
$noship->store_shipping_easy_status = 4; | |
$noship->store_shipping_easy_trackingid = $myTracking; | |
$noship->save(); | |
} | |
} // end mark if shipped... | |
} // end of foreach | |
////////////////////////////// | |
} // end of no name namespace |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment