Skip to content

Instantly share code, notes, and snippets.

@danyay
Last active December 23, 2016 22:31
Show Gist options
  • Save danyay/6d5b133e3b71fa903d3bb442e7393943 to your computer and use it in GitHub Desktop.
Save danyay/6d5b133e3b71fa903d3bb442e7393943 to your computer and use it in GitHub Desktop.
Send a Pushover notification when new orders arrive.
<?php
/**
Amazon Seller Central Order Notifier
Send me a "push" notification for new orders (after polling)
*/
// Disable error reporting for AWS' shitty sdk
error_reporting(E_ALL & ~E_NOTICE);
chdir(dirname(__FILE__));
require_once('.config.inc.php');
$serviceUrl = "https://mws.amazonservices.com/Orders/2013-09-01";
$config = array (
'ServiceURL' => $serviceUrl,
'ProxyHost' => null,
'ProxyPort' => -1,
'MaxErrorRetry' => 3,
);
$service = new MarketplaceWebServiceOrders_Client(
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
APPLICATION_NAME,
APPLICATION_VERSION,
$config);
// When was the last time we ran the order processing?
$date_since = @file_get_contents(".lastran");
if(! $date_since)
{
//error_log("Notifier never ran. Checking as of 1 day ago.");
$date_since = gmdate('c', strtotime('-1 day'));
}
$request = new MarketplaceWebServiceOrders_Model_ListOrdersRequest();
$request->setSellerId(MERCHANT_ID);
$request->setMarketplaceId(MARKETPLACE_ID);
$request->setCreatedAfter($date_since);
// Invoke the list
invokeListOrders($service, $request);
function invokeListOrders(MarketplaceWebServiceOrders_Interface $service, $request)
{
try {
$response = $service->ListOrders($request);
$obj = json_decode(json_encode((array) simplexml_load_string($response->toXML())),1);
$items_sold = 0;
$orders = 0;
// Write the current date as the last ran date
file_put_contents(".lastran", gmdate('c'));
if (! array_key_exists('Order', $obj['ListOrdersResult']['Orders'])) exit;
foreach($obj['ListOrdersResult']['Orders']['Order'] as $order)
{
$items_sold += $order['NumberOfItemsShipped'];
$items_sold += $order['NumberOfItemsUnshipped'];
$orders++;
}
if($items_sold > 0)
{
$order_str = ($orders == 1) ? "Order" : "Orders";
#pushover("Amazon Order",$items_sold . " Items Sold in " . $orders . " " . $order_str . "!");
pushover("Amazon Order", "New items sold on Amazon!");
}
} catch (MarketplaceWebServiceOrders_Exception $ex) {
if($ex->getStatusCode() == 503)
{
print "AWS MWS is currently throttling us. Please try again soon.\n";
exit(0);
}
if($ex->getErrorCode() == "InvalidParameterValue")
{
print "An error occurred. You probably need to wait longer before checking for new orders.\n";
exit(0);
}
echo("Caught Exception: " . $ex->getMessage() . "\n");
echo("Response Status Code: " . $ex->getStatusCode() . "\n");
echo("Error Code: " . $ex->getErrorCode() . "\n");
echo("Error Type: " . $ex->getErrorType() . "\n");
echo("Request ID: " . $ex->getRequestId() . "\n");
echo("XML: " . $ex->getXML() . "\n");
echo("ResponseHeaderMetadata: " . $ex->getResponseHeaderMetadata() . "\n");
}
}
function pushover($title, $message, $sound = "bugle")
{
curl_setopt_array($ch = curl_init(), array(
CURLOPT_URL => "https://api.pushover.net/1/messages.json",
CURLOPT_POSTFIELDS => array(
"token" => "INSERT_PUSHOVER_TOKEN",
"user" => "INSERT_PUSHOVER_USER",
"title" => $title,
"message" => $message,
"sound" => $sound
),
CURLOPT_SAFE_UPLOAD => true,
CURLOPT_RETURNTRANSFER => true,
));
curl_exec($ch);
curl_close($ch);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment